make delete and clone buttons project specific
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 27 May 2008 18:39:27 +0000 (20:39 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 27 May 2008 18:39:27 +0000 (20:39 +0200)
src/net/pterodactylus/jsite/core/Core.java
src/net/pterodactylus/jsite/core/CoreImpl.java
src/net/pterodactylus/jsite/core/CoreListener.java
src/net/pterodactylus/jsite/gui/MainWindow.java
src/net/pterodactylus/jsite/gui/ProjectPanel.java
src/net/pterodactylus/jsite/gui/SwingInterface.java

index 4909b4d..325d4c5 100644 (file)
@@ -112,16 +112,22 @@ public interface Core {
        public void disconnectFromNode(Node node);
 
        /**
-        * Creates a new project. The returned {@link Project} will contain a newly
-        * generated key pair.
+        * Creates a new project.
         * 
-        * @return A newly created project
         * @throws IOException
         *             if an I/O error occured communicating with the node
         * @throws JSiteException
         *             if there is a problem with the node
         */
-       public Project createProject() throws IOException, JSiteException;
+       public void createProject() throws IOException, JSiteException;
+
+       /**
+        * Removes the given project.
+        * 
+        * @param project
+        *            The project to remove
+        */
+       public void removeProject(Project project);
 
        /**
         * Returns a list of all projects.
index d0e78b9..dbc5997 100644 (file)
@@ -120,6 +120,30 @@ public class CoreImpl implements Core, NodeListener, RequestListener {
        }
 
        /**
+        * Notifies all listeners that a project was added.
+        * 
+        * @param project
+        *            The project that was added
+        */
+       private void fireProjectAdded(Project project) {
+               for (CoreListener coreListener: coreListeners) {
+                       coreListener.projectAdded(project);
+               }
+       }
+
+       /**
+        * Notifies all listeners that a project was removed.
+        * 
+        * @param project
+        *            The project that was removed
+        */
+       private void fireProjectRemoved(Project project) {
+               for (CoreListener coreListener: coreListeners) {
+                       coreListener.projectRemoved(project);
+               }
+       }
+
+       /**
         * Notifies all listeners that the nodes were successfully loaded.
         * 
         * @param directory
@@ -414,10 +438,19 @@ public class CoreImpl implements Core, NodeListener, RequestListener {
        /**
         * {@inheritDoc}
         */
-       public Project createProject() throws IOException, JSiteException {
-               return projectManager.createProject();
+       public void createProject() throws IOException, JSiteException {
+               Project newProject = projectManager.createProject();
+               fireProjectAdded(newProject);
        }
 
+       /**
+        * {@inheritDoc}
+        */
+       public void removeProject(Project project) {
+               projectManager.removeProject(project);
+               fireProjectRemoved(project);
+       }
+       
        //
        // PRIVATE METHODS
        //
index d4a522e..977e2fe 100644 (file)
@@ -69,6 +69,22 @@ public interface CoreListener {
         */
        public void savingProjectsFailed(String directory, Throwable throwable);
 
+       /**
+        * Notifies a listener that the given project was added.
+        * 
+        * @param project
+        *            The project that was added.
+        */
+       public void projectAdded(Project project);
+
+       /**
+        * Notifies a listener that the given project was removed.
+        * 
+        * @param project
+        *            The project that was removed.
+        */
+       public void projectRemoved(Project project);
+
        //
        // node configuration
        //
index 7c2795f..843eaa1 100644 (file)
@@ -478,8 +478,6 @@ public class MainWindow extends JFrame implements WindowListener, I18nable, Prop
                swingInterface.getQuitAction().updateI18n();
                swingInterface.getAddNodeAction().updateI18n();
                swingInterface.getAddProjectAction().updateI18n();
-               swingInterface.getCloneProjectAction().updateI18n();
-               swingInterface.getDeleteProjectAction().updateI18n();
                swingInterface.getHelpAboutAction().updateI18n();
                jSiteMenu.updateI18n();
                nodeMenu.updateI18n();
@@ -490,6 +488,10 @@ public class MainWindow extends JFrame implements WindowListener, I18nable, Prop
                        swingInterface.getNodeEditAction(node).updateI18n();
                        swingInterface.getNodeDeleteAction(node).updateI18n();
                }
+               for (Project project: swingInterface.getProjects()) {
+                       swingInterface.getCloneProjectAction(project).updateI18n();
+                       swingInterface.getDeleteProjectAction(project).updateI18n();
+               }
                for (I18nAction languageAction: swingInterface.getLanguageActions()) {
                        languageAction.updateI18n();
                }
index 837f5fe..73d901f 100644 (file)
@@ -211,8 +211,8 @@ public class ProjectPanel extends JPanel implements DocumentListener, I18nable {
                JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.TRAILING, 12, 12));
                buttonPanel.setBorder(BorderFactory.createEmptyBorder(-12, -12, -12, -12));
 
-               buttonPanel.add(new JButton(swingInterface.getDeleteProjectAction()));
-               buttonPanel.add(new JButton(swingInterface.getCloneProjectAction()));
+               buttonPanel.add(new JButton(swingInterface.getDeleteProjectAction(project)));
+               buttonPanel.add(new JButton(swingInterface.getCloneProjectAction(project)));
 
                return buttonPanel;
        }
index 3b8971c..2c032ea 100644 (file)
@@ -115,11 +115,11 @@ public class SwingInterface implements CoreListener, LoggingListener, PropertyCh
        /** The “add project” action. */
        private I18nAction addProjectAction;
 
-       /** The “clone project” action. */
-       private I18nAction cloneProjectAction;
+       /** The “clone project” actions. */
+       private Map<Project, I18nAction> cloneProjectActions = new HashMap<Project, I18nAction>();
 
-       /** The “delete project” action. */
-       private I18nAction deleteProjectAction;
+       /** The “delete project” actions. */
+       private Map<Project, I18nAction> deleteProjectActions = new HashMap<Project, I18nAction>();
 
        /** The “about” dialog. */
        private AboutDialog aboutDialog;
@@ -133,6 +133,9 @@ public class SwingInterface implements CoreListener, LoggingListener, PropertyCh
        /** The list of all defined nodes. */
        private List<Node> nodeList = Collections.synchronizedList(new ArrayList<Node>());
 
+       /** The list of all projects. */
+       private List<Project> projectList = Collections.synchronizedList(new ArrayList<Project>());
+
        //
        // CONFIGURATION
        //
@@ -350,21 +353,25 @@ public class SwingInterface implements CoreListener, LoggingListener, PropertyCh
        }
 
        /**
-        * Returns the “clone project” action.
+        * Returns the “clone project” action for the given project.
         * 
+        * @param project
+        *            The project to get the “clone project” action for
         * @return The “clone project” action
         */
-       I18nAction getCloneProjectAction() {
-               return cloneProjectAction;
+       I18nAction getCloneProjectAction(Project project) {
+               return cloneProjectActions.get(project);
        }
 
        /**
-        * Returns the “delete project” action.
+        * Returns the “delete project” action for the given project.
         * 
+        * @param project
+        *            The project to get the “delete project” action for
         * @return The “delete project” action
         */
-       I18nAction getDeleteProjectAction() {
-               return deleteProjectAction;
+       I18nAction getDeleteProjectAction(Project project) {
+               return deleteProjectActions.get(project);
        }
 
        /**
@@ -377,6 +384,15 @@ public class SwingInterface implements CoreListener, LoggingListener, PropertyCh
        }
 
        /**
+        * Returns a list of all projects.
+        * 
+        * @return All projects
+        */
+       List<Project> getProjects() {
+               return projectList;
+       }
+
+       /**
         * Returns the thread pool used for off-thread processes.
         * 
         * @return The thread pool
@@ -574,26 +590,6 @@ public class SwingInterface implements CoreListener, LoggingListener, PropertyCh
                                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();
-                       }
-               };
        }
 
        /**
@@ -765,10 +761,7 @@ public class SwingInterface implements CoreListener, LoggingListener, PropertyCh
         */
        private void addProject() {
                try {
-                       Project project = core.createProject();
-                       project.setName(I18n.get("general.newProject.name"));
-                       project.setDescription(I18n.get("general.newProject.description", new Date()));
-                       mainWindow.addProject(project, true);
+                       core.createProject();
                } catch (JSiteException nne1) {
                        /* TODO - add i18n */
                        JOptionPane.showMessageDialog(mainWindow, I18n.get(""), I18n.get(""), JOptionPane.ERROR_MESSAGE);
@@ -780,16 +773,23 @@ public class SwingInterface implements CoreListener, LoggingListener, PropertyCh
 
        /**
         * Clones a project.
+        * 
+        * @param project
+        *            The project to clone
         */
-       private void cloneProject() {
+       private void cloneProject(Project project) {
+               System.out.println("clone " + project);
                /* TODO */
        }
 
        /**
         * Deletes a project.
+        * 
+        * @param project
+        *            The project to delete
         */
-       private void deleteProject() {
-               /* TODO */
+       private void deleteProject(Project project) {
+               System.out.println("delete " + project);
        }
 
        //
@@ -802,7 +802,7 @@ public class SwingInterface implements CoreListener, LoggingListener, PropertyCh
        public void loadingProjectsDone(String directory) {
                mainWindow.setStatusBarText(I18n.get("mainWindow.statusBar.projectLoadingDone"));
                for (Project project: core.getProjects()) {
-                       mainWindow.addProject(project, false);
+                       projectAdded(project, false);
                }
        }
 
@@ -830,6 +830,52 @@ public class SwingInterface implements CoreListener, LoggingListener, PropertyCh
        /**
         * {@inheritDoc}
         */
+       public void projectAdded(Project project) {
+               projectAdded(project, true);
+       }
+       
+       /**
+        * 
+        * @param project
+        * @param switchToProject
+        */
+       private void projectAdded(final Project project, boolean switchToProject) {
+               project.setName(I18n.get("general.newProject.name"));
+               project.setDescription(I18n.get("general.newProject.description", new Date()));
+               cloneProjectActions.put(project, new I18nAction("mainWindow.button.cloneProject") {
+
+                       /**
+                        * {@inheritDoc}
+                        */
+                       @SuppressWarnings("synthetic-access")
+                       public void actionPerformed(ActionEvent actionEvent) {
+                               cloneProject(project);
+                       }
+               });
+               deleteProjectActions.put(project, new I18nAction("mainWindow.button.deleteProject") {
+
+                       /**
+                        * {@inheritDoc}
+                        */
+                       @SuppressWarnings("synthetic-access")
+                       public void actionPerformed(ActionEvent actionEvent) {
+                               deleteProject(project);
+                       }
+               });
+               projectList.add(project);
+               mainWindow.addProject(project, switchToProject);
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       public void projectRemoved(Project project) {
+               /* TODO - implement */
+       }
+
+       /**
+        * {@inheritDoc}
+        */
        public void loadingNodesDone(String directory) {
                mainWindow.setStatusBarText(I18n.get("mainWindow.statusBar.loadingNodesDone"));
        }