.classpath | 3
LocalisedText.properties | 6
build.xml | 72 +-
data/profiles/LIST_OF_PROFILES | 6
data/profiles/ORDocking.predefined | 5
data/profiles/ORDocking.profile | 3
data/profiles/default.profile | 2
data/profiles/default.profiles | 7
data/profiles/pbem.predefined | 2
data/profiles/prettyUI.predefined | 11
data/profiles/prettyUI.profile | 9
data/profiles/root | 63 +
data/profiles/root.profile | 63 -
data/profiles/test | 15
data/profiles/test.profile | 15
lib/commons-io-2.1/commons-io-2.1.jar |binary
rails/common/ConfigItem.java | 180 +++++
rails/common/ConfigManager.java | 259 ++++++++
rails/common/ConfigProfile.java | 303 +++++++++
rails/common/LocalText.java | 1
rails/common/MoneyFormatter.java | 1
rails/common/parser/Config.java | 606 -------------------
rails/common/parser/ConfigItem.java | 151 ----
rails/game/Bank.java | 2
rails/game/MapManager.java | 1
rails/game/ReportBuffer.java | 2
rails/sound/SoundConfig.java | 2
rails/ui/swing/AbstractReportWindow.java | 2
rails/ui/swing/ConfigWindow.java | 306 ++-------
rails/ui/swing/GameSetupWindow.java | 21
rails/ui/swing/GameUIManager.java | 3
rails/ui/swing/GridPanel.java | 2
rails/ui/swing/ImageLoader.java | 2
rails/ui/swing/ORPanel.java | 2
rails/ui/swing/ORUIManager.java | 2
rails/ui/swing/ORWindow.java | 2
rails/ui/swing/ReportWindow.java | 2
rails/ui/swing/Scale.java | 2
rails/ui/swing/SplashWindow.java | 2
rails/ui/swing/StatusWindow.java | 17
rails/ui/swing/WindowSettings.java | 2
rails/ui/swing/elements/DockingFrame.java | 2
rails/ui/swing/elements/RailsIconButton.java | 2
rails/ui/swing/hexmap/HexHighlightMouseListener.java | 2
rails/ui/swing/hexmap/HexMap.java | 2
rails/ui/swing/hexmap/HexMapImage.java | 2
rails/util/RunGame.java | 4
rails/util/SystemOS.java | 94 ++
test/TestGame.java | 2
test/TestGameBuilder.java | 7
tools/ListAndFixSavedFiles.java | 5
51 files changed, 1126 insertions(+), 1153 deletions(-)
New commits:
commit 83e4a63c76f277e11ff75bf86f0dcb84d673c215
Author: Stefan Frey <ste...@we...>
Date: Mon Feb 20 12:19:28 2012 +0100
New configuration system based on a profile tree.
Added ConfigManager and ConfigProfile classes
Refactored Config and ConfigWindow, Config is only a proxy to ConfigManager
Added SystemOS class for os related functions
Added common-io library for additional file i/o functions
diff --git a/.classpath b/.classpath
index 4dd2569..a1de363 100644
--- a/.classpath
+++ b/.classpath
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
- <classpathentry excluding="lib/JLayer1.0.1/|lib/df_1.1.0/|lib/batik-1.7/|lib/batik-1.7/|lib/batik-1.7/lib/|lib/batik-1.7/lib/|lib/jgraph5/|lib/jgraph5/|lib/jgrapht-0.7.3/|lib/jgrapht-0.7.3/|lib/junit-4.8.2|lib/log4j-1.2/|lib/log4j-1.2/" kind="src" path=""/>
+ <classpathentry excluding="lib/JLayer1.0.1/|lib/batik-1.7/|lib/batik-1.7/|lib/batik-1.7/lib/|lib/batik-1.7/lib/|lib/commons-io-2.1/|lib/df_1.1.0/|lib/jgraph5/|lib/jgraph5/|lib/jgrapht-0.7.3/|lib/jgrapht-0.7.3/|lib/junit-4.8.2|lib/log4j-1.2/|lib/log4j-1.2/" kind="src" path=""/>
<classpathentry exported="true" kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry exported="true" kind="lib" path="lib/log4j-1.2/log4j-1.2.14.jar"/>
<classpathentry exported="true" kind="lib" path="lib/jgrapht-0.7.3/jgrapht-jdk1.5.jar"/>
@@ -29,5 +29,6 @@
<classpathentry kind="lib" path="lib/batik-1.7/lib/batik-codec.jar"/>
<classpathentry kind="lib" path="lib/df_1.1.0/dockingFramesCommon.jar"/>
<classpathentry kind="lib" path="lib/df_1.1.0/dockingFramesCore.jar"/>
+ <classpathentry exported="true" kind="lib" path="lib/commons-io-2.1/commons-io-2.1.jar"/>
<classpathentry kind="output" path="classes"/>
</classpath>
diff --git a/LocalisedText.properties b/LocalisedText.properties
index ce07410..059e471 100644
--- a/LocalisedText.properties
+++ b/LocalisedText.properties
@@ -78,10 +78,8 @@ COMPANY_DETAILS=Company details
CONFIG=Configuration
CONFIG_APPLY_MESSAGE=<html>Current changes (will be) applied.<br>Be aware that some changes to be active require <br> UI redraws or a restart of Rails.</html>
CONFIG_APPLY_TITLE=Apply confirmation
-CONFIG_CURRENT_PATH=Active filepath = {0}
-CONFIG_CURRENT_PROFILE=Active profile = {0} (based on = {1})
-CONFIG_DEFAULT_TITLE=Default profile
-CONFIG_DEFAULT_MESSAGE=Select a template for settings
+CONFIG_USER_PROFILE=Active user profile = {0}
+CONFIG_PREDEFINED_PROFILE=Active predefined profile = {0}
CONFIG_INFO_TITLE=Info text for {0}
CONFIF_LOAD_ERROR_MESSAGE=An error occurred during load of the file {0}.\nProfile was not loaded.
CONFIG_LOAD_TITLE=Load of profile
diff --git a/build.xml b/build.xml
index c098278..e832108 100644
--- a/build.xml
+++ b/build.xml
@@ -6,6 +6,7 @@
as the first entry and export the buildfile again. -->
<project basedir="." default="build" name="Rails">
<property environment="env"/>
+ <property name="ECLIPSE_HOME" value="../../../installed/eclipse"/>
<property name="junit.output.dir" value="junit"/>
<property name="debuglevel" value="source,lines,vars"/>
<property name="target" value="1.5"/>
@@ -17,9 +18,6 @@
<pathelement location="lib/jgrapht-0.7.3/jgrapht-jdk1.5.jar"/>
<pathelement location="lib/jgraph5/jgraph.jar"/>
<pathelement location="lib/junit-4.8.2/junit.jar"/>
- <pathelement location="lib/JLayer1.0.1/jl1.0.1.jar"/>
- <pathelement location="lib/df_1.1.0/dockingFramesCommon.jar"/>
- <pathelement location="lib/df_1.1.0/dockingFramesCore.jar"/>
<pathelement location="lib/batik-1.7/lib/batik-transcoder.jar"/>
<pathelement location="lib/batik-1.7/lib/batik-dom.jar"/>
<pathelement location="lib/batik-1.7/lib/batik-swing.jar"/>
@@ -36,23 +34,50 @@
<pathelement location="lib/batik-1.7/lib/batik-svg-dom.jar"/>
<pathelement location="lib/batik-1.7/lib/batik-xml.jar"/>
<pathelement location="lib/batik-1.7/lib/js.jar"/>
+ <pathelement location="lib/JLayer1.0.1/jl1.0.1.jar"/>
<pathelement location="lib/batik-1.7/lib/xerces_2_5_0.jar"/>
<pathelement location="lib/batik-1.7/lib/xml-apis-ext.jar"/>
<pathelement location="lib/batik-1.7/lib/batik-anim.jar"/>
<pathelement location="lib/batik-1.7/lib/batik-codec.jar"/>
+ <pathelement location="lib/df_1.1.0/dockingFramesCommon.jar"/>
+ <pathelement location="lib/df_1.1.0/dockingFramesCore.jar"/>
+ <pathelement location="lib/commons-io-2.1/commons-io-2.1.jar"/>
</path>
<target name="init">
<mkdir dir="classes"/>
<copy includeemptydirs="false" todir="classes">
<fileset dir=".">
- <exclude name="**/*.launch"/>
+ <exclude name="**/*.ucls"/>
+ <exclude name="**/*.uad"/>
+ <exclude name="**/*.ucd"/>
+ <exclude name="**/*.uld"/>
+ <exclude name="**/*.upd"/>
+ <exclude name="**/*.udd"/>
+ <exclude name="**/*.uod"/>
+ <exclude name="**/*.usd"/>
+ <exclude name="**/*.utd"/>
+ <exclude name="**/*.uud"/>
+ <exclude name="**/*.odd"/>
+ <exclude name="**/*.ead"/>
+ <exclude name="**/*.ecd"/>
+ <exclude name="**/*.eld"/>
+ <exclude name="**/*.epd"/>
+ <exclude name="**/*.edd"/>
+ <exclude name="**/*.eod"/>
+ <exclude name="**/*.esd"/>
+ <exclude name="**/*.etd"/>
+ <exclude name="**/*.eud"/>
+ <exclude name="**/*.urd"/>
+ <exclude name="**/*.uml"/>
+ <exclude name="**/*.ecore"/>
<exclude name="**/*.java"/>
+ <exclude name="lib/JLayer1.0.1/"/>
<exclude name="lib/batik-1.7/"/>
<exclude name="lib/batik-1.7/"/>
<exclude name="lib/batik-1.7/lib/"/>
<exclude name="lib/batik-1.7/lib/"/>
- <exclude name="lib/JLayer1.0.1"/>
- <exclude name="lib/df_1.1.0"/>
+ <exclude name="lib/commons-io-2.1/"/>
+ <exclude name="lib/df_1.1.0/"/>
<exclude name="lib/jgraph5/"/>
<exclude name="lib/jgraph5/"/>
<exclude name="lib/jgrapht-0.7.3/"/>
@@ -71,14 +96,15 @@
<target name="build-subprojects"/>
<target depends="init" name="build-project">
<echo message="${ant.project.name}: ${ant.file}"/>
- <javac debug="true" debuglevel="${debuglevel}" destdir="classes" includeantruntime="false" source="${source}" target="${target}">
+ <javac debug="true" debuglevel="${debuglevel}" destdir="classes" source="${source}" target="${target}">
<src path="."/>
+ <exclude name="lib/JLayer1.0.1/"/>
<exclude name="lib/batik-1.7/"/>
<exclude name="lib/batik-1.7/"/>
<exclude name="lib/batik-1.7/lib/"/>
<exclude name="lib/batik-1.7/lib/"/>
- <exclude name="lib/JLayer1.0.1"/>
- <exclude name="lib/df_1.1.0"/>
+ <exclude name="lib/commons-io-2.1/"/>
+ <exclude name="lib/df_1.1.0/"/>
<exclude name="lib/jgraph5/"/>
<exclude name="lib/jgraph5/"/>
<exclude name="lib/jgrapht-0.7.3/"/>
@@ -103,14 +129,31 @@
<property name="build.compiler" value="org.eclipse.jdt.core.JDTCompilerAdapter"/>
<antcall target="build"/>
</target>
+ <target name="Check Save File (ListAndFixSavedFiles)">
+ <java classname="tools.ListAndFixSavedFiles" failonerror="true" fork="yes">
+ <classpath refid="Rails.classpath"/>
+ </java>
+ </target>
<target name="Start Rails (RunGame)">
<java classname="rails.util.RunGame" failonerror="true" fork="yes">
- <jvmarg line="-DRevenueCalculation=suggest -Dswing.defaultlaf=com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"/>
+ <jvmarg line="-DRevenueCalculation=suggest -Dawt.useSystemAAFontSettings=on"/>
<classpath refid="Rails.classpath"/>
</java>
</target>
- <target name="Check Save File (ListAndFixSavedFiles)">
- <java classname="tools.ListAndFixSavedFiles" failonerror="true" fork="yes">
+ <target name="Start Rails (RunGame) (Profiled)">
+ <java classname="rails.util.RunGame" failonerror="true" fork="yes">
+ <jvmarg line="-DRevenueCalculation=suggest -Dawt.useSystemAAFontSettings=on"/>
+ <classpath refid="Rails.classpath"/>
+ </java>
+ </target>
+ <target name="Delete and Update Test Game Reports (TestGameBuilder)">
+ <java classname="test.TestGameBuilder" failonerror="true" fork="yes">
+ <classpath refid="Rails.classpath"/>
+ </java>
+ </target>
+ <target name="Start Rails (RunGame) Nimbus LAF">
+ <java classname="rails.util.RunGame" failonerror="true" fork="yes">
+ <jvmarg line="-DRevenueCalculation=suggest -Dswing.defaultlaf=com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"/>
<classpath refid="Rails.classpath"/>
</java>
</target>
@@ -122,11 +165,6 @@
<classpath refid="Rails.classpath"/>
</junit>
</target>
- <target name="Delete and Update Test Game Reports (TestGameBuilder)">
- <java classname="test.TestGameBuilder" failonerror="true" fork="yes">
- <classpath refid="Rails.classpath"/>
- </java>
- </target>
<target name="junitreport">
<junitreport todir="${junit.output.dir}">
<fileset dir="${junit.output.dir}">
diff --git a/data/profiles/LIST_OF_PROFILES b/data/profiles/LIST_OF_PROFILES
new file mode 100644
index 0000000..d061833
--- /dev/null
+++ b/data/profiles/LIST_OF_PROFILES
@@ -0,0 +1,6 @@
+### List of predefined profiles
+### available for user selection
+### with extension .predefined
+pbem
+ORDocking
+prettyUI
diff --git a/data/profiles/ORDocking.predefined b/data/profiles/ORDocking.predefined
new file mode 100644
index 0000000..aa4985d
--- /dev/null
+++ b/data/profiles/ORDocking.predefined
@@ -0,0 +1,5 @@
+### ORDocking profile
+### activates the docking framework
+or.window.dockablePanels=yes
+### cannot be derived
+profile.final=yes
diff --git a/data/profiles/ORDocking.profile b/data/profiles/ORDocking.profile
deleted file mode 100644
index e5f8d80..0000000
--- a/data/profiles/ORDocking.profile
+++ /dev/null
@@ -1,3 +0,0 @@
-### ORDocking profile
-### activates the docking framework
-or.window.dockablePanels=yes
diff --git a/data/profiles/default.profile b/data/profiles/default.profile
deleted file mode 100644
index 122c1b7..0000000
--- a/data/profiles/default.profile
+++ /dev/null
@@ -1,2 +0,0 @@
-### Default profile currently is identical to root.profile
-### so no change here
diff --git a/data/profiles/default.profiles b/data/profiles/default.profiles
deleted file mode 100644
index 9cedd38..0000000
--- a/data/profiles/default.profiles
+++ /dev/null
@@ -1,7 +0,0 @@
-### This is the list of (predefined) profiles and their file locations
-### A dot (".") before the profilename prevents displaying those to the user selection
-.root=data/profiles/root.profile
-.test=data/profiles/test.profile
-default=data/profiles/default.profile
-prettyUI=data/profiles/prettyUI.profile
-ORDocking=data/profiles/ORDocking.profile
diff --git a/data/profiles/pbem.predefined b/data/profiles/pbem.predefined
new file mode 100644
index 0000000..122c1b7
--- /dev/null
+++ b/data/profiles/pbem.predefined
@@ -0,0 +1,2 @@
+### Default profile currently is identical to root.profile
+### so no change here
diff --git a/data/profiles/prettyUI.predefined b/data/profiles/prettyUI.predefined
new file mode 100644
index 0000000..f2e51b3
--- /dev/null
+++ b/data/profiles/prettyUI.predefined
@@ -0,0 +1,11 @@
+### prettyUI profile
+### activates the new UI options of Frederick
+button.iconPosition=left
+button.iconText=text and icon
+button.iconSize=small
+gridPanel.tableBorders=enabled
+map.image.display=yes
+map.displayCurrentRoutes=yes
+map.highlightHexes=yes
+### cannot be derived
+profile.final=yes
diff --git a/data/profiles/prettyUI.profile b/data/profiles/prettyUI.profile
deleted file mode 100644
index a537864..0000000
--- a/data/profiles/prettyUI.profile
+++ /dev/null
@@ -1,9 +0,0 @@
-### prettyUI profile
-### activates the new UI options of Frederick
-button.iconPosition=left
-button.iconText=text and icon
-button.iconSize=small
-gridPanel.tableBorders=enabled
-map.image.display=yes
-map.displayCurrentRoutes=yes
-map.highlightHexes=yes
diff --git a/data/profiles/root b/data/profiles/root
new file mode 100644
index 0000000..3e08d4d
--- /dev/null
+++ b/data/profiles/root
@@ -0,0 +1,63 @@
+### Root profile defines the settings used for all (predefined) default profiles
+### All others define only deviations
+
+### Panel General
+locale=en_US
+default_game=1830
+default_players=
+local.player.names=
+
+### Panel Save
+save.directory=
+save.filename.date_time_pattern=yyyyMMdd_HHmm
+save.filename.date_time_zone=UTC
+save.filename.suffix=NEXT_PLAYER
+save.filename.extension=rails
+save.recovery.active=no
+save.recovery.filepath=18xx_autosave.rails
+
+### Panel Font
+font.ui.scale=1
+font.ui.name=
+font.ui.style=bold
+
+### Panel Map
+map.autoscroll=yes
+map.defaultZoomFitOption=none
+map.displayCurrentRoutes=no
+map.highlightHexes=no
+map.image.display=no
+map.zoomstep=10
+
+### Panel Windows
+or.window.dockablePanels=no
+report.window.type=dynamic
+report.window.open=yes
+report.window.editable=no
+splash.window.open=yes
+stockchart.window.open=yes
+
+### Panel Format
+money_format=
+or.number_format=composite
+
+### Panel Appearance
+button.iconText=only text
+button.iconSize=small
+button.iconPosition=left
+gridPanel.tableBorder=disabled
+route.colour.1=00ffff
+route.colour.2=ffc0cb
+route.colour.3=ffa500
+route.colour.4=808080
+
+### Panel Log
+#report.directory=log
+#report.filename.date_time_pattern=yyyyMMdd
+#report.filename.extension=log
+
+### Panel Music
+sound.backgroundMusic=disabled
+
+### Panel SFX
+sound.sfx=disabled
diff --git a/data/profiles/root.profile b/data/profiles/root.profile
deleted file mode 100644
index 3e08d4d..0000000
--- a/data/profiles/root.profile
+++ /dev/null
@@ -1,63 +0,0 @@
-### Root profile defines the settings used for all (predefined) default profiles
-### All others define only deviations
-
-### Panel General
-locale=en_US
-default_game=1830
-default_players=
-local.player.names=
-
-### Panel Save
-save.directory=
-save.filename.date_time_pattern=yyyyMMdd_HHmm
-save.filename.date_time_zone=UTC
-save.filename.suffix=NEXT_PLAYER
-save.filename.extension=rails
-save.recovery.active=no
-save.recovery.filepath=18xx_autosave.rails
-
-### Panel Font
-font.ui.scale=1
-font.ui.name=
-font.ui.style=bold
-
-### Panel Map
-map.autoscroll=yes
-map.defaultZoomFitOption=none
-map.displayCurrentRoutes=no
-map.highlightHexes=no
-map.image.display=no
-map.zoomstep=10
-
-### Panel Windows
-or.window.dockablePanels=no
-report.window.type=dynamic
-report.window.open=yes
-report.window.editable=no
-splash.window.open=yes
-stockchart.window.open=yes
-
-### Panel Format
-money_format=
-or.number_format=composite
-
-### Panel Appearance
-button.iconText=only text
-button.iconSize=small
-button.iconPosition=left
-gridPanel.tableBorder=disabled
-route.colour.1=00ffff
-route.colour.2=ffc0cb
-route.colour.3=ffa500
-route.colour.4=808080
-
-### Panel Log
-#report.directory=log
-#report.filename.date_time_pattern=yyyyMMdd
-#report.filename.extension=log
-
-### Panel Music
-sound.backgroundMusic=disabled
-
-### Panel SFX
-sound.sfx=disabled
diff --git a/data/profiles/test b/data/profiles/test
new file mode 100644
index 0000000..897ee6d
--- /dev/null
+++ b/data/profiles/test
@@ -0,0 +1,15 @@
+####################### Test preferences ################################
+#
+# Those are the settings used for automated testing
+#
+########################################################################
+locale=te_ST
+
+# An overriding format can be specified here, but then applies to all games.
+# The @ character must be present and is replaced by the amount.
+# Example: �@ to specify a pound sign prefix: �100.
+money_format=@
+
+save.directory=test/data
+report.filename.extension=report
+failed.filename.extension=failed
diff --git a/data/profiles/test.profile b/data/profiles/test.profile
deleted file mode 100644
index 897ee6d..0000000
--- a/data/profiles/test.profile
+++ /dev/null
@@ -1,15 +0,0 @@
-####################### Test preferences ################################
-#
-# Those are the settings used for automated testing
-#
-########################################################################
-locale=te_ST
-
-# An overriding format can be specified here, but then applies to all games.
-# The @ character must be present and is replaced by the amount.
-# Example: �@ to specify a pound sign prefix: �100.
-money_format=@
-
-save.directory=test/data
-report.filename.extension=report
-failed.filename.extension=failed
diff --git a/lib/commons-io-2.1/commons-io-2.1.jar b/lib/commons-io-2.1/commons-io-2.1.jar
new file mode 100644
index 0000000..b5c7d69
Binary files /dev/null and b/lib/commons-io-2.1/commons-io-2.1.jar differ
diff --git a/rails/common/ConfigItem.java b/rails/common/ConfigItem.java
new file mode 100644
index 0000000..382d9a9
--- /dev/null
+++ b/rails/common/ConfigItem.java
@@ -0,0 +1,180 @@
+package rails.common;
+
+import java.util.Arrays;
+import java.util.List;
+
+import org.apache.log4j.Logger;
+
+import rails.common.parser.ConfigurationException;
+import rails.common.parser.Tag;
+import rails.util.Util;
+
+/**
+ * Defines an item used for the configuration of rails
+ * T represents the value type
+ */
+
+public final class ConfigItem {
+
+ protected static Logger log =
+ Logger.getLogger(ConfigItem.class.getPackage().getName());
+
+ /**
+ * Defines possible types (Java classes used as types in ConfigItem below
+ */
+ public static enum ConfigType {
+ BOOLEAN, INTEGER, PERCENT, STRING, LIST, FONT, DIRECTORY, FILE, COLOR;
+ }
+
+ // static attributes
+ public final String name;
+ public final ConfigType type;
+ public final List<String> allowedValues;
+ public final String formatMask;
+
+ // method call attributes
+ private final String initClass;
+ private final String initMethod;
+ private final boolean alwaysCallInit;
+ private final boolean initParameter;
+
+ // dynamic attributes
+ private String newValue;
+ private String currentValue;
+
+ ConfigItem(Tag tag) throws ConfigurationException {
+ // check name and type (required)
+ String name = tag.getAttributeAsString("name");
+ if (Util.hasValue(name)) {
+ this.name = name;
+ } else {
+ throw new ConfigurationException("Missing name for configuration item");
+ }
+ // optional: list of allowed values
+ String valueString = tag.getAttributeAsString("values");
+ if (Util.hasValue(valueString)) {
+ allowedValues = Arrays.asList(valueString.split(","));
+ this.type = ConfigType.LIST;
+ } else {
+ allowedValues = null;
+ String type = tag.getAttributeAsString("type");
+ if (Util.hasValue(type)) {
+ try {
+ this.type = ConfigType.valueOf(type.toUpperCase());
+ } catch (Exception e) {
+ throw new ConfigurationException("Missing or invalid type for configuration item, exception = " + e);
+ }
+ } else {
+ throw new ConfigurationException("Missing or invalid type for configuration item");
+ }
+ if (this.type == ConfigType.LIST) {
+ throw new ConfigurationException("No values defined for LIST config item");
+ }
+ }
+
+ // optional: formatMask
+ formatMask = tag.getAttributeAsString("formatMask");
+
+ // optional: init method attributes
+ initClass = tag.getAttributeAsString("initClass");
+ initMethod = tag.getAttributeAsString("initMethod");
+ alwaysCallInit = tag.getAttributeAsBoolean("alwaysCallInit",false);
+ initParameter = tag.getAttributeAsBoolean("initParameter", false);
+
+ // intialize values
+ currentValue = null;
+ newValue = null;
+ }
+
+
+ public boolean hasChanged() {
+ if (newValue == null) return false;
+ return !getCurrentValue().equals(newValue);
+ }
+
+ public String getValue() {
+ if (hasChanged()) {
+ return getNewValue();
+ } else {
+ return getCurrentValue();
+ }
+ }
+
+ public String getCurrentValue() {
+ if (currentValue == null) return "";
+ return currentValue;
+ }
+
+ public void setCurrentValue(String value) {
+ currentValue = value;
+ newValue = null;
+ }
+
+ @Deprecated
+ public boolean hasNewValue() {
+ return (newValue != null);
+ }
+
+ public String getNewValue() {
+ if (newValue == null) return "";
+ return newValue;
+ }
+
+ public void setNewValue(String value) {
+ if (value == null || value.equals("") || value.equals(currentValue)) {
+ newValue = null;
+ } else {
+ newValue = value;
+ }
+ log.debug("ConfigItem " + name + " set to new value " + newValue);
+ }
+
+ public void resetValue() {
+ if (hasChanged()) {
+ currentValue = newValue;
+ newValue = null;
+ }
+ }
+
+ /**
+ * @param applyInitMethod Specifies whether init should be called. Can be overruled
+ * by an additional tag alwaysCallInit
+ */
+ void callInitMethod(boolean applyInitMethod) {
+ if (!applyInitMethod && !alwaysCallInit) return;
+ if (initClass == null || initMethod == null) return;
+
+ // call without parameter
+ try {
+ Class<?> clazz = Class.forName(initClass);
+
+ if (initParameter) {
+ clazz.getMethod(initMethod, String.class).invoke(null, newValue);
+
+ } else {
+ clazz.getMethod(initMethod).invoke(null);
+ }
+ } catch (Exception e) {
+ log.error("Config profile: cannot call initMethod, Exception = " + e.toString());
+ }
+ }
+
+
+ public String toString() {
+ StringBuffer s = new StringBuffer();
+ s.append("Configuration Item: name = " + name + ", type = " + type);
+ s.append(", current value = " + getCurrentValue()) ;
+ s.append(", new value = " + getNewValue());
+ if (allowedValues != null) {
+ s.append(", allowedValues = " + allowedValues);
+ }
+ if (formatMask != null) {
+ s.append(", formatMask = " + formatMask);
+ }
+
+ return s.toString();
+ }
+
+
+
+}
diff --git a/rails/common/ConfigManager.java b/rails/common/ConfigManager.java
new file mode 100644
index 0000000..c2420c9
--- /dev/null
+++ b/rails/common/ConfigManager.java
@@ -0,0 +1,259 @@
+package rails.common;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+import java.util.Set;
+
+import org.apache.log4j.Logger;
+
+import rails.common.ConfigManager;
+import rails.common.parser.ConfigurableComponentI;
+import rails.common.parser.ConfigurationException;
+import rails.common.parser.Tag;
+import rails.game.Game;
+import rails.game.GameManagerI;
+import rails.util.Util;
+
+/**
+ * ConfigManager is a utility class that collects all functions
+ * used to define and control configuration options
+ *
+ * It is a rewrite of the previouslsy used static class Config
+ */
+
+public class ConfigManager implements ConfigurableComponentI {
+
+ protected static Logger log;
+
+ // STATIC CONSTANTS
+
+ // Log4j command line option, compare log4j documentation
+ private static final String LOG4J_CLI_OPTION = "log4j.configuration";
+ // Default Log4j configuration-file
+ private static final String LOG4J_CONFIG_FILE = "log4j.properties";
+
+ // XML setup
+ private static final String CONFIG_XML_DIR = "data";
+ private static final String CONFIG_XML_FILE = "Properties.xml";
+ private static final String CONFIG_TAG = "Properties";
+ private static final String SECTION_TAG = "Section";
+ private static final String ITEM_TAG = "Property";
+
+ // singleton configuration for ConfigManager
+ private static final ConfigManager instance = new ConfigManager();
+
+ // INSTANCE DATA
+
+ // configuration items: replace with Multimap in Rails 2.0
+ private final Map<String, List<ConfigItem>> configSections = new HashMap<String, List<ConfigItem>>();
+
+ // profile storage
+ private ConfigProfile activeProfile;
+
+ /**
+ * Initial configuration immediately after startup:
+ * Setting of log4j and start logger
+ * @param test if true configurations are setup for integration tests, false for productive use
+ */
+ private static void startlog4j() {
+
+ // log4j settings
+ String log4jSelection = System.getProperty(LOG4J_CLI_OPTION);
+ if (!Util.hasValue(log4jSelection)) {
+ log4jSelection = LOG4J_CONFIG_FILE;
+ }
+ // Sets those settings
+ System.setProperty("log4j.configuration", log4jSelection);
+ System.out.println("log4j.configuration = " + log4jSelection);
+
+ // Activate logger
+ log = Logger.getLogger(ConfigManager.class.getPackage().getName());
+ log.debug("Activate log4j logging using configuration file = " + log4jSelection);
+
+ }
+
+ public static void initConfiguration(boolean test) {
+ startlog4j();
+
+ try {
+ List<String> directories = new ArrayList<String>();
+ directories.add(CONFIG_XML_DIR);
+ // Find the config tag inside the the config xml file
+ Tag configTag =
+ Tag.findTopTagInFile(CONFIG_XML_FILE, directories, CONFIG_TAG);
+ log.debug("Opened config xml, filename = " + CONFIG_XML_FILE);
+ instance.configureFromXML(configTag);
+ } catch (ConfigurationException e) {
+ log.error("Configuration error in setup of " + CONFIG_XML_FILE + ", exception = " + e);
+ }
+
+ if (test) {
+ instance.initTest();
+ } else {
+ instance.init();
+ }
+ }
+
+
+ /**
+ * @return singleton instance of ConfigManager
+ */
+ public static ConfigManager getInstance() {
+ return instance;
+ }
+
+ // private constructor to allow only creation of a singleton
+ private ConfigManager() {}
+
+ /**
+ * Reads the config.xml file that defines all config items
+ */
+ public void configureFromXML(Tag tag) throws ConfigurationException {
+
+ // find sections
+ List<Tag> sectionTags = tag.getChildren(SECTION_TAG);
+ if (sectionTags != null) {
+ for (Tag sectionTag:sectionTags) {
+ // find name attribute
+ String sectionName = sectionTag.getAttributeAsString("name");
+ if (!Util.hasValue(sectionName)) continue;
+
+ // find items
+ List<Tag> itemTags = sectionTag.getChildren(ITEM_TAG);
+ if (itemTags == null || itemTags.size() == 0) continue;
+ List<ConfigItem> sectionItems = new ArrayList<ConfigItem>();
+ for (Tag itemTag:itemTags) {
+ sectionItems.add(new ConfigItem(itemTag));
+ }
+ configSections.put(sectionName, sectionItems);
+ }
+ }
+
+ }
+
+
+ public void finishConfiguration(GameManagerI parent)
+ throws ConfigurationException {
+ // do nothing
+ }
+
+ private void init() {
+
+ // define profiles
+ ConfigProfile.readPredefined();
+ ConfigProfile.readUser();
+
+ // load root and default profile
+ ConfigProfile.loadRoot();
+ changeProfile(ConfigProfile.getDefault());
+
+ initVersion();
+ }
+
+ private void initTest() {
+ ConfigProfile.loadRoot();
+ activeProfile = ConfigProfile.loadTest();
+ initVersion();
+ }
+
+ private void initVersion() {
+ // TODO: Check if this is the right place for this
+ /* Load version number and develop flag */
+ Properties versionNumber = new Properties();
+ ConfigProfile.loadProperties(versionNumber, "version.number", true);
+
+ String version = versionNumber.getProperty("version");
+ if (Util.hasValue("version")) {
+ Game.setVersion(version);
+ }
+
+ String develop = versionNumber.getProperty("develop");
+ if (Util.hasValue(develop)) {
+ Game.setDevelop(develop != "");
+ }
+ }
+
+ String getValue(String key, String defaultValue) {
+
+ // get value from active profile (this escalates)
+ String value = activeProfile.getProperty(key);
+ if (Util.hasValue(value)) {
+ return value.trim();
+ } else {
+ return defaultValue;
+ }
+ }
+
+ public String getActiveProfile() {
+ return activeProfile.getName();
+ }
+
+ public boolean IsActiveUserProfile() {
+ return activeProfile.getType() == ConfigProfile.Type.USER;
+ }
+
+ public Set<String> getProfiles() {
+ return ConfigProfile.getListofProfiles();
+ }
+
+ public Map<String, List<ConfigItem>> getConfigSections() {
+ return configSections;
+ }
+
+ public int getMaxElementsInPanels() {
+ int maxElements = 0;
+ for (List<ConfigItem> panel:configSections.values()) {
+ maxElements = Math.max(maxElements, panel.size());
+ }
+ log.debug("Configuration sections with maximum elements of " + maxElements);
+ return maxElements;
+ }
+
+ private void changeProfile(ConfigProfile profile) {
+ // check if profiles have been loaded
+ if (!profile.isLoaded()) {
+ profile.load();
+ }
+ activeProfile = profile;
+
+ // define configItems
+ for (List<ConfigItem> items:configSections.values()) {
+ for (ConfigItem item:items) {
+ item.setCurrentValue(getValue(item.name, null));
+ }
+ }
+ }
+
+ public void changeProfile(String profileName) {
+ changeProfile(ConfigProfile.getProfile(profileName));
+ }
+
+ /**
+ * updates the user profile according to the changes in configItems
+ */
+ public boolean saveProfile(boolean applyInitMethods) {
+ for (List<ConfigItem> items:configSections.values()) {
+ for (ConfigItem item:items) {
+ // if item has changed ==> change profile and call init Method
+ if (item.hasChanged()) {
+ activeProfile.setProperty(item.name, item.getNewValue());
+ log.debug("User properties for = " + item.name + " set to value = " + item.getCurrentValue());
+ item.callInitMethod(applyInitMethods);
+ item.resetValue();
+ }
+ }
+ }
+ return activeProfile.store();
+ }
+
+ public boolean saveNewProfile(String name, boolean applyInitMethods) {
+ activeProfile = activeProfile.deriveUserProfile(name);
+ return saveProfile(applyInitMethods);
+ }
+
+
+
+}
diff --git a/rails/common/ConfigProfile.java b/rails/common/ConfigProfile.java
new file mode 100644
index 0000000..3ad6f20
--- /dev/null
+++ b/rails/common/ConfigProfile.java
@@ -0,0 +1,303 @@
+package rails.common;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.FilenameFilter;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Properties;
+import java.util.Set;
+
+import org.apache.commons.io.FilenameUtils;
+import org.apache.commons.io.IOCase;
+import org.apache.commons.io.filefilter.SuffixFileFilter;
+import org.apache.log4j.Logger;
+
+import rails.util.SystemOS;
+import rails.util.Util;
+
+/**
+ * A profile storing configuration settings
+ */
+
+public final class ConfigProfile {
+
+ protected static Logger log =
+ Logger.getLogger(ConfigProfile.class.getPackage().getName());
+
+ // available profile types
+ public enum Type {SYSTEM, PREDEFINED, USER};
+
+ // Filename extension of profiles
+ public static final String PROFILE_EXTENSION = ".rails_profile";
+ private static final String PREDEFINED_EXTENSION = ".predefined";
+
+ // Location of predefined profiles
+ private static final String PREDEFINED_FOLDER = "data/profiles/";
+
+ // predefined default profiles
+ private static final String ROOT_PROFILE = "root";
+ private static final String TEST_PROFILE = "test";
+ private static final String STANDARD_PROFILE = "pbem";
+
+ // file that stores the list of predefined profiles
+ private static final String LIST_OF_PROFILES = "LIST_OF_PROFILES";
+
+ // property key of predefined profile in user profile
+ private static final String PARENT_KEY = "profile.parent";
+ private static final String FINAL_KEY = "profile.final";
+
+ // map of all profiles
+ private static final Map<String, ConfigProfile> profiles = new HashMap<String, ConfigProfile>();
+
+ // root profile
+ private static ConfigProfile root;
+
+ // profile type
+ private final Type type;
+
+ // profile name
+ private final String name;
+
+ // profile properties
+ private final Properties properties = new Properties();
+
+ // profile loaded
+ private boolean loaded = false;
+
+ // profile parent
+ private ConfigProfile parent = null;
+
+
+ static void loadRoot() {
+ root = new ConfigProfile(Type.SYSTEM, ROOT_PROFILE);
+ root.load();
+ }
+
+ static ConfigProfile loadTest() {
+ ConfigProfile test = new ConfigProfile(Type.SYSTEM, TEST_PROFILE);
+ test.load();
+ return test;
+ }
+
+ static void readPredefined() {
+ Properties list = new Properties();
+ String filePath = PREDEFINED_FOLDER + LIST_OF_PROFILES;
+ loadProperties(list, filePath, true);
+ for (String name:list.stringPropertyNames()) {
+ new ConfigProfile(Type.PREDEFINED, name);
+ }
+ }
+
+ static void readUser() {
+ File userFolder = SystemOS.get().getConfigurationFolder(false);
+ if (userFolder == null) return;
+ FilenameFilter filter = new SuffixFileFilter(PROFILE_EXTENSION, IOCase.SYSTEM);
+ for (String fileName:userFolder.list(filter)) {
+ new ConfigProfile(Type.USER, FilenameUtils.getBaseName(fileName));
+ }
+ }
+
+ static ConfigProfile getDefault() {
+ return profiles.get(STANDARD_PROFILE);
+ }
+
+ static ConfigProfile getProfile(String name) {
+ return profiles.get(name);
+ }
+
+ static Set<String> getListofProfiles() {
+ return profiles.keySet();
+ }
+
+ private ConfigProfile(Type type, String name) {
+ this.type = type;
+ this.name = name;
+ if (type != Type.SYSTEM) {
+ profiles.put(name, this);
+ }
+ }
+
+ public Type getType() {
+ return type;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ boolean isLoaded() {
+ return loaded;
+ }
+
+ boolean isFinal() {
+ if (!loaded) return true;
+ if (Util.hasValue(properties.getProperty(FINAL_KEY))) {
+ return Util.parseBoolean(properties.getProperty(FINAL_KEY));
+ }
+ return false;
+ }
+
+ ConfigProfile setParent(ConfigProfile parent) {
+ this.parent = parent;
+ properties.setProperty(PARENT_KEY, parent.getName());
+ return this;
+ }
+
+ private ConfigProfile setParent(String name) {
+ return setParent(profiles.get(name));
+ }
+
+ ConfigProfile getParent() {
+ return parent;
+ }
+
+ String getProperty(String key) {
+ if (this == root || properties.containsKey(key)) {
+ return properties.getProperty(key);
+ } else {
+ return parent.getProperty(key);
+ }
+ }
+
+ void setProperty(String key, String value) {
+ if (!parent.getProperty(key).equals(value)) {
+ properties.setProperty(key, value);
+ } else {
+ properties.remove(key);
+ }
+ }
+
+ ConfigProfile deriveUserProfile(String name) {
+ ConfigProfile newProfile = new ConfigProfile(Type.USER, name);
+
+ ConfigProfile reference;
+ if (isFinal()) {
+ // set reference for final to the own parent
+ reference = parent;
+ } else {
+ // otherwise to this
+ reference = this;
+ }
+ newProfile.setParent(reference);
+
+ // copy properties
+ for (String key:properties.stringPropertyNames()){
+ if (!key.equals(PARENT_KEY) && !key.equals(FINAL_KEY)) {
+ newProfile.setProperty(key, properties.getProperty(key));
+ }
+ }
+
+ return newProfile;
+ }
+
+ boolean load() {
+ // loaded is set independent of success
+ loaded = true;
+ // ... the same for clearing the current selection
+ properties.clear();
+
+ // loading
+ boolean result;
+ if (type == Type.USER) {
+ result = loadUser();
+ } else {
+ result = loadResource();
+ }
+
+ // post-load processing
+ // set parent according to properties or root
+ if (Util.hasValue(properties.getProperty(PARENT_KEY))) {
+ setParent(properties.getProperty(PARENT_KEY));
+ }
+ if (parent == null) {
+ setParent(root);
+ }
+
+ // set save directory to the working directory for predefined values
+ // TODO: This is a hack as workaround to be replaced in the future
+ if (type == Type.PREDEFINED && !Util.hasValue(properties.getProperty("save.directory"))) {
+ properties.put("save.directory", System.getProperty("user.dir"));
+ }
+
+ // check if parent has been loaded, otherwise load parent
+ if (!parent.isLoaded()) {
+ result = result && parent.load();
+ }
+
+ return result;
+ }
+
+ private boolean loadUser() {
+ File folder = SystemOS.get().getConfigurationFolder(false);
+ if (folder == null) {
+ return false;
+ } else {
+ File profile = new File(folder, name + PROFILE_EXTENSION);
+ return loadProperties(properties, profile.getAbsolutePath(), false);
+ }
+ }
+
+ private boolean loadResource(){
+ String filePath = null;
+ switch(type) {
+ case SYSTEM:
+ filePath = PREDEFINED_FOLDER + name;
+ break;
+ case PREDEFINED:
+ filePath = PREDEFINED_FOLDER + name + PREDEFINED_EXTENSION ;
+ break;
+ }
+ return loadProperties(properties, filePath, true);
+ }
+
+ boolean store() {
+ if (type != Type.USER) return false;
+ loaded = true;
+ File folder = SystemOS.get().getConfigurationFolder(true);
+ if (folder == null) {
+ return false;
+ } else {
+ File profile = new File(folder, name + PROFILE_EXTENSION);
+ return storeProperties(properties, profile);
+ }
+ }
+
+ static boolean loadProperties(Properties properties, String filePath, boolean resource) {
+ try {
+ log.info("Loading properties from file " + filePath);
+ InputStream inFile;
+ if (resource) {
+ inFile = ConfigProfile.class.getClassLoader().getResourceAsStream(filePath);
+ } else {
+ inFile = new FileInputStream(filePath);
+ }
+ properties.load(inFile);
+ } catch (Exception e) {
+ log.error(e + " whilst loading properties file "
+ + filePath, e);
+ return false;
+ }
+ return true;
+ }
+
+ static boolean storeProperties(Properties properties, File file) {
+ boolean result = true;
+ try {
+ properties.store(new FileOutputStream(file), "Automatically generated, do not edit");
+ log.info("Storing properties to file " + file.getAbsolutePath());
+ } catch (IOException e) {
+ log.error(e + " whilst storing properties file "
+ + file.getAbsolutePath());
+ result = false;
+ }
+ return result;
+ }
+
+
+}
+
+
diff --git a/rails/common/LocalText.java b/rails/common/LocalText.java
index 88d93f4..6ea92e3 100644
--- a/rails/common/LocalText.java
+++ b/rails/common/LocalText.java
@@ -9,7 +9,6 @@ import java.util.ResourceBundle;
import org.apache.log4j.Logger;
-import rails.common.parser.Config;
import rails.util.Util;
public class LocalText extends ResourceBundle {
diff --git a/rails/common/MoneyFormatter.java b/rails/common/MoneyFormatter.java
index 30326a7..127d5c8 100644
--- a/rails/common/MoneyFormatter.java
+++ b/rails/common/MoneyFormatter.java
@@ -1,7 +1,6 @@
/* $Header: /Users/blentz/rails_rcs/cvs/18xx/rails/util/Format.java,v 1.3 2008/06/04 19:00:39 evos Exp $*/
package rails.common;
-import rails.common.parser.Config;
import rails.util.Util;
public class MoneyFormatter {
diff --git a/rails/common/parser/Config.java b/rails/common/parser/Config.java
deleted file mode 100644
index 296fcb3..0000000
--- a/rails/common/parser/Config.java
+++ /dev/null
@@ -1,606 +0,0 @@
-/* $Header: /Users/blentz/rails_rcs/cvs/18xx/rails/util/Config.java,v 1.13 2010/06/24 21:48:08 stefanfrey Exp $*/
-package rails.common.parser;
-
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileOutputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.LinkedHashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.Properties;
-
-import org.apache.log4j.Logger;
-
-import rails.game.Game;
-import rails.game.GameManager;
-import rails.util.Util;
-
-/**
- * This is a simple utility class with a collection of static functions to load
- * a property object from a property file, to retrieve a particular value from
- * the property file etc.
- *
- * @author Ramiah Bala,
- * @author Erik Vos
- * @author Stefan Frey
- * @version 2.0
- */
-public final class Config {
-
- protected static Logger log;
-
- /** Commandline options */
- private static final String CONFIGFILE_CMDLINE = "configfile";
- private static final String PROFILE_CMDLINE = "profile";
-
- /** XML setup */
- private static final String CONFIG_XML_DIR = "data";
- private static final String CONFIG_XML_FILE = "Properties.xml";
- private static final String CONFIG_TAG = "Properties";
- private static final String SECTION_TAG = "Section";
- private static final String ITEM_TAG = "Property";
-
- /** Log 4j configuration */
- private static final String LOG4J_CONFIG_FILE = "log4j.properties";
-
- /** Rails profile configurations */
- private static String defaultProfilesFile = "data/profiles/default.profiles";
- private static Properties defaultProfiles = new Properties();
- private static String userProfilesFile = "user.profiles";
- private static Properties userProfiles = new Properties();
- private static boolean profilesLoaded = false;
- private static String DEFAULT_PROFILE_SELECTION = "default"; // can be overwritten
- private static final String ROOT_PROFILE_SELECTION = ".root"; // used for the 3-tier structure: ROOT-DEFAULT-USER
- private static final String TEST_PROFILE_SELECTION = ".test"; // used as default profile for integration tests
- private static final String STANDARD_PROFILE_SELECTION = "user";
- private static final String DEFAULTPROFILE_PROPERTY = "default.profile";
- private static final String PROFILENAME_PROPERTY = "profile.name";
-
- /** selected profile */
- private static String selectedProfile;
- private static boolean legacyConfigFile;
-
- /** properties storage. */
- private static Properties rootProperties = null;
- private static Properties defaultProperties = null;
- private static Properties userProperties = new Properties();
- private static boolean propertiesLoaded = false;
-
- /** Map that holds the panel, which contains config items */
- private static Map<String, List<ConfigItem>> configSections = null;
-
- /**
- * Hidden constructor, the class is never instantiated, everything is static
- */
- private Config() {}
-
- /**
- * Reads the config.xml file that defines all config items
- */
- public static void readConfigSetupXML() {
- List<String> directories = new ArrayList<String>();
- directories.add(CONFIG_XML_DIR);
- try {
- // Find the config tag inside the the config xml file
- Tag configTag =
- Tag.findTopTagInFile(CONFIG_XML_FILE, directories, CONFIG_TAG);
- log.debug("Opened config xml, filename = " + CONFIG_XML_FILE);
-
- // define sections
- configSections = new LinkedHashMap<String, List<ConfigItem>>();
-
- // find sections
- List<Tag> sectionTags = configTag.getChildren(SECTION_TAG);
- if (sectionTags != null) {
- for (Tag sectionTag:sectionTags) {
- // find name attribute
- String sectionName = sectionTag.getAttributeAsString("name");
- if (!Util.hasValue(sectionName)) continue;
-
- // find items
- List<Tag> itemTags = sectionTag.getChildren(ITEM_TAG);
- if (itemTags == null || itemTags.size() == 0) continue;
- List<ConfigItem> sectionItems = new ArrayList<ConfigItem>();
- for (Tag itemTag:itemTags) {
- sectionItems.add(new ConfigItem(itemTag));
- }
- configSections.put(sectionName, sectionItems);
- }
- }
-
- } catch (ConfigurationException e) {
- log.error("Configuration error in setup of " + CONFIG_XML_FILE + ", exception = " + e);
- }
- }
-
- public static Map<String, List<ConfigItem>> getConfigSections() {
- if (configSections == null) {
- readConfigSetupXML();
- }
- log.debug("Configuration setup = " + configSections);
- return configSections;
- }
-
- public static int getMaxElementsInPanels() {
- int maxElements = 0;
- for (List<ConfigItem> panel:configSections.values()) {
- maxElements = Math.max(maxElements, panel.size());
- }
- log.debug("Configuration sections with maximum elements of " + maxElements);
- return maxElements;
- }
-
- /**
- * updates the profile according to the changes in configitems
- */
- public static void updateProfile(boolean applyInitMethods) {
- for (List<ConfigItem> items:configSections.values()) {
- for (ConfigItem item:items) {
- if (!item.hasNewValue()) continue;
- if (item.getNewValue().equals(defaultProperties.get(item.name))) {
- userProperties.remove(item.name);
- continue;
- }
- userProperties.setProperty(item.name, item.getNewValue());
- item.callInitMethod(applyInitMethods);
- log.debug("Changed property name = " + item.name + " to value = " + item.getNewValue());
- item.setNewValue(null);
- }
- }
- }
-
- /**
- * reverts all changes in configitems
- */
- public static void revertProfile() {
- for (List<ConfigItem> items:configSections.values()) {
- for (ConfigItem item:items) {
- item.setNewValue(null);
- }
- }
- }
-
- /**
- * First tries to return {key}.{gameName}, if undefined returns {key}
- */
- public static String getGameSpecific(String key) {
- return Config.getSpecific(key, GameManager.getInstance().getGameName());
- }
-
- /**
- * First tries to return {key}.{appendix}, if undefined returns {key}
- */
- public static String getSpecific(String key, String appendix) {
- String value = Config.get(key + "." + appendix);
- if (value == "") {
- value = Config.get(key);
- }
- return value;
- }
-
- public static String get(String key) {
- return get(key, "");
- }
-
- public static String get(String key, String defaultValue) {
- if (defaultProperties.isEmpty() || !propertiesLoaded) {
- initialLoad();
- }
- if (userProperties.containsKey(key)) return userProperties.getProperty(key).trim();
- if (defaultProperties.containsKey(key)) return defaultProperties.getProperty(key).trim();
- if (rootProperties.containsKey(key)) return rootProperties.getProperty(key).trim();
-
- return defaultValue;
- }
-
-
- /**
- * save active Profile
- */
- public static boolean saveActiveProfile() {
- String filepath = userProfiles.getProperty(selectedProfile);
- if (Util.hasValue(filepath)) {
- return storePropertyFile(userProperties, filepath);
- } else {
- return false;
- }
- }
-
- /**
- * change active Profile
- */
- public static boolean changeActiveProfile(String profileName) {
- readConfigSetupXML();
- loadProfile(profileName);
- selectedProfile = profileName;
- return true;
- }
-
- /**
- * create new profile
- */
- public static boolean createUserProfile(String profileName, String defaultProfile) {
- userProperties = new Properties();
- defaultProperties = new Properties();
-
- // add to list of user profiles
- userProfiles.setProperty(profileName, "");
-
- // define and load default profile
- String defaultConfigFile = defaultProfiles.getProperty(defaultProfile);
- userProperties.setProperty(PROFILENAME_PROPERTY, profileName);
- userProperties.setProperty(DEFAULTPROFILE_PROPERTY, defaultProfile);
- loadPropertyFile(defaultProperties, defaultConfigFile, true);
- setSaveDirDefaults();
-
- selectedProfile = profileName;
- return true;
- }
-
-
- private static Map<String, String> convertProperties(Properties properties, boolean visibleOnly) {
- Map<String, String> converted = new HashMap<String, String>();
- for (Object key:properties.keySet()) {
- if (visibleOnly && ((String)key).substring(0,1).equals(".")) continue;
- converted.put((String) key, (String) properties.get(key));
- }
- return converted;
- }
-
- /**
- * get all default profiles
- */
- public static List<String> getDefaultProfiles(boolean visibleOnly) {
- List<String> profiles = new ArrayList<String>(convertProperties(defaultProfiles, visibleOnly).keySet());
- Collections.sort(profiles);
- return profiles;
- }
-
- public static String getDefaultProfileSelection() {
- return DEFAULT_PROFILE_SELECTION;
- }
-
- /**
- * get all user profiles
- */
- public static List<String> getUserProfiles() {
- List<String> profiles = new ArrayList<String>(convertProperties(userProfiles, true).keySet());
- Collections.sort(profiles);
- return profiles;
- }
-
- /**
- * get all (visible default + user) profiles
- */
- public static List<String> getAllProfiles() {
- List<String> profiles = getDefaultProfiles(true);
- profiles.addAll(getUserProfiles());
- return profiles;
- }
-
- /**
- * checks if profile is default profile
- */
- public static boolean isDefaultProfile(String profileName) {
- return !(defaultProfiles.get(profileName) == null);
- }
-
- /**
- * returns name of (active) default profile
- */
- public static String getDefaultProfileName() {
- return userProperties.getProperty(DEFAULTPROFILE_PROPERTY);
- }
-
- /**
- * returns name of active profile
- */
- public static String getActiveProfileName() {
- return selectedProfile;
- }
-
- /**
- * returns true if legacy configfile is used
- */
- public static boolean isLegacyConfigFile() {
- return legacyConfigFile;
- }
-
- /**
- * sets filename for an active profile (and store list of profiles)
- * @return false if list of profiles cannot be stored
- */
- public static boolean setActiveFilepath(String filepath) {
- userProfiles.setProperty(selectedProfile, filepath);
- return storePropertyFile(userProfiles, userProfilesFile);
- }
-
- /**
- * returns filename of active profile, (null if undefined or default profile)
- */
- public static String getActiveFilepath() {
- return userProfiles.getProperty(selectedProfile);
- }
-
- /**
- * @return if user location is defined
- */
- public static boolean isFilePathDefined() {
- return Util.hasValue(userProfiles.getProperty(selectedProfile));
- }
-
-
- /**
- * activates settings used for testing
- */
- public static void setConfigTest() {
- /*
- * Set the system property that tells log4j to use this file. (Note:
- * this MUST be done before updating Config)
- */
- String log4jSelection = System.getProperty("log4j.configuration");
- if (!Util.hasValue(log4jSelection)) {
- log4jSelection = LOG4J_CONFIG_FILE;
- }
- System.setProperty("log4j.configuration", log4jSelection);
- System.out.println("log4j.configuration = " + log4jSelection);
-
- // delayed setting of logger
- log = Logger.getLogger(Config.class.getPackage().getName());
-
- // define settings for testing
- legacyConfigFile = false;
- DEFAULT_PROFILE_SELECTION = TEST_PROFILE_SELECTION;
- selectedProfile = null;
-
- initialLoad();
- }
-
-
- /**
- * activates configuration settings based on default settings
- */
- public static void setConfigSelection() {
- /*
- * Set the system property that tells log4j to use this file. (Note:
- * this MUST be done before updating Config)
- */
- String log4jSelection = System.getProperty("log4j.configuration");
- if (!Util.hasValue(log4jSelection)) {
- log4jSelection = LOG4J_CONFIG_FILE;
- }
- System.setProperty("log4j.configuration", log4jSelection);
- System.out.println("log4j.configuration = " + log4jSelection);
-
- // delayed setting of logger
- log = Logger.getLogger(Config.class.getPackage().getName());
-
- /*
- * Check if the profile has been set from the command line
- * to do this is adding an option to the java command: -Dprofile=<profile-name>
- */
- String configSelection = System.getProperty(PROFILE_CMDLINE);
- System.out.println("Cmdline profile selection = " + configSelection);
-
- legacyConfigFile = false;
- if (configSelection == null) {
- /*
- * Check if the property file has been set on the command line. The way
- * to do this is adding an option to the java command: -Dconfigfile=<property-filename>
- *
- * This is for legacy reasons only
- */
- configSelection = System.getProperty(CONFIGFILE_CMDLINE);
-
- if (Util.hasValue(configSelection)) {
- System.out.println("Cmdline configfile selection (legacy!) = " + configSelection);
- ...
[truncated message content] |