persist interface config
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Mon, 7 Apr 2008 17:34:31 +0000 (17:34 +0000)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Mon, 7 Apr 2008 17:34:31 +0000 (17:34 +0000)
git-svn-id: http://trooper/svn/projects/jSite/trunk@644 c3eda9e8-030b-0410-8277-bc7414b0a119

src/net/pterodactylus/jsite/gui/SwingInterface.java
src/net/pterodactylus/jsite/main/Main.java

index 1f41af1..02142a9 100644 (file)
 package net.pterodactylus.jsite.gui;
 
 import java.awt.event.ActionEvent;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Locale;
+import java.util.Properties;
 
 import javax.swing.JOptionPane;
 
@@ -31,6 +36,7 @@ import net.pterodactylus.jsite.core.CoreListener;
 import net.pterodactylus.jsite.core.Node;
 import net.pterodactylus.jsite.i18n.I18n;
 import net.pterodactylus.jsite.i18n.gui.I18nAction;
+import net.pterodactylus.util.io.Closer;
 
 /**
  * TODO
@@ -42,6 +48,9 @@ public class SwingInterface implements CoreListener {
 
        /** The application core. */
        private final Core core;
+       
+       /** The configuration directory. */
+       private String configDirectory;
 
        /** The main window. */
        private MainWindow mainWindow;
@@ -214,6 +223,16 @@ public class SwingInterface implements CoreListener {
                return addProjectAction;
        }
 
+       /**
+        * Sets the configuration directory.
+        * 
+        * @param configDirectory
+        *            The directory the configuration is stored in
+        */
+       public void setConfigDirectory(String configDirectory) {
+               this.configDirectory = configDirectory;
+       }
+
        //
        // ACTIONS
        //
@@ -237,7 +256,56 @@ public class SwingInterface implements CoreListener {
         * Loads the configuration of the interface.
         */
        private void loadConfig() {
-               beautify = true;
+               /* initialize default stuff. */
+               beautify = false;
+               /* now read config. */
+               File configFile = new File(configDirectory, "swing-interface.properties");
+               if (!configFile.exists() || !configFile.canRead() || !configFile.isFile()) {
+                       System.err.println("could not find “" + configFile.getAbsolutePath() + "”!");
+                       return;
+               }
+               Properties configProperties = new Properties();
+               FileInputStream configInputStream = null;
+               try {
+                       configInputStream = new FileInputStream(configFile);
+                       configProperties.load(configInputStream);
+               } catch (IOException ioe1) {
+                       System.err.println("could not load config, " + ioe1.getMessage());
+               } finally {
+                       Closer.close(configInputStream);
+               }
+               if (configProperties.containsKey("beautify")) {
+                       beautify = Boolean.valueOf(configProperties.getProperty("beautify"));
+               }
+       }
+
+       /**
+        * Saves the configuration.
+        */
+       private void saveConfig() {
+               File configDirectory = new File(this.configDirectory);
+               if (!configDirectory.exists()) {
+                       if (!configDirectory.mkdirs()) {
+                               System.err.println("could not create “" + this.configDirectory + "”!");
+                               return;
+                       }
+               }
+               if (!configDirectory.exists() || !configDirectory.isDirectory() || !configDirectory.canWrite()) {
+                       System.err.println("can not access “" + this.configDirectory + "”!");
+                       return;
+               }
+               File configFile = new File(configDirectory, "swing-interface.properties");
+               Properties configProperties = new Properties();
+               configProperties.setProperty("beautify", String.valueOf(beautify));
+               FileOutputStream configOutputStream = null;
+               try {
+                       configOutputStream = new FileOutputStream(configFile);
+                       configProperties.store(configOutputStream, "configuration of swing interface");
+               } catch (IOException ioe1) {
+                       System.err.println("could not save config, " + ioe1.getMessage());
+               } finally {
+                       Closer.close(configOutputStream);
+               }
        }
 
        /**
@@ -360,6 +428,7 @@ public class SwingInterface implements CoreListener {
                configurationDialog.setVisible(true);
                if (!configurationDialog.wasCancelled()) {
                        beautify = configurationDialog.getBeautify();
+                       saveConfig();
                }
        }
 
@@ -373,6 +442,7 @@ public class SwingInterface implements CoreListener {
         * Quits jSite.
         */
        private void quit() {
+               saveConfig();
                System.exit(0);
        }
 
index 39024ec..871db5f 100644 (file)
@@ -49,11 +49,14 @@ public class Main {
        private void start() {
                Core core = new Core();
 
-               ProjectManager projectManager = new ProjectManager(System.getProperty("user.home") + File.pathSeparator + ".jSite");
+               String configDirectory = System.getProperty("user.home") + File.separator + ".jSite";
+               
+               ProjectManager projectManager = new ProjectManager(configDirectory);
                core.setProjectManager(projectManager);
 
                SwingInterface swingInterface = new SwingInterface(core);
                core.addCoreListener(swingInterface);
+               swingInterface.setConfigDirectory(configDirectory);
                swingInterface.start();
 
                core.start();