From 1d99762c7965d351510cfb5e08eac25e48d96038 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Fri, 6 Jun 2025 08:02:52 +0300 Subject: Modernize project structure, update Maven config, move sources, add logging config, update README and .gitignore --- src/main/java/prefs/VSDefaultPrefs.java | 275 +++++ src/main/java/prefs/VSPrefs.java | 1163 ++++++++++++++++++++ src/main/java/prefs/VSPrefsRestriction.java | 137 +++ src/main/java/prefs/VSSerializablePrefs.java | 37 + .../java/prefs/editors/VSAbstractBetterEditor.java | 101 ++ src/main/java/prefs/editors/VSAbstractEditor.java | 1056 ++++++++++++++++++ src/main/java/prefs/editors/VSColorChooser.java | 61 + src/main/java/prefs/editors/VSEditorFrame.java | 98 ++ src/main/java/prefs/editors/VSEditorTable.java | 290 +++++ src/main/java/prefs/editors/VSProcessEditor.java | 107 ++ src/main/java/prefs/editors/VSSimulatorEditor.java | 120 ++ 11 files changed, 3445 insertions(+) create mode 100644 src/main/java/prefs/VSDefaultPrefs.java create mode 100644 src/main/java/prefs/VSPrefs.java create mode 100644 src/main/java/prefs/VSPrefsRestriction.java create mode 100644 src/main/java/prefs/VSSerializablePrefs.java create mode 100644 src/main/java/prefs/editors/VSAbstractBetterEditor.java create mode 100644 src/main/java/prefs/editors/VSAbstractEditor.java create mode 100644 src/main/java/prefs/editors/VSColorChooser.java create mode 100644 src/main/java/prefs/editors/VSEditorFrame.java create mode 100644 src/main/java/prefs/editors/VSEditorTable.java create mode 100644 src/main/java/prefs/editors/VSProcessEditor.java create mode 100644 src/main/java/prefs/editors/VSSimulatorEditor.java (limited to 'src/main/java/prefs') diff --git a/src/main/java/prefs/VSDefaultPrefs.java b/src/main/java/prefs/VSDefaultPrefs.java new file mode 100644 index 0000000..f980734 --- /dev/null +++ b/src/main/java/prefs/VSDefaultPrefs.java @@ -0,0 +1,275 @@ +package prefs; + +import java.awt.Color; +import java.awt.event.KeyEvent; + +/** + * The class VSDefaultPrefs, makes sure that the simulator has its default + * configuration values. + * + * @author Paul C. Buetow + */ +public class VSDefaultPrefs extends VSSerializablePrefs { + /** + * Inits a prefs object with default values. + * + * @return the lang.process.removeprefs + */ + public static VSPrefs init() { + VSDefaultPrefs prefs = new VSDefaultPrefs(); + prefs.fillWithDefaults(); + return prefs; + } + + /** + * Fill everything with ts defaults. + */ + public void fillWithDefaults() { + super.clear(); + addWithDefaults(); + } + + /** + * Adds default values if not existent. + */ + public void addWithDefaults() { + fillDefaultBooleans(); + fillDefaultColors(); + fillDefaultFloats(); + fillDefaultIntegers(); + fillDefaultLongs(); + fillDefaultStrings(); + } + + /** + * Fill with default strings. + */ + public void fillDefaultStrings() { + initString("lang.about", "About"); + initString("lang.about.info", "This program used to be the diploma thesis of Paul C. Buetow. Please contact vs-sim@dev.buetow.org, if you find any errors!"); + initString("lang.activate", "activate"); + initString("lang.activated", "activated"); + initString("lang.actualize", "Activation"); + initString("lang.all", "All"); + initString("lang.antialiasing", "Anti-Aliasing"); + initString("lang.cancel", "Abort"); + initString("lang.client", "Client"); + initString("lang.clientrequest.start", "Start client request"); + initString("lang.close", "Close"); + initString("lang.colorchooser", "Color chooser"); + initString("lang.colorchooser2", "Please select color"); + initString("lang.copy", "Copy"); + initString("lang.crashed", "Crashed"); + initString("lang.dat", "Simulation (.dat)"); + initString("langactivate", "deactivate"); + initString("langactivated", "deactivated"); + initString("lang.default", "Defaults"); + initString("lang.edit", "Edit"); + initString("lang.editor", "Editor"); + initString("lang.event", "Event"); + initString("lang.event.add.global", "Insert global event"); + initString("lang.event.add.local", "Insert local event"); + initString("lang.event.add.time", "at"); + initString("lang.events", "Events"); + initString("lang.events.process", "Process events"); + initString("lang.file", "File"); + initString("lang.filter", "Filter"); + initString("lang.loging.active", "Logging"); + initString("lang.loging.clear", "Delete logs"); + initString("lang.message", "Message"); + initString("lang.message.recv", "Message received"); + initString("lang.message.sent", "Message sent"); + initString("lang.mode.expert", "Expert mode"); + initString("lang.name", "VS-Simulator 1.2-beta"); + initString("lang.ok", "OK"); + initString("lang.open", "Open"); + initString("lang.pause", "Pause"); + initString("lang.prefs", "Preferences"); + initString("lang.prefs.color", "Color preferences"); + initString("lang.prefs.diverse", "Diverse preferences"); + initString("lang.prefs.ext", "Extended preferences"); + initString("lang.prefs.message", "Message preferences"); + initString("lang.prefs.message.defaults", "Message prefs. for new processes"); + initString("lang.prefs.more", "More preferences"); + initString("lang.prefs.process", "Process preferences"); + initString("lang.prefs.process", "Standard process preferences"); + initString("lang.prefs.process.defaults", "Preferences for new processes"); + initString("lang.prefs.process.ext", "Extended process preferences"); + initString("lang.prefs.protocols", "Protocol preferences"); + initString("lang.prefs.simulator", "Simulator preferences"); + initString("lang.process", "Process"); + initString("lang.process.add.new", "Insert new process"); + initString("lang.process.crash", "Crash process"); + initString("lang.process.edit", "Edit process"); + initString("lang.process.id", "PID"); + initString("lang.process.new", "New process"); + initString("lang.process.not.selected", "No process selected"); + initString("lang.process.recover", "Recover process"); + initString("lang.process.remove", "Remove process"); + initString("lang.process.selected", "Selected process"); + initString("lang.process.time.local", "Local time"); + initString("lang.processes.all", "All processes"); + initString("lang.protocol", "Protocol"); + initString("lang.protocol.client", "Client side"); + initString("lang.protocol.editor", "Protocol editor"); + initString("lang.protocol.server", "Server side"); + initString("lang.protocol.tasks.activation", "Client-/Server protocol activation"); + initString("lang.protocol.tasks.client", "Client Task-Manager (Client request)"); + initString("lang.protocols", "Protocols"); + initString("lang.quit", "Quit"); + initString("lang.recovered", "Recovered"); + initString("lang.remove", "Remove"); + initString("lang.replay", "Repeat"); + initString("lang.reset", "Reset"); + initString("lang.save", "Save"); + initString("lang.saveas", "Save as"); + initString("lang.server", "Server"); + initString("lang.serverrequest.start", "Start server request"); + initString("lang.simulator", "Simulator"); + initString("lang.simulator.close", "Close simulation"); + initString("lang.simulator.finished", "Simulation closed"); + initString("lang.simulator.new", "New simulation"); + initString("lang.simulator.paused", "Simulation paused"); + initString("lang.simulator.resetted", "Simulation resetted"); + initString("lang.simulator.started", "Simulation started"); + initString("lang.start", "Start"); + initString("lang.stop", "Stop"); + initString("lang.takeover", "Take over"); + initString("lang.task", "Task"); + initString("lang.task.manager", "Event editor"); + initString("lang.tasks.fullfilled", "Fullfilled tasks"); + initString("lang.tasks.global", "GLobal tasks"); + initString("lang.tasks.local", "Local tasks"); + initString("lang.time", "Time"); + initString("lang.time.lamport", "Lamport time"); + initString("lang.time.vector", "Vector time"); + initString("lang.timed.global", "Global events"); + initString("lang.timed.local", "Local events"); + initString("lang.type", "Type"); + initString("lang.value", "Value"); + initString("lang.variable", "Variable"); + initString("lang.variables", "Variables"); + initString("lang.variables.global", "Global variables"); + initString("lang.window.close", "Close window"); + initString("lang.window.new", "New window"); + + /* Protocol names */ + initString("lang.events.implementations.VSProcessCrashEvent", "Process Crash Event"); + initString("lang.events.implementations.VSProcessCrashEvent.short", "Process Crash"); + initString("lang.events.implementations.VSProcessRecoverEvent", "Process Recover Event"); + initString("lang.events.implementations.VSProcessRecoverEvent.short", "Process Recover"); + initString("lang.protocols.implementations.VSBasicMulticastProtocol", "Basic Multicast Protocol"); + initString("lang.protocols.implementations.VSBasicMulticastProtocol.short", "Basic Multicast"); + initString("lang.protocols.implementations.VSBerkelyTimeProtocol", "Berkley algorithm for internal sync."); + initString("lang.protocols.implementations.VSBerkelyTimeProtocol.short", "Berkley Protocol"); + initString("lang.protocols.implementations.VSBroadcastProtocol", "Broadcast Protocol"); + initString("lang.protocols.implementations.VSBroadcastProtocol.short", "Broadcast"); + initString("lang.protocols.implementations.VSDummyProtocol", "Example/Dummy Protocol"); + initString("lang.protocols.implementations.VSDummyProtocol.short", "Example/Dummy"); + initString("lang.protocols.implementations.VSExternalTimeSyncProtocol", "Christians method for external sync."); + initString("lang.protocols.implementations.VSExternalTimeSyncProtocol.short", "Christians"); + initString("lang.protocols.implementations.VSInternalTimeSyncProtocol", "Internal Synchronization Protocol"); + initString("lang.protocols.implementations.VSInternalTimeSyncProtocol.short", "Internal sync."); + initString("lang.protocols.implementations.VSOnePhaseCommitProtocol", "One-Phase Commit Protocol"); + initString("lang.protocols.implementations.VSOnePhaseCommitProtocol.short", "1-Phase Commit"); + initString("lang.protocols.implementations.VSPingPongProtocol", "Ping-Pong Protocol"); + initString("lang.protocols.implementations.VSPingPongProtocol.short", "Ping-Pong"); + initString("lang.protocols.implementations.VSReliableMulticastProtocol", "Reliable Multicast Protocol"); + initString("lang.protocols.implementations.VSReliableMulticastProtocol.short", "Reliable Multicast"); + initString("lang.protocols.implementations.VSTwoPhaseCommitProtocol", "Two-Phase Commit"); + initString("lang.protocols.implementations.VSTwoPhaseCommitProtocol.short", "2-Phase Commit"); + } + + /** + * Fill with default integers. + */ + public void fillDefaultIntegers() { + /* Simulator prefs */ + initInteger("sim.process.num", 3, "Number of processes", 1, 6); + initInteger("message.prob.outage", 0, "Message lost prob.", 0, 100, "%"); + initInteger("process.prob.crash", 0, "Process crash prob.", 0, 100, "%"); + initInteger("sim.seconds", 15, "Simulation duration", 5, 120, "s"); + + /* Internal prefs */ + initInteger("keyevent.about", KeyEvent.VK_A, null, 0, 100); + initInteger("keyevent.cancel", KeyEvent.VK_A, null, 0, 100); + initInteger("keyevent.close", KeyEvent.VK_C, null, 0, 100); + initInteger("keyevent.default", KeyEvent.VK_F, null, 0, 100); + initInteger("keyevent.edit", KeyEvent.VK_E, null, 0, 100); + initInteger("keyevent.file", KeyEvent.VK_D, null, 0, 100); + initInteger("keyevent.new", KeyEvent.VK_N, null, 0, 100); + initInteger("keyevent.actualize", KeyEvent.VK_A, null, 0, 100); + initInteger("keyevent.takeover", KeyEvent.VK_B, null, 0, 100); + initInteger("keyevent.ok", KeyEvent.VK_O, null, 0, 100); + initInteger("keyevent.open", KeyEvent.VK_O, null, 0, 100); + initInteger("keyevent.pause", KeyEvent.VK_P, null, 0, 100); + initInteger("keyevent.prefs", KeyEvent.VK_P, null, 0, 100); + initInteger("keyevent.prefs.ext", KeyEvent.VK_E, null, 0, 100); + initInteger("keyevent.quit", KeyEvent.VK_B, null, 0, 100); + initInteger("keyevent.replay", KeyEvent.VK_W, null, 0, 100); + initInteger("keyevent.reset", KeyEvent.VK_R, null, 0, 100); + initInteger("keyevent.save", KeyEvent.VK_S, null, 0, 100); + initInteger("keyevent.saveas", KeyEvent.VK_V, null, 0, 100); + initInteger("keyevent.simulator", KeyEvent.VK_S, null, 0, 100); + initInteger("keyevent.start", KeyEvent.VK_S, null, 0, 100); + initInteger("keyevent.stop", KeyEvent.VK_P, null, 0, 100); + + initInteger("div.window.prefs.xsize", 400, "Configuration window X-Axis", 550, 3200, "px"); + initInteger("div.window.prefs.ysize", 400, "Configuration window Y-Axis", 640, 2400, "px"); + initInteger("div.window.logsize", 300, "Log window Y-Axis", 100, 1000, "px"); + initInteger("div.window.splitsize", 320, "Toolbar X-Axis", 100, 1000, "px"); + initInteger("div.window.xsize", 1024, "Main window X-Axis", 750, 3200, "px"); + initInteger("div.window.ysize", 768, "Main window Y-Axis", 600, 2400, "px"); + } + + /** + * Fill with default floats. + */ + public void fillDefaultFloats() { + /* Simulator prefs */ + initFloat("process.clock.variance", 0, "Clock variance"); + initFloat("sim.clock.speed", 0.5f, "Simulation play speed"); + } + + /** + * Fill default longs. + */ + public void fillDefaultLongs() { + /* Simulator prefs */ + initLong("message.sendingtime.min", 500, "Max transmission time", "ms"); + initLong("message.sendingtime.max", 2000, "Min transmission time", "ms"); + } + + /** + * Fill with default colors. + */ + public void fillDefaultColors() { + /* Internal prefs */ + initColor("col.background", new Color(0xFF, 0xFF, 0xFF)); + initColor("col.process.default", new Color(0x00, 0x00, 0x00)); + initColor("col.process.running", new Color(0x0D, 0xD8, 0x09)); + initColor("col.process.crashed", new Color(0xff, 0x00, 0x00)); + initColor("col.process.highlight", new Color(0xff, 0xA5, 0x00)); + initColor("col.process.line", new Color(0x00, 0x00, 0x00)); + initColor("col.process.secondline", new Color(0xAA, 0xAA, 0xAA)); + initColor("col.process.sepline", new Color(0xff, 0x00, 0x00)); + initColor("col.process.stopped", new Color(0x00, 0x00, 0x00)); + initColor("col.message.arrived", new Color(0x00, 0x85, 0xD2)); + initColor("col.message.sending", new Color(0x0D, 0xD8, 0x09)); + initColor("col.message.lost", new Color(0xFF, 0x00, 0x00)); + } + + /** + * Fill with default booleans. + */ + public void fillDefaultBooleans() { + initBoolean("sim.mode.expert", false, "Expert mode"); + initBoolean("sim.message.own.recv", false, "Processes receive own messages"); + initBoolean("sim.message.prob.mean", true, "Use mean value of message lost prob."); + initBoolean("sim.message.sendingtime.mean", true, "Use mean value of transmission times"); + initBoolean("sim.messages.relevant", true, "Only show relevant messages"); + initBoolean("sim.periodic", false, "Repeat simulation periodically"); + initBoolean("sim.update.lamporttime.all", false, "Lamport timestamps affect all events"); + initBoolean("sim.update.vectortime.all", false, "Vector timestamps affect all events"); + } +} diff --git a/src/main/java/prefs/VSPrefs.java b/src/main/java/prefs/VSPrefs.java new file mode 100644 index 0000000..e7b5295 --- /dev/null +++ b/src/main/java/prefs/VSPrefs.java @@ -0,0 +1,1163 @@ +package prefs; + +import java.awt.Color; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.Set; +import java.util.Vector; + +import serialize.VSSerialize; + +/** + * The class VSPrefs, this class is for dynamic data storage. It can hold + * various different types such as Boolean, Floats, Integers, Strings, Colors. + * + * @author Paul C. Buetow + */ +public class VSPrefs { + /** The Constant BOOLEAN_PREFIX. */ + public static final String BOOLEAN_PREFIX = "Boolean: "; + + /** The Constant COLOR_PREFIX. */ + public static final String COLOR_PREFIX = "Color: "; + + /** The Constant FLOAT_PREFIX. */ + public static final String FLOAT_PREFIX = "Float: "; + + /** The Constant INTEGER_PREFIX. */ + public static final String INTEGER_PREFIX = "Integer: "; + + /** The Constant VECTOR_PREFIX. */ + public static final String VECTOR_PREFIX = "Vector: "; + + /** The Constant LONG_PREFIX. */ + public static final String LONG_PREFIX = "Long: "; + + /** The Constant STRING_PREFIX. */ + public static final String STRING_PREFIX = "String: "; + + /** The color prefs. */ + private HashMap colorPrefs; + + /** The float prefs. */ + private HashMap floatPrefs; + + /** The integer prefs. */ + private HashMap integerPrefs; + + /** The integer vector prefs. */ + private HashMap> vectorPrefs; + + /** The long prefs. */ + private HashMap longPrefs; + + /** The setting restriction prefs. */ + private HashMap restrictions; + + /** The description prefs. */ + private HashMap descriptionPrefs; + + /** The string prefs. */ + private HashMap stringPrefs; + + /** The boolean prefs. */ + private HashMap booleanPrefs; + + /** The object prefs. */ + private HashMap objectPrefs; + + /** The units. */ + private HashMap units; + + /** The id counter. */ + private static int idCounter; + + /** The id. */ + protected int id; + + /** + * Instantiates a new lang.process.removeprefs. + */ + public VSPrefs() { + colorPrefs = new HashMap(); + descriptionPrefs = new HashMap(); + floatPrefs = new HashMap(); + integerPrefs = new HashMap(); + vectorPrefs = new HashMap>(); + longPrefs = new HashMap(); + restrictions = new HashMap(); + stringPrefs = new HashMap(); + booleanPrefs = new HashMap(); + objectPrefs = new HashMap(); + units = new HashMap(); + id = ++idCounter; + } + + /** + * Clear. + */ + protected synchronized void clear() { + colorPrefs.clear(); + floatPrefs.clear(); + integerPrefs.clear(); + vectorPrefs.clear(); + longPrefs.clear(); + stringPrefs.clear(); + booleanPrefs.clear(); + objectPrefs.clear(); + descriptionPrefs.clear(); + restrictions.clear(); + } + + /* Unit methods */ + + /** + * Gets the unit. + * + * @param fullKey the full key + * + * @return the unit + */ + public synchronized String getUnit(String fullKey) { + return units.get(fullKey); + } + + /** + * Sets the unit. + * + * @param key the key + * @param unit the unit + */ + public synchronized void initUnit(String key, String unit) { + if (unit == null /*|| units.containsKey(key)*/) + return; + units.put(key, unit); + } + + /* Description methods */ + /** + * Sets the description if unset. + * + * @param key the key + * @param descr the descr + */ + public synchronized void initDescription(String key, String descr) { + if (descr == null /*|| descriptionPrefs.containsKey(key)*/) + return; + descriptionPrefs.put(key, descr); + } + + /** + * Gets the description. + * + * @param fullKey the full key + * + * @return the description + */ + public synchronized String getDescription(String fullKey) { + return descriptionPrefs.get(fullKey); + } + + /* Restriction methods */ + + /** + * Gets the restriction. + * + * @param fullKey the full key + * + * @return the restriction + */ + public synchronized VSPrefsRestriction getRestriction(String fullKey) { + return restrictions.get(fullKey); + } + + /** + * Sets the restriction. + * + * @param key the key + * @param settingRestriction the setting restriction + */ + public synchronized void initRestriction(String key, + VSPrefsRestriction settingRestriction) { + restrictions.put(key, settingRestriction); + } + + /* Object methods */ + + /** + * Object exists. + * + * @param key the key + * + * @return true, if successful + */ + public synchronized boolean objectExists(String key) { + return null != objectPrefs.get(key); + } + + /** + * Gets the object. + * + * @param key the key + * + * @return the object + */ + public synchronized Object getObject(String key) { + Object val = objectPrefs.get(key); + + if (val == null) { + System.err.println("Fatal: No such object config value \"" + key + "\""); + System.exit(1); + } + + return val; + } + + /** + * Removes the object. + * + * @param key the key + */ + public synchronized void removeObject(String key) { + objectPrefs.remove(key); + } + + /** + * Sets the object. + * + * @param key the key + * @param val the val + */ + public synchronized void setObject(String key, Object val) { + objectPrefs.put(key, val); + } + + /* Boolean methods */ + + /** + * Gets the boolean. + * + * @param key the key + * + * @return the boolean + */ + public boolean getBoolean(String key) { + return getBooleanObj(key).booleanValue(); + } + + /** + * Gets the boolean key set. + * + * @return the boolean key set + */ + public synchronized Set getBooleanKeySet() { + return booleanPrefs.keySet(); + } + + /** + * Gets the boolean obj. + * + * @param key the key + * + * @return the boolean obj + */ + public synchronized Boolean getBooleanObj(String key) { + Boolean val = booleanPrefs.get(key); + + if (val == null) + return Boolean.valueOf(false); + + return val; + } + + /** + * Inits the boolean. + * + * @param key the key + * @param val the val + */ + public synchronized void initBoolean(String key, Boolean val) { + if (!booleanPrefs.containsKey(key)) + booleanPrefs.put(key, val); + } + + /** + * Inits the boolean. + * + * @param key the key + * @param val the val + */ + public void initBoolean(String key, boolean val) { + initBoolean(key, Boolean.valueOf(val)); + } + + /** + * Inits the boolean. + * + * @param key the key + * @param val the val + * @param descr the descr + */ + public void initBoolean(String key, boolean val, String descr) { + initBoolean(key, val); + initDescription(BOOLEAN_PREFIX + key, descr); + } + + /** + * Sets the boolean. + * + * @param key the key + * @param val the val + */ + public synchronized void setBoolean(String key, Boolean val) { + booleanPrefs.put(key, val); + } + + /** + * Sets the boolean. + * + * @param key the key + * @param val the val + */ + public void setBoolean(String key, boolean val) { + setBoolean(key, Boolean.valueOf(val)); + } + + /* Color methods */ + + /** + * Gets the color. + * + * @param key the key + * + * @return the color + */ + public synchronized Color getColor(String key) { + Color color = colorPrefs.get(key); + + if (color == null) { + System.err.println("Fatal: No such color config value \"" + + key + "\""); + System.exit(1); + } + + return color; + } + + /** + * Gets the color key set. + * + * @return the color key set + */ + public synchronized Set getColorKeySet() { + return colorPrefs.keySet(); + } + + /** + * Inits the color. + * + * @param key the key + * @param color the color + */ + public synchronized void initColor(String key, Color color) { + if (!colorPrefs.containsKey(key)) + colorPrefs.put(key, color); + } + + /** + * Inits the color. + * + * @param key the key + * @param val the val + * @param descr the descr + */ + public void initColor(String key, Color val, String descr) { + initColor(key, val); + initDescription(COLOR_PREFIX + key, descr); + } + + /** + * Sets the color. + * + * @param key the key + * @param color the color + */ + public synchronized void setColor(String key, Color color) { + colorPrefs.put(key, color); + } + + /* Float methods */ + + /** + * Gets the float. + * + * @param key the key + * + * @return the float + */ + public float getFloat(String key) { + return getFloatObj(key).floatValue(); + } + + /** + * Gets the float key set. + * + * @return the float key set + */ + public synchronized Set getFloatKeySet() { + return floatPrefs.keySet(); + } + + /** + * Gets the float obj. + * + * @param key the key + * + * @return the float obj + */ + public synchronized Float getFloatObj(String key) { + Float val = floatPrefs.get(key); + + if (val == null) { + System.err.println("Fatal: No such float config value \"" + + key + "\""); + System.exit(1); + } + + return val; + } + + /** + * Inits the float. + * + * @param key the key + * @param val the val + */ + public synchronized void initFloat(String key, Float val) { + if (!floatPrefs.containsKey(key)) + floatPrefs.put(key, val); + } + + /** + * Inits the float. + * + * @param key the key + * @param val the val + */ + public void initFloat(String key, float val) { + initFloat(key, Float.valueOf(val)); + } + + /** + * Inits the float. + * + * @param key the key + * @param val the val + * @param descr the descr + */ + public void initFloat(String key, float val, String descr) { + initFloat(key, val); + initDescription(FLOAT_PREFIX + key, descr); + } + + /** + * Inits the float plus unit. + * + * @param key the key + * @param val the val + * @param descr the descr + * @param unit the unit + */ + public void initFloat(String key, float val, String descr, String unit) { + initFloat(key, val, descr); + initUnit(FLOAT_PREFIX + key, unit); + } + + /** + * Sets the float. + * + * @param key the key + * @param val the val + */ + public synchronized void setFloat(String key, Float val) { + floatPrefs.put(key, val); + } + + /** + * Sets the float. + * + * @param key the key + * @param val the val + */ + public void setFloat(String key, float val) { + setFloat(key, Float.valueOf(val)); + } + + /* Integer methods */ + + /** + * Gets the integer. + * + * @param key the key + * + * @return the integer + */ + public int getInteger(String key) { + return getIntegerObj(key).intValue(); + } + + /** + * Gets the integer key set. + * + * @return the integer key set + */ + public synchronized Set getIntegerKeySet() { + return integerPrefs.keySet(); + } + + /** + * Gets the integer obj. + * + * @param key the key + * + * @return the integer obj + */ + public synchronized Integer getIntegerObj(String key) { + Integer val = integerPrefs.get(key); + + if (val == null) { + System.err.println("Fatal: No such integer config value \"" + key + "\""); + System.exit(1); + } + + return val; + } + + /** + * Inits the integer. + * + * @param key the key + * @param val the val + */ + public void initInteger(String key, int val) { + if (!integerPrefs.containsKey(key)) + setInteger(key, Integer.valueOf(val)); + } + + /** + * Inits the integer. + * + * @param key the key + * @param val the val + * @param descr the descr + */ + public void initInteger(String key, int val, String descr) { + initInteger(key, val); + initDescription(INTEGER_PREFIX + key, descr); + } + + /** + * Inits the integer. + * + * @param key the key + * @param val the val + * @param descr the descr + * @param r the restriction + */ + public void initInteger(String key, int val, String descr, + VSPrefsRestriction.VSIntegerPrefsRestriction r) { + initInteger(key, val, descr); + initRestriction(INTEGER_PREFIX + key, r); + } + + /** + * Inits the integer. + * + * @param key the key + * @param val the val + * @param descr the descr + * @param r the restriction + */ + public void initInteger(String key, int val, String descr, + VSPrefsRestriction.VSIntegerPrefsRestriction r, + String unit) { + initInteger(key, val, descr, r); + initUnit(INTEGER_PREFIX + key, unit); + } + + /** + * Inits the integer. + * + * @param key the key + * @param val the val + * @param descr the descr + * @param minValue the min value + * @param maxValue the max value + */ + public void initInteger(String key, int val, String descr, int minValue, + int maxValue) { + initInteger(key, val, descr, + new VSPrefsRestriction.VSIntegerPrefsRestriction( + minValue, maxValue)); + } + + /** + * Inits the integer plus unit. + * + * @param key the key + * @param val the val + * @param descr the descr + * @param minValue the min value + * @param maxValue the max value + * @param unit the unit + */ + public void initInteger(String key, int val, String descr, int minValue, + int maxValue, String unit) { + initInteger(key, val, descr, minValue, maxValue); + initUnit(INTEGER_PREFIX + key, unit); + } + + /** + * Sets the integer. + * + * @param key the key + * @param val the val + */ + public synchronized void setInteger(String key, Integer val) { + integerPrefs.put(key, val); + } + + /** + * Sets the integer. + * + * @param key the key + * @param val the val + */ + public void setInteger(String key, int val) { + setInteger(key, Integer.valueOf(val)); + } + + /* Integer vector methods */ + + /** + * Gets the integer key set. + * + * @return the integer key set + */ + public synchronized Set getVectorKeySet() { + return vectorPrefs.keySet(); + } + + /** + * Gets the integer obj. + * + * @param key the key + * + * @return the integer obj + */ + public synchronized Vector getVector(String key) { + Vector val = vectorPrefs.get(key); + + if (val == null) { + System.err.println("Fatal: No such integer config value \"" + key + "\""); + System.exit(1); + } + + return val; + } + + /** + * Inits the integer. + * + * @param key the key + * @param val the val + */ + public synchronized void initVector(String key, Vector val) { + if (!vectorPrefs.containsKey(key)) + setVector(key, val); + } + + /** + * Inits the integer vector. + * + * @param key the key + * @param val the val + * @param descr the descr + */ + public void initVector(String key, Vector val, String descr) { + initVector(key, val); + initDescription(VECTOR_PREFIX + key, descr); + } + + /** + * Inits the integer vector plus unit. + * + * @param key the key + * @param val the val + * @param descr the descr + */ + public void initVector(String key, Vector val, String descr, + String unit) { + initVector(key, val, descr); + initUnit(VECTOR_PREFIX + key, unit); + } + + /** + * Sets the integer vector. + * + * @param key the key + * @param val the val + */ + public synchronized void setVector(String key, Vector val) { + vectorPrefs.put(key, val); + } + + /* Long methods */ + + /** + * Gets the long. + * + * @param key the key + * + * @return the long + */ + public long getLong(String key) { + return getLongObj(key).longValue(); + } + + /** + * Gets the long key set. + * + * @return the long key set + */ + public synchronized Set getLongKeySet() { + return longPrefs.keySet(); + } + + /** + * Gets the long obj. + * + * @param key the key + * + * @return the long obj + */ + public synchronized Long getLongObj(String key) { + Long val = longPrefs.get(key); + + if (val == null) { + System.err.println("Fatal: No such long config value \"" + key + "\""); + System.exit(1); + } + + return val; + } + + /** + * Inits the long. + * + * @param key the key + * @param val the val + */ + public synchronized void initLong(String key, Long val) { + if (!longPrefs.containsKey(key)) + longPrefs.put(key, val); + } + + /** + * Sets the long if unset. + * + * @param key the key + * @param val the val + */ + public void initLong(String key, long val) { + initLong(key, Long.valueOf(val)); + } + + /** + * Inits the long. + * + * @param key the key + * @param val the val + * @param descr the descr + */ + public void initLong(String key, long val, String descr) { + initLong(key, val); + initDescription(LONG_PREFIX + key, descr); + } + + /** + * Inits the long unit. + * + * @param key the key + * @param val the val + * @param descr the descr + * @param unit the unit + */ + public void initLong(String key, long val, String descr, String unit) { + initLong(key, val, descr); + initUnit(LONG_PREFIX + key, unit); + } + + /** + * Deletes the long. + * + * @param key the key + */ + public synchronized void deleteLong(String key) { + longPrefs.remove(key); + } + + /** + * Sets the long. + * + * @param key the key + * @param val the val + */ + public synchronized void setLong(String key, Long val) { + longPrefs.put(key, val); + } + + /** + * Sets the long. + * + * @param key the key + * @param val the val + */ + public void setLong(String key, long val) { + setLong(key, Long.valueOf(val)); + } + + /* String methods */ + + /** + * Gets the string. + * + * @param key the key + * + * @return the string + */ + public synchronized String getString(String key) { + String val = stringPrefs.get(key); + + if (val == null) { + System.err.println("Fatal: No such string config value \"" + key + "\""); + System.exit(1); + } + + return val; + } + + /** + * Gets the string key set. + * + * @return the string key set + */ + public synchronized Set getStringKeySet() { + return stringPrefs.keySet(); + } + + /** + * Inits the string. + * + * @param key the key + * @param val the val + */ + public synchronized void initString(String key, String val) { + //if (!stringPrefs.containsKey(key)) + stringPrefs.put(key, val); + } + + /** + * Inits the string. + * + * @param key the key + * @param val the val + * @param descr the descr + */ + public void initString(String key, String val, String descr) { + initString(key, val); + initDescription(STRING_PREFIX + key, descr); + } + + /** + * Sets the string. + * + * @param key the key + * @param val the val + */ + public synchronized void setString(String key, String val) { + stringPrefs.put(key, val); + } + + /* (non-Javadoc) + * @see serialize.VSSerializable#serialize(serialize.VSSerialize, + * java.io.ObjectOutputStream) + */ + protected synchronized void serialize_( + VSSerialize serialize, ObjectOutputStream objectOutputStream) + throws IOException { + /** For later backwards compatibility, to add more stuff */ + objectOutputStream.writeObject(Boolean.valueOf(false)); + + objectOutputStream.writeObject(booleanPrefs); + objectOutputStream.writeObject(colorPrefs); + objectOutputStream.writeObject(descriptionPrefs); + objectOutputStream.writeObject(floatPrefs); + objectOutputStream.writeObject(integerPrefs); + objectOutputStream.writeObject(longPrefs); + objectOutputStream.writeObject(stringPrefs); + objectOutputStream.writeObject(units); + objectOutputStream.writeObject(vectorPrefs); + objectOutputStream.writeObject(restrictions); + + /** For later backwards compatibility, to add more stuff */ + objectOutputStream.writeObject(Boolean.valueOf(false)); + } + + /* (non-Javadoc) + * @see serialize.VSSerializable#deserialize(serialize.VSSerialize, + * java.io.ObjectInputStream) + */ + @SuppressWarnings("unchecked") + protected synchronized void deserialize_(VSSerialize serialize, + ObjectInputStream objectInputStream) + throws IOException, ClassNotFoundException { + objectPrefs.clear(); + + /** For later backwards compatibility, to add more stuff */ + objectInputStream.readObject(); + + booleanPrefs = (HashMap) objectInputStream.readObject(); + colorPrefs = (HashMap) objectInputStream.readObject(); + descriptionPrefs = (HashMap) objectInputStream.readObject(); + floatPrefs = (HashMap) objectInputStream.readObject(); + integerPrefs = (HashMap) objectInputStream.readObject(); + longPrefs = (HashMap) objectInputStream.readObject(); + stringPrefs = (HashMap) objectInputStream.readObject(); + units = (HashMap) objectInputStream.readObject(); + vectorPrefs = (HashMap>) objectInputStream.readObject(); + restrictions = (HashMap) objectInputStream.readObject(); + + /** For later backwards compatibility, to add more stuff */ + objectInputStream.readObject(); + } + + /** + * Copies integers into another VSPrefs object. + * + * @param copyInto the copy into + * @param keys the keys + */ + public void copyIntegers(VSPrefs copyInto, String[] keys) { + for (String key : keys) + copyInto.initInteger(key, + getInteger(key), + getDescription(INTEGER_PREFIX + key), + (VSPrefsRestriction.VSIntegerPrefsRestriction) + getRestriction(INTEGER_PREFIX + key), + getUnit(INTEGER_PREFIX + key)); + } + + /** + * Copies longs into another VSPrefs object. + * + * @param copyInto the copy into + * @param keys the keys + */ + public void copyLongs(VSPrefs copyInto, String[] keys) { + for (String key : keys) + copyInto.initLong(key, getLong(key), + getDescription(LONG_PREFIX + key), + getUnit(LONG_PREFIX + key)); + } + + /** + * Copies floats into another VSPrefs object. + * + * @param copyInto the copy into + * @param keys the keys + */ + public void copyFloats(VSPrefs copyInto, String[] keys) { + for (String key : keys) + copyInto.initFloat(key, getFloat(key), + getDescription(FLOAT_PREFIX + key), + getUnit(FLOAT_PREFIX + key)); + } + + /** + * Copies strings into another VSPrefs object. + * + * @param copyInto the copy into + * @param keys the keys + */ + public void copyStrings(VSPrefs copyInto, String[] keys) { + for (String key : keys) + copyInto.initString(key, getString(key), + getDescription(STRING_PREFIX + key)); + } + + /** + * Copies color references into another VSPrefs object. + * + * @param copyInto the copy into + * @param keys the keys + */ + public void copyColors(VSPrefs copyInto, String[] keys) { + for (String key : keys) { + Color color = getColor(key); + float comp[] = color.getComponents(null); + copyInto.initColor(key, new Color(comp[0], comp[1], comp[2]), + getDescription(COLOR_PREFIX + key)); + } + } + + /** + * Copies colors. + * + * @param copyInto the copy into + * @param keys the keys + */ + public void copyBooleans(VSPrefs copyInto, String[] keys) { + for (String key : keys) + copyInto.initBoolean(key, getBoolean(key), + getDescription(BOOLEAN_PREFIX + key)); + } + + /* (non-Javadoc) + * @see java.lang.Object#toString() + */ + public String toString() { + String descr = ""; + + Set set = null; + + set = getIntegerKeySet(); + if (set.size() > 0) { + descr += INTEGER_PREFIX; + for (String key : set) + descr += key + "=" + getInteger(key) + "; "; + } + + set = getVectorKeySet(); + if (set.size() > 0) { + descr += VECTOR_PREFIX; + for (String key : set) + descr += key + "=" + getVector(key) + "; "; + } + + set = getLongKeySet(); + if (set.size() > 0) { + descr += LONG_PREFIX; + for (String key : set) + descr += key + "=" + getLong(key) + "; "; + } + + set = getFloatKeySet(); + if (set.size() > 0) { + descr += FLOAT_PREFIX; + for (String key : set) + descr += key + "=" + getFloat(key) + "; "; + } + + set = getBooleanKeySet(); + if (set.size() > 0) { + descr += BOOLEAN_PREFIX; + for (String key : set) + descr += key + "=" + getBoolean(key) + "; "; + } + + set = getStringKeySet(); + if (set.size() > 0) { + descr += STRING_PREFIX; + for (String key : set) + descr += key + "=" + getString(key) + "; "; + } + + if (descr.endsWith("; ")) + return descr.substring(0, descr.length() - 2); + + return descr; + } + + /** + * Gets the iD. + * + * @return the iD + */ + public int getID() { + return id; + } + + /** + * Checks if the prefs are empty. + * + * @return true, if empty + */ + public boolean isEmpty() { + if (!colorPrefs.isEmpty()) + return false; + + if (!floatPrefs.isEmpty()) + return false; + + if (!integerPrefs.isEmpty()) + return false; + + if (!vectorPrefs.isEmpty()) + return false; + + if (!longPrefs.isEmpty()) + return false; + + if (!stringPrefs.isEmpty()) + return false; + + if (!booleanPrefs.isEmpty()) + return false; + + return true; + } + + /** + * Returns all full keys. + * + * @return All full keys + */ + public ArrayList getAllFullKeys() { + ArrayList allKeys = new ArrayList(); + + Set set = null; + + set = getIntegerKeySet(); + for (String key : set) + allKeys.add(INTEGER_PREFIX + key); + + set = getVectorKeySet(); + for (String key : set) + allKeys.add(VECTOR_PREFIX + key); + + set = getLongKeySet(); + for (String key : set) + allKeys.add(LONG_PREFIX + key); + + set = getFloatKeySet(); + for (String key : set) + allKeys.add(FLOAT_PREFIX + key); + + set = getBooleanKeySet(); + for (String key : set) + allKeys.add(BOOLEAN_PREFIX + key); + + set = getStringKeySet(); + for (String key : set) + allKeys.add(STRING_PREFIX + key); + + Collections.sort(allKeys); + + return allKeys; + } +} diff --git a/src/main/java/prefs/VSPrefsRestriction.java b/src/main/java/prefs/VSPrefsRestriction.java new file mode 100644 index 0000000..27e828d --- /dev/null +++ b/src/main/java/prefs/VSPrefsRestriction.java @@ -0,0 +1,137 @@ +package prefs; + +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.io.Serializable; +import java.util.Vector; + +/** + * The class VSPrefsRestriction. + */ +abstract public class VSPrefsRestriction implements Serializable { + private static final long serialVersionUID = 2L; + + /** + * The class VSIntegerPrefsRestriction. + */ + public static class VSIntegerPrefsRestriction extends VSPrefsRestriction { + private static final long serialVersionUID = 2L; + /** The min value. */ + private int minValue; + + /** The max value. */ + private int maxValue; + + /** + * Instantiates a new integer setting restriction. + * + * @param minValue the min value + * @param maxValue the max value + */ + public VSIntegerPrefsRestriction(int minValue, int maxValue) { + this.minValue = minValue; + this.maxValue = maxValue; + } + + /** + * Gets the min value. + * + * @return the min value + */ + public int getMinValue() { + return minValue; + } + + /** + * Gets the max value. + * + * @return the max value + */ + public int getMaxValue() { + return maxValue; + } + + /* (non-Javadoc) + * @see prefs.VSPrefsRestriction#writeObject(java.io.ObjectOutputStream) + */ + public void writeObject(ObjectOutputStream out) + throws IOException { + out.writeObject(Integer.valueOf(minValue)); + out.writeObject(Integer.valueOf(maxValue)); + } + + /* (non-Javadoc) + * @see prefs.VSPrefsRestriction#readObject(java.io.ObjectInputStream) + */ + public void readObject(ObjectInputStream in) + throws IOException, ClassNotFoundException { + minValue = ((Integer) in.readObject()).intValue(); + maxValue = ((Integer) in.readObject()).intValue(); + } + } + + /** + * The class VSStringPrefsRestriction. + */ + public static class VSStringPrefsRestriction extends VSPrefsRestriction { + private static final long serialVersionUID = 2L; + + /** The possible selections. */ + Vector possibleSelections; + + /** + * Instantiates a new string setting restriction. + * + * @param possibleSelections the possible selections + */ + public VSStringPrefsRestriction(String [] possibleSelections) { + this.possibleSelections = new Vector(); + + for (String elem : possibleSelections) + this.possibleSelections.add(elem); + } + + /** + * Gets the possible selections. + * + * @return the possible selections + */ + public Vector getPossibleSelections() { + return possibleSelections; + } + + /* (non-Javadoc) + * @see prefs.VSPrefsRestriction#writeObject(java.io.ObjectOutputStream) + */ + public void writeObject(ObjectOutputStream out) + throws IOException { + out.writeObject(possibleSelections); + } + + /* (non-Javadoc) + * @see prefs.VSPrefsRestriction#readObject(java.io.ObjectInputStream) + */ + @SuppressWarnings("unchecked") + public void readObject(ObjectInputStream in) + throws IOException, ClassNotFoundException { + possibleSelections = (Vector) in.readObject(); + } + } + + /** + * Serializes the object. + * + * @param out The output stream + */ + abstract public void writeObject(ObjectOutputStream out) + throws IOException; + + /** + * Deserializes the object. + * + * @param in The input stream + */ + abstract public void readObject(ObjectInputStream in) + throws IOException, ClassNotFoundException; +} diff --git a/src/main/java/prefs/VSSerializablePrefs.java b/src/main/java/prefs/VSSerializablePrefs.java new file mode 100644 index 0000000..b175b41 --- /dev/null +++ b/src/main/java/prefs/VSSerializablePrefs.java @@ -0,0 +1,37 @@ +package prefs; + +//import java.util.*; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; + +import serialize.VSSerializable; +import serialize.VSSerialize; + +/** + * The class VSSerializablePrefs, this class is used if the VSPrefs should + * be serializable. + * + * @author Paul C. Buetow + */ +public class VSSerializablePrefs extends VSPrefs implements VSSerializable { + /* (non-Javadoc) + * @see serialize.VSSerializable#serialize(serialize.VSSerialize, + * java.io.ObjectOutputStream) + */ + public synchronized void serialize(VSSerialize serialize, + ObjectOutputStream objectOutputStream) + throws IOException { + super.serialize_(serialize, objectOutputStream); + } + + /* (non-Javadoc) + * @see serialize.VSSerializable#deserialize(serialize.VSSerialize, + * java.io.ObjectInputStream) + */ + public synchronized void deserialize(VSSerialize serialize, + ObjectInputStream objectInputStream) + throws IOException, ClassNotFoundException { + super.deserialize_(serialize, objectInputStream); + } +} diff --git a/src/main/java/prefs/editors/VSAbstractBetterEditor.java b/src/main/java/prefs/editors/VSAbstractBetterEditor.java new file mode 100644 index 0000000..87296b6 --- /dev/null +++ b/src/main/java/prefs/editors/VSAbstractBetterEditor.java @@ -0,0 +1,101 @@ +package prefs.editors; + +import java.awt.Color; +import java.awt.Container; +import java.awt.event.ActionEvent; + +import javax.swing.BoxLayout; +import javax.swing.JPanel; + +import prefs.VSPrefs; + +/** + * The class VSAbstractBetterEditor, is an improved VSAbstractEditor. + * + * @author Paul C. Buetow + */ +public abstract class VSAbstractBetterEditor extends VSAbstractEditor { + /** The content pane. */ + private Container contentPane; + + /** The title. */ + private String title; + + /** + * An simple constructor. + * + * @param prefs the prefs + * @param prefsToEdit the prefs to edit + * @param title the title + */ + public VSAbstractBetterEditor(VSPrefs prefs, VSPrefs prefsToEdit, + String title) { + super(prefs, prefsToEdit); + this.title = title; + this.contentPane = createContentPane(); + } + + /** + * Gets the title. + * + * @return the title + */ + public String getTitle() { + return title; + } + + /** + * Gets the content pane. + * + * @return the content pane + */ + public Container getContentPane() { + contentPane.setBackground(Color.WHITE); + return contentPane; + } + + /** + * Creates the content pane. + * + * @return the j panel + */ + private JPanel createContentPane() { + JPanel panel = new JPanel(); + panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); + + JPanel editPanel = getEditPanel(); + JPanel buttonPanel = getButtonPanel(); + + panel.add(editPanel); + panel.add(buttonPanel); + + return panel; + } + + /* (non-Javadoc) + * @see prefs.editors.VSAbstractEditor#addToButtonPanelFront( + * javax.swing.JPanel) + */ + protected void addToButtonPanelFront(JPanel buttonPanel) { } + + /* (non-Javadoc) + * @see prefs.editors.VSAbstractEditor#addToButtonPanelLast( + * javax.swing.JPanel) + */ + protected void addToButtonPanelLast(JPanel buttonPanel) { } + + /* (non-Javadoc) + * @see prefs.editors.VSAbstractEditor#addToEditTableLast() + */ + protected void addToEditTableLast() { } + + /* (non-Javadoc) + * @see prefs.editors.VSAbstractEditor#actionPerformed( + * java.awt.event.ActionEvent) + */ + public void actionPerformed(ActionEvent e) { + //String actionCommand = e.getActionCommand(); + /* More action in the super class!!! */ + super.actionPerformed(e); + } +} diff --git a/src/main/java/prefs/editors/VSAbstractEditor.java b/src/main/java/prefs/editors/VSAbstractEditor.java new file mode 100644 index 0000000..266b62c --- /dev/null +++ b/src/main/java/prefs/editors/VSAbstractEditor.java @@ -0,0 +1,1056 @@ +package prefs.editors; + +import java.awt.Color; +import java.awt.Component; +import java.awt.GridBagLayout; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.event.MouseEvent; +import java.awt.event.MouseListener; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.Set; +import java.util.Vector; + +import javax.swing.BoxLayout; +import javax.swing.JButton; +import javax.swing.JCheckBox; +import javax.swing.JComboBox; +import javax.swing.JComponent; +import javax.swing.JFrame; +import javax.swing.JLabel; +import javax.swing.JPanel; +import javax.swing.JScrollPane; +import javax.swing.JTextField; + +import prefs.VSPrefs; +import prefs.VSPrefsRestriction; +import utils.VS3Tupel; +import utils.VSFrame; + +/** + * The class VSAbstractEditor, an object of this class is used in order to + * edit a VSPrefs object. + * + * @author Paul C. Buetow + */ +public abstract class VSAbstractEditor implements ActionListener { + /** The boolean keys. */ + private ArrayList booleanKeys; + + /** The color keys. */ + private ArrayList colorKeys; + + /** The float keys. */ + private ArrayList floatKeys; + + /** The integer keys. */ + private ArrayList integerKeys; + + /** The vector keys. */ + private ArrayList vectorKeys; + + /** The long keys. */ + private ArrayList longKeys; + + /** The string keys. */ + private ArrayList stringKeys; + + /** The boolean fields. */ + private HashMap booleanFields; + + /** The integer fields. */ + private HashMap> integerFields; + + /** The vector fields. */ + private HashMap vectorFields; + + /** The color fields. */ + private HashMap colorFields; + + /** The float fields. */ + private HashMap floatFields; + + /** The long fields. */ + private HashMap longFields; + + /** The string fields. */ + private HashMap stringFields; + + /** The prefs to edit map. */ + private HashMap prefsToEditMap; + + /** The button panel. */ + private JPanel buttonPanel; + + /** The edit panel. */ + private JPanel editPanel; + + /** The edit table. */ + private VSEditorTable editTable; + + /** The frame. */ + private VSFrame frame; + + /** The expert mode changed. */ + private boolean expertModeChanged; + + /** The prefs. */ + protected VSPrefs prefs; + + /** The prefs to edit. */ + protected VSPrefs prefsToEdit; + + /** The Constant MIN_UNIT_LENGTH. */ + protected static final int MIN_UNIT_LENGTH = 5; + + /** The Constant VALUE_FIELD_COLS. */ + protected static final int VALUE_FIELD_COLS = 9; + + /** The Constant ALL_PREFERENCES. */ + public static final int ALL_PREFERENCES = 0; + + /** The Constant SIMULATION_PREFERENCES. */ + public static final int SIMULATION_PREFERENCES = 1; + + /** + * The standard constructor. + * + * @param prefs the prefs + * @param prefsToEdit the prefs to edit + */ + public VSAbstractEditor(VSPrefs prefs, VSPrefs prefsToEdit) { + init(prefs, prefsToEdit); + } + + /** + * Adds components to the front of the button panel . + * + * @param buttonPanel the button panel + */ + abstract protected void addToButtonPanelFront(JPanel buttonPanel); + + /** + * Adds components to last of the button panel. + * + * @param buttonPanel the button panel + */ + abstract protected void addToButtonPanelLast(JPanel buttonPanel); + + /** + * Adds the to edit table last. + */ + abstract protected void addToEditTableLast(); + + /** + * Sets the default prefs. + * + * @param prefs the new prefs + */ + public void setPrefs(VSPrefs prefs) { + this.prefs = prefs; + } + + /** + * Sets the prefs to edit. + * + * @param prefsToEdit the new prefs to edit + */ + public void setPrefsToEdit(VSPrefs prefsToEdit) { + this.prefsToEdit = prefsToEdit; + } + + /** + * Sets the frame being used by the editor, if any. + * + * @param frame the new frame + */ + public void setFrame(VSFrame frame) { + this.frame = frame; + } + + /** + * Gets the frame. + * + * @return the frame + */ + public VSFrame getFrame() { + return frame; + } + + /** + * The given editors frame will get disposed if the "OK" button has been + * pressed. This can only happen if the editor has its own frame. + */ + protected void disposeFrameIfExists() { + if (frame != null) + frame.dispose(); + } + + /** + * The given editors frame will get disposed if its parent component + * disposes. This can only happen if the editor has its own frame. + */ + protected void disposeFrameWithParentIfExists() { + if (frame != null) + frame.disposeWithParent(); + } + + /** + * Inits the editor. + * + * @param prefs the prefs + * @param prefsToEdit the prefs to edit + */ + private void init(VSPrefs prefs, VSPrefs prefsToEdit) { + this.prefs = prefs; + this.prefsToEdit = prefsToEdit; + + editPanel = createEditPanel(); + buttonPanel = createButtonPanel(); + + prefsToEditMap = new HashMap(); + + colorFields = new HashMap(); + floatFields = new HashMap(); + integerFields = new HashMap>(); + vectorFields = new HashMap(); + longFields = new HashMap(); + booleanFields = new HashMap(); + stringFields = new HashMap(); + + colorKeys = filterKeys(prefsToEdit.getColorKeySet()); + floatKeys = filterKeys(prefsToEdit.getFloatKeySet()); + integerKeys = filterKeys(prefsToEdit.getIntegerKeySet()); + vectorKeys = filterKeys(prefsToEdit.getVectorKeySet()); + longKeys = filterKeys(prefsToEdit.getLongKeySet()); + booleanKeys = filterKeys(prefsToEdit.getBooleanKeySet()); + stringKeys = filterKeys(prefsToEdit.getStringKeySet()); + + fillEditPanelFront(prefsToEdit); + fillEditPanel(prefsToEdit); + } + + /** + * Filters out all keys to edit. + * + * @param set the set which contains all keys of a given hash + * + * @return the filtered keys + */ + private ArrayList filterKeys(Set set) { + ArrayList filtered = new ArrayList(); + boolean expertMode = prefs.getBoolean("sim.mode.expert"); + + for (String elem : set) { + if (!elem.startsWith("lang.") && !elem.startsWith("keyevent.")) { + if (expertMode) + filtered.add(elem); + else if (!elem.startsWith("col.") && (!elem.startsWith("div."))) + filtered.add(elem); + } + } + + return filtered; + } + + /** + * Creates the button panel. + * + * @return the panel + */ + private JPanel createButtonPanel() { + JPanel buttonPanel = new JPanel(); + buttonPanel.setBackground(Color.WHITE); + addToButtonPanelFront(buttonPanel); + + JButton resetButton = new JButton( + prefs.getString("lang.reset")); + resetButton.setMnemonic(prefs.getInteger("keyevent.reset")); + resetButton.addActionListener(this); + buttonPanel.add(resetButton); + + addToButtonPanelLast(buttonPanel); + + return buttonPanel; + } + + /** + * Creates the unit panel. + * + * @param comp the comp + * @param key the key + * + * @return the panel + */ + private JPanel createUnitPanel(VSPrefs prefsToEdit, Component comp, + String fullKey) { + JPanel unitPanel = new JPanel(new GridBagLayout()); + unitPanel.setBackground(Color.WHITE); + unitPanel.setBorder(null); + + String unitText = prefsToEdit.getUnit(fullKey); + if (unitText == null) + unitText = ""; + + unitText = " " + unitText; + while (unitText.length() < MIN_UNIT_LENGTH) + unitText = unitText + " "; + JLabel unitLabel = new JLabel(unitText); + + unitPanel.setLayout(new BoxLayout(unitPanel, BoxLayout.X_AXIS)); + unitPanel.add(comp); + unitPanel.add(unitLabel); + + return unitPanel; + } + + /** + * Creates the edit panel. + * + * @return the panel + */ + private JPanel createEditPanel() { + JPanel editPanel = new JPanel(); + editPanel.setLayout(new BoxLayout(editPanel, BoxLayout.Y_AXIS)); + editPanel.setBackground(Color.WHITE); + + editTable = new VSEditorTable(prefs); + JScrollPane scrollPane = new JScrollPane(editTable); + editPanel.add(scrollPane); + + return editPanel; + } + + /** + * Creates the integer component. + * + * @param fullKey the full key + * @param key the key + * @param prefsToEdit the prefs to edit + * +