Skip to content

Commit bb9b6ed

Browse files
committed
make keymap config internal
1 parent 9ef2af3 commit bb9b6ed

File tree

9 files changed

+158
-153
lines changed

9 files changed

+158
-153
lines changed

src/main/java/cn/enaium/joe/config/ConfigManager.java

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.lang.reflect.InvocationTargetException;
2828
import java.nio.file.Files;
2929
import java.util.*;
30+
import java.util.function.Function;
3031
import java.util.stream.Collectors;
3132

3233
/**
@@ -41,7 +42,6 @@ public ConfigManager() {
4142
addByInstance(new CFRConfig());
4243
addByInstance(new FernFlowerConfig());
4344
addByInstance(new ProcyonConfig());
44-
addByInstance(new KeymapConfig());
4545
}
4646

4747
@SuppressWarnings("unchecked")
@@ -114,28 +114,7 @@ public void load() {
114114
File file = new File(System.getProperty("."), config.getName() + ".json");
115115
if (file.exists()) {
116116
if (JsonParser.parseString(Files.readString(file.toPath())) instanceof JsonObject jsonObject) {
117-
for (Field configField : klass.getDeclaredFields()) {
118-
configField.setAccessible(true);
119-
if (!jsonObject.has(configField.getName())) {
120-
continue;
121-
}
122-
123-
if (!jsonObject.get(configField.getName()).isJsonObject()) {
124-
continue;
125-
}
126-
127-
if (!jsonObject.get(configField.getName()).getAsJsonObject().has("value")) {
128-
continue;
129-
}
130-
131-
JsonElement valueJsonElement = jsonObject.get(configField.getName()).getAsJsonObject().get("value");
132-
133-
Object valueObject = configField.get(config);
134-
if (valueObject instanceof Value<?>) {
135-
Value<?> value = (Value<?>) valueObject;
136-
value.decode(valueJsonElement);
137-
}
138-
}
117+
decodeConfig(config, jsonObject);
139118
} else {
140119
MessageUtil.error("Could not read the config '" + classConfigEntry.getValue().getName() + "'");
141120
}
@@ -147,6 +126,38 @@ public void load() {
147126
}
148127
}
149128

129+
public static void decodeConfig(Config config, JsonObject jsonObject){
130+
Class<?> klass = config.getClass();
131+
for (Field configField : klass.getDeclaredFields()) {
132+
configField.setAccessible(true);
133+
if (!jsonObject.has(configField.getName())) {
134+
continue;
135+
}
136+
137+
if (!jsonObject.get(configField.getName()).isJsonObject()) {
138+
continue;
139+
}
140+
141+
if (!jsonObject.get(configField.getName()).getAsJsonObject().has("value")) {
142+
continue;
143+
}
144+
145+
JsonElement valueJsonElement = jsonObject.get(configField.getName()).getAsJsonObject().get("value");
146+
147+
Object valueObject = null;
148+
try {
149+
valueObject = configField.get(config);
150+
} catch (IllegalAccessException e) {
151+
MessageUtil.error("Could not access the config '" + config.getName() + "'", e);
152+
}
153+
if (valueObject instanceof Value<?> value) {
154+
value.decode(valueJsonElement);
155+
}
156+
}
157+
158+
config.update();
159+
}
160+
150161
public void save() {
151162
for (Config value : configMap.values()) {
152163
try {

src/main/java/cn/enaium/joe/config/extend/ApplicationConfig.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
import cn.enaium.joe.util.LangUtil;
2424
import cn.enaium.joe.util.compiler.environment.RecompileEnvironment;
2525

26+
import javax.swing.*;
27+
import java.awt.event.InputEvent;
28+
import java.awt.event.KeyEvent;
2629
import java.util.Arrays;
2730
import java.util.HashSet;
2831

@@ -40,6 +43,8 @@ public class ApplicationConfig extends Config {
4043
public IntegerValue scale = new IntegerValue("Scale", 0, "UI scale,it doesn't scale if value 0");
4144

4245
public EnableValue makeDemoRecompileEnvironment = new EnableValue("makeDemoRecompileEnvironment", false, "[EXP] Make the Demo Recompile Symbol environment.");
46+
public ConfigValue<KeymapConfig> keymap = new ConfigValue<>(new KeymapConfig(), "The setting of key setting.");
47+
4348
public ApplicationConfig() {
4449
super("Application");
4550
this.language.addListener(LangUtil.locales);
@@ -48,4 +53,25 @@ public ApplicationConfig() {
4853
});
4954
this.makeDemoRecompileEnvironment.addListener(RecompileEnvironment.environment);
5055
}
56+
57+
/**
58+
* @author Enaium
59+
* @since 1.4.0
60+
*/
61+
public static class KeymapConfig extends Config {
62+
63+
public KeyValue edit = new KeyValue("Edit", KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "Edit method instruction");
64+
public KeyValue clone = new KeyValue("Clone", KeyStroke.getKeyStroke(KeyEvent.VK_D, InputEvent.CTRL_DOWN_MASK), "Clone method instruction");
65+
public KeyValue remove = new KeyValue("Remove", KeyStroke.getKeyStroke(KeyEvent.VK_Y, InputEvent.CTRL_DOWN_MASK), "Remove method instruction");
66+
public KeyValue copy = new KeyValue("Copy", KeyStroke.getKeyStroke(KeyEvent.VK_C, InputEvent.CTRL_DOWN_MASK), "Copy method instruction text");
67+
public KeyValue insertBefore = new KeyValue("InsertBefore", KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, InputEvent.CTRL_DOWN_MASK | InputEvent.ALT_DOWN_MASK), "Insert method instruction before current");
68+
public KeyValue insertAfter = new KeyValue("InsertAfter", KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, InputEvent.CTRL_DOWN_MASK), "Insert method instruction after current");
69+
public KeyValue moveUp = new KeyValue("Move Up", KeyStroke.getKeyStroke(KeyEvent.VK_UP, InputEvent.CTRL_DOWN_MASK | InputEvent.SHIFT_DOWN_MASK), "Move method instruction up");
70+
public KeyValue moveDown = new KeyValue("Move Up", KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, InputEvent.CTRL_DOWN_MASK | InputEvent.SHIFT_DOWN_MASK), "Move method instruction down");
71+
public KeyValue save = new KeyValue("Save", KeyStroke.getKeyStroke(KeyEvent.VK_S, InputEvent.CTRL_DOWN_MASK), "Save something");
72+
73+
public KeymapConfig() {
74+
super("keymap");
75+
}
76+
}
5177
}

