feat(script): add options support

This commit is contained in:
Skylot
2022-07-21 20:39:05 +01:00
parent d9af91bc4d
commit 278d7fa3f9
10 changed files with 237 additions and 28 deletions
@@ -21,6 +21,7 @@ import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import javax.swing.AbstractAction;
@@ -608,13 +609,12 @@ public class JadxSettingsWindow extends JDialog {
JadxPluginOptions optPlugin = (JadxPluginOptions) plugin;
for (OptionDescription opt : optPlugin.getOptionsDescriptions()) {
String title = "[" + pluginInfo.getPluginId() + "] " + opt.description();
if (opt.values().isEmpty()) {
JTextField textField = new JTextField();
textField.getDocument().addDocumentListener(new DocumentUpdateListener(event -> {
settings.getPluginOptions().put(opt.name(), textField.getText());
needReload();
}));
pluginsGroup.addRow(title, textField);
if (opt.values().isEmpty() || opt.getType() == OptionDescription.OptionType.BOOLEAN) {
try {
pluginsGroup.addRow(title, getPluginOptionEditor(opt));
} catch (Exception e) {
LOG.error("Failed to add editor for plugin option: {}", opt.name(), e);
}
} else {
String curValue = settings.getPluginOptions().get(opt.name());
JComboBox<String> combo = new JComboBox<>(opt.values().toArray(new String[0]));
@@ -630,6 +630,54 @@ public class JadxSettingsWindow extends JDialog {
return pluginsGroup;
}
private JComponent getPluginOptionEditor(OptionDescription opt) {
String curValue = settings.getPluginOptions().get(opt.name());
String value = curValue == null ? opt.defaultValue() : curValue;
switch (opt.getType()) {
case STRING:
JTextField textField = new JTextField();
textField.setText(value == null ? "" : value);
textField.getDocument().addDocumentListener(new DocumentUpdateListener(event -> {
settings.getPluginOptions().put(opt.name(), textField.getText());
needReload();
}));
return textField;
case NUMBER:
JSpinner numberField = new JSpinner();
numberField.setValue(safeStringToInt(value, 0));
numberField.addChangeListener(e -> {
settings.getPluginOptions().put(opt.name(), numberField.getValue().toString());
needReload();
});
return numberField;
case BOOLEAN:
JCheckBox boolField = new JCheckBox();
boolField.setSelected(Objects.equals(value, "yes") || Objects.equals(value, "true"));
boolField.addItemListener(e -> {
boolean editorValue = e.getStateChange() == ItemEvent.SELECTED;
settings.getPluginOptions().put(opt.name(), editorValue ? "yes" : "no");
needReload();
});
return boolField;
}
return null;
}
private int safeStringToInt(String value, int defValue) {
if (value == null) {
return defValue;
}
try {
return Integer.parseInt(value);
} catch (Exception e) {
LOG.warn("Failed parse string to int: {}", value, e);
return defValue;
}
}
private SettingsGroup makeOtherGroup() {
JComboBox<LangLocale> languageCbx = new JComboBox<>(NLS.getLangLocales());
for (LangLocale locale : NLS.getLangLocales()) {