add more events to core listener
[jSite2.git] / src / net / pterodactylus / jsite / core / Core.java
index f617717..7c37808 100644 (file)
@@ -25,7 +25,7 @@ import java.util.List;
 
 /**
  * The core of jSite.
- * 
+ *
  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
  * @version $Id$
  */
@@ -43,19 +43,13 @@ public class Core {
        /** List of currently connected nodes. */
        private List<Node> connectedNodes = new ArrayList<Node>();
 
-       /**
-        * Creates a new core.
-        */
-       public Core() {
-       }
-
        //
        // LISTENER MANAGEMENT
        //
 
        /**
         * Adds the given listener to the list of registered listeners.
-        * 
+        *
         * @param coreListener
         *            The listener to add
         */
@@ -65,7 +59,7 @@ public class Core {
 
        /**
         * Removes the given listener from the list of registered listeners.
-        * 
+        *
         * @param coreListener
         *            The listener to remove
         */
@@ -76,13 +70,41 @@ public class Core {
        /**
         * Notifies all core listeners that loading the projects from the given
         * directory has failed.
-        * 
+        *
         * @param directory
         *            The directory the projects were tried to load from
+        * @param throwable
+        *            The exception that occured when loading projects
+        */
+       private void fireLoadingProjectsFailed(String directory, Throwable throwable) {
+               for (CoreListener coreListener: coreListeners) {
+                       coreListener.loadingProjectsFailed(directory, throwable);
+               }
+       }
+
+       /**
+        * Notifies all listeners that the projects were successfully saved.
+        *
+        * @param directory
+        *            The directory the projects were saved to
         */
-       private void fireLoadingProjectsFailed(String directory) {
+       private void fireSavingProjectsDone(String directory) {
                for (CoreListener coreListener: coreListeners) {
-                       coreListener.loadingProjectsFailed(directory);
+                       coreListener.savingProjectsDone(directory);
+               }
+       }
+
+       /**
+        * Notifies all listeners that the projects could not be saved.
+        *
+        * @param directory
+        *            The directory the projects were to be saved to
+        * @param throwable
+        *            The exception that occured when saving the projects
+        */
+       private void fireSavingProjectsFailed(String directory, Throwable throwable) {
+               for (CoreListener coreListener: coreListeners) {
+                       coreListener.savingProjectsFailed(directory, throwable);
                }
        }
 
@@ -95,13 +117,22 @@ public class Core {
                }
        }
 
+       /**
+        * Notifies all listeners that the core was stopped.
+        */
+       private void fireCoreStopped() {
+               for (CoreListener coreListener: coreListeners) {
+                       coreListener.coreStopped();
+               }
+       }
+
        //
        // ACCESSORS
        //
 
        /**
         * Returns the project manager.
-        * 
+        *
         * @return The project manager
         */
        public ProjectManager getProjectManager() {
@@ -110,7 +141,7 @@ public class Core {
 
        /**
         * Sets the project manager to use.
-        * 
+        *
         * @param projectManager
         *            The project manager to use
         */
@@ -120,7 +151,7 @@ public class Core {
 
        /**
         * Returns the list of all configured nodes.
-        * 
+        *
         * @return All configured nodes
         */
        public List<Node> getNodes() {
@@ -129,7 +160,7 @@ public class Core {
 
        /**
         * Returns whether the core is currently connected to the given node.
-        * 
+        *
         * @param node
         *            The node to check
         * @return <code>true</code> if the core is currently connected to the
@@ -150,18 +181,52 @@ public class Core {
                try {
                        projectManager.load();
                } catch (IOException ioe1) {
-                       fireLoadingProjectsFailed(projectManager.getDirectory());
+                       fireLoadingProjectsFailed(projectManager.getDirectory(), ioe1);
                }
                fireCoreLoaded();
        }
 
        /**
+        * Stops the core.
+        */
+       public void stop() {
+               try {
+                       projectManager.save();
+                       fireSavingProjectsDone(projectManager.getDirectory());
+               } catch (IOException ioe1) {
+                       fireSavingProjectsFailed(projectManager.getDirectory(), ioe1);
+               }
+               fireCoreStopped();
+       }
+
+       /**
         * Connects to the given node.
-        * 
+        *
         * @param node
         *            The node to connect to
         */
        public void connectToNode(Node node) {
+               /* TODO */
+       }
+
+       //
+       // PRIVATE METHODS
+       //
+
+       /**
+        * Loads the configuration.
+        */
+       @SuppressWarnings("unused")
+       private void loadConfig() {
+               /* TODO */
+       }
+
+       /**
+        * Saves the configuration.
+        */
+       @SuppressWarnings("unused")
+       private void saveConfig() {
+               /* TODO */
        }
 
 }