src/main/java/cn/enaium/joe/config/extend/KeymapConfig.java

Lines changed: 0 additions & 45 deletions
This file was deleted.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package cn.enaium.joe.config.value;
2+
3+
import cn.enaium.joe.config.Config;
4+
import cn.enaium.joe.config.ConfigManager;
5+
import cn.enaium.joe.util.MessageUtil;
6+
import com.google.gson.JsonElement;
7+
8+
public class ConfigValue<T extends Config> extends Value<T>{
9+
public ConfigValue(T config, String description){
10+
super(config.getClass(), config.getName(), config, description);
11+
}
12+
13+
@Override
14+
public void decode(JsonElement jsonElement) {
15+
if (jsonElement.isJsonObject()) {
16+
ConfigManager.decodeConfig(this.getValue(), jsonElement.getAsJsonObject());
17+
} else {
18+
MessageUtil.error("Could Not decode config for " + jsonElement + " as " + this.getName() + "@" + this.getType().getTypeName());
19+
}
20+
}
21+
}

src/main/java/cn/enaium/joe/dialog/ConfigDialog.java

Lines changed: 69 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,17 @@
2727
import javax.swing.*;
2828
import javax.swing.event.DocumentEvent;
2929
import javax.swing.event.DocumentListener;
30-
import java.awt.*;
31-
import java.awt.event.KeyAdapter;
32-
import java.awt.event.KeyEvent;
33-
import java.awt.event.WindowAdapter;
34-
import java.awt.event.WindowEvent;
30+
import java.awt.event.*;
3531
import java.lang.reflect.Field;
32+
import java.util.HashMap;
33+
import java.util.function.Function;
3634

3735
/**
3836
* @author Enaium
3937
* @since 0.7.0
4038
*/
4139
public class ConfigDialog extends Dialog {
42-
public ConfigDialog(Config config) {
40+
public ConfigDialog(final Config config) {
4341
super(LangUtil.i18n("menu.config"));
4442

4543
setContentPane(new JScrollPane(new JPanel(new MigLayout("fillx", "[fill][fill]")) {{
@@ -53,79 +51,79 @@ public ConfigDialog(Config config) {
5351

5452
Object o = declaredField.get(config);
5553

56-
if (o instanceof Value) {
57-
Value<?> value = (Value<?>) o;
58-
54+
if (o instanceof Value<?> value) {
5955
add(new JLabel(value.getName()) {{
6056
setToolTipText(value.getDescription());
6157
}});
62-
}
63-
64-
if (o instanceof StringValue) {
65-
StringValue stringValue = (StringValue) o;
66-
add(new JTextField(25) {{
67-
JTextField jTextField = this;
68-
jTextField.setText(stringValue.getValue());
69-
getDocument().addDocumentListener(new DocumentListener() {
70-
@Override
71-
public void insertUpdate(DocumentEvent e) {
72-
stringValue.setValue(jTextField.getText());
73-
}
58+
switch (value) {
59+
case StringValue stringValue -> add(new JTextField(25) {{
60+
JTextField jTextField = this;
61+
jTextField.setText(stringValue.getValue());
62+
getDocument().addDocumentListener(new DocumentListener() {
63+
@Override
64+
public void insertUpdate(DocumentEvent e) {
65+
stringValue.setValue(jTextField.getText());
66+
}
7467

75-
@Override
76-
public void removeUpdate(DocumentEvent e) {
77-
stringValue.setValue(jTextField.getText());
78-
}
68+
@Override
69+
public void removeUpdate(DocumentEvent e) {
70+
stringValue.setValue(jTextField.getText());
71+
}
7972

80-
@Override
81-
public void changedUpdate(DocumentEvent e) {
82-
stringValue.setValue(jTextField.getText());
83-
}
84-
});
85-
}}, "wrap");
86-
} else if (o instanceof IntegerValue) {
87-
IntegerValue integerValue = (IntegerValue) o;
88-
add(new JSpinner() {{
89-
setValue(integerValue.getValue());
90-
addChangeListener(e -> integerValue.setValue(Integer.parseInt(getValue().toString())));
91-
}}, "wrap");
92-
} else if (o instanceof EnableValue) {
93-
EnableValue enableValue = (EnableValue) o;
94-
add(new JCheckBox() {{
95-
JCheckBox jCheckBox = this;
96-
setHorizontalAlignment(JCheckBox.RIGHT);
97-
setSelected(enableValue.getValue());
98-
addActionListener(e -> {
99-
enableValue.setValue(jCheckBox.isSelected());
100-
});
101-
}}, "wrap");
102-
} else if (o instanceof ModeValue) {
103-
add(new JComboBox<String>(new DefaultComboBoxModel<>()) {{
104-
JComboBox<String> jComboBox = this;
105-
ModeValue modeValue = (ModeValue) o;
106-
for (String s : modeValue.getMode()) {
107-
DefaultComboBoxModel<String> model = (DefaultComboBoxModel<String>) getModel();
108-
model.addElement(s);
109-
model.setSelectedItem(modeValue.getValue());
110-
jComboBox.addActionListener(e -> {
111-
modeValue.setValue(model.getSelectedItem().toString());
73+
@Override
74+
public void changedUpdate(DocumentEvent e) {
75+
stringValue.setValue(jTextField.getText());
76+
}
11277
});
113-
}
114-
}}, "wrap");
115-
} else if (o instanceof KeyValue) {
116-
KeyValue keyValue = (KeyValue) o;
117-
add(new JButton(keyValue.getValue().toString()) {{
118-
addKeyListener(new KeyAdapter() {
119-
@Override
120-
public void keyPressed(KeyEvent e) {
121-
if (e.getKeyChar() != 65535) {
122-
KeyStroke newKey = KeyStroke.getKeyStroke(e.getKeyCode(), e.getModifiers());
123-
keyValue.setValue(newKey);
124-
setText(newKey.toString());
78+
}}, "wrap");
79+
case IntegerValue integerValue -> add(new JSpinner() {{
80+
setValue(integerValue.getValue());
81+
addChangeListener(e -> integerValue.setValue(Integer.parseInt(getValue().toString())));
82+
}}, "wrap");
83+
case EnableValue enableValue -> add(new JCheckBox() {{
84+
JCheckBox jCheckBox = this;
85+
setHorizontalAlignment(JCheckBox.RIGHT);
86+
setSelected(enableValue.getValue());
87+
addActionListener(e -> {
88+
enableValue.setValue(jCheckBox.isSelected());
89+
});
90+
}}, "wrap");
91+
case ModeValue modeValue -> add(new JComboBox<String>(new DefaultComboBoxModel<>()) {{
92+
JComboBox<String> jComboBox = this;
93+
for (String s : modeValue.getMode()) {
94+
DefaultComboBoxModel<String> model = (DefaultComboBoxModel<String>) getModel();
95+
model.addElement(s);
96+
model.setSelectedItem(modeValue.getValue());
97+
jComboBox.addActionListener(e -> {
98+
modeValue.setValue(model.getSelectedItem().toString());
99+
});
100+
}
101+
}}, "wrap");
102+
case KeyValue keyValue -> add(new JButton(keyValue.getValue().toString()) {{
103+
addKeyListener(new KeyAdapter() {
104+
@Override
105+
public void keyPressed(KeyEvent e) {
106+
if (e.getKeyChar() != 65535) {
107+
KeyStroke newKey = KeyStroke.getKeyStroke(e.getKeyCode(), e.getModifiersEx());
108+
keyValue.setValue(newKey);
109+
setText(newKey.toString());
110+
}
125111
}
112+
});
113+
}}, "wrap");
114+
case ConfigValue<?> configValue -> add(new JButton(configValue.getName()) {
115+
{
116+
addMouseListener(new MouseAdapter() {
117+
@Override
118+
public void mouseClicked(MouseEvent e) {
119+
new ConfigDialog(configValue.getValue()).setVisible(true);
120+
}
121+
});
126122
}
127-
});
128-
}}, "wrap");
123+
}, "wrap");
124+
default -> {
125+
}
126+
}
129127
}
130128
}
131129
} catch (IllegalAccessException e) {

src/main/java/cn/enaium/joe/gui/panel/file/tabbed/tab/classes/ASMifierTablePanel.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
package cn.enaium.joe.gui.panel.file.tabbed.tab.classes;
1818

1919
import cn.enaium.joe.JavaOctetEditor;
20+
import cn.enaium.joe.config.extend.ApplicationConfig;
2021
import cn.enaium.joe.util.compiler.Compiler;
21-
import cn.enaium.joe.config.extend.KeymapConfig;
2222
import cn.enaium.joe.event.events.EditSaveSuccessEvent;
2323
import cn.enaium.joe.gui.panel.CodeAreaPanel;
2424
import cn.enaium.joe.util.*;
@@ -43,7 +43,7 @@ public ASMifierTablePanel(ClassNode classNode) {
4343
super(classNode);
4444
setLayout(new BorderLayout());
4545
CodeAreaPanel codeAreaPanel = this.codeAreaPanel = new CodeAreaPanel() {{
46-
KeyStrokeUtil.register(getTextArea(), JavaOctetEditor.getInstance().config.getByClass(KeymapConfig.class).save.getValue(), () -> {
46+
KeyStrokeUtil.register(getTextArea(), JavaOctetEditor.getInstance().config.getByClass(ApplicationConfig.class).keymap.getValue().save.getValue(), () -> {
4747
try {
4848
String className = "ASMifier" + Integer.toHexString(classNode.name.hashCode()) + Integer.toHexString(getTextArea().getText().hashCode());
4949
String stringBuilder =

0 commit comments

Comments
 (0)