From 1b87930e43eb7d14068a21ed0b488f28cc6ce49b Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Tue, 27 May 2008 21:53:51 +0200 Subject: [PATCH] implement project cloning --- src/net/pterodactylus/jsite/core/Core.java | 10 ++++++ src/net/pterodactylus/jsite/core/CoreImpl.java | 19 ++++++++++ src/net/pterodactylus/jsite/core/CoreListener.java | 10 ++++++ .../pterodactylus/jsite/gui/SwingInterface.java | 12 +++++-- src/net/pterodactylus/jsite/project/Project.java | 22 ++++++++++++ .../jsite/project/ProjectManager.java | 42 ++++++++++++++++++++-- 6 files changed, 109 insertions(+), 6 deletions(-) diff --git a/src/net/pterodactylus/jsite/core/Core.java b/src/net/pterodactylus/jsite/core/Core.java index 325d4c5..456ddae 100644 --- a/src/net/pterodactylus/jsite/core/Core.java +++ b/src/net/pterodactylus/jsite/core/Core.java @@ -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 diff --git a/src/net/pterodactylus/jsite/core/CoreImpl.java b/src/net/pterodactylus/jsite/core/CoreImpl.java index dbc5997..9073655 100644 --- a/src/net/pterodactylus/jsite/core/CoreImpl.java +++ b/src/net/pterodactylus/jsite/core/CoreImpl.java @@ -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); diff --git a/src/net/pterodactylus/jsite/core/CoreListener.java b/src/net/pterodactylus/jsite/core/CoreListener.java index 977e2fe..8da73bb 100644 --- a/src/net/pterodactylus/jsite/core/CoreListener.java +++ b/src/net/pterodactylus/jsite/core/CoreListener.java @@ -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 diff --git a/src/net/pterodactylus/jsite/gui/SwingInterface.java b/src/net/pterodactylus/jsite/gui/SwingInterface.java index e5f569c..a376734 100644 --- a/src/net/pterodactylus/jsite/gui/SwingInterface.java +++ b/src/net/pterodactylus/jsite/gui/SwingInterface.java @@ -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); diff --git a/src/net/pterodactylus/jsite/project/Project.java b/src/net/pterodactylus/jsite/project/Project.java index b6c514b..c54f211 100644 --- a/src/net/pterodactylus/jsite/project/Project.java +++ b/src/net/pterodactylus/jsite/project/Project.java @@ -77,6 +77,28 @@ public class Project extends AbstractBean { private List virtualEntries = new ArrayList(); /** + * 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 diff --git a/src/net/pterodactylus/jsite/project/ProjectManager.java b/src/net/pterodactylus/jsite/project/ProjectManager.java index 4480faa..2cad1e4 100644 --- a/src/net/pterodactylus/jsite/project/ProjectManager.java +++ b/src/net/pterodactylus/jsite/project/ProjectManager.java @@ -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 // -- 2.7.4