Skip to content

Commit c087dc9

Browse files
committed
Refactor settings
Make update check / show file container name persistent
1 parent 8f3e5a1 commit c087dc9

File tree

5 files changed

+58
-37
lines changed

5 files changed

+58
-37
lines changed

src/main/java/the/bytecode/club/bytecodeviewer/BytecodeViewer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@ public static void main(String[] args)
8989
return;
9090
if (CLI == CommandLineInput.OPEN_FILE)
9191
{
92-
viewer = new MainViewerGUI();
9392
Settings.loadGUI();
93+
viewer = new MainViewerGUI();
9494
Boot.boot();
9595
BytecodeViewer.BOOT(args, false);
9696
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package the.bytecode.club.bytecodeviewer;
2+
3+
/**
4+
* Used to handle loading/saving the GUI (options).
5+
*
6+
* @author Konloch
7+
*/
8+
public class Setting
9+
{
10+
private String key;
11+
private String value;
12+
13+
public Setting(String key, String value)
14+
{
15+
this.key = key;
16+
this.value = value;
17+
Settings.ALL_SETTINGS.put(this.key, this);
18+
}
19+
20+
public String get()
21+
{
22+
return this.value;
23+
}
24+
25+
public boolean getBool()
26+
{
27+
return Boolean.parseBoolean(this.value);
28+
}
29+
30+
public void set(Object value)
31+
{
32+
this.value = value.toString();
33+
}
34+
35+
public boolean isEmpty()
36+
{
37+
return this.value == null || this.value.isEmpty();
38+
}
39+
}

src/main/java/the/bytecode/club/bytecodeviewer/Settings.java

Lines changed: 9 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -14,36 +14,14 @@
1414
*
1515
* @author Konloch
1616
*/
17-
public class Settings<T>
17+
public class Settings
1818
{
19-
private static final Map<String, Settings> ALL_SETTINGS = new HashMap<>();
19+
static final Map<String, Setting> ALL_SETTINGS = new HashMap<>();
2020

21-
public static final Settings<String> JAVA_LOCATION = new Settings<>("javalocation");
22-
public static final Settings<String> PATH = new Settings<>("path");
23-
24-
private String key;
25-
private T value;
26-
27-
public Settings(String key)
28-
{
29-
this.key = key;
30-
ALL_SETTINGS.put(this.key, this);
31-
}
32-
33-
public T get()
34-
{
35-
return this.value;
36-
}
37-
38-
public void set(T value)
39-
{
40-
this.value = value;
41-
}
42-
43-
public boolean isEmpty()
44-
{
45-
return this.value == null || (this.value instanceof String && ((String) this.value).isEmpty());
46-
}
21+
public static final Setting JAVA_LOCATION = new Setting("javalocation", "");
22+
public static final Setting PATH = new Setting("path", "");
23+
public static final Setting SHOW_CONTAINER_NAME = new Setting("showfilename", "false");
24+
public static final Setting DO_UPDATE_CHECK = new Setting("doupdatecheck", "true");
4725

4826
public static void saveGUI()
4927
{
@@ -59,11 +37,11 @@ public static void saveGUI()
5937
settings.add("settings", new JsonObject());
6038
}
6139
JsonObject rootSettings = settings.get("settings").asObject();
62-
for (Map.Entry<String, Settings> setting : Settings.ALL_SETTINGS.entrySet())
40+
for (Map.Entry<String, Setting> setting : Settings.ALL_SETTINGS.entrySet())
6341
{
6442
if (setting.getValue().get() != null)
6543
{
66-
rootSettings.add(setting.getKey(), setting.getValue().get().toString());
44+
rootSettings.add(setting.getKey(), setting.getValue().get());
6745
}
6846
}
6947
FileOutputStream out = new FileOutputStream(BytecodeViewer.settingsFile);
@@ -95,7 +73,7 @@ public static void loadGUI()
9573
if (settings.get("settings") != null)
9674
{
9775
JsonObject rootSettings = settings.get("settings").asObject();
98-
for (Map.Entry<String, Settings> setting : Settings.ALL_SETTINGS.entrySet())
76+
for (Map.Entry<String, Setting> setting : Settings.ALL_SETTINGS.entrySet())
9977
{
10078
if (rootSettings.get(setting.getKey()) != null)
10179
{

src/main/java/the/bytecode/club/bytecodeviewer/gui/FileChooser.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
package the.bytecode.club.bytecodeviewer.gui;
22

33
import the.bytecode.club.bytecodeviewer.BytecodeViewer;
4-
import the.bytecode.club.bytecodeviewer.Settings;
4+
import the.bytecode.club.bytecodeviewer.Setting;
55

66
import javax.swing.*;
77
import javax.swing.filechooser.FileFilter;
88
import java.io.File;
99

1010
public class FileChooser
1111
{
12-
private Settings<String> target;
12+
private Setting target;
1313
private String message;
1414

15-
public FileChooser(Settings<String> target, String message)
15+
public FileChooser(Setting target, String message)
1616
{
1717
this.target = target;
1818
this.message = message;

src/main/java/the/bytecode/club/bytecodeviewer/gui/MainViewerGUI.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,8 @@ else if ((oldState & Frame.MAXIMIZED_BOTH) != 0 && (newState & Frame.MAXIMIZED_B
263263

264264
settingsMenu.add(new JSeparator());
265265

266-
mntmUpdateCheck.setSelected(true);
266+
mntmUpdateCheck.setSelected(Settings.DO_UPDATE_CHECK.getBool());
267+
mntmUpdateCheck.addActionListener(e -> Settings.DO_UPDATE_CHECK.set(mntmUpdateCheck.isSelected()));
267268
settingsMenu.add(mntmUpdateCheck);
268269

269270
settingsMenu.add(new JSeparator());
@@ -389,7 +390,7 @@ else if ((oldState & Frame.MAXIMIZED_BOTH) != 0 && (newState & Frame.MAXIMIZED_B
389390
viewMenu.add(mnFontSize);
390391
mnFontSize.add(fontSpinner);
391392

392-
viewMenu.add(mnShowContainer);
393+
mnShowContainer.setSelected(Settings.SHOW_CONTAINER_NAME.getBool());
393394
mnShowContainer.addItemListener(e -> {
394395
JTabbedPane tabs = workPane.tabs;
395396
Component[] components = tabs.getComponents();
@@ -404,7 +405,10 @@ else if ((oldState & Frame.MAXIMIZED_BOTH) != 0 && (newState & Frame.MAXIMIZED_B
404405
workPane.tabs.setTitleAt(idx, c.getName());
405406
}
406407
}
408+
Settings.SHOW_CONTAINER_NAME.set(mnShowContainer.isSelected());
407409
});
410+
viewMenu.add(mnShowContainer);
411+
408412
panelGroup1.setSelected(allDecompilersRev.get(panelGroup1).get(Decompiler.FERNFLOWER).getModel(), true);
409413
panelGroup2.setSelected(allDecompilersRev.get(panelGroup2).get(Decompiler.BYTECODE).getModel(), true);
410414
panelGroup3.setSelected(allDecompilersRev.get(panelGroup3).get(null).getModel(), true);

0 commit comments

Comments
 (0)