ignite-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sboi...@apache.org
Subject [26/50] [abbrv] incubator-ignite git commit: # IGNITE-32: refactored preferences to properties.
Date Mon, 09 Feb 2015 13:12:20 GMT
# IGNITE-32: refactored preferences to properties.


Project: http://git-wip-us.apache.org/repos/asf/incubator-ignite/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-ignite/commit/bbd393ca
Tree: http://git-wip-us.apache.org/repos/asf/incubator-ignite/tree/bbd393ca
Diff: http://git-wip-us.apache.org/repos/asf/incubator-ignite/diff/bbd393ca

Branch: refs/heads/ignite-201
Commit: bbd393ca6201d2f2609eee3da8027beb471376a2
Parents: f5fec4b
Author: AKuznetsov <akuznetsov@gridgain.com>
Authored: Mon Feb 9 10:20:04 2015 +0700
Committer: AKuznetsov <akuznetsov@gridgain.com>
Committed: Mon Feb 9 10:20:04 2015 +0700

----------------------------------------------------------------------
 .../apache/ignite/schema/ui/SchemaLoadApp.java  | 245 +++++++++++++------
 1 file changed, 174 insertions(+), 71 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/bbd393ca/modules/schema-load/src/main/java/org/apache/ignite/schema/ui/SchemaLoadApp.java
----------------------------------------------------------------------
diff --git a/modules/schema-load/src/main/java/org/apache/ignite/schema/ui/SchemaLoadApp.java
b/modules/schema-load/src/main/java/org/apache/ignite/schema/ui/SchemaLoadApp.java
index cd34a10..f4f76e9 100644
--- a/modules/schema-load/src/main/java/org/apache/ignite/schema/ui/SchemaLoadApp.java
+++ b/modules/schema-load/src/main/java/org/apache/ignite/schema/ui/SchemaLoadApp.java
@@ -37,7 +37,6 @@ import java.net.*;
 import java.sql.*;
 import java.util.*;
 import java.util.concurrent.*;
-import java.util.prefs.*;
 
 import static javafx.embed.swing.SwingFXUtils.*;
 import static org.apache.ignite.schema.ui.Controls.*;
@@ -92,6 +91,22 @@ public class SchemaLoadApp extends Application {
         }
     }
 
+    /** Default presets for popular databases. */
+    private final Preset[] presets = {
+        new Preset("h2", "H2 Database", "h2.jar", "org.h2.Driver", "jdbc:h2:[database]",
"sa"),
+        new Preset("db2", "DB2", "db2jcc4.jar", "com.ibm.db2.jcc.DB2Driver", "jdbc:db2://[host]:[port]/[database]",
+            "db2admin"),
+        new Preset("oracle", "Oracle", "ojdbc6.jar", "oracle.jdbc.OracleDriver",
+            "jdbc:oracle:thin:@[host]:[port]:[database]", "system"),
+        new Preset("mysql", "MySQL", "mysql-connector-java-5-bin.jar", "com.mysql.jdbc.Driver",
+            "jdbc:mysql://[host]:[port]/[database]", "root"),
+        new Preset("mssql", "Microsoft SQL Server", "sqljdbc41.jar", "com.microsoft.sqlserver.jdbc.SQLServerDriver",
+            "jdbc:sqlserver://[host]:[port][;databaseName=database]", "sa"),
+        new Preset("posgresql", "PostgreSQL", "postgresql-9.3.jdbc4.jar", "org.postgresql.Driver",
+            "jdbc:postgresql://[host]:[port]/[database]", "sa"),
+        new Preset("custom", "Custom server...", "custom-jdbc.jar", "org.custom.Driver",
"jdbc:custom", "sa")
+    };
+
     /** */
     private Stage owner;
 
@@ -122,22 +137,6 @@ public class SchemaLoadApp extends Application {
     /** */
     private ComboBox<Preset> rdbmsCb;
 
-    /** Default presets for popular databases. */
-    private final Preset[] presets = {
-        new Preset("h2", "H2 Database", "h2.jar", "org.h2.Driver", "jdbc:h2:[database]",
"sa"),
-        new Preset("db2", "DB2", "db2jcc4.jar", "com.ibm.db2.jcc.DB2Driver", "jdbc:db2://[host]:[port]/[database]",
-            "db2admin"),
-        new Preset("oracle", "Oracle", "ojdbc6.jar", "oracle.jdbc.OracleDriver",
-            "jdbc:oracle:thin:@[host]:[port]:[database]", "system"),
-        new Preset("mysql", "MySQL", "mysql-connector-java-5-bin.jar", "com.mysql.jdbc.Driver",
-            "jdbc:mysql://[host]:[port]/[database]", "root"),
-        new Preset("mssql", "Microsoft SQL Server", "sqljdbc41.jar", "com.microsoft.sqlserver.jdbc.SQLServerDriver",
-            "jdbc:sqlserver://[host]:[port][;databaseName=database]", "sa"),
-        new Preset("posgresql", "PostgreSQL", "postgresql-9.3.jdbc4.jar", "org.postgresql.Driver",
-            "jdbc:postgresql://[host]:[port]/[database]", "sa"),
-        new Preset("custom", "Custom server...", "custom-jdbc.jar", "org.custom.Driver",
"jdbc:custom", "sa")
-    };
-
     /** */
     private TextField jdbcDrvJarTf;
 
@@ -210,7 +209,13 @@ public class SchemaLoadApp extends Application {
     /** */
     private final Map<String, Driver> drivers = new HashMap<>();
 
-    /** */
+    /** Application preferences. */
+    private final Properties prefs = new Properties();
+
+    /** File path for storing on local file system. */
+    private final File prefsFile = new File(System.getProperty("user.home"), ".ignite-schema-load");
+
+    /** Empty POJO fields model. */
     private static final ObservableList<PojoField> NO_FIELDS = FXCollections.emptyObservableList();
 
     /** */
@@ -1114,10 +1119,108 @@ public class SchemaLoadApp extends Application {
         return res;
     }
 
+    /**
+     * Gets string property.
+     *
+     * @param key Property key.
+     * @param dflt Default value.
+     */
+    private String getStringProp(String key, String dflt) {
+        String val = prefs.getProperty(key);
+
+        if (val != null)
+            return val;
+
+        return dflt;
+    }
+
+    /**
+     * Sets string property.
+     *
+     * @param key Property key.
+     * @param val Value to set.
+     */
+    private void setStringProp(String key, String val) {
+        prefs.put(key, val);
+    }
+
+    /**
+     * Gets int property.
+     *
+     * @param key Property key.
+     * @param dflt Default value.
+     */
+    private int getIntProp(String key, int dflt) {
+        String val = prefs.getProperty(key);
+
+        if (val != null)
+            try {
+                return Integer.parseInt(val);
+            }
+            catch (NumberFormatException e) {
+                return dflt;
+            }
+
+        return dflt;
+    }
+
+    /**
+     * Sets int property.
+     *
+     * @param key Property key.
+     * @param val Value to set.
+     */
+    private void setIntProp(String key, int val) {
+        prefs.put(key, String.valueOf(val));
+    }
+
+    /**
+     * Gets boolean property.
+     *
+     * @param key Property key.
+     * @param dflt Default value.
+     */
+    private boolean getBoolProp(String key, boolean dflt) {
+        String val = prefs.getProperty(key);
+
+        if (val != null)
+            return Boolean.parseBoolean(val);
+
+        return dflt;
+    }
+
+    /**
+     * Sets boolean property.
+     *
+     * @param key Property key.
+     * @param val Value to set.
+     */
+    private void setBoolProp(String key, boolean val) {
+        prefs.put(key, String.valueOf(val));
+    }
+
     /** {@inheritDoc} */
     @Override public void start(Stage primaryStage) {
         owner = primaryStage;
 
+        if (prefsFile.exists())
+            try (BufferedInputStream in = new BufferedInputStream(new FileInputStream(prefsFile)))
{
+                prefs.load(in);
+            }
+            catch (IOException ignore) {
+                // No-op.
+            }
+
+        // Restore presets.
+        for (Preset preset : presets) {
+            String key = "presets." + preset.pref + ".";
+
+            preset.jar = getStringProp(key + "jar", preset.jar);
+            preset.drv = getStringProp(key + "drv", preset.drv);
+            preset.url = getStringProp(key + "url", preset.url);
+            preset.user = getStringProp(key + "user", preset.user);
+        }
+
         primaryStage.setTitle("Apache Ignite Auto Schema Load Utility");
 
         primaryStage.getIcons().addAll(
@@ -1145,24 +1248,12 @@ public class SchemaLoadApp extends Application {
 
         prev();
 
-        Preferences prefs = Preferences.userNodeForPackage(getClass());
-
-        // Restore presets.
-        for (Preset preset : presets) {
-            String key = "presets." + preset.pref + ".";
-
-            preset.jar = prefs.get(key + "jar", preset.jar);
-            preset.drv = prefs.get(key + "drv", preset.drv);
-            preset.url = prefs.get(key + "url", preset.url);
-            preset.user = prefs.get(key + "user", preset.user);
-        }
-
         // Restore window pos and size.
-        if (prefs.get("window.x", null) != null) {
-            double x = prefs.getDouble("window.x", 100);
-            double y = prefs.getDouble("window.y", 100);
-            double w = prefs.getDouble("window.width", 650);
-            double h = prefs.getDouble("window.height", 650);
+        if (prefs.getProperty("window.x") != null) {
+            int x = getIntProp("window.x", 100);
+            int y = getIntProp("window.y", 100);
+            int w = getIntProp("window.width", 650);
+            int h = getIntProp("window.height", 650);
 
             // Ensure that window fit any available screen.
             if (!Screen.getScreensForRectangle(x, y, w, h).isEmpty()) {
@@ -1182,23 +1273,23 @@ public class SchemaLoadApp extends Application {
         String userHome = System.getProperty("user.home").replace('\\', '/');
 
         // Restore connection pane settings.
-        rdbmsCb.getSelectionModel().select(prefs.getInt("jdbc.db.preset", 0));
-        jdbcDrvJarTf.setText(prefs.get("jdbc.driver.jar", "h2.jar"));
-        jdbcDrvClsTf.setText(prefs.get("jdbc.driver.class", "org.h2.Driver"));
-        jdbcUrlTf.setText(prefs.get("jdbc.url", "jdbc:h2:" + userHome + "/ignite-schema-load/db"));
-        userTf.setText(prefs.get("jdbc.user", "sa"));
+        rdbmsCb.getSelectionModel().select(getIntProp("jdbc.db.preset", 0));
+        jdbcDrvJarTf.setText(getStringProp("jdbc.driver.jar", "h2.jar"));
+        jdbcDrvClsTf.setText(getStringProp("jdbc.driver.class", "org.h2.Driver"));
+        jdbcUrlTf.setText(getStringProp("jdbc.url", "jdbc:h2:" + userHome + "/ignite-schema-load/db"));
+        userTf.setText(getStringProp("jdbc.user", "sa"));
 
         // Restore generation pane settings.
-        outFolderTf.setText(prefs.get("out.folder", userHome + "/ignite-schema-load/out"));
+        outFolderTf.setText(getStringProp("out.folder", userHome + "/ignite-schema-load/out"));
 
-        pkgTf.setText(prefs.get("pojo.package", "org.apache.ignite"));
-        pojoIncludeKeysCh.setSelected(prefs.getBoolean("pojo.include", true));
-        pojoConstructorCh.setSelected(prefs.getBoolean("pojo.constructor", false));
+        pkgTf.setText(getStringProp("pojo.package", "org.apache.ignite"));
+        pojoIncludeKeysCh.setSelected(getBoolProp("pojo.include", true));
+        pojoConstructorCh.setSelected(getBoolProp("pojo.constructor", false));
 
-        xmlSingleFileCh.setSelected(prefs.getBoolean("xml.single", true));
+        xmlSingleFileCh.setSelected(getBoolProp("xml.single", true));
 
-        regexTf.setText(prefs.get("naming.pattern", "(\\w+)"));
-        replaceTf.setText(prefs.get("naming.replace", "$1_SomeText"));
+        regexTf.setText(getStringProp("naming.pattern", "(\\w+)"));
+        replaceTf.setText(getStringProp("naming.replace", "$1_SomeText"));
 
         primaryStage.show();
     }
@@ -1209,51 +1300,63 @@ public class SchemaLoadApp extends Application {
      * @param preset Preset to save.
      */
     private void savePreset(Preset preset) {
-        Preferences prefs = Preferences.userNodeForPackage(getClass());
-
         String key = "presets." + preset.pref + ".";
 
         preset.jar = jdbcDrvJarTf.getText();
-        prefs.put(key + "jar", preset.jar);
+        setStringProp(key + "jar", preset.jar);
 
         preset.drv = jdbcDrvClsTf.getText();
-        prefs.put(key + "drv", preset.drv);
+        setStringProp(key + "drv", preset.drv);
 
         preset.url = jdbcUrlTf.getText();
-        prefs.put(key + "url", preset.url);
+        setStringProp(key + "url", preset.url);
 
         preset.user = userTf.getText();
-        prefs.put(key + "user", preset.user);
+        setStringProp(key + "user", preset.user);
+
+        savePreferences();
+    }
+
+    /**
+     * Save user preferences.
+     */
+    private void savePreferences() {
+        try (FileOutputStream out = new FileOutputStream(prefsFile)) {
+            prefs.store(out, "Apache Ignite Schema Load Utility");
+        }
+        catch (IOException e) {
+            MessageBox.errorDialog(owner, "Failed to save preferences!", e);
+        }
     }
 
     /** {@inheritDoc} */
     @Override public void stop() throws Exception {
-        Preferences userPrefs = Preferences.userNodeForPackage(getClass());
-
         // Save window pos and size.
-        userPrefs.putDouble("window.x", owner.getX());
-        userPrefs.putDouble("window.y", owner.getY());
-        userPrefs.putDouble("window.width", owner.getWidth());
-        userPrefs.putDouble("window.height", owner.getHeight());
+        setIntProp("window.x", (int)owner.getX());
+        setIntProp("window.y", (int)owner.getY());
+        setIntProp("window.width", (int)owner.getWidth());
+        setIntProp("window.height", (int)owner.getHeight());
 
         // Save connection pane settings.
-        userPrefs.putInt("jdbc.db.preset", rdbmsCb.getSelectionModel().getSelectedIndex());
-        userPrefs.put("jdbc.driver.jar", jdbcDrvJarTf.getText());
-        userPrefs.put("jdbc.driver.class", jdbcDrvClsTf.getText());
-        userPrefs.put("jdbc.url", jdbcUrlTf.getText());
-        userPrefs.put("jdbc.user", userTf.getText());
+        setIntProp("jdbc.db.preset", rdbmsCb.getSelectionModel().getSelectedIndex());
+        setStringProp("jdbc.driver.jar", jdbcDrvJarTf.getText());
+        setStringProp("jdbc.driver.class", jdbcDrvClsTf.getText());
+        setStringProp("jdbc.url", jdbcUrlTf.getText());
+        setStringProp("jdbc.user", userTf.getText());
 
         // Save generation pane settings.
-        userPrefs.put("out.folder", outFolderTf.getText());
+        setStringProp("out.folder", outFolderTf.getText());
+
+        setStringProp("pojo.package", pkgTf.getText());
+        setBoolProp("pojo.include", pojoIncludeKeysCh.isSelected());
+        setBoolProp("pojo.constructor", pojoConstructorCh.isSelected());
 
-        userPrefs.put("pojo.package", pkgTf.getText());
-        userPrefs.putBoolean("pojo.include", pojoIncludeKeysCh.isSelected());
-        userPrefs.putBoolean("pojo.constructor", pojoConstructorCh.isSelected());
+        setBoolProp("xml.single", xmlSingleFileCh.isSelected());
 
-        userPrefs.putBoolean("xml.single", xmlSingleFileCh.isSelected());
+        setStringProp("naming.pattern", regexTf.getText());
+        setStringProp("naming.replace", replaceTf.getText());
 
-        userPrefs.put("naming.pattern", regexTf.getText());
-        userPrefs.put("naming.replace", replaceTf.getText());
+        savePreferences();
     }
 
     /**


Mime
View raw message