Skip to content

Commit b4d05f5

Browse files
committed
Font options GUI, Settings rework
1 parent 15b52a1 commit b4d05f5

File tree

7 files changed

+104
-64
lines changed

7 files changed

+104
-64
lines changed

pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@
7676
<artifactId>zt-zip</artifactId>
7777
<version>1.9</version>
7878
</dependency>
79+
<dependency>
80+
<groupId>com.miglayout</groupId>
81+
<artifactId>miglayout-swing</artifactId>
82+
<version>5.0</version>
83+
</dependency>
7984
<dependency>
8085
<groupId>eu.bibl</groupId>
8186
<artifactId>byteanalysis</artifactId>
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package the.bytecode.club.jda.gui;
2+
3+
import net.miginfocom.swing.MigLayout;
4+
import the.bytecode.club.jda.settings.Settings;
5+
6+
import javax.swing.*;
7+
import java.awt.*;
8+
9+
public class FontOptionsDialog {
10+
private final JPanel dialog;
11+
private final JSpinner sizeSpinner;
12+
private final JComboBox<String> fontBox;
13+
private final JCheckBox boldBox, italicsBox;
14+
15+
public FontOptionsDialog() {
16+
dialog = new JPanel(new MigLayout());
17+
18+
dialog.add(new JLabel("Font Family:"));
19+
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
20+
String[] array = ge.getAvailableFontFamilyNames();
21+
dialog.add(fontBox = new JComboBox<>(array), "span, wrap");
22+
fontBox.setEditable(true);
23+
fontBox.setSelectedItem(Settings.FONT_FAMILY.get());
24+
25+
dialog.add(new JLabel("Font Size:"));
26+
dialog.add(sizeSpinner = new JSpinner());
27+
sizeSpinner.setPreferredSize(new Dimension(42, 20));
28+
sizeSpinner.setModel(new SpinnerNumberModel(Settings.FONT_SIZE.getInt(), 1, null, 1));
29+
dialog.add(new JLabel("pt"), "wrap");
30+
31+
dialog.add(new JLabel("Font Options:"));
32+
dialog.add(boldBox = new JCheckBox("Bold"));
33+
boldBox.setSelected((Settings.FONT_OPTIONS.getInt() & Font.BOLD) != 0);
34+
dialog.add(italicsBox = new JCheckBox("Italic"), "wrap");
35+
italicsBox.setSelected((Settings.FONT_OPTIONS.getInt() & Font.ITALIC) != 0);
36+
}
37+
38+
public void display() {
39+
if (JOptionPane.showConfirmDialog(null, dialog, "Font Options", JOptionPane.OK_CANCEL_OPTION) == JOptionPane.OK_OPTION) {
40+
Settings.FONT_FAMILY.set(fontBox.getSelectedItem());
41+
Settings.FONT_SIZE.set(sizeSpinner.getValue());
42+
43+
int fontOptions = Font.PLAIN;
44+
if (boldBox.isSelected())
45+
fontOptions |= Font.BOLD;
46+
if (italicsBox.isSelected())
47+
fontOptions |= Font.ITALIC;
48+
Settings.FONT_OPTIONS.set(String.valueOf(fontOptions));
49+
}
50+
}
51+
}

src/main/java/the/bytecode/club/jda/gui/GraphicialReflectionKit.java

Lines changed: 0 additions & 35 deletions
This file was deleted.

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

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ public void actionPerformed(ActionEvent arg0) {
6767
public Map<ButtonGroup, Map<Decompiler, JRadioButtonMenuItem>> allDecompilersRev = new HashMap<>();
6868
public Map<ButtonGroup, Map<Decompiler, JCheckBoxMenuItem>> editButtons = new HashMap<>();
6969
public JMenu mnRecentFiles = new JMenu("Recent Files");
70-
public JSpinner fontSpinner = new JSpinner();
7170
private JMenuItem spinnerMenu = new JMenuItem("");
71+
public FontOptionsDialog fontOptionsDialog = new FontOptionsDialog();
7272

7373
public MainViewerGUI() {
7474
initializeWindows();
@@ -158,7 +158,7 @@ private void initializeMenubar() {
158158
final JCheckBox mnShowContainer = new JCheckBox("Show Containing File's Name");
159159
final JCheckBox mnSnapToEdges = new JCheckBox("Snap Windows to Edges");
160160
final JMenuItem mntmSetOptionalLibrary = new JMenuItem("Set Optional Library Folder");
161-
final JMenu mnFontSize = new JMenu("Font Size");
161+
final JMenuItem mntmFontSettings = new JMenuItem("Font...");
162162

163163
menuBar = new JMenuBar();
164164
fileMenu = new JMenu("File");
@@ -292,11 +292,8 @@ private void initializeMenubar() {
292292

293293
menuBar.add(spinnerMenu);
294294

295-
fontSpinner.setPreferredSize(new Dimension(42, 20));
296-
fontSpinner.setSize(new Dimension(42, 20));
297-
fontSpinner.setModel(new SpinnerNumberModel(12, 1, null, 1));
298-
viewMenu.add(mnFontSize);
299-
mnFontSize.add(fontSpinner);
295+
mntmFontSettings.addActionListener(e -> fontOptionsDialog.display());
296+
viewMenu.add(mntmFontSettings);
300297

301298
mnShowContainer.setSelected(Settings.SHOW_CONTAINER_NAME.getBool());
302299
mnShowContainer.addItemListener(e -> {

src/main/java/the/bytecode/club/jda/gui/PaneUpdaterThread.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import the.bytecode.club.jda.JDA;
77
import the.bytecode.club.jda.api.ExceptionUI;
88
import the.bytecode.club.jda.decompilers.Decompiler;
9+
import the.bytecode.club.jda.settings.Settings;
910

1011
import javax.swing.*;
1112
import java.awt.*;
@@ -44,7 +45,7 @@ public void run() {
4445
panelArea.setCaretPosition(0);
4546
panelArea.setEditable(viewer.isPaneEditable(paneId));
4647
scrollPane.setColumnHeaderView(new JLabel(decompiler.getName() + " Decompiler - Editable: " + panelArea.isEditable()));
47-
panelArea.setFont(new Font(Font.MONOSPACED, Font.PLAIN, (int) JDA.viewer.fontSpinner.getValue()));
48+
panelArea.setFont(new Font(Settings.FONT_FAMILY.get(), Settings.FONT_OPTIONS.getInt(), Settings.FONT_SIZE.getInt()));
4849

4950
SwingUtilities.invokeLater(() -> target.add(scrollPane));
5051
viewer.updatePane(paneId, panelArea, decompiler);

src/main/java/the/bytecode/club/jda/settings/Setting.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,31 @@
66
* @author Konloch
77
*/
88
public class Setting {
9-
private String key;
9+
public final String node;
10+
public final String key;
1011
private String value;
1112

1213
public Setting(String key, String value) {
14+
this("settings", key, value);
15+
}
16+
17+
public Setting(String node, String key, String value) {
18+
this.node = node;
1319
this.key = key;
1420
this.value = value;
15-
Settings.ALL_SETTINGS.put(this.key, this);
21+
Settings.ALL_SETTINGS.add(this);
1622
}
1723

1824
public String get() {
19-
return this.value;
25+
return value;
2026
}
2127

2228
public boolean getBool() {
23-
return Boolean.parseBoolean(this.value);
29+
return Boolean.parseBoolean(value);
30+
}
31+
32+
public int getInt() {
33+
return Integer.parseInt(value);
2434
}
2535

2636
public void set(Object value) {

src/main/java/the/bytecode/club/jda/settings/Settings.java

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package the.bytecode.club.jda.settings;
22

33
import com.eclipsesource.json.JsonObject;
4+
import com.eclipsesource.json.JsonValue;
45
import the.bytecode.club.jda.JDA;
56
import the.bytecode.club.jda.decompilers.Decompiler;
67
import the.bytecode.club.jda.gui.JDAWindow;
@@ -9,16 +10,16 @@
910
import java.awt.*;
1011
import java.io.FileOutputStream;
1112
import java.io.FileReader;
12-
import java.util.HashMap;
13-
import java.util.Map;
13+
import java.util.ArrayList;
14+
import java.util.List;
1415

1516
/**
1617
* Used to handle loading/saving the GUI (options).
1718
*
1819
* @author Konloch
1920
*/
2021
public class Settings {
21-
static final Map<String, Setting> ALL_SETTINGS = new HashMap<>();
22+
static final List<Setting> ALL_SETTINGS = new ArrayList<>();
2223

2324
public static final Setting PATH = new Setting("path", "");
2425
// todo: I should really refactor this
@@ -27,21 +28,22 @@ public class Settings {
2728
public static final Setting DO_UPDATE_CHECK = new Setting("doupdatecheck", "true");
2829
public static final Setting REFRESH_ON_VIEW_CHANGE = new Setting("refreshonviewchange", "false");
2930

31+
public static final Setting FONT_SIZE = new Setting("font", "fontsize", "12");
32+
public static final Setting FONT_FAMILY = new Setting("font", "fontfamily", Font.MONOSPACED);
33+
public static final Setting FONT_OPTIONS = new Setting("font", "fontoptions", String.valueOf(Font.PLAIN));
34+
3035
public static void saveGUI() {
3136
try {
3237
JsonObject settings = new JsonObject();
3338
Decompiler.CFR.getSettings().saveTo(settings);
3439
Decompiler.FERNFLOWER.getSettings().saveTo(settings);
3540
Decompiler.PROCYON.getSettings().saveTo(settings);
3641
Decompiler.BYTECODE.getSettings().saveTo(settings);
37-
if (settings.get("settings") == null) {
38-
settings.add("settings", new JsonObject());
39-
}
40-
JsonObject rootSettings = settings.get("settings").asObject();
41-
for (Map.Entry<String, Setting> setting : Settings.ALL_SETTINGS.entrySet()) {
42-
if (setting.getValue().get() != null) {
43-
rootSettings.add(setting.getKey(), setting.getValue().get());
44-
}
42+
43+
44+
for (Setting setting : Settings.ALL_SETTINGS) {
45+
String nodeId = setting.node;
46+
getNode(settings, setting.node).add(setting.key, setting.get());
4547
}
4648

4749
if (settings.get("windows") == null)
@@ -59,6 +61,15 @@ public static void saveGUI() {
5961
}
6062
}
6163

64+
private static JsonObject getNode(JsonObject parent, String nodeId) {
65+
final JsonValue nodeValue = parent.get(nodeId);
66+
if (nodeValue != null)
67+
return nodeValue.asObject();
68+
final JsonObject node = new JsonObject();
69+
parent.add(nodeId, node);
70+
return node;
71+
}
72+
6273
public static void saveFrame(JsonObject windowsSection, IPersistentWindow f) {
6374
String name = f.getWindowId();
6475
if (windowsSection.get(name) == null)
@@ -81,12 +92,12 @@ public static void loadGUI() {
8192
Decompiler.FERNFLOWER.getSettings().loadFrom(settings);
8293
Decompiler.PROCYON.getSettings().loadFrom(settings);
8394
Decompiler.BYTECODE.getSettings().loadFrom(settings);
84-
if (settings.get("settings") != null) {
85-
JsonObject rootSettings = settings.get("settings").asObject();
86-
for (Map.Entry<String, Setting> setting : Settings.ALL_SETTINGS.entrySet()) {
87-
if (rootSettings.get(setting.getKey()) != null) {
88-
setting.getValue().set(rootSettings.get(setting.getKey()).asString());
89-
}
95+
for (Setting setting : Settings.ALL_SETTINGS) {
96+
String nodeId = setting.node;
97+
JsonValue nodeValue = settings.get(nodeId);
98+
if (nodeValue != null) {
99+
if ((nodeValue = nodeValue.asObject().get(setting.key)) != null)
100+
setting.set(nodeValue.asString());
90101
}
91102
}
92103
} catch (Exception e) {

0 commit comments

Comments
 (0)