From 47785ad9af35a11abd874ca81047066443aea754 Mon Sep 17 00:00:00 2001 From: Lenni0451 <20379977+Lenni0451@users.noreply.github.com> Date: Fri, 13 Jan 2023 23:14:34 +0100 Subject: [PATCH] Save ui settings on close and load them on launch --- .../raphimc/viaproxy/saves/SaveManager.java | 2 + .../raphimc/viaproxy/saves/impl/UISave.java | 94 +++++++++++++++++++ .../java/net/raphimc/viaproxy/ui/AUITab.java | 3 + .../net/raphimc/viaproxy/ui/ViaProxyUI.java | 8 ++ .../raphimc/viaproxy/ui/impl/GeneralTab.java | 35 +++++-- 5 files changed, 134 insertions(+), 8 deletions(-) create mode 100644 src/main/java/net/raphimc/viaproxy/saves/impl/UISave.java diff --git a/src/main/java/net/raphimc/viaproxy/saves/SaveManager.java b/src/main/java/net/raphimc/viaproxy/saves/SaveManager.java index 4a4582a..a16704e 100644 --- a/src/main/java/net/raphimc/viaproxy/saves/SaveManager.java +++ b/src/main/java/net/raphimc/viaproxy/saves/SaveManager.java @@ -21,6 +21,7 @@ import com.google.gson.Gson; import com.google.gson.JsonObject; import net.lenni0451.reflect.stream.RStream; import net.raphimc.viaproxy.saves.impl.AccountsSave; +import net.raphimc.viaproxy.saves.impl.UISave; import net.raphimc.viaproxy.util.logging.Logger; import java.io.File; @@ -33,6 +34,7 @@ public class SaveManager { private static final Gson GSON = new Gson(); public final AccountsSave accountsSave = new AccountsSave(); + public final UISave uiSave = new UISave(); public SaveManager() { this.load(); diff --git a/src/main/java/net/raphimc/viaproxy/saves/impl/UISave.java b/src/main/java/net/raphimc/viaproxy/saves/impl/UISave.java new file mode 100644 index 0000000..25ac9f9 --- /dev/null +++ b/src/main/java/net/raphimc/viaproxy/saves/impl/UISave.java @@ -0,0 +1,94 @@ +/* + * This file is part of ViaProxy - https://github.com/RaphiMC/ViaProxy + * Copyright (C) 2023 RK_01/RaphiMC and contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package net.raphimc.viaproxy.saves.impl; + +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import net.raphimc.viaproxy.saves.AbstractSave; + +import javax.swing.*; +import java.util.HashMap; +import java.util.Map; + +public class UISave extends AbstractSave { + + private final Map values; + + public UISave() { + super("ui"); + + this.values = new HashMap<>(); + } + + @Override + public void load(JsonElement jsonElement) { + this.values.clear(); + for (Map.Entry entry : jsonElement.getAsJsonObject().entrySet()) this.values.put(entry.getKey(), entry.getValue().getAsString()); + } + + @Override + public JsonElement save() { + JsonObject jsonObject = new JsonObject(); + for (Map.Entry entry : this.values.entrySet()) jsonObject.addProperty(entry.getKey(), entry.getValue()); + return jsonObject; + } + + public void put(final String key, final String value) { + this.values.put(key, value); + } + + public void loadTextField(final String key, final JTextField textField) { + try { + String value = this.values.get(key); + if (value != null) textField.setText(value); + } catch (Throwable ignored) { + } + } + + public void loadComboBox(final String key, final JComboBox comboBox) { + try { + int index = Integer.parseInt(this.values.get(key)); + if (index >= 0 && index < comboBox.getItemCount()) comboBox.setSelectedIndex(index); + } catch (Throwable ignored) { + } + } + + public void loadSpinner(final String key, final JSpinner spinner) { + try { + Integer value = Integer.valueOf(this.values.get(key)); + if (spinner.getModel() instanceof SpinnerNumberModel) { + SpinnerNumberModel model = (SpinnerNumberModel) spinner.getModel(); + Comparable minimum = (Comparable) model.getMinimum(); + Comparable maximum = (Comparable) model.getMaximum(); + if (minimum.compareTo(value) <= 0 && maximum.compareTo(value) >= 0) spinner.setValue(value); + } else { + spinner.setValue(value); + } + } catch (Throwable ignored) { + } + } + + public void loadCheckBox(final String key, final JCheckBox checkBox) { + try { + boolean value = Boolean.parseBoolean(this.values.get(key)); + checkBox.setSelected(value); + } catch (Throwable ignored) { + } + } + +} diff --git a/src/main/java/net/raphimc/viaproxy/ui/AUITab.java b/src/main/java/net/raphimc/viaproxy/ui/AUITab.java index f26c794..4dbfdad 100644 --- a/src/main/java/net/raphimc/viaproxy/ui/AUITab.java +++ b/src/main/java/net/raphimc/viaproxy/ui/AUITab.java @@ -43,4 +43,7 @@ public abstract class AUITab { public void setReady() { } + public void onClose() { + } + } diff --git a/src/main/java/net/raphimc/viaproxy/ui/ViaProxyUI.java b/src/main/java/net/raphimc/viaproxy/ui/ViaProxyUI.java index 1d3e943..ee8d4e0 100644 --- a/src/main/java/net/raphimc/viaproxy/ui/ViaProxyUI.java +++ b/src/main/java/net/raphimc/viaproxy/ui/ViaProxyUI.java @@ -26,6 +26,8 @@ import net.raphimc.viaproxy.util.logging.Logger; import javax.swing.*; import java.awt.*; +import java.awt.event.WindowAdapter; +import java.awt.event.WindowEvent; import java.net.URI; import java.util.ArrayList; import java.util.List; @@ -67,6 +69,12 @@ public class ViaProxyUI extends JFrame { this.setTitle("ViaProxy v" + ViaProxy.VERSION); this.setIconImage(this.icon.getImage()); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + this.addWindowListener(new WindowAdapter() { + @Override + public void windowClosing(WindowEvent e) { + for (AUITab tab : tabs) tab.onClose(); + } + }); this.setSize(500, 403); this.setResizable(false); this.setLocationRelativeTo(null); 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 05a9369..fe778c1 100644 --- a/src/main/java/net/raphimc/viaproxy/ui/impl/GeneralTab.java +++ b/src/main/java/net/raphimc/viaproxy/ui/impl/GeneralTab.java @@ -21,6 +21,7 @@ import com.google.common.net.HostAndPort; import net.raphimc.viaprotocolhack.util.VersionEnum; import net.raphimc.viaproxy.ViaProxy; import net.raphimc.viaproxy.cli.options.Options; +import net.raphimc.viaproxy.saves.impl.UISave; import net.raphimc.viaproxy.ui.AUITab; import net.raphimc.viaproxy.ui.ViaProxyUI; import net.raphimc.viaproxy.util.logging.Logger; @@ -77,6 +78,7 @@ public class GeneralTab extends AUITab { this.serverAddress = new JTextField(); this.serverAddress.setBounds(10, 70, 465, 20); + ViaProxy.saveManager.uiSave.loadTextField("server_address", this.serverAddress); contentPane.add(this.serverAddress); } { @@ -96,6 +98,7 @@ public class GeneralTab extends AUITab { return super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); } }); + ViaProxy.saveManager.uiSave.loadComboBox("server_version", this.serverVersion); contentPane.add(this.serverVersion); } { @@ -105,6 +108,7 @@ public class GeneralTab extends AUITab { this.bindPort = new JSpinner(new SpinnerNumberModel(25568, 1, 65535, 1)); this.bindPort.setBounds(10, 170, 465, 20); + ViaProxy.saveManager.uiSave.loadSpinner("bind_port", this.bindPort); contentPane.add(this.bindPort); } { @@ -114,17 +118,20 @@ public class GeneralTab extends AUITab { this.authMethod = new JComboBox<>(new String[]{"Use no account", "Use selected account", "Use OpenAuthMod"}); this.authMethod.setBounds(10, 220, 465, 20); + ViaProxy.saveManager.uiSave.loadComboBox("auth_method", this.authMethod); contentPane.add(this.authMethod); } { this.betaCraftAuth = new JCheckBox("BetaCraft Auth (Classic)"); this.betaCraftAuth.setBounds(10, 250, 150, 20); + ViaProxy.saveManager.uiSave.loadCheckBox("betacraft_auth", this.betaCraftAuth); contentPane.add(this.betaCraftAuth); } { this.proxyOnlineMode = new JCheckBox("Proxy Online Mode"); this.proxyOnlineMode.setBounds(350, 250, 465, 20); this.proxyOnlineMode.setToolTipText("Enabling Proxy Online Mode requires your client to have a valid account.\nProxy Online Mode allows your client to see skins on online mode servers and use the signed chat features."); + ViaProxy.saveManager.uiSave.loadCheckBox("proxy_online_mode", this.proxyOnlineMode); contentPane.add(this.proxyOnlineMode); } { @@ -145,6 +152,26 @@ public class GeneralTab extends AUITab { } } + @Override + public void setReady() { + SwingUtilities.invokeLater(() -> { + this.stateButton.setText("Start"); + this.stateButton.setEnabled(true); + }); + } + + @Override + public void onClose() { + UISave save = ViaProxy.saveManager.uiSave; + save.put("server_address", this.serverAddress.getText()); + save.put("server_version", String.valueOf(this.serverVersion.getSelectedIndex())); + save.put("bind_port", String.valueOf(this.bindPort.getValue())); + save.put("auth_method", String.valueOf(this.authMethod.getSelectedIndex())); + save.put("betacraft_auth", String.valueOf(this.betaCraftAuth.isSelected())); + save.put("proxy_online_mode", String.valueOf(this.proxyOnlineMode.isSelected())); + ViaProxy.saveManager.save(); + } + private void setComponentsEnabled(final boolean state) { this.serverAddress.setEnabled(state); this.serverVersion.setEnabled(state); @@ -233,12 +260,4 @@ public class GeneralTab extends AUITab { this.setComponentsEnabled(true); } - @Override - public void setReady() { - SwingUtilities.invokeLater(() -> { - this.stateButton.setText("Start"); - this.stateButton.setEnabled(true); - }); - } - }