|
From: <ste...@us...> - 2010-07-29 23:33:31
|
Revision: 1361
http://rails.svn.sourceforge.net/rails/?rev=1361&view=rev
Author: stefanfrey
Date: 2010-07-29 23:33:24 +0000 (Thu, 29 Jul 2010)
Log Message:
-----------
Update configuration mechanism
Modified Paths:
--------------
trunk/18xx/LocalisedText.properties
trunk/18xx/data/Properties.xml
trunk/18xx/data/profiles/default.profile
trunk/18xx/rails/ui/swing/ConfigWindow.java
trunk/18xx/rails/ui/swing/hexmap/HexMap.java
trunk/18xx/rails/util/Config.java
trunk/18xx/rails/util/ConfigItem.java
trunk/18xx/rails/util/LocalText.java
trunk/18xx/rails/util/Util.java
Modified: trunk/18xx/LocalisedText.properties
===================================================================
--- trunk/18xx/LocalisedText.properties 2010-07-25 22:45:34 UTC (rev 1360)
+++ trunk/18xx/LocalisedText.properties 2010-07-29 23:33:24 UTC (rev 1361)
@@ -7,6 +7,7 @@
ALL=All
ALL_PASSED=All players have passed.
ALSO_GETS={0} also gets {1}
+APPLY=Apply
AT_PRICE=at {0}
AcceptingConfigFailure=Temporarily accepting configuration failure
AcquiresBonus={0} acquires a {1} +{2} bonus on hex(es) {3}
Modified: trunk/18xx/data/Properties.xml
===================================================================
--- trunk/18xx/data/Properties.xml 2010-07-25 22:45:34 UTC (rev 1360)
+++ trunk/18xx/data/Properties.xml 2010-07-29 23:33:24 UTC (rev 1361)
@@ -1,17 +1,51 @@
<?xml version="1.0" encoding="utf-8"?>
-<!-- Supported properties
-Defines rails configuration options, but do not stores the actual values
- -->
+<!-- Supported properties Defines rails configuration options, but do not
+ stores the actual values -->
<Properties>
<Panel name="General">
- <Property name="locale" type="LIST" values="en_US,te_ST" toolTip="Language Selection"
- initClass="rails.util.LocalText" initMethod="setLocale" initParameter="yes"/>
+ <Property name="locale" type="LIST" values="en_US,te_ST"
+ initClass="rails.util.LocalText" initMethod="setLocale"
+ initParameter="yes" />
+ <Property name="default_game" type="STRING" />
+ <Property name="default_players" type="STRING" />
+ <Property name="local.player.name" type="STRING" />
+
</Panel>
<Panel name="Save">
- <Property name="save.directory" type="DIRECTORY" toolTip="Default save file directory" />
- <Property name="save.filename.date_time_pattern" type="STRING" toolTip="Datetime pattern for filename" />
+ <Property name="save.directory" type="DIRECTORY" />
+ <Property name="save.filename.date_time_pattern" type="STRING" />
+ <Property name="save.filename.suffix" type="STRING" />
+ <Property name="save.filename.extension" type="STRING" />
+ <Property name="save.recovery.active" type="BOOLEAN" />
+ <Property name="save.recovery.filepath" type="FILE" />
</Panel>
- <Panel name="Colors">
- <Property name="route.colour.1" type="COLOR" toolTip="Route color of first train" />
+ <Panel name="UI">
+ <Property name="report.window.open" type="BOOLEAN" />
+ <Property name="report.window.editable" type="BOOLEAN" />
+ <Property name="stockchart.window.open" type="BOOLEAN" />
+ <Property name="font.ui.scale" type="PERCENT" />
+ <Property name="font.ui.name" type="FONT" />
+ <Property name="font.ui.style" type="LIST" values="plain,bold" />
</Panel>
+ <Panel name="Map">
+ <Property name="map.autoscroll" type="BOOLEAN" />
+ <Property name="map.zoomstep" type="INTEGER" />
+ </Panel>
+ <Panel name="Format">
+ <Property name="money_format" type="STRING" />
+ <Property name="or.number_format" type="LIST" values="simple,composite" />
+ <Property name="route.colour.1" type="COLOR"
+ initClass="rails.ui.swing.hexmap.HexMap" initMethod="setRouteColours" />
+ <Property name="route.colour.2" type="COLOR"
+ initClass="rails.ui.swing.hexmap.HexMap" initMethod="setRouteColours" />
+ <Property name="route.colour.3" type="COLOR"
+ initClass="rails.ui.swing.hexmap.HexMap" initMethod="setRouteColours" />
+ <Property name="route.colour.4" type="COLOR"
+ initClass="rails.ui.swing.hexmap.HexMap" initMethod="setRouteColours" />
+ </Panel>
+ <Panel name="Log">
+ <Property name="report.directory" type="DIRECTORY" />
+ <Property name="report.filename.date_time_pattern" type="STRING" />
+ <Property name="report.filename.extension" type="STRING" />
+ </Panel>
</Properties>
\ No newline at end of file
Modified: trunk/18xx/data/profiles/default.profile
===================================================================
--- trunk/18xx/data/profiles/default.profile 2010-07-25 22:45:34 UTC (rev 1360)
+++ trunk/18xx/data/profiles/default.profile 2010-07-29 23:33:24 UTC (rev 1361)
@@ -1,7 +1,39 @@
+### 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 UI
+report.window.open=yes
+report.window.editable=no
+stockchart.window.open=yes
+font.ui.scale=1
+font.ui.name=
+font.ui.style=bold
+
+### Panel Map
+map.autoscroll=yes
+map.zoomstep=10
+
+### Panel Format
+money_format=
+or.number_format=composite
+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
Modified: trunk/18xx/rails/ui/swing/ConfigWindow.java
===================================================================
--- trunk/18xx/rails/ui/swing/ConfigWindow.java 2010-07-25 22:45:34 UTC (rev 1360)
+++ trunk/18xx/rails/ui/swing/ConfigWindow.java 2010-07-29 23:33:24 UTC (rev 1361)
@@ -2,6 +2,8 @@
import java.awt.Color;
import java.awt.EventQueue;
+import java.awt.Font;
+import java.awt.GraphicsEnvironment;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
@@ -24,14 +26,18 @@
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;
+import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
+import javax.swing.JSpinner;
import javax.swing.JTabbedPane;
+import javax.swing.SpinnerNumberModel;
import javax.swing.SwingConstants;
+import javax.swing.UIManager;
import javax.swing.WindowConstants;
import javax.swing.border.Border;
@@ -73,7 +79,7 @@
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
- cancelConfig();
+ closeConfig();
}
});
}
@@ -136,6 +142,8 @@
}
+
+
private void setupConfigPane() {
configPane.removeAll();
@@ -144,7 +152,8 @@
configPane.setBorder(titled);
Map<String, List<ConfigItem>> configPanels = Config.getConfigPanels();
-
+ int maxElements = Config.getMaxElementsInPanels();
+
for (String panelName:configPanels.keySet()) {
JPanel newPanel = new JPanel();
newPanel.setLayout(new GridBagLayout());
@@ -153,14 +162,19 @@
gbc.gridheight = 1;
gbc.weightx = 0.8;
gbc.weighty = 0.8;
- gbc.insets = new Insets(10,10,10,10);
+ gbc.insets = new Insets(5,5,5,5);
gbc.anchor = GridBagConstraints.WEST;
- int y = 0;
for (ConfigItem item:configPanels.get(panelName)) {
gbc.gridx = 0;
- gbc.gridy = y++;
+ gbc.gridy++;
defineElement(newPanel, item, gbc);
}
+ // fill up to maxElements
+ gbc.gridx = 0;
+ for (gbc.gridy++; gbc.gridy < maxElements; gbc.gridy++) {
+ JLabel emptyLabel = new JLabel("");
+ newPanel.add(emptyLabel, gbc);
+ }
configPane.addTab(LocalText.getText("Config.panel." + panelName), newPanel);
}
}
@@ -171,23 +185,94 @@
gbc.gridx ++;
}
+ private void addEmptyLabel(JComponent container, GridBagConstraints gbc) {
+ JLabel label = new JLabel("");
+ addToGridBag(container, label, gbc );
+ }
+
private void defineElement(JPanel panel, final ConfigItem item, GridBagConstraints gbc) {
// standard components
- final String configValue = item.getCurrentValue();
- final String toolTip = item.toolTip;
+ String configValue = item.getCurrentValue();
+// final String toolTip = item.toolTip;
// item label
JLabel label = new JLabel(LocalText.getText("Config." + item.name));
- label.setToolTipText(toolTip);
+// label.setToolTipText(toolTip);
gbc.fill = GridBagConstraints.NONE;
addToGridBag(panel, label, gbc);
switch (item.type) {
+ case BOOLEAN:
+ final JCheckBox checkBox = new JCheckBox();
+ boolean selected = Util.parseBoolean(configValue);
+ checkBox.setSelected(selected);
+ checkBox.addFocusListener(new FocusListener() {
+ public void focusGained(FocusEvent arg0) {
+ // do nothing
+ }
+ public void focusLost(FocusEvent arg0) {
+ if (checkBox.isSelected()) {
+ item.setNewValue("yes");
+ } else {
+ item.setNewValue("no");
+ }
+ }
+ }
+ );
+ gbc.fill = GridBagConstraints.HORIZONTAL;
+ addToGridBag(panel, checkBox, gbc);
+ break;
+ case PERCENT: // percent uses a spinner with 5 changes
+ case INTEGER:
+ int spinnerStepSize;
+ final int spinnerMultiple;
+ if (item.type == ConfigItem.ConfigType.PERCENT) {
+ spinnerStepSize = 5;
+ spinnerMultiple = 100;
+ } else {
+ spinnerStepSize = 1;
+ spinnerMultiple = 1;
+ }
+ int spinnerValue;
+ try {
+ spinnerValue = (int)Math.round(Double.parseDouble(configValue) * spinnerMultiple);
+ } catch (NumberFormatException e) {
+ spinnerValue = 0;
+ }
+ final JSpinner spinner = new JSpinner(new SpinnerNumberModel
+ (spinnerValue, Integer.MIN_VALUE, Integer.MAX_VALUE, spinnerStepSize));
+ ((JSpinner.DefaultEditor)spinner.getEditor()).getTextField().
+ addFocusListener(new FocusListener() {
+ public void focusGained(FocusEvent arg0) {
+ // do nothing
+ }
+ public void focusLost(FocusEvent arg0) {
+ int value = (Integer)spinner.getValue();
+ Double adjValue = (double)value / spinnerMultiple;
+ item.setNewValue(adjValue.toString());
+ }
+ }
+ );
+ gbc.fill = GridBagConstraints.HORIZONTAL;
+ addToGridBag(panel, spinner, gbc);
+ addEmptyLabel(panel, gbc);
+ break;
+ case FONT: // fonts are a special list
+ if (!Util.hasValue(configValue)) {
+ configValue = ((Font)UIManager.getDefaults().get("Label.font")).getFamily();
+ }
case LIST:
- final JComboBox comboBox = new JComboBox(item.allowedValues.toArray());
+ String[] allowedValues;
+ if (item.type == ConfigItem.ConfigType.FONT) {
+ allowedValues = GraphicsEnvironment.getLocalGraphicsEnvironment().
+ getAvailableFontFamilyNames();
+ } else {
+ allowedValues = (String[])item.allowedValues.toArray();
+ }
+ final JComboBox comboBox = new JComboBox(allowedValues);
comboBox.setSelectedItem(configValue);
- comboBox.setToolTipText(toolTip);
+// comboBox.setToolTipText(toolTip));
comboBox.addFocusListener(new FocusListener() {
public void focusGained(FocusEvent arg0) {
// do nothing
@@ -199,11 +284,13 @@
);
gbc.fill = GridBagConstraints.HORIZONTAL;
addToGridBag(panel, comboBox, gbc);
+ addEmptyLabel(panel, gbc);
break;
case DIRECTORY:
+ case FILE:
final JLabel dirLabel = new JLabel(configValue);
dirLabel.setHorizontalAlignment(SwingConstants.CENTER);
- dirLabel.setToolTipText(toolTip);
+// dirLabel.setToolTipText(toolTip);
gbc.fill = GridBagConstraints.HORIZONTAL;
addToGridBag(panel, dirLabel, gbc);
JButton dirButton = new JButton("Choose...");
@@ -211,7 +298,11 @@
new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFileChooser fc = new JFileChooser(dirLabel.getText());
- fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
+ if (item.type == ConfigItem.ConfigType.DIRECTORY) {
+ fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
+ } else {
+ fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
+ }
int state = fc.showOpenDialog(ConfigWindow.this);
if ( state == JFileChooser.APPROVE_OPTION ){
File file = fc.getSelectedFile();
@@ -240,7 +331,7 @@
} else {
colorLabel.setForeground(Color.BLACK);
}
- colorLabel.setToolTipText(toolTip);
+// colorLabel.setToolTipText(toolTip);
gbc.fill = GridBagConstraints.HORIZONTAL;
addToGridBag(panel, colorLabel, gbc);
JButton colorButton = new JButton("Choose...");
@@ -249,7 +340,7 @@
public void actionPerformed(ActionEvent e) {
Color selectedColor=JColorChooser.showDialog(ConfigWindow.this, "", colorLabel.getBackground());
if (selectedColor == null) return;
- String newValue = Integer.toHexString(selectedColor.getRGB()).substring(3);
+ String newValue = Integer.toHexString(selectedColor.getRGB()).substring(2);
colorLabel.setText(newValue);
item.setNewValue(newValue);
colorLabel.setBackground(selectedColor);
@@ -279,6 +370,7 @@
);
gbc.fill = GridBagConstraints.HORIZONTAL;
addToGridBag(panel, textField, gbc);
+ addEmptyLabel(panel, gbc);
break;
}
}
@@ -310,12 +402,22 @@
buttonPanel.add(saveButton);
}
+ JButton applyButton = new JButton(LocalText.getText("APPLY"));
+ applyButton.addActionListener(
+ new ActionListener() {
+ public void actionPerformed(ActionEvent arg0) {
+ ConfigWindow.this.applyConfig();
+ }
+ }
+ );
+ buttonPanel.add(applyButton);
+
JButton cancelButton = new JButton(LocalText.getText("CANCEL"));
cancelButton.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
- ConfigWindow.this.cancelConfig();
+ ConfigWindow.this.closeConfig();
}
}
);
@@ -420,8 +522,13 @@
});
}
}
-
- private void cancelConfig() {
+
+ private void applyConfig() {
+ Config.updateProfile(); // transfer the configitem to the active profile
+ }
+
+ private void closeConfig() {
+ Config.revertProfile();
StatusWindow.uncheckMenuItemBox(StatusWindow.CONFIG_CMD);
this.setVisible(false);
}
Modified: trunk/18xx/rails/ui/swing/hexmap/HexMap.java
===================================================================
--- trunk/18xx/rails/ui/swing/hexmap/HexMap.java 2010-07-25 22:45:34 UTC (rev 1360)
+++ trunk/18xx/rails/ui/swing/hexmap/HexMap.java 2010-07-29 23:33:24 UTC (rev 1361)
@@ -69,7 +69,6 @@
protected List<GeneralPath> trainPaths;
private static Color colour1, colour2, colour3, colour4;
- protected Color[] trainColors = new Color[4];
protected int strokeWidth = 5;
protected int strokeCap = BasicStroke.CAP_ROUND;
protected int strokeJoin = BasicStroke.JOIN_BEVEL;
@@ -85,7 +84,7 @@
protected double coordinateXMargin;
protected double coordinateYMargin;
- static {
+ public static void setRouteColours () {
try {
colour1 = Util.parseColour(Config.get("route.colour.1", null));
colour2 = Util.parseColour(Config.get("route.colour.2", null));
@@ -99,7 +98,7 @@
if (colour4 == null) colour4 = Color.GRAY;
}
}
-
+
public void init(ORUIManager orUIManager, MapManager mapManager) {
this.orUIManager = orUIManager;
@@ -120,14 +119,15 @@
setupHexes();
initializeSettings();
+
+ setRouteColours();
}
/**
* defines settings from the config files
*/
private void initializeSettings() {
- trainColors = new Color[]{colour1, colour2, colour3, colour4};
-
+
// define zoomStep from config
String zoomStepSetting = Config.getGameSpecific("map.zoomstep");
if (Util.hasValue(zoomStepSetting)) {
@@ -143,6 +143,9 @@
}
}
+
+
+
protected void setupHexesGUI() {
hexes = new ArrayList<GUIHex>();
@@ -296,6 +299,8 @@
Stroke trainStroke =
new BasicStroke((int)(strokeWidth * zoomFactor), strokeCap, strokeJoin);
g2.setStroke(trainStroke);
+
+ Color[] trainColors = new Color[]{colour1, colour2, colour3, colour4};
int color = 0;
for (GeneralPath path:trainPaths) {
g2.setColor(trainColors[color++ % trainColors.length]);
Modified: trunk/18xx/rails/util/Config.java
===================================================================
--- trunk/18xx/rails/util/Config.java 2010-07-25 22:45:34 UTC (rev 1360)
+++ trunk/18xx/rails/util/Config.java 2010-07-29 23:33:24 UTC (rev 1361)
@@ -107,7 +107,7 @@
}
} catch (ConfigurationException e) {
- log.error("Configuration error in setup of ");
+ log.error("Configuration error in setup of " + CONFIG_XML_FILE + ", exception = " + e);
}
}
@@ -119,6 +119,15 @@
return configPanels;
}
+ public static int getMaxElementsInPanels() {
+ int maxElements = 0;
+ for (List<ConfigItem> panel:configPanels.values()) {
+ maxElements = Math.max(maxElements, panel.size());
+ }
+ log.debug("maxelements" + maxElements);
+ return maxElements;
+ }
+
/**
* updates the profile according to the changes in configitems
*/
@@ -135,6 +144,17 @@
}
/**
+ * reverts all changes in configitems
+ */
+ public static void revertProfile() {
+ for (List<ConfigItem> items:configPanels.values()) {
+ for (ConfigItem item:items) {
+ item.setNewValue(null);
+ }
+ }
+ }
+
+ /**
* First tries to return {key}.{gameName}, if undefined returns {key}
*/
public static String getGameSpecific(String key) {
Modified: trunk/18xx/rails/util/ConfigItem.java
===================================================================
--- trunk/18xx/rails/util/ConfigItem.java 2010-07-25 22:45:34 UTC (rev 1360)
+++ trunk/18xx/rails/util/ConfigItem.java 2010-07-29 23:33:24 UTC (rev 1361)
@@ -1,6 +1,5 @@
package rails.util;
-import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;
@@ -22,7 +21,7 @@
* Defines possible types (Java classes used as types in ConfigItem below
*/
public static enum ConfigType {
- INTEGER, FLOAT, STRING, BOOLEAN, LIST, DIRECTORY, COLOR;
+ BOOLEAN, INTEGER, PERCENT, STRING, LIST, FONT, DIRECTORY, FILE, COLOR;
}
// static attributes
@@ -30,8 +29,6 @@
public final ConfigType type;
public final List<String> allowedValues;
public final String formatMask;
- public final String toolTip;
- public final String helpText;
// method call attributes
private final String initClass;
@@ -62,7 +59,7 @@
try {
this.type = ConfigType.valueOf(type.toUpperCase());
} catch (Exception e) {
- throw new ConfigurationException("Missing or invalid type for configuration item");
+ throw new ConfigurationException("Missing or invalid type for configuration item, exception = " + e);
}
} else {
throw new ConfigurationException("Missing or invalid type for configuration item");
@@ -74,10 +71,6 @@
// optional: formatMask
formatMask = tag.getAttributeAsString("formatMask");
-
- // optional: helpText and toolTip
- toolTip = tag.getAttributeAsString("toolTip");
- helpText = tag.getAttributeAsString("helpText");
// optional: init method attributes
initClass = tag.getAttributeAsString("initClass");
@@ -144,9 +137,6 @@
if (formatMask != null) {
s.append(", formatMask = " + formatMask);
}
- if (helpText != null) {
- s.append(", helpText = " + helpText);
- }
return s.toString();
}
Modified: trunk/18xx/rails/util/LocalText.java
===================================================================
--- trunk/18xx/rails/util/LocalText.java 2010-07-25 22:45:34 UTC (rev 1360)
+++ trunk/18xx/rails/util/LocalText.java 2010-07-29 23:33:24 UTC (rev 1361)
@@ -32,7 +32,6 @@
return getText(key, new Object[] { parameter });
}
- //public static String getText(String key, Object[] parameters) {
public static String getText(String key, Object... parameters) {
String result = "";
Modified: trunk/18xx/rails/util/Util.java
===================================================================
--- trunk/18xx/rails/util/Util.java 2010-07-25 22:45:34 UTC (rev 1360)
+++ trunk/18xx/rails/util/Util.java 2010-07-29 23:33:24 UTC (rev 1361)
@@ -126,6 +126,18 @@
}
/**
+ * Parse a boolean value for Rails
+ * @param value string (allowed values for true: standard Boolean.parseBoolean and yes (after conversion to lowercase)
+ * @return parsed value
+ */
+ public static boolean parseBoolean(String s) {
+ if (s.toLowerCase().equals("yes")) {
+ return true;
+ }
+ return Boolean.parseBoolean(s);
+ }
+
+ /**
* Parse a colour definition string.
* Currently supported formats:
* "RRGGBB" - each character being a hexadecimal digit
@@ -159,6 +171,9 @@
return c;
}
+
+
+
/**
* Is a colour dark? (to check if FG colour needs be reversed)
*/
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|