Mark selected account in list and allow moving accounts using arrow keys and context menu

This commit is contained in:
Lenni0451 2023-01-07 13:23:20 +01:00
parent 2b76d000c4
commit 06e45c4886
3 changed files with 129 additions and 35 deletions

View File

@ -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);
}

View File

@ -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();

View File

@ -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,27 +65,50 @@ public class OnlineModeTab extends AUITab {
DefaultListModel<String> model = new DefaultListModel<>();
this.accountsList = new JList<>(model);
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");
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) this.markSelected(index);
});
contextMenu.add(selectItem);
}
{
JMenuItem removeItem = new JMenuItem("Remove");
removeItem.addActionListener(e -> {
int index = this.accountsList.getSelectedIndex();
if (index != -1) {
model.remove(index);
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);
@ -96,6 +121,23 @@ public class OnlineModeTab extends AUITab {
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<String> model = (DefaultListModel<String>) 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<String> model = (DefaultListModel<String>) this.accountsList.getModel();
for (int i = 0; i < model.getSize(); i++) model.setElementAt(model.getElementAt(i).replaceAll("<[^>]+>", ""), i);
model.setElementAt("<html><span style=\"color:rgb(0, 180, 0)\"><b>" + model.getElementAt(index) + "</b></span></html>", 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<String> model = (DefaultListModel<String>) 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<String> model = (DefaultListModel<String>) 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");
}
}
}