From: <ste...@us...> - 2010-07-18 08:28:43
|
Revision: 1348 http://rails.svn.sourceforge.net/rails/?rev=1348&view=rev Author: stefanfrey Date: 2010-07-18 08:28:37 +0000 (Sun, 18 Jul 2010) Log Message: ----------- Further update of config mechanism Modified Paths: -------------- trunk/18xx/rails/ui/swing/ConfigWindow.java trunk/18xx/rails/ui/swing/StatusWindow.java trunk/18xx/rails/util/Config.java Modified: trunk/18xx/rails/ui/swing/ConfigWindow.java =================================================================== --- trunk/18xx/rails/ui/swing/ConfigWindow.java 2010-07-17 22:45:27 UTC (rev 1347) +++ trunk/18xx/rails/ui/swing/ConfigWindow.java 2010-07-18 08:28:37 UTC (rev 1348) @@ -1,7 +1,6 @@ package rails.ui.swing; import java.awt.Color; -import java.awt.Component; import java.awt.EventQueue; import java.awt.GridLayout; import java.awt.event.ActionEvent; @@ -18,20 +17,16 @@ import javax.swing.JButton; import javax.swing.JColorChooser; import javax.swing.JComboBox; -import javax.swing.JComponent; import javax.swing.JFileChooser; import javax.swing.filechooser.FileFilter; 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.SpinnerListModel; import javax.swing.SwingConstants; import javax.swing.border.Border; -import javax.swing.event.DocumentEvent; -import javax.swing.event.DocumentListener; import rails.game.ConfigurationException; import rails.util.Config; @@ -41,6 +36,9 @@ class ConfigWindow extends JFrame { private static final long serialVersionUID = 1L; + + private static final String CONFIG_EXTENSION = ".rails_config"; + private static final String CONFIG_DESCRIPTION = "Rails configuration files (.rails_config)"; private JPanel profilePanel; private JTabbedPane configPane; @@ -90,19 +88,23 @@ comboBoxUser.setSelectedItem(Config.getActiveProfileName()); comboBoxUser.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent arg0) { - Config.changeActiveProfile((String)comboBoxUser.getSelectedItem()); - EventQueue.invokeLater(new Runnable() { - public void run() { - init(); - ConfigWindow.this.repaint(); - } - } - ); + changeProfile((String)comboBoxUser.getSelectedItem()); } } ); profilePanel.add(comboBoxUser); + // new button + JButton newButton = new JButton(LocalText.getText("NEW")); + newButton.addActionListener( + new ActionListener() { + public void actionPerformed(ActionEvent arg0) { + newProfile(); + } + } + ); + profilePanel.add(newButton); + } private void setupConfigPane() { @@ -206,6 +208,17 @@ private void setupButtonPanel() { buttonPanel.removeAll(); + // saveas button + JButton saveAsButton = new JButton(LocalText.getText("SAVEAS")); + saveAsButton.addActionListener( + new ActionListener() { + public void actionPerformed(ActionEvent arg0) { + ConfigWindow.this.saveAsConfig(); + } + } + ); + buttonPanel.add(saveAsButton); + // save button if (Config.isFilePathDefined()) { JButton saveButton = new JButton(LocalText.getText("SAVE")); @@ -219,15 +232,6 @@ buttonPanel.add(saveButton); } - JButton saveAsButton = new JButton(LocalText.getText("SAVEAS")); - saveAsButton.addActionListener( - new ActionListener() { - public void actionPerformed(ActionEvent arg0) { - ConfigWindow.this.saveAsConfig(); - } - } - ); - buttonPanel.add(saveAsButton); JButton cancelButton = new JButton(LocalText.getText("CANCEL")); cancelButton.addActionListener( @@ -241,12 +245,47 @@ } + private void newProfile() { + String newProfile = JOptionPane.showInputDialog(ConfigWindow.this, LocalText.getText("CONFIG_NEW_MESSAGE"), + LocalText.getText("CONFIG_NEW_TITLE"), JOptionPane.QUESTION_MESSAGE); + if (Util.hasValue(newProfile)) { + String defaultProfile = (String)JOptionPane.showInputDialog(ConfigWindow.this, LocalText.getText("CONFIG_DEFAULT_MESSAGE"), + LocalText.getText("CONFIG_DEFAULT_TITLE"), JOptionPane.QUESTION_MESSAGE, null, + Config.getDefaultProfiles().toArray(), Config.getDefaultProfileSelection()); + Config.createUserProfile(newProfile, defaultProfile); + } + EventQueue.invokeLater(new Runnable() { + public void run() { + init(); + ConfigWindow.this.repaint(); + } + }); + } + + private void changeProfile(String profileName) { + Config.changeActiveProfile(profileName); + EventQueue.invokeLater(new Runnable() { + public void run() { + init(); + ConfigWindow.this.repaint(); + } + }); + } + private void saveConfig() { Config.saveActiveProfile(); } private void saveAsConfig() { + String directory = Config.get("save.directory"); + String filepath; + if (Util.hasValue(directory)) { + filepath = directory + File.separator + Config.getActiveProfileName() + CONFIG_EXTENSION; + } else { + filepath = Config.getActiveProfileName() + CONFIG_EXTENSION; + } JFileChooser fc = new JFileChooser(); + fc.setSelectedFile(new File(filepath)); fc.setFileFilter( new FileFilter() { public boolean accept( File f ){ @@ -254,12 +293,11 @@ f.getName().toLowerCase().endsWith( ".rails_config" ); } public String getDescription() { - return "Rails Config"; + return CONFIG_DESCRIPTION; } } ); - - int state = fc.showOpenDialog( null ); + int state = fc.showSaveDialog(this); if ( state == JFileChooser.APPROVE_OPTION ) { File file = fc.getSelectedFile(); Modified: trunk/18xx/rails/ui/swing/StatusWindow.java =================================================================== --- trunk/18xx/rails/ui/swing/StatusWindow.java 2010-07-17 22:45:27 UTC (rev 1347) +++ trunk/18xx/rails/ui/swing/StatusWindow.java 2010-07-18 08:28:37 UTC (rev 1348) @@ -176,12 +176,15 @@ menuItem.addActionListener(this); optMenu.add(menuItem); - menuItem = new JCheckBoxMenuItem(LocalText.getText("CONFIG")); - menuItem.setName(CONFIG_CMD); - menuItem.setActionCommand(CONFIG_CMD); - menuItem.setMnemonic(KeyEvent.VK_C); - menuItem.addActionListener(this); - optMenu.add(menuItem); + // new config menu only for non legacy configgfiles + if (!Config.isLegacyConfigFile()) { + menuItem = new JCheckBoxMenuItem(LocalText.getText("CONFIG")); + menuItem.setName(CONFIG_CMD); + menuItem.setActionCommand(CONFIG_CMD); + menuItem.setMnemonic(KeyEvent.VK_C); + menuItem.addActionListener(this); + optMenu.add(menuItem); + } menuBar.add(optMenu); Modified: trunk/18xx/rails/util/Config.java =================================================================== --- trunk/18xx/rails/util/Config.java 2010-07-17 22:45:27 UTC (rev 1347) +++ trunk/18xx/rails/util/Config.java 2010-07-18 08:28:37 UTC (rev 1348) @@ -3,7 +3,6 @@ import java.io.File; import java.io.FileInputStream; -import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; @@ -14,7 +13,6 @@ import java.util.List; import java.util.Map; import java.util.Properties; -import java.util.Set; import org.apache.log4j.Logger; @@ -26,8 +24,8 @@ * a property object from a property file, to retrieve a particular value from * the property file etc. * - * @author Ramiah Bala, rewritten by Erik Vos - * @version 1.0 + * @author Ramiah Bala, rewritten by Erik Vos, rewritten by Stefan Frey + * @version 2.0 */ public final class Config { @@ -48,8 +46,7 @@ /** Log 4j configuration */ private static final String LOG4J_CONFIG_FILE = "log4j.properties"; - - + /** Rails profile configurations */ private static String defaultProfilesFile = "default.profiles"; private static Properties defaultProfiles = new Properties(); @@ -156,13 +153,6 @@ } - /** - * @return if user location is defined - */ - public static boolean isFilePathDefined() { - return Util.hasValue(userProfiles.getProperty(selectedProfile)); - } - private static boolean storePropertyFile(Properties properties, String filepath) { File outFile = new File(filepath); boolean result = true; @@ -196,6 +186,27 @@ 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(DEFAULT_PROFILE_PROPERTY, defaultProfile); + loadPropertyFile(defaultProperties, defaultConfigFile, true); + setSaveDirDefaults(); + + selectedProfile = profileName; + return true; + } + + private static Map<String, String> convertProperties(Properties properties) { Map<String, String> converted = new HashMap<String, String>(); for (Object key:properties.keySet()) { @@ -204,16 +215,18 @@ return converted; } - /** * get all default profiles */ public static List<String> getDefaultProfiles() { List<String> profiles = new ArrayList<String>(convertProperties(defaultProfiles).keySet()); - profiles.remove(DEFAULT_PROFILE_PROPERTY); Collections.sort(profiles); return profiles; } + + public static String getDefaultProfileSelection() { + return DEFAULT_PROFILE_SELECTION; + } /** * get all user profiles @@ -239,6 +252,13 @@ } /** + * returns true if legacy configfile is used + */ + public static boolean isLegacyConfigFile() { + return legacyConfigFile; + } + + /** * sets filename for an active profile (and store list of profiles) */ public static boolean setActiveFilepath(String filepath) { @@ -252,6 +272,14 @@ 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 This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |