[Schmitzm-commits] r2408 - in trunk/schmitzm-core/src/main: java/de/schmitzm/lang java/de/schmitzm/swing resources/de/schmitzm/swing/resource/locales

scm-commit at wald.intevation.org scm-commit at wald.intevation.org
Wed Nov 26 17:31:13 CET 2014


Author: mojays
Date: 2014-11-26 17:31:13 +0100 (Wed, 26 Nov 2014)
New Revision: 2408

Modified:
   trunk/schmitzm-core/src/main/java/de/schmitzm/lang/ApplicationProps.java
   trunk/schmitzm-core/src/main/java/de/schmitzm/swing/SwingUtil.java
   trunk/schmitzm-core/src/main/resources/de/schmitzm/swing/resource/locales/SwingResourceBundle.properties
   trunk/schmitzm-core/src/main/resources/de/schmitzm/swing/resource/locales/SwingResourceBundle_de.properties
Log:
ApplicationProps: methods added to load/store properties from/to individual file; method to clear properties
SwingUtil: new method createPropertiesMenu(..) to create JMenu to import/export ApplicationProps


Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/lang/ApplicationProps.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/lang/ApplicationProps.java	2014-11-24 15:08:13 UTC (rev 2407)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/lang/ApplicationProps.java	2014-11-26 16:31:13 UTC (rev 2408)
@@ -497,16 +497,19 @@
   }
   
   /**
-   * Loads the properties from the file.
-   * @see #getPropertiesFile()
+   * Loads the properties from an individual file.
+   * @param file file to load the properties from (if {@code null} the standard file
+   *             {@link #getPropertiesFile()} is used)
    */
-  public void load() {
+  public void load(File file) {
+    if ( file == null )
+      file = getPropertiesFile();
     /**
      * It is not a problem if the propertiesFile for the can't
      * be loaded
      */
     try {
-        properties.load(new FileInputStream(getPropertiesFile()));
+        properties.load(new FileInputStream(file));
     } catch (FileNotFoundException e) {
     } catch (IOException e) {
         LOGGER.error(e);
@@ -514,6 +517,14 @@
   }
 
   /**
+   * Loads the properties from the file.
+   * @see #getPropertiesFile()
+   */
+  public void load() {
+    load(null);
+  }
+
+  /**
    * Deletes the .properties in the ApplicationPreferences directory and
    * creates a default .properties file
    * 
@@ -559,17 +570,21 @@
 
 
   /**
-   * Save the changes to the .properties file
+   * Save the changes to a .properties file
+   * @param file file to store the properties in (if {@code null} the default
+   *             properties file {@link #getPropertiesFile()} is used)
    */
-  public void store() {
+  public void store(File file) {
+      if ( file == null )
+        file = getPropertiesFile();
       // LOGGER.debug("STORE AS PROPS");
       try {
-          FOS = new FileOutputStream(getPropertiesFile());
+          FOS = new FileOutputStream(file);
           haveToCloseFOS = true;
 
           properties.store(FOS,getPropertiesFileDescription());
       } catch (IOException e) {
-          LOGGER.error("Can't write to " + getPropertiesFile().toString(), e);
+          LOGGER.error("Can't write to " + file.toString(), e);
       } finally {
           if (haveToCloseFOS)
               try {
@@ -581,4 +596,19 @@
               }
       }
   }
+
+  /**
+   * Save the changes to the .properties file
+   * @see #getPropertiesFile()
+   */
+  public void store() {
+    store(null);
+  }
+  
+  /**
+   * Clears all properties.
+   */
+  public void clear() {
+    properties.clear();
+  }
 }

Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/swing/SwingUtil.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/swing/SwingUtil.java	2014-11-24 15:08:13 UTC (rev 2407)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/swing/SwingUtil.java	2014-11-26 16:31:13 UTC (rev 2408)
@@ -95,6 +95,7 @@
 import javax.swing.JComboBox;
 import javax.swing.JComponent;
 import javax.swing.JDialog;
+import javax.swing.JFileChooser;
 import javax.swing.JFormattedTextField;
 import javax.swing.JFrame;
 import javax.swing.JInternalFrame;
@@ -124,6 +125,7 @@
 import javax.swing.UIDefaults;
 import javax.swing.UIManager;
 import javax.swing.event.ChangeListener;
+import javax.swing.filechooser.FileFilter;
 import javax.swing.plaf.FontUIResource;
 import javax.swing.plaf.basic.BasicProgressBarUI;
 import javax.swing.table.DefaultTableCellRenderer;
@@ -146,6 +148,7 @@
 import com.toedter.calendar.JDateChooser;
 
 import de.schmitzm.io.IOUtil;
+import de.schmitzm.lang.ApplicationProps;
 import de.schmitzm.lang.LangUtil;
 import de.schmitzm.lang.ResourceProvider;
 import de.schmitzm.swing.event.SmoothProgressBarUpdater;
@@ -2545,7 +2548,98 @@
 		return logMenu;
 
 	}
+	
+	/**
+	 * Creates a sub menu with options to maintain the application properties (import/export). 
+	 * @param props application properties to create the menu for
+	 * @param parent parent component for file chooser dialog
+	 * @param menuLabel label for the menu (if {@code null} a default label is used)
+     * @param menuTooltip tooltip for the menu (if {@code null} a default tooltip is used)
+     * @param subsequentActionListener {@link ActionListener} called after successful maintainence action
+     *                                 to continue with application specific actions (e.g. update GUI fields
+     *                                 according to updated properties)
+	 * @see ApplicationProps
+	 */
+	public static <E> JMenu createPropertiesMenu(final ApplicationProps<E> props, final Component parent, String menuLabel, String menuTooltip, final ActionListener subsequentActionListener) {
+	  // Create file chooser for import/export actions
+      final JFileChooser PROPS_FILE_CHOOSER = new JFileChooser();
+      FileFilter filter = new FileExtensionFilter( R("Menu.Properties.filechooser.filter.desc"),true,".properties",".prop");
+      PROPS_FILE_CHOOSER.setAcceptAllFileFilterUsed(true);
+      PROPS_FILE_CHOOSER.addChoosableFileFilter(filter);
+      PROPS_FILE_CHOOSER.setFileFilter(filter );
 
+      // Create ActionListener for actions
+      ActionListener actionListener = new ActionListener() {
+        @Override
+        public void actionPerformed(ActionEvent e) {
+          String actionID = e.getActionCommand().trim().toUpperCase();
+          if ( "EXPORT_PROPS".equals(actionID) ) {
+            // set current directory
+            PROPS_FILE_CHOOSER.setCurrentDirectory(props.getApplicationPropertiesDir());
+            int ret = PROPS_FILE_CHOOSER.showSaveDialog(parent);
+            if ( ret != JFileChooser.APPROVE_OPTION || PROPS_FILE_CHOOSER.getSelectedFile() == null )
+              return;
+            File exportFile = PROPS_FILE_CHOOSER.getSelectedFile();
+            // append file extension
+            if ( StringUtils.isBlank(IOUtil.getFileExt(exportFile)) )
+              exportFile = IOUtil.appendFileExt(exportFile, "properties");
+            // Confirm overwrite
+            if ( exportFile.exists() ) {
+              ret = JOptionPane.showConfirmDialog(parent,
+                                                  R("Menu.Properties.filechooser.overwrite.mess",exportFile.getName()),
+                                                  R("Menu.Properties.filechooser.save.title"),
+                                                  JOptionPane.OK_CANCEL_OPTION,
+                                                  JOptionPane.QUESTION_MESSAGE);
+              if ( ret != JOptionPane.OK_OPTION )
+                return;
+            }
+            // Store properties in selected file
+            props.store(exportFile);
+            // call action listener for subsequent actions
+            if ( subsequentActionListener != null )
+              subsequentActionListener.actionPerformed(e);
+            return;
+          }
+          
+          if ( "IMPORT_PROPS".equals(actionID) ) {
+// reuse last selected directory
+//            // set current directory
+//            PROPS_FILE_CHOOSER.setCurrentDirectory(null);
+            int ret = PROPS_FILE_CHOOSER.showOpenDialog(parent);
+            if ( ret != JFileChooser.APPROVE_OPTION || PROPS_FILE_CHOOSER.getSelectedFile() == null )
+              return;
+            File importFile = PROPS_FILE_CHOOSER.getSelectedFile();
+            // check that file exists
+            if ( !importFile.exists() || !importFile.isFile() ) {
+              JOptionPane.showMessageDialog(parent,
+                                            R("Menu.Properties.filechooser.filenotfound.mess",importFile.getAbsolutePath()),
+                                            R("Menu.Properties.filechooser.open.title"),
+                                            JOptionPane.ERROR_MESSAGE);
+              return;
+            }
+            // Import properties
+            props.clear(); // initialize current props
+            props.load(importFile); // load new props from selected file
+            props.store();  // store props to default file
+            // call action listener for subsequent actions
+            if ( subsequentActionListener != null )
+              subsequentActionListener.actionPerformed(e);
+            return;
+          }
+        }
+      };
+      
+	  // Create menue
+      final Action importPropsAction = createAction(R("Menu.Properties.action.IMPORT_PROPS"), actionListener, "IMPORT_PROPS", R("Menu.Properties.action.IMPORT_PROPS.desc"), null);
+      final Action exportPropsAction = createAction(R("Menu.Properties.action.EXPORT_PROPS"), actionListener, "EXPORT_PROPS", R("Menu.Properties.action.EXPORT_PROPS.desc"), null);
+      final JMenu propertiesMenu = new JMenu( menuLabel != null ? menuLabel : R("Menu.Properties") );
+      propertiesMenu.setToolTipText( menuTooltip != null ? menuTooltip : R("Menu.Properties.desc") );
+	  propertiesMenu.add(importPropsAction);
+	  propertiesMenu.add(exportPropsAction);
+	  
+	  return propertiesMenu;
+	}
+
 	/**
 	 * Convenience method to ask a simple Yes/No question. It can be started
 	 * from any Thread and will always show on the EDT.

Modified: trunk/schmitzm-core/src/main/resources/de/schmitzm/swing/resource/locales/SwingResourceBundle.properties
===================================================================
--- trunk/schmitzm-core/src/main/resources/de/schmitzm/swing/resource/locales/SwingResourceBundle.properties	2014-11-24 15:08:13 UTC (rev 2407)
+++ trunk/schmitzm-core/src/main/resources/de/schmitzm/swing/resource/locales/SwingResourceBundle.properties	2014-11-26 16:31:13 UTC (rev 2408)
@@ -257,6 +257,18 @@
 Menu.Logging.ERROR=Log only errors
 Menu.Logging.OpenFile=Open ${0}
 
+Menu.Properties=Import / Export
+Menu.Properties.desc=Import or export properties
+Menu.Properties.filechooser.filter.desc=Properties files
+Menu.Properties.filechooser.open.title=Import properties
+Menu.Properties.filechooser.save.title=Export properties
+Menu.Properties.filechooser.overwrite.mess='${0}' already exist! Overwrite?
+Menu.Properties.filechooser.filenotfound.mess=File does not exist: ${0}
+Menu.Properties.action.EXPORT_PROPS=Export settings...
+Menu.Properties.action.EXPORT_PROPS.desc=Export settings to .properties file
+Menu.Properties.action.IMPORT_PROPS=Import settings...
+Menu.Properties.action.IMPORT_PROPS.desc=Import settings from .properties file
+
 AtlasStatusDialog.jnlp.generalDownload.Title=Downloading
 AtlasStatusDialog.jnlp.generalDownload.Desc=Downloading data...
 AtlasStatusDialog.jnlp.progress=Downloading ${0} (${1}%)

Modified: trunk/schmitzm-core/src/main/resources/de/schmitzm/swing/resource/locales/SwingResourceBundle_de.properties
===================================================================
--- trunk/schmitzm-core/src/main/resources/de/schmitzm/swing/resource/locales/SwingResourceBundle_de.properties	2014-11-24 15:08:13 UTC (rev 2407)
+++ trunk/schmitzm-core/src/main/resources/de/schmitzm/swing/resource/locales/SwingResourceBundle_de.properties	2014-11-26 16:31:13 UTC (rev 2408)
@@ -201,9 +201,6 @@
 ResourceProviderManagerFrame.FinishMess=Property-Dateien erfolgreich erstellt...
 ResourceProviderManagerFrame.UpdateMess=${0} Sprach-Pakete aktualisiert...
 
-
-
-
 AtlasStatusDialog.jnlp.downloadFailed=Download fehlgeschlagen: ${0} ${1}
 AtlasStatusDialog.jnlp.generalDownload.Desc=Herunterladen...
 AtlasStatusDialog.jnlp.generalDownload.Title=Downloading
@@ -212,12 +209,26 @@
 AtlasStatusDialog.jnlp.validating=\u00DCberpr\u00FCfe ${0} (${1}%)
 Details=Details...
 Information=Information
+
 Menu.Logging=Logging...
 Menu.Logging.DEBUG=Alle Nachrichten schreiben
 Menu.Logging.ERROR=Nur Fehler schreiben
 Menu.Logging.INFO=Infos, Warnungen und Fehler schreiben
 Menu.Logging.OpenFile=\u00D6ffne ${0}
 Menu.Logging.WARN=Warnungen und Fehler schreiben
+
+Menu.Properties=Import / Export
+Menu.Properties.desc=Einstellungen importieren und exportieren
+Menu.Properties.filechooser.filter.desc=Einstellungs-Dateien
+Menu.Properties.filechooser.open.title=Einstellungen importieren
+Menu.Properties.filechooser.save.title=Einstellungen exportieren
+Menu.Properties.filechooser.overwrite.mess='${0}' existiert bereits! \u00dcberschreiben?
+Menu.Properties.filechooser.filenotfound.mess=Datei nicht gefunden: ${0}
+Menu.Properties.action.EXPORT_PROPS=Einstellungen expotieren...
+Menu.Properties.action.EXPORT_PROPS.desc=Einstellungen in .properties-Datei speichern
+Menu.Properties.action.IMPORT_PROPS=Einstellungen importieren...
+Menu.Properties.action.IMPORT_PROPS.desc=Einstellungen aus .properties-Datei einlesen
+
 Ok=Ok
 OperationTreePanel.OpDesc.NaN=NaN
 OperationTreePanel.OpDesc.atan=arctan($NUMBER)



More information about the Schmitzm-commits mailing list