Skip to content

Commit

Permalink
[not tested] save for sub cfg
Browse files Browse the repository at this point in the history
  • Loading branch information
cubewhy committed Jan 13, 2024
1 parent 9a8f9ec commit a8f2df5
Showing 1 changed file with 17 additions and 12 deletions.
29 changes: 17 additions & 12 deletions src/main/java/org/cubewhy/celestial/gui/pages/GuiSettings.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.cubewhy.celestial.gui.pages;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import lombok.extern.slf4j.Slf4j;
import org.cubewhy.celestial.gui.layouts.VerticalFlowLayout;
Expand Down Expand Up @@ -104,42 +105,43 @@ private void initGui() {
JPanel panelUnclaimed = new JPanel();
panelUnclaimed.setBorder(new TitledBorder(null, f.getString("gui.settings.unclaimed"), TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, null, Color.orange));
panelUnclaimed.setLayout(new VerticalFlowLayout(VerticalFlowLayout.LEFT));
addUnclaimed(panelUnclaimed, config.getConfig().entrySet());
addUnclaimed(panelUnclaimed, config.getConfig());
panel.add(panelUnclaimed);
}

private void addUnclaimed(JPanel basePanel, Set<Map.Entry<String, JsonElement>> entry) {
for (Map.Entry<String, JsonElement> s : entry) {
private void addUnclaimed(JPanel basePanel, JsonObject json) {
for (Map.Entry<String, JsonElement> s : json.entrySet()) {
if (!claimed.contains(s.getKey())) {
// unclaimed
if (s.getValue().isJsonPrimitive()) {
JPanel p = getSimplePanel(s.getKey(), s.getValue().getAsJsonPrimitive());
JPanel p = getSimplePanel(json, s.getKey(), s.getValue().getAsJsonPrimitive());
basePanel.add(p);
}
if (s.getValue().isJsonObject()) {
JPanel subPanel = new JPanel();
subPanel.setBorder(new TitledBorder(null, s.getKey(), TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, null, Color.orange));
subPanel.setLayout(new VerticalFlowLayout(VerticalFlowLayout.LEFT));
basePanel.add(subPanel);
addUnclaimed(subPanel, s.getValue().getAsJsonObject().entrySet());
addUnclaimed(subPanel, s.getValue().getAsJsonObject());
}
}
}
}

private @NotNull JPanel getSimplePanel(String key, @NotNull JsonPrimitive value) {
private @NotNull JPanel getSimplePanel(JsonObject json, String key, @NotNull JsonPrimitive value) {
JPanel panel = new JPanel();
if (value.isBoolean()) {
JCheckBox cb = new JCheckBox(key);
cb.setSelected(value.getAsBoolean());
cb.addActionListener((e) -> {
JCheckBox source = (JCheckBox) e.getSource();
config.setValue(key, source.isSelected());
json.addProperty(key, source.isSelected());
config.save();
});
panel.add(cb);
} else if (value.isString()) {
panel.add(new JLabel(key));
JTextField input = getAutoSaveTextField(key, value);
JTextField input = getAutoSaveTextField(key, value, json);
panel.add(input);
} else if (value.isNumber()) {
panel.add(new JLabel(key));
Expand All @@ -150,7 +152,8 @@ private void addUnclaimed(JPanel basePanel, Set<Map.Entry<String, JsonElement>>
spinner.addChangeListener((e) -> {
JSpinner source = (JSpinner) e.getSource();
Number v = (Number) source.getValue();
config.setValue(key, v);
json.addProperty(key, v);
config.save();
});
textField.setColumns(20);
panel.add(spinner);
Expand All @@ -160,19 +163,21 @@ private void addUnclaimed(JPanel basePanel, Set<Map.Entry<String, JsonElement>>
}

@NotNull
private static JTextField getAutoSaveTextField(String key, @NotNull JsonPrimitive value) {
private static JTextField getAutoSaveTextField(String key, @NotNull JsonPrimitive value, JsonObject json) {
JTextField input = new JTextField(value.getAsString());
input.addActionListener((e) -> {
JTextField source = (JTextField) e.getSource();
// save value
config.setValue(key, source.getText());
json.addProperty(key, source.getText());
config.save();
});
input.addFocusListener(new FocusAdapter() {
@Override
public void focusLost(FocusEvent e) {
JTextField source = (JTextField) e.getSource();
// save value
config.setValue(key, source.getText());
json.addProperty(key, source.getText());
config.save();
}
});
return input;
Expand Down

0 comments on commit a8f2df5

Please sign in to comment.