diff --git a/src/main/java/de/neemann/digital/core/element/Key.java b/src/main/java/de/neemann/digital/core/element/Key.java index 7c37b53d4..d07546499 100644 --- a/src/main/java/de/neemann/digital/core/element/Key.java +++ b/src/main/java/de/neemann/digital/core/element/Key.java @@ -27,6 +27,7 @@ public class Key { private String name; private String description; private boolean isSecondary; + private boolean requiresRestart = false; /** * Creates a new Key @@ -202,6 +203,23 @@ public class Key { return this; } + /** + * Called if this setting needs a restart. + * + * @return this for chained calls + */ + public Key setRequiresRestart() { + requiresRestart = true; + return this; + } + + /** + * @return true if changing this value needs a restart + */ + public boolean getRequiresRestart() { + return requiresRestart; + } + /** * A integer attribute. * Stores additional combo box values diff --git a/src/main/java/de/neemann/digital/core/element/Keys.java b/src/main/java/de/neemann/digital/core/element/Keys.java index 44f8d967b..880a83271 100644 --- a/src/main/java/de/neemann/digital/core/element/Keys.java +++ b/src/main/java/de/neemann/digital/core/element/Keys.java @@ -402,13 +402,13 @@ public final class Keys { * shape setting */ public static final Key SETTINGS_IEEE_SHAPES - = new Key<>("IEEEShapes", Locale.getDefault().getLanguage().equals(Locale.US.getLanguage())); + = new Key<>("IEEEShapes", Locale.getDefault().getLanguage().equals(Locale.US.getLanguage())).setRequiresRestart(); /** * The GUI Language */ public static final Key SETTINGS_LANGUAGE - = new Key<>("Language", new Language()); + = new Key<>("Language", new Language()).setRequiresRestart(); /** @@ -439,7 +439,7 @@ public final class Keys { * enables the MAC mouse mode */ public static final Key SETTINGS_MAC_MOUSE - = new Key<>("macMouse", Screen.isMac()); + = new Key<>("macMouse", Screen.isMac()).setRequiresRestart(); /** * output format for numbers @@ -572,7 +572,8 @@ public final class Keys { new Key.KeyInteger("fontSize", Screen.getDefaultFontScaling()) .setComboBoxValues(new Integer[]{100, 120, 150, 180, 200, 250, 300}) .setMin(50) - .setMax(400); + .setMax(400) + .setRequiresRestart(); /** * true if a enable input is needed @@ -608,7 +609,7 @@ public final class Keys { * A jar containing custom java components */ public static final Key SETTINGS_JAR_PATH - = new Key.KeyFile("jarPath", new File("")).setSecondary(); + = new Key.KeyFile("jarPath", new File("")).setSecondary().setRequiresRestart(); /** * The manager which contains all the roms data @@ -686,7 +687,7 @@ public final class Keys { * Selects the wide shapes as the default */ public static final Key SETTINGS_USE_WIDE_SHAPES - = new Key<>("wideShapeAsDefault", false).setSecondary(); + = new Key<>("wideShapeAsDefault", false).setSecondary().setRequiresRestart(); } diff --git a/src/main/java/de/neemann/digital/gui/Main.java b/src/main/java/de/neemann/digital/gui/Main.java index 5022646a8..51e3bc0b0 100644 --- a/src/main/java/de/neemann/digital/gui/Main.java +++ b/src/main/java/de/neemann/digital/gui/Main.java @@ -680,16 +680,14 @@ public final class Main extends JFrame implements ClosingWindowListener.ConfirmS ElementAttributes modified = new AttributeDialog(Main.this, Settings.getInstance().getKeys(), Settings.getInstance().getAttributes()).showDialog(); if (modified != null) { FormatToExpression.setDefaultFormat(modified.get(Keys.SETTINGS_EXPRESSION_FORMAT)); - if (!Settings.getInstance().getAttributes().equalsKey(Keys.SETTINGS_LANGUAGE, modified) - || !Settings.getInstance().getAttributes().equalsKey(Keys.SETTINGS_IEEE_SHAPES, modified) - || !Settings.getInstance().getAttributes().equalsKey(Keys.SETTINGS_FONT_SCALING, modified) - || !Settings.getInstance().getAttributes().equalsKey(Keys.SETTINGS_MAC_MOUSE, modified) - || !Settings.getInstance().getAttributes().equalsKey(Keys.SETTINGS_JAR_PATH, modified)) { + + if (Settings.getInstance().requiresRestart(modified)) { Lang.setLanguage(modified.get(Keys.SETTINGS_LANGUAGE)); JOptionPane.showMessageDialog(Main.this, Lang.get("msg_restartNeeded")); } if (!Settings.getInstance().getAttributes().equalsKey(Keys.SETTINGS_GRID, modified)) circuitComponent.repaintNeeded(); + Settings.getInstance().getAttributes().getValuesFrom(modified); } } diff --git a/src/main/java/de/neemann/digital/gui/Settings.java b/src/main/java/de/neemann/digital/gui/Settings.java index adc40bcb0..e74d1d609 100644 --- a/src/main/java/de/neemann/digital/gui/Settings.java +++ b/src/main/java/de/neemann/digital/gui/Settings.java @@ -14,6 +14,7 @@ import de.neemann.digital.core.element.Keys; import de.neemann.digital.draw.elements.Circuit; import java.io.*; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -106,7 +107,7 @@ public final class Settings implements AttributeListener { @Override public void attributeChanged() { XStream xStream = Circuit.getxStream(); - try (Writer out = new OutputStreamWriter(new FileOutputStream(filename), "utf-8")) { + try (Writer out = new OutputStreamWriter(new FileOutputStream(filename), StandardCharsets.UTF_8)) { out.write("\n"); xStream.marshal(attributes, new PrettyPrintWriter(out)); } catch (Exception e) { @@ -121,5 +122,19 @@ public final class Settings implements AttributeListener { return settingsKeys; } + /** + * Returns true if the given modification requires a restart. + * + * @param modified the modified settings + * @return true if the modification requires a restart + */ + public boolean requiresRestart(ElementAttributes modified) { + for (Key key : settingsKeys) + if (key.getRequiresRestart() && !attributes.equalsKey(key, modified)) + return true; + + return false; + } + }