Skip to content

Commit 47785ad

Browse files
committed
Save ui settings on close and load them on launch
1 parent 9bd0ec0 commit 47785ad

File tree

5 files changed

+134
-8
lines changed

5 files changed

+134
-8
lines changed

src/main/java/net/raphimc/viaproxy/saves/SaveManager.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import com.google.gson.JsonObject;
2222
import net.lenni0451.reflect.stream.RStream;
2323
import net.raphimc.viaproxy.saves.impl.AccountsSave;
24+
import net.raphimc.viaproxy.saves.impl.UISave;
2425
import net.raphimc.viaproxy.util.logging.Logger;
2526

2627
import java.io.File;
@@ -33,6 +34,7 @@ public class SaveManager {
3334
private static final Gson GSON = new Gson();
3435

3536
public final AccountsSave accountsSave = new AccountsSave();
37+
public final UISave uiSave = new UISave();
3638

3739
public SaveManager() {
3840
this.load();
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
* This file is part of ViaProxy - https://github.com/RaphiMC/ViaProxy
3+
* Copyright (C) 2023 RK_01/RaphiMC and contributors
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
package net.raphimc.viaproxy.saves.impl;
19+
20+
import com.google.gson.JsonElement;
21+
import com.google.gson.JsonObject;
22+
import net.raphimc.viaproxy.saves.AbstractSave;
23+
24+
import javax.swing.*;
25+
import java.util.HashMap;
26+
import java.util.Map;
27+
28+
public class UISave extends AbstractSave {
29+
30+
private final Map<String, String> values;
31+
32+
public UISave() {
33+
super("ui");
34+
35+
this.values = new HashMap<>();
36+
}
37+
38+
@Override
39+
public void load(JsonElement jsonElement) {
40+
this.values.clear();
41+
for (Map.Entry<String, JsonElement> entry : jsonElement.getAsJsonObject().entrySet()) this.values.put(entry.getKey(), entry.getValue().getAsString());
42+
}
43+
44+
@Override
45+
public JsonElement save() {
46+
JsonObject jsonObject = new JsonObject();
47+
for (Map.Entry<String, String> entry : this.values.entrySet()) jsonObject.addProperty(entry.getKey(), entry.getValue());
48+
return jsonObject;
49+
}
50+
51+
public void put(final String key, final String value) {
52+
this.values.put(key, value);
53+
}
54+
55+
public void loadTextField(final String key, final JTextField textField) {
56+
try {
57+
String value = this.values.get(key);
58+
if (value != null) textField.setText(value);
59+
} catch (Throwable ignored) {
60+
}
61+
}
62+
63+
public void loadComboBox(final String key, final JComboBox<?> comboBox) {
64+
try {
65+
int index = Integer.parseInt(this.values.get(key));
66+
if (index >= 0 && index < comboBox.getItemCount()) comboBox.setSelectedIndex(index);
67+
} catch (Throwable ignored) {
68+
}
69+
}
70+
71+
public void loadSpinner(final String key, final JSpinner spinner) {
72+
try {
73+
Integer value = Integer.valueOf(this.values.get(key));
74+
if (spinner.getModel() instanceof SpinnerNumberModel) {
75+
SpinnerNumberModel model = (SpinnerNumberModel) spinner.getModel();
76+
Comparable<Integer> minimum = (Comparable<Integer>) model.getMinimum();
77+
Comparable<Integer> maximum = (Comparable<Integer>) model.getMaximum();
78+
if (minimum.compareTo(value) <= 0 && maximum.compareTo(value) >= 0) spinner.setValue(value);
79+
} else {
80+
spinner.setValue(value);
81+
}
82+
} catch (Throwable ignored) {
83+
}
84+
}
85+
86+
public void loadCheckBox(final String key, final JCheckBox checkBox) {
87+
try {
88+
boolean value = Boolean.parseBoolean(this.values.get(key));
89+
checkBox.setSelected(value);
90+
} catch (Throwable ignored) {
91+
}
92+
}
93+
94+
}

src/main/java/net/raphimc/viaproxy/ui/AUITab.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,7 @@ public void add(final JTabbedPane tabbedPane) {
4343
public void setReady() {
4444
}
4545

46+
public void onClose() {
47+
}
48+
4649
}

src/main/java/net/raphimc/viaproxy/ui/ViaProxyUI.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626

2727
import javax.swing.*;
2828
import java.awt.*;
29+
import java.awt.event.WindowAdapter;
30+
import java.awt.event.WindowEvent;
2931
import java.net.URI;
3032
import java.util.ArrayList;
3133
import java.util.List;
@@ -67,6 +69,12 @@ private void initWindow() {
6769
this.setTitle("ViaProxy v" + ViaProxy.VERSION);
6870
this.setIconImage(this.icon.getImage());
6971
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
72+
this.addWindowListener(new WindowAdapter() {
73+
@Override
74+
public void windowClosing(WindowEvent e) {
75+
for (AUITab tab : tabs) tab.onClose();
76+
}
77+
});
7078
this.setSize(500, 403);
7179
this.setResizable(false);
7280
this.setLocationRelativeTo(null);

src/main/java/net/raphimc/viaproxy/ui/impl/GeneralTab.java

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import net.raphimc.viaprotocolhack.util.VersionEnum;
2222
import net.raphimc.viaproxy.ViaProxy;
2323
import net.raphimc.viaproxy.cli.options.Options;
24+
import net.raphimc.viaproxy.saves.impl.UISave;
2425
import net.raphimc.viaproxy.ui.AUITab;
2526
import net.raphimc.viaproxy.ui.ViaProxyUI;
2627
import net.raphimc.viaproxy.util.logging.Logger;
@@ -77,6 +78,7 @@ public void mouseReleased(MouseEvent e) {
7778

7879
this.serverAddress = new JTextField();
7980
this.serverAddress.setBounds(10, 70, 465, 20);
81+
ViaProxy.saveManager.uiSave.loadTextField("server_address", this.serverAddress);
8082
contentPane.add(this.serverAddress);
8183
}
8284
{
@@ -96,6 +98,7 @@ public Component getListCellRendererComponent(JList<?> list, Object value, int i
9698
return super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
9799
}
98100
});
101+
ViaProxy.saveManager.uiSave.loadComboBox("server_version", this.serverVersion);
99102
contentPane.add(this.serverVersion);
100103
}
101104
{
@@ -105,6 +108,7 @@ public Component getListCellRendererComponent(JList<?> list, Object value, int i
105108

106109
this.bindPort = new JSpinner(new SpinnerNumberModel(25568, 1, 65535, 1));
107110
this.bindPort.setBounds(10, 170, 465, 20);
111+
ViaProxy.saveManager.uiSave.loadSpinner("bind_port", this.bindPort);
108112
contentPane.add(this.bindPort);
109113
}
110114
{
@@ -114,17 +118,20 @@ public Component getListCellRendererComponent(JList<?> list, Object value, int i
114118

115119
this.authMethod = new JComboBox<>(new String[]{"Use no account", "Use selected account", "Use OpenAuthMod"});
116120
this.authMethod.setBounds(10, 220, 465, 20);
121+
ViaProxy.saveManager.uiSave.loadComboBox("auth_method", this.authMethod);
117122
contentPane.add(this.authMethod);
118123
}
119124
{
120125
this.betaCraftAuth = new JCheckBox("BetaCraft Auth (Classic)");
121126
this.betaCraftAuth.setBounds(10, 250, 150, 20);
127+
ViaProxy.saveManager.uiSave.loadCheckBox("betacraft_auth", this.betaCraftAuth);
122128
contentPane.add(this.betaCraftAuth);
123129
}
124130
{
125131
this.proxyOnlineMode = new JCheckBox("Proxy Online Mode");
126132
this.proxyOnlineMode.setBounds(350, 250, 465, 20);
127133
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.");
134+
ViaProxy.saveManager.uiSave.loadCheckBox("proxy_online_mode", this.proxyOnlineMode);
128135
contentPane.add(this.proxyOnlineMode);
129136
}
130137
{
@@ -145,6 +152,26 @@ public Component getListCellRendererComponent(JList<?> list, Object value, int i
145152
}
146153
}
147154

155+
@Override
156+
public void setReady() {
157+
SwingUtilities.invokeLater(() -> {
158+
this.stateButton.setText("Start");
159+
this.stateButton.setEnabled(true);
160+
});
161+
}
162+
163+
@Override
164+
public void onClose() {
165+
UISave save = ViaProxy.saveManager.uiSave;
166+
save.put("server_address", this.serverAddress.getText());
167+
save.put("server_version", String.valueOf(this.serverVersion.getSelectedIndex()));
168+
save.put("bind_port", String.valueOf(this.bindPort.getValue()));
169+
save.put("auth_method", String.valueOf(this.authMethod.getSelectedIndex()));
170+
save.put("betacraft_auth", String.valueOf(this.betaCraftAuth.isSelected()));
171+
save.put("proxy_online_mode", String.valueOf(this.proxyOnlineMode.isSelected()));
172+
ViaProxy.saveManager.save();
173+
}
174+
148175
private void setComponentsEnabled(final boolean state) {
149176
this.serverAddress.setEnabled(state);
150177
this.serverVersion.setEnabled(state);
@@ -233,12 +260,4 @@ private void stop() {
233260
this.setComponentsEnabled(true);
234261
}
235262

236-
@Override
237-
public void setReady() {
238-
SwingUtilities.invokeLater(() -> {
239-
this.stateButton.setText("Start");
240-
this.stateButton.setEnabled(true);
241-
});
242-
}
243-
244263
}

0 commit comments

Comments
 (0)