mirror of
https://github.com/ViaVersion/ViaProxy.git
synced 2024-11-14 19:15:08 -05:00
Mark selected account in list and allow moving accounts using arrow keys and context menu
This commit is contained in:
parent
2b76d000c4
commit
06e45c4886
3 changed files with 129 additions and 35 deletions
|
@ -9,7 +9,9 @@ import net.raphimc.viaproxy.saves.AbstractSave;
|
||||||
import net.raphimc.viaproxy.util.logging.Logger;
|
import net.raphimc.viaproxy.util.logging.Logger;
|
||||||
import org.apache.http.impl.client.CloseableHttpClient;
|
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 {
|
public class AccountsSave extends AbstractSave {
|
||||||
|
|
||||||
|
@ -40,6 +42,10 @@ public class AccountsSave extends AbstractSave {
|
||||||
this.accounts.add(profile);
|
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) {
|
public void removeAccount(final StepMCProfile.MCProfile profile) {
|
||||||
this.accounts.remove(profile);
|
this.accounts.remove(profile);
|
||||||
}
|
}
|
||||||
|
|
|
@ -164,11 +164,7 @@ public class GeneralTab extends AUITab {
|
||||||
Options.PROTOCOL_VERSION = serverVersion;
|
Options.PROTOCOL_VERSION = serverVersion;
|
||||||
Options.BETACRAFT_AUTH = betaCraftAuth;
|
Options.BETACRAFT_AUTH = betaCraftAuth;
|
||||||
|
|
||||||
if (authMethod == 0 && Options.MC_ACCOUNT == null && !ViaProxy.saveManager.accountsSave.getAccounts().isEmpty()) {
|
if (authMethod == 1) Options.OPENAUTHMOD_AUTH = true;
|
||||||
Options.MC_ACCOUNT = ViaProxy.saveManager.accountsSave.getAccounts().get(0);
|
|
||||||
} else if (authMethod == 1) {
|
|
||||||
Options.OPENAUTHMOD_AUTH = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
ViaProxy.startProxy();
|
ViaProxy.startProxy();
|
||||||
|
|
||||||
|
|
|
@ -9,6 +9,8 @@ import net.raphimc.viaproxy.ui.ViaProxyUI;
|
||||||
import net.raphimc.viaproxy.ui.popups.AddAccountPopup;
|
import net.raphimc.viaproxy.ui.popups.AddAccountPopup;
|
||||||
|
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
|
import java.awt.event.KeyAdapter;
|
||||||
|
import java.awt.event.KeyEvent;
|
||||||
import java.awt.event.MouseAdapter;
|
import java.awt.event.MouseAdapter;
|
||||||
import java.awt.event.MouseEvent;
|
import java.awt.event.MouseEvent;
|
||||||
import java.util.concurrent.TimeoutException;
|
import java.util.concurrent.TimeoutException;
|
||||||
|
@ -63,39 +65,79 @@ public class OnlineModeTab extends AUITab {
|
||||||
|
|
||||||
DefaultListModel<String> model = new DefaultListModel<>();
|
DefaultListModel<String> model = new DefaultListModel<>();
|
||||||
this.accountsList = new JList<>(model);
|
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);
|
scrollPane.setViewportView(this.accountsList);
|
||||||
|
|
||||||
JPopupMenu contextMenu = new JPopupMenu();
|
JPopupMenu contextMenu = new JPopupMenu();
|
||||||
JMenuItem selectItem = new JMenuItem("Use to join online mode servers");
|
{
|
||||||
selectItem.addActionListener(e -> {
|
JMenuItem selectItem = new JMenuItem("Use to join online mode servers");
|
||||||
int index = this.accountsList.getSelectedIndex();
|
selectItem.addActionListener(e -> {
|
||||||
if (index != -1) {
|
int index = this.accountsList.getSelectedIndex();
|
||||||
final StepMCProfile.MCProfile account = ViaProxy.saveManager.accountsSave.getAccounts().get(index);
|
if (index != -1) this.markSelected(index);
|
||||||
if (account != null) {
|
});
|
||||||
Options.MC_ACCOUNT = account;
|
contextMenu.add(selectItem);
|
||||||
} else {
|
}
|
||||||
throw new IllegalStateException("Account is null");
|
{
|
||||||
|
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");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
if (index < model.getSize()) this.accountsList.setSelectedIndex(index);
|
||||||
});
|
else if (index > 0) this.accountsList.setSelectedIndex(index - 1);
|
||||||
contextMenu.add(selectItem);
|
});
|
||||||
JMenuItem removeItem = new JMenuItem("Remove");
|
contextMenu.add(removeItem);
|
||||||
removeItem.addActionListener(e -> {
|
}
|
||||||
int index = this.accountsList.getSelectedIndex();
|
{
|
||||||
if (index != -1) {
|
JMenuItem moveUp = new JMenuItem("Move up ↑");
|
||||||
model.remove(index);
|
moveUp.addActionListener(e -> {
|
||||||
final StepMCProfile.MCProfile account = ViaProxy.saveManager.accountsSave.getAccounts().get(index);
|
int index = this.accountsList.getSelectedIndex();
|
||||||
if (account != null) {
|
if (index != -1) this.moveUp(index);
|
||||||
ViaProxy.saveManager.accountsSave.removeAccount(account);
|
});
|
||||||
ViaProxy.saveManager.save();
|
contextMenu.add(moveUp);
|
||||||
} else {
|
}
|
||||||
throw new IllegalStateException("Account is null");
|
{
|
||||||
}
|
JMenuItem moveDown = new JMenuItem("Move down ↓");
|
||||||
}
|
moveDown.addActionListener(e -> {
|
||||||
if (index < model.getSize()) this.accountsList.setSelectedIndex(index);
|
int index = this.accountsList.getSelectedIndex();
|
||||||
else if (index > 0) this.accountsList.setSelectedIndex(index - 1);
|
if (index != -1) this.moveDown(index);
|
||||||
});
|
});
|
||||||
contextMenu.add(removeItem);
|
contextMenu.add(moveDown);
|
||||||
|
}
|
||||||
this.accountsList.setComponentPopupMenu(contextMenu);
|
this.accountsList.setComponentPopupMenu(contextMenu);
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
|
@ -145,6 +187,7 @@ public class OnlineModeTab extends AUITab {
|
||||||
public void setReady() {
|
public void setReady() {
|
||||||
final DefaultListModel<String> model = (DefaultListModel<String>) this.accountsList.getModel();
|
final DefaultListModel<String> model = (DefaultListModel<String>) this.accountsList.getModel();
|
||||||
ViaProxy.saveManager.accountsSave.getAccounts().forEach(account -> model.addElement(account.name()));
|
ViaProxy.saveManager.accountsSave.getAccounts().forEach(account -> model.addElement(account.name()));
|
||||||
|
if (!model.isEmpty()) this.markSelected(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void closePopup() {
|
private void closePopup() {
|
||||||
|
@ -155,4 +198,53 @@ public class OnlineModeTab extends AUITab {
|
||||||
this.addAccountButton.setEnabled(true);
|
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");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue