Save ui settings on close and load them on launch

This commit is contained in:
Lenni0451 2023-01-13 23:14:34 +01:00
parent 9bd0ec0687
commit 47785ad9af
5 changed files with 134 additions and 8 deletions

View File

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

View File

@ -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 <http://www.gnu.org/licenses/>.
*/
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<String, String> values;
public UISave() {
super("ui");
this.values = new HashMap<>();
}
@Override
public void load(JsonElement jsonElement) {
this.values.clear();
for (Map.Entry<String, JsonElement> entry : jsonElement.getAsJsonObject().entrySet()) this.values.put(entry.getKey(), entry.getValue().getAsString());
}
@Override
public JsonElement save() {
JsonObject jsonObject = new JsonObject();
for (Map.Entry<String, String> 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<Integer> minimum = (Comparable<Integer>) model.getMinimum();
Comparable<Integer> maximum = (Comparable<Integer>) 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) {
}
}
}

View File

@ -43,4 +43,7 @@ public abstract class AUITab {
public void setReady() {
}
public void onClose() {
}
}

View File

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

View File

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