Refactoring of "restart required" detection if settings are modified.

This commit is contained in:
hneemann 2018-09-30 12:25:28 +02:00
parent b7a1dccc3d
commit 2227a609e7
4 changed files with 44 additions and 12 deletions

View File

@ -27,6 +27,7 @@ public class Key<VALUE> {
private String name; private String name;
private String description; private String description;
private boolean isSecondary; private boolean isSecondary;
private boolean requiresRestart = false;
/** /**
* Creates a new Key * Creates a new Key
@ -202,6 +203,23 @@ public class Key<VALUE> {
return this; return this;
} }
/**
* Called if this setting needs a restart.
*
* @return this for chained calls
*/
public Key<VALUE> setRequiresRestart() {
requiresRestart = true;
return this;
}
/**
* @return true if changing this value needs a restart
*/
public boolean getRequiresRestart() {
return requiresRestart;
}
/** /**
* A integer attribute. * A integer attribute.
* Stores additional combo box values * Stores additional combo box values

View File

@ -402,13 +402,13 @@ public final class Keys {
* shape setting * shape setting
*/ */
public static final Key<Boolean> SETTINGS_IEEE_SHAPES public static final Key<Boolean> 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 * The GUI Language
*/ */
public static final Key<Language> SETTINGS_LANGUAGE public static final Key<Language> 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 * enables the MAC mouse mode
*/ */
public static final Key<Boolean> SETTINGS_MAC_MOUSE public static final Key<Boolean> SETTINGS_MAC_MOUSE
= new Key<>("macMouse", Screen.isMac()); = new Key<>("macMouse", Screen.isMac()).setRequiresRestart();
/** /**
* output format for numbers * output format for numbers
@ -572,7 +572,8 @@ public final class Keys {
new Key.KeyInteger("fontSize", Screen.getDefaultFontScaling()) new Key.KeyInteger("fontSize", Screen.getDefaultFontScaling())
.setComboBoxValues(new Integer[]{100, 120, 150, 180, 200, 250, 300}) .setComboBoxValues(new Integer[]{100, 120, 150, 180, 200, 250, 300})
.setMin(50) .setMin(50)
.setMax(400); .setMax(400)
.setRequiresRestart();
/** /**
* true if a enable input is needed * true if a enable input is needed
@ -608,7 +609,7 @@ public final class Keys {
* A jar containing custom java components * A jar containing custom java components
*/ */
public static final Key<File> SETTINGS_JAR_PATH public static final Key<File> 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 * The manager which contains all the roms data
@ -686,7 +687,7 @@ public final class Keys {
* Selects the wide shapes as the default * Selects the wide shapes as the default
*/ */
public static final Key<Boolean> SETTINGS_USE_WIDE_SHAPES public static final Key<Boolean> SETTINGS_USE_WIDE_SHAPES
= new Key<>("wideShapeAsDefault", false).setSecondary(); = new Key<>("wideShapeAsDefault", false).setSecondary().setRequiresRestart();
} }

View File

@ -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(); ElementAttributes modified = new AttributeDialog(Main.this, Settings.getInstance().getKeys(), Settings.getInstance().getAttributes()).showDialog();
if (modified != null) { if (modified != null) {
FormatToExpression.setDefaultFormat(modified.get(Keys.SETTINGS_EXPRESSION_FORMAT)); 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) if (Settings.getInstance().requiresRestart(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)) {
Lang.setLanguage(modified.get(Keys.SETTINGS_LANGUAGE)); Lang.setLanguage(modified.get(Keys.SETTINGS_LANGUAGE));
JOptionPane.showMessageDialog(Main.this, Lang.get("msg_restartNeeded")); JOptionPane.showMessageDialog(Main.this, Lang.get("msg_restartNeeded"));
} }
if (!Settings.getInstance().getAttributes().equalsKey(Keys.SETTINGS_GRID, modified)) if (!Settings.getInstance().getAttributes().equalsKey(Keys.SETTINGS_GRID, modified))
circuitComponent.repaintNeeded(); circuitComponent.repaintNeeded();
Settings.getInstance().getAttributes().getValuesFrom(modified); Settings.getInstance().getAttributes().getValuesFrom(modified);
} }
} }

View File

@ -14,6 +14,7 @@ import de.neemann.digital.core.element.Keys;
import de.neemann.digital.draw.elements.Circuit; import de.neemann.digital.draw.elements.Circuit;
import java.io.*; import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
@ -106,7 +107,7 @@ public final class Settings implements AttributeListener {
@Override @Override
public void attributeChanged() { public void attributeChanged() {
XStream xStream = Circuit.getxStream(); 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("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"); out.write("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
xStream.marshal(attributes, new PrettyPrintWriter(out)); xStream.marshal(attributes, new PrettyPrintWriter(out));
} catch (Exception e) { } catch (Exception e) {
@ -121,5 +122,19 @@ public final class Settings implements AttributeListener {
return settingsKeys; 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;
}
} }