implement project cloning
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 27 May 2008 19:53:51 +0000 (21:53 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 27 May 2008 19:53:51 +0000 (21:53 +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/SwingInterface.java
src/net/pterodactylus/jsite/project/Project.java
src/net/pterodactylus/jsite/project/ProjectManager.java

index 325d4c5..456ddae 100644 (file)
@@ -122,6 +122,16 @@ public interface Core {
        public void createProject() throws IOException, JSiteException;
 
        /**
+        * Clones the given project. {@link CoreListener}s will be notified of the
+        * new clone via the {@link CoreListener#projectCloned(Project, Project)}
+        * event.
+        * 
+        * @param project
+        *            The project to clone
+        */
+       public void cloneProject(Project project);
+
+       /**
         * Removes the given project.
         * 
         * @param project
index dbc5997..9073655 100644 (file)
@@ -130,6 +130,17 @@ public class CoreImpl implements Core, NodeListener, RequestListener {
                        coreListener.projectAdded(project);
                }
        }
+       
+       /**
+        * Notifies all listeners that a project was cloned.
+        * @param clonedProject The project that was cloned
+        * @param projectClone The clone of the project
+        */
+       private void fireProjectCloned(Project clonedProject, Project projectClone) {
+               for (CoreListener coreListener: coreListeners) {
+                       coreListener.projectCloned(clonedProject, projectClone);
+               }
+       }
 
        /**
         * Notifies all listeners that a project was removed.
@@ -446,6 +457,14 @@ public class CoreImpl implements Core, NodeListener, RequestListener {
        /**
         * {@inheritDoc}
         */
+       public void cloneProject(Project project) {
+               Project projectClone = projectManager.cloneProject(project);
+               fireProjectCloned(project, projectClone);
+       }
+       
+       /**
+        * {@inheritDoc}
+        */
        public void removeProject(Project project) {
                projectManager.removeProject(project);
                fireProjectRemoved(project);
index 977e2fe..8da73bb 100644 (file)
@@ -78,6 +78,16 @@ public interface CoreListener {
        public void projectAdded(Project project);
 
        /**
+        * Notifies a listener that a project was cloned.
+        * 
+        * @param clonedProject
+        *            The clones project
+        * @param projectClone
+        *            The clone of the project
+        */
+       public void projectCloned(Project clonedProject, Project projectClone);
+
+       /**
         * Notifies a listener that the given project was removed.
         * 
         * @param project
index e5f569c..a376734 100644 (file)
@@ -778,7 +778,7 @@ public class SwingInterface implements CoreListener, LoggingListener, PropertyCh
         *            The project to clone
         */
        private void cloneProject(Project project) {
-               /* TODO */
+               core.cloneProject(project);
        }
 
        /**
@@ -838,9 +838,8 @@ public class SwingInterface implements CoreListener, LoggingListener, PropertyCh
                project.setDescription(I18n.get("general.newProject.description", new Date()));
                projectAdded(project, true);
        }
-       
+
        /**
-        * 
         * @param project
         * @param switchToProject
         */
@@ -872,6 +871,13 @@ public class SwingInterface implements CoreListener, LoggingListener, PropertyCh
        /**
         * {@inheritDoc}
         */
+       public void projectCloned(Project clonedProject, Project projectClone) {
+               projectAdded(projectClone, true);
+       }
+
+       /**
+        * {@inheritDoc}
+        */
        public void projectRemoved(Project project) {
                mainWindow.removeProject(project);
                cloneProjectActions.remove(project);
index b6c514b..c54f211 100644 (file)
@@ -77,6 +77,28 @@ public class Project extends AbstractBean {
        private List<Entry> virtualEntries = new ArrayList<Entry>();
 
        /**
+        * Creates a new project.
+        */
+       public Project() {
+               /* do nothing. */
+       }
+
+       /**
+        * Clones the given project.
+        * 
+        * @param project
+        */
+       Project(Project project) {
+               this.name = project.name;
+               this.description = project.description;
+               this.publicKey = project.publicKey;
+               this.privateKey = project.privateKey;
+               this.basePath = project.basePath;
+               this.basePathEntries.addAll(project.basePathEntries);
+               this.virtualEntries.addAll(project.virtualEntries);
+       }
+
+       /**
         * Returns the internal ID.
         * 
         * @return The internal ID
index 4480faa..2cad1e4 100644 (file)
@@ -201,9 +201,7 @@ public class ProjectManager implements PropertyChangeListener {
        public Project createProject() throws IOException, JSiteException {
                Project project = new Project();
                String[] keyPair = nodeManager.generateKeyPair();
-               byte[] idBytes = new byte[16];
-               random.nextBytes(idBytes);
-               project.setId(Hex.toHex(idBytes));
+               project.setId(generateId());
                project.setName("");
                project.setDescription("");
                project.setPrivateKey(keyPair[0]);
@@ -220,6 +218,28 @@ public class ProjectManager implements PropertyChangeListener {
        }
 
        /**
+        * Clones the given project and returns the clone. The clone will be
+        * identical in all user-exposed fields, except for the project’s
+        * {@link Project#getId ID}.
+        * 
+        * @param project
+        *            The project to clone
+        * @return The cloned project
+        */
+       public Project cloneProject(Project project) {
+               Project projectClone = new Project(project);
+               projects.add(projectClone);
+               projectClone.setId(generateId());
+               projectClone.addPropertyChangeListener(this);
+               try {
+                       save();
+               } catch (IOException ioe1) {
+                       /* ignore. */
+               }
+               return projectClone;
+       }
+
+       /**
         * Removes the given project.
         * 
         * @param project
@@ -235,6 +255,22 @@ public class ProjectManager implements PropertyChangeListener {
        }
 
        //
+       // PRIVATE METHODS
+       //
+
+       /**
+        * Generates a new random ID, consisting of 16 random bytes converted to a
+        * hexadecimal number.
+        * 
+        * @return The new ID
+        */
+       private static String generateId() {
+               byte[] idBytes = new byte[16];
+               random.nextBytes(idBytes);
+               return Hex.toHex(idBytes);
+       }
+
+       //
        // INTERFACE PropertyChangeListener
        //