add more status notifications to core listener
[jSite2.git] / src / net / pterodactylus / jsite / gui / SwingInterface.java
index 02142a9..e022cf1 100644 (file)
@@ -34,13 +34,14 @@ import javax.swing.JOptionPane;
 import net.pterodactylus.jsite.core.Core;
 import net.pterodactylus.jsite.core.CoreListener;
 import net.pterodactylus.jsite.core.Node;
+import net.pterodactylus.jsite.core.Project;
 import net.pterodactylus.jsite.i18n.I18n;
 import net.pterodactylus.jsite.i18n.gui.I18nAction;
 import net.pterodactylus.util.io.Closer;
 
 /**
- * TODO
- * 
+ * The Swing user interface.
+ *
  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
  * @version $Id$
  */
@@ -48,9 +49,9 @@ public class SwingInterface implements CoreListener {
 
        /** The application core. */
        private final Core core;
-       
+
        /** The configuration directory. */
-       private String configDirectory;
+       private final String configDirectory;
 
        /** The main window. */
        private MainWindow mainWindow;
@@ -85,6 +86,12 @@ public class SwingInterface implements CoreListener {
        /** The “add project” action. */
        private I18nAction addProjectAction;
 
+       /** The “clone project” action. */
+       private I18nAction cloneProjectAction;
+
+       /** The “delete project” action. */
+       private I18nAction deleteProjectAction;
+
        /** The “about” dialog. */
        private AboutDialog aboutDialog;
 
@@ -98,23 +105,39 @@ public class SwingInterface implements CoreListener {
        // CONFIGURATION
        //
 
-       /** Whether to beautify the GUI. */
-       private boolean beautify;
+       /** The advanced mode. */
+       private boolean advancedMode;
+
+       /** Whether to antialias the GUI. */
+       private boolean antialias;
+
+       /** The control font. */
+       private String controlFont;
+
+       /** The user font. */
+       private String userFont;
 
        /**
         * Creates a new swing interface.
-        * 
+        *
         * @param core
         *            The core to operate on
+        * @param configDirectory
+        *            The directory the configuration is stored in
         */
-       public SwingInterface(Core core) {
+       public SwingInterface(Core core, String configDirectory) {
                this.core = core;
-               I18n.setLocale(Locale.ENGLISH); /* TODO - load config */
+               this.configDirectory = configDirectory;
+               I18n.setLocale(Locale.ENGLISH);
                loadConfig();
-               if (beautify) {
+               if (antialias) {
                        System.setProperty("swing.aatext", "true");
-                       System.setProperty("swing.plaf.metal.controlFont", "Tahoma");
-                       System.setProperty("swing.plaf.metal.userFont", "Tahoma");
+               }
+               if (controlFont != null) {
+                       System.setProperty("swing.plaf.metal.controlFont", controlFont);
+               }
+               if (userFont != null) {
+                       System.setProperty("swing.plaf.metal.userFont", userFont);
                }
                initActions();
                initDialogs();
@@ -126,7 +149,7 @@ public class SwingInterface implements CoreListener {
 
        /**
         * Returns the core that is controlled by the Swing interface.
-        * 
+        *
         * @return The core
         */
        Core getCore() {
@@ -135,7 +158,7 @@ public class SwingInterface implements CoreListener {
 
        /**
         * Returns the main window of the Swing interface.
-        * 
+        *
         * @return The main window
         */
        MainWindow getMainWindow() {
@@ -144,7 +167,7 @@ public class SwingInterface implements CoreListener {
 
        /**
         * Returns the “configure” action.
-        * 
+        *
         * @return The “configure” action
         */
        I18nAction getConfigureAction() {
@@ -153,7 +176,7 @@ public class SwingInterface implements CoreListener {
 
        /**
         * Returns the “import config” action.
-        * 
+        *
         * @return The “import config” action
         */
        I18nAction getImportConfigAction() {
@@ -162,7 +185,7 @@ public class SwingInterface implements CoreListener {
 
        /**
         * Returns the “quit” action.
-        * 
+        *
         * @return The “quit” action
         */
        I18nAction getQuitAction() {
@@ -171,7 +194,7 @@ public class SwingInterface implements CoreListener {
 
        /**
         * Returns the “manage nodes” action.
-        * 
+        *
         * @return The “manage nodes” action
         */
        I18nAction getManageNodesAction() {
@@ -180,7 +203,7 @@ public class SwingInterface implements CoreListener {
 
        /**
         * Returns the “connect to node” action.
-        * 
+        *
         * @return The “connect to node” action
         */
        I18nAction getNodeConnectAction() {
@@ -189,7 +212,7 @@ public class SwingInterface implements CoreListener {
 
        /**
         * Returns the “disconnect from node” action.
-        * 
+        *
         * @return The “disconnect from node” action
         */
        I18nAction getNodeDisconnectAction() {
@@ -198,7 +221,7 @@ public class SwingInterface implements CoreListener {
 
        /**
         * Returns all language actions.
-        * 
+        *
         * @return All language actions
         */
        List<I18nAction> getLanguageActions() {
@@ -207,7 +230,7 @@ public class SwingInterface implements CoreListener {
 
        /**
         * Returns the “about” action.
-        * 
+        *
         * @return The “about” action
         */
        I18nAction getHelpAboutAction() {
@@ -216,7 +239,7 @@ public class SwingInterface implements CoreListener {
 
        /**
         * Returns the “add project” action.
-        * 
+        *
         * @return The “add project” action
         */
        I18nAction getAddProjectAction() {
@@ -224,13 +247,21 @@ public class SwingInterface implements CoreListener {
        }
 
        /**
-        * Sets the configuration directory.
-        * 
-        * @param configDirectory
-        *            The directory the configuration is stored in
+        * Returns the “clone project” action.
+        *
+        * @return The “clone project” action
         */
-       public void setConfigDirectory(String configDirectory) {
-               this.configDirectory = configDirectory;
+       I18nAction getCloneProjectAction() {
+               return cloneProjectAction;
+       }
+
+       /**
+        * Returns the “delete project” action.
+        *
+        * @return The “delete project” action
+        */
+       I18nAction getDeleteProjectAction() {
+               return deleteProjectAction;
        }
 
        //
@@ -257,7 +288,7 @@ public class SwingInterface implements CoreListener {
         */
        private void loadConfig() {
                /* initialize default stuff. */
-               beautify = false;
+               antialias = false;
                /* now read config. */
                File configFile = new File(configDirectory, "swing-interface.properties");
                if (!configFile.exists() || !configFile.canRead() || !configFile.isFile()) {
@@ -274,8 +305,20 @@ public class SwingInterface implements CoreListener {
                } finally {
                        Closer.close(configInputStream);
                }
-               if (configProperties.containsKey("beautify")) {
-                       beautify = Boolean.valueOf(configProperties.getProperty("beautify"));
+               if (configProperties.containsKey("advancedMode")) {
+                       advancedMode = Boolean.valueOf(configProperties.getProperty("advancedMode"));
+               }
+               if (configProperties.containsKey("antialias")) {
+                       antialias = Boolean.valueOf(configProperties.getProperty("antialias"));
+               }
+               if (configProperties.containsKey("controlFont")) {
+                       controlFont = configProperties.getProperty("controlFont");
+               }
+               if (configProperties.containsKey("userFont")) {
+                       userFont = configProperties.getProperty("userFont");
+               }
+               if (configProperties.containsKey("language")) {
+                       I18n.setLocale(new Locale(configProperties.getProperty("language")));
                }
        }
 
@@ -296,7 +339,15 @@ public class SwingInterface implements CoreListener {
                }
                File configFile = new File(configDirectory, "swing-interface.properties");
                Properties configProperties = new Properties();
-               configProperties.setProperty("beautify", String.valueOf(beautify));
+               configProperties.setProperty("advancedMode", String.valueOf(advancedMode));
+               configProperties.setProperty("antialias", String.valueOf(antialias));
+               if (controlFont != null) {
+                       configProperties.setProperty("controlFont", controlFont);
+               }
+               if (userFont != null) {
+                       configProperties.setProperty("userFont", userFont);
+               }
+               configProperties.setProperty("language", I18n.getLocale().getLanguage());
                FileOutputStream configOutputStream = null;
                try {
                        configOutputStream = new FileOutputStream(configFile);
@@ -405,6 +456,26 @@ public class SwingInterface implements CoreListener {
                                addProject();
                        }
                };
+               cloneProjectAction = new I18nAction("mainWindow.button.cloneProject") {
+
+                       /**
+                        * {@inheritDoc}
+                        */
+                       @SuppressWarnings("synthetic-access")
+                       public void actionPerformed(ActionEvent actionEvent) {
+                               cloneProject();
+                       }
+               };
+               deleteProjectAction = new I18nAction("mainWindow.button.deleteProject") {
+
+                       /**
+                        * {@inheritDoc}
+                        */
+                       @SuppressWarnings("synthetic-access")
+                       public void actionPerformed(ActionEvent actionEvent) {
+                               deleteProject();
+                       }
+               };
        }
 
        /**
@@ -424,10 +495,19 @@ public class SwingInterface implements CoreListener {
         * Shows the configuration dialog.
         */
        private void configure() {
-               configurationDialog.setBeautify(beautify);
+               configurationDialog.setAdvancedMode(advancedMode);
+               configurationDialog.setAntialias(antialias);
+               configurationDialog.setControlFont(controlFont);
+               configurationDialog.setUserFont(userFont);
                configurationDialog.setVisible(true);
                if (!configurationDialog.wasCancelled()) {
-                       beautify = configurationDialog.getBeautify();
+                       advancedMode = configurationDialog.isAdvancedMode();
+                       if (!advancedMode && (nodeList.size() > 1)) {
+                               JOptionPane.showMessageDialog(mainWindow, I18n.get("mainWindow.warning.multipleNodesNotAdvancedMode.message"), I18n.get("mainWindow.warning.multipleNodesNotAdvancedMode.title"), JOptionPane.WARNING_MESSAGE);
+                       }
+                       antialias = configurationDialog.isAntialias();
+                       controlFont = configurationDialog.getControlFont();
+                       userFont = configurationDialog.getUserFont();
                        saveConfig();
                }
        }
@@ -436,6 +516,7 @@ public class SwingInterface implements CoreListener {
         * Imports old jSite configuration.
         */
        private void importConfig() {
+               /* TODO */
        }
 
        /**
@@ -450,27 +531,51 @@ public class SwingInterface implements CoreListener {
         * Pops up the “manage nodes” dialog.
         */
        private void manageNodes() {
-               manageNodesDialog.setNodeList(nodeList);
-               manageNodesDialog.setVisible(true);
-               nodeList = manageNodesDialog.getNodeList();
+               if (advancedMode) {
+                       manageNodesDialog.setNodeList(nodeList);
+                       manageNodesDialog.setVisible(true);
+                       nodeList = manageNodesDialog.getNodeList();
+               } else {
+                       if (nodeList.isEmpty()) {
+                               Node newNode = new Node();
+                               newNode.setName(I18n.get("general.defaultNode.name"));
+                               newNode.setHostname("localhost");
+                               newNode.setPort(9481);
+                               nodeList.add(newNode);
+                       }
+                       Node firstNode = nodeList.get(0);
+                       EditNodeDialog editNodeDialog = manageNodesDialog.getEditNodeDialog();
+                       editNodeDialog.setNodeName(firstNode.getName());
+                       editNodeDialog.setNodeHostname(firstNode.getHostname());
+                       editNodeDialog.setNodePort(firstNode.getPort());
+                       editNodeDialog.setVisible(true);
+                       if (!editNodeDialog.wasCancelled()) {
+                               firstNode.setName(editNodeDialog.getNodeName());
+                               firstNode.setHostname(editNodeDialog.getNodeHostname());
+                               firstNode.setPort(editNodeDialog.getNodePort());
+                               /* TODO - give to core. */
+                       }
+               }
        }
 
        /**
         * Connects to the node.
         */
        private void nodeConnect() {
+               /* TODO */
        }
 
        /**
         * Disconnects from the node.
         */
        private void nodeDisconnect() {
+               /* TODO */
        }
 
        /**
         * Changes the language of the interface. This method also disables the
         * action for the newly set language and enables all others.
-        * 
+        *
         * @param newLocale
         *            The new language
         * @param languageAction
@@ -494,45 +599,122 @@ public class SwingInterface implements CoreListener {
         * Adds a project.
         */
        private void addProject() {
+               Project project = new Project();
+               project.setName("New Project");
+               project.setDescription("");
+       }
+
+       /**
+        * Clones a project.
+        */
+       private void cloneProject() {
+               /* TODO */
+       }
+
+       /**
+        * Deletes a project.
+        */
+       private void deleteProject() {
+               /* TODO */
        }
 
        //
        // INTERFACE CoreListener
        //
 
+
+       /**
+        * {@inheritDoc}
+        */
+       public void loadingProjectsDone(String directory) {
+               mainWindow.setStatusBarText(I18n.get("mainWindow.statusBar.projectLoadingDone"));
+       }
+
        /**
         * {@inheritDoc}
         */
-       public void loadingProjectsFailed(String directory) {
+       public void loadingProjectsFailed(String directory, Throwable throwable) {
                JOptionPane.showMessageDialog(mainWindow, I18n.get("mainWindow.error.projectLoadingFailed.message", directory), I18n.get("mainWindow.error.projectLoadingFailed.title"), JOptionPane.ERROR_MESSAGE);
        }
 
        /**
         * {@inheritDoc}
         */
+       public void savingProjectsDone(String directory) {
+               mainWindow.setStatusBarText(I18n.get("mainWindow.statusBar.projectSavingDone"));
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       public void savingProjectsFailed(String directory, Throwable throwabled) {
+               /* TODO */
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       public void loadingNodesDone(String directory) {
+               mainWindow.setStatusBarText(I18n.get("mainWindow.statusBar.loadingNodesDone"));
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       public void loadingNodesFailed(String directory, Throwable throwable) {
+               /* TODO */
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       public void savingNodesDone(String directory) {
+               mainWindow.setStatusBarText(I18n.get("mainWindow.statusBar.savingNodesDone"));
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       public void savingNodesFailed(String directory, Throwable throwable) {
+               /* TODO */
+       }
+
+       /**
+        * {@inheritDoc}
+        */
        public void coreLoaded() {
                this.nodeList = core.getNodes();
                manageNodesDialog.setNodeList(nodeList);
                mainWindow.setVisible(true);
-               mainWindow.setStatusBarText("Core loaded.");
+               mainWindow.setStatusBarText(I18n.get("mainWindow.statusBar.coreLoaded"));
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       public void coreStopped() {
+               mainWindow.setStatusBarText(I18n.get("mainWindow.statusBar.coreStopped"));
        }
 
        /**
         * {@inheritDoc}
         */
        public void nodeConnected(Node node) {
+               /* TODO */
        }
 
        /**
         * {@inheritDoc}
         */
        public void nodeConnecting(Node node) {
+               /* TODO */
        }
 
        /**
         * {@inheritDoc}
         */
        public void nodeDisconnected(Node node) {
+               /* TODO */
        }
 
 }