From 06e45c4886cbe84b33f79b7e651d7da5343c24ef Mon Sep 17 00:00:00 2001 From: Lenni0451 <20379977+Lenni0451@users.noreply.github.com> Date: Sat, 7 Jan 2023 13:23:20 +0100 Subject: [PATCH] Mark selected account in list and allow moving accounts using arrow keys and context menu --- .../viaproxy/saves/impl/AccountsSave.java | 8 +- .../raphimc/viaproxy/ui/impl/GeneralTab.java | 6 +- .../viaproxy/ui/impl/OnlineModeTab.java | 150 ++++++++++++++---- 3 files changed, 129 insertions(+), 35 deletions(-) diff --git a/src/main/java/net/raphimc/viaproxy/saves/impl/AccountsSave.java b/src/main/java/net/raphimc/viaproxy/saves/impl/AccountsSave.java index 9598dce..af82d27 100644 --- a/src/main/java/net/raphimc/viaproxy/saves/impl/AccountsSave.java +++ b/src/main/java/net/raphimc/viaproxy/saves/impl/AccountsSave.java @@ -9,7 +9,9 @@ import net.raphimc.viaproxy.saves.AbstractSave; import net.raphimc.viaproxy.util.logging.Logger; import org.apache.http.impl.client.CloseableHttpClient; -import java.util.*; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; public class AccountsSave extends AbstractSave { @@ -40,6 +42,10 @@ public class AccountsSave extends AbstractSave { this.accounts.add(profile); } + public void addAccount(final int index, final StepMCProfile.MCProfile profile) { + this.accounts.add(index, profile); + } + public void removeAccount(final StepMCProfile.MCProfile profile) { this.accounts.remove(profile); } diff --git a/src/main/java/net/raphimc/viaproxy/ui/impl/GeneralTab.java b/src/main/java/net/raphimc/viaproxy/ui/impl/GeneralTab.java index 992a9d5..35cd1e6 100644 --- a/src/main/java/net/raphimc/viaproxy/ui/impl/GeneralTab.java +++ b/src/main/java/net/raphimc/viaproxy/ui/impl/GeneralTab.java @@ -164,11 +164,7 @@ public class GeneralTab extends AUITab { Options.PROTOCOL_VERSION = serverVersion; Options.BETACRAFT_AUTH = betaCraftAuth; - if (authMethod == 0 && Options.MC_ACCOUNT == null && !ViaProxy.saveManager.accountsSave.getAccounts().isEmpty()) { - Options.MC_ACCOUNT = ViaProxy.saveManager.accountsSave.getAccounts().get(0); - } else if (authMethod == 1) { - Options.OPENAUTHMOD_AUTH = true; - } + if (authMethod == 1) Options.OPENAUTHMOD_AUTH = true; ViaProxy.startProxy(); diff --git a/src/main/java/net/raphimc/viaproxy/ui/impl/OnlineModeTab.java b/src/main/java/net/raphimc/viaproxy/ui/impl/OnlineModeTab.java index 525c4b1..d809284 100644 --- a/src/main/java/net/raphimc/viaproxy/ui/impl/OnlineModeTab.java +++ b/src/main/java/net/raphimc/viaproxy/ui/impl/OnlineModeTab.java @@ -9,6 +9,8 @@ import net.raphimc.viaproxy.ui.ViaProxyUI; import net.raphimc.viaproxy.ui.popups.AddAccountPopup; import javax.swing.*; +import java.awt.event.KeyAdapter; +import java.awt.event.KeyEvent; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.util.concurrent.TimeoutException; @@ -63,39 +65,79 @@ public class OnlineModeTab extends AUITab { DefaultListModel model = new DefaultListModel<>(); this.accountsList = new JList<>(model); + this.accountsList.addMouseListener(new MouseAdapter() { + public void mousePressed(MouseEvent e) { + if (SwingUtilities.isRightMouseButton(e)) { + int row = accountsList.locationToIndex(e.getPoint()); + accountsList.setSelectedIndex(row); + } + } + }); + this.accountsList.addKeyListener(new KeyAdapter() { + @Override + public void keyPressed(KeyEvent e) { + int index = accountsList.getSelectedIndex(); + if (index == -1) return; + if (e.getKeyCode() == KeyEvent.VK_UP) { + moveUp(index); + e.consume(); + } else if (e.getKeyCode() == KeyEvent.VK_DOWN) { + moveDown(index); + e.consume(); + } + } + }); scrollPane.setViewportView(this.accountsList); JPopupMenu contextMenu = new JPopupMenu(); - JMenuItem selectItem = new JMenuItem("Use to join online mode servers"); - selectItem.addActionListener(e -> { - int index = this.accountsList.getSelectedIndex(); - if (index != -1) { - final StepMCProfile.MCProfile account = ViaProxy.saveManager.accountsSave.getAccounts().get(index); - if (account != null) { - Options.MC_ACCOUNT = account; - } else { - throw new IllegalStateException("Account is null"); + { + JMenuItem selectItem = new JMenuItem("Use to join online mode servers"); + selectItem.addActionListener(e -> { + int index = this.accountsList.getSelectedIndex(); + if (index != -1) this.markSelected(index); + }); + contextMenu.add(selectItem); + } + { + JMenuItem removeItem = new JMenuItem("Remove"); + removeItem.addActionListener(e -> { + int index = this.accountsList.getSelectedIndex(); + if (index != -1) { + String removedName = model.remove(index); + if (removedName.contains("<")) { + if (model.isEmpty()) this.markSelected(-1); + else this.markSelected(0); + } + + final StepMCProfile.MCProfile account = ViaProxy.saveManager.accountsSave.getAccounts().get(index); + if (account != null) { + ViaProxy.saveManager.accountsSave.removeAccount(account); + ViaProxy.saveManager.save(); + } else { + throw new IllegalStateException("Account is null"); + } } - } - }); - contextMenu.add(selectItem); - JMenuItem removeItem = new JMenuItem("Remove"); - removeItem.addActionListener(e -> { - int index = this.accountsList.getSelectedIndex(); - if (index != -1) { - model.remove(index); - final StepMCProfile.MCProfile account = ViaProxy.saveManager.accountsSave.getAccounts().get(index); - if (account != null) { - ViaProxy.saveManager.accountsSave.removeAccount(account); - ViaProxy.saveManager.save(); - } else { - throw new IllegalStateException("Account is null"); - } - } - if (index < model.getSize()) this.accountsList.setSelectedIndex(index); - else if (index > 0) this.accountsList.setSelectedIndex(index - 1); - }); - contextMenu.add(removeItem); + if (index < model.getSize()) this.accountsList.setSelectedIndex(index); + else if (index > 0) this.accountsList.setSelectedIndex(index - 1); + }); + contextMenu.add(removeItem); + } + { + JMenuItem moveUp = new JMenuItem("Move up ↑"); + moveUp.addActionListener(e -> { + int index = this.accountsList.getSelectedIndex(); + if (index != -1) this.moveUp(index); + }); + contextMenu.add(moveUp); + } + { + JMenuItem moveDown = new JMenuItem("Move down ↓"); + moveDown.addActionListener(e -> { + int index = this.accountsList.getSelectedIndex(); + if (index != -1) this.moveDown(index); + }); + contextMenu.add(moveDown); + } this.accountsList.setComponentPopupMenu(contextMenu); } { @@ -145,6 +187,7 @@ public class OnlineModeTab extends AUITab { public void setReady() { final DefaultListModel model = (DefaultListModel) this.accountsList.getModel(); ViaProxy.saveManager.accountsSave.getAccounts().forEach(account -> model.addElement(account.name())); + if (!model.isEmpty()) this.markSelected(0); } private void closePopup() { @@ -155,4 +198,53 @@ public class OnlineModeTab extends AUITab { this.addAccountButton.setEnabled(true); } + private void markSelected(final int index) { + if (index == -1) { + Options.MC_ACCOUNT = null; + return; + } + + DefaultListModel model = (DefaultListModel) this.accountsList.getModel(); + for (int i = 0; i < model.getSize(); i++) model.setElementAt(model.getElementAt(i).replaceAll("<[^>]+>", ""), i); + model.setElementAt("" + model.getElementAt(index) + "", index); + + StepMCProfile.MCProfile account = ViaProxy.saveManager.accountsSave.getAccounts().get(index); + if (account != null) Options.MC_ACCOUNT = account; + else throw new IllegalStateException("Account is null"); //Lists desynced + } + + private void moveUp(final int index) { + DefaultListModel model = (DefaultListModel) this.accountsList.getModel(); + if (index == 0) return; + String name = model.remove(index); + model.add(index - 1, name); + this.accountsList.setSelectedIndex(index - 1); + + StepMCProfile.MCProfile account = ViaProxy.saveManager.accountsSave.getAccounts().get(index); + if (account != null) { + ViaProxy.saveManager.accountsSave.removeAccount(account); + ViaProxy.saveManager.accountsSave.addAccount(index - 1, account); + ViaProxy.saveManager.save(); + } else { + throw new IllegalStateException("Account is null"); + } + } + + private void moveDown(final int index) { + DefaultListModel model = (DefaultListModel) this.accountsList.getModel(); + if (index == model.getSize() - 1) return; + String name = model.remove(index); + model.add(index + 1, name); + this.accountsList.setSelectedIndex(index + 1); + + StepMCProfile.MCProfile account = ViaProxy.saveManager.accountsSave.getAccounts().get(index); + if (account != null) { + ViaProxy.saveManager.accountsSave.removeAccount(account); + ViaProxy.saveManager.accountsSave.addAccount(index + 1, account); + ViaProxy.saveManager.save(); + } else { + throw new IllegalStateException("Account is null"); + } + } + }