From: David ‘Bombe’ Roden Date: Mon, 7 Apr 2008 07:02:30 +0000 (+0000) Subject: add project manager X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=98585bac12bf6597d824a0123f25915f4abf0331;p=jSite2.git add project manager git-svn-id: http://trooper/svn/projects/jSite/trunk@628 c3eda9e8-030b-0410-8277-bc7414b0a119 --- diff --git a/src/net/pterodactylus/jsite/core/Core.java b/src/net/pterodactylus/jsite/core/Core.java index c2072cb..f617717 100644 --- a/src/net/pterodactylus/jsite/core/Core.java +++ b/src/net/pterodactylus/jsite/core/Core.java @@ -19,6 +19,7 @@ package net.pterodactylus.jsite.core; +import java.io.IOException; import java.util.ArrayList; import java.util.List; @@ -33,6 +34,9 @@ public class Core { /** The core listeners. */ private final List coreListeners = new ArrayList(); + /** The project manager. */ + private ProjectManager projectManager; + /** The node list. */ private List configuredNodes = new ArrayList(); @@ -70,6 +74,19 @@ 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 + */ + private void fireLoadingProjectsFailed(String directory) { + for (CoreListener coreListener: coreListeners) { + coreListener.loadingProjectsFailed(directory); + } + } + + /** * Notifies all core listeners that the core has loaded and is ready to run. */ private void fireCoreLoaded() { @@ -83,6 +100,25 @@ public class Core { // /** + * Returns the project manager. + * + * @return The project manager + */ + public ProjectManager getProjectManager() { + return projectManager; + } + + /** + * Sets the project manager to use. + * + * @param projectManager + * The project manager to use + */ + public void setProjectManager(ProjectManager projectManager) { + this.projectManager = projectManager; + } + + /** * Returns the list of all configured nodes. * * @return All configured nodes @@ -111,6 +147,11 @@ public class Core { * Starts the core. */ public void start() { + try { + projectManager.load(); + } catch (IOException ioe1) { + fireLoadingProjectsFailed(projectManager.getDirectory()); + } fireCoreLoaded(); } diff --git a/src/net/pterodactylus/jsite/core/CoreListener.java b/src/net/pterodactylus/jsite/core/CoreListener.java index 45772bc..5fe47f9 100644 --- a/src/net/pterodactylus/jsite/core/CoreListener.java +++ b/src/net/pterodactylus/jsite/core/CoreListener.java @@ -28,6 +28,14 @@ package net.pterodactylus.jsite.core; public interface CoreListener { /** + * Notifies all listeners that loading the projects has failed. + * + * @param directory + * The directory the projects were tried to load from + */ + public void loadingProjectsFailed(String directory); + + /** * Notifies all listeners that the core has loaded. */ public void coreLoaded(); diff --git a/src/net/pterodactylus/jsite/core/ProjectLifetime.java b/src/net/pterodactylus/jsite/core/ProjectLifetime.java new file mode 100644 index 0000000..02167c5 --- /dev/null +++ b/src/net/pterodactylus/jsite/core/ProjectLifetime.java @@ -0,0 +1,54 @@ +/* + * jSite2 - ProjectLifetime.java - + * Copyright © 2008 David Roden + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +package net.pterodactylus.jsite.core; + +/** + * Lifetime statistics of a project. + * + * @author David ‘Bombe’ Roden <bombe@freenetproject.org> + * @version $Id$ + */ +public class ProjectLifetime { + + /** The creation time of the project. */ + private final long creationTime; + + /** + * Creates new lifetime statistics. + * + * @param creationTime + * The creation time of the project + */ + public ProjectLifetime(long creationTime) { + this.creationTime = creationTime; + } + + /** + * Returns the creation time of the project. The time is given in + * milliseconds since Jan 1, 1970 UTC. + * + * @see System#currentTimeMillis() + * @return The creation time of the project + */ + public long getCreationTime() { + return creationTime; + } + +} diff --git a/src/net/pterodactylus/jsite/core/ProjectManager.java b/src/net/pterodactylus/jsite/core/ProjectManager.java new file mode 100644 index 0000000..8034189 --- /dev/null +++ b/src/net/pterodactylus/jsite/core/ProjectManager.java @@ -0,0 +1,162 @@ +/* + * jSite2 - ProjectManager.java - + * Copyright © 2008 David Roden + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +package net.pterodactylus.jsite.core; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Properties; + +import net.pterodactylus.util.io.Closer; + +/** + * Manages projects, taking care of persistence, lifetime statistics, and other + * things. + * + * @author David ‘Bombe’ Roden <bombe@freenetproject.org> + * @version $Id$ + */ +public class ProjectManager { + + /** The directory the projects are stored in. */ + private final String directory; + + /** The list of project names. */ + private final List projectNames = new ArrayList(); + + /** Mapping from project name to project. */ + private final Map projects = new HashMap(); + + /** Mapping from project to lifetime statistics. */ + @SuppressWarnings("unused") + private final Map projectLifetimes = new HashMap(); + + /** + * Creates a new project manager that saves and restores its state to/from + * the given directory. + * + * @param directory + * The directory to save and restore states to/from + */ + public ProjectManager(String directory) { + this.directory = directory; + } + + // + // ACCESSORS + // + + /** + * Returns the directory the projects are loaded from and saved to. + * + * @return The directory for storing the projects + */ + public String getDirectory() { + return directory; + } + + /** + * Returns a list of all projects, sorted by their name. + * + * @return A list of all projects + */ + public List getProjects() { + List projects = new ArrayList(); + for (String projectName: projectNames) { + projects.add(this.projects.get(projectName)); + } + return projects; + } + + // + // ACTIONS + // + + /** + * Loads projects and statistics. + * + * @throws IOException + * if an I/O error occurs + */ + public void load() throws IOException { + File directoryFile = new File(directory); + File projectFile = new File(directoryFile, "projects.properties"); + if (!projectFile.exists() || !projectFile.isFile() || !projectFile.canRead()) { + return; + } + Properties projectProperties = new Properties(); + InputStream projectInputStream = null; + try { + projectInputStream = new FileInputStream(projectFile); + projectProperties.load(projectInputStream); + } finally { + Closer.close(projectInputStream); + } + int projectIndex = 0; + while (projectProperties.containsKey("projects." + projectIndex + ".name")) { + String projectPrefix = "projects." + projectIndex; + String projectName = projectProperties.getProperty(projectPrefix + ".name"); + Project project = new Project(); + project.setName(projectName); + projectNames.add(projectName); + projects.put(projectName, project); + projectIndex++; + } + } + + /** + * Saves projects and statistics. + * + * @throws IOException + * if an I/O error occurs + */ + public void save() throws IOException { + File directoryFile = new File(directory); + if (!directoryFile.exists()) { + if (!directoryFile.mkdirs()) { + throw new IOException("could not create directory: " + directory); + } + } + Properties projectProperties = new Properties(); + int projectIndex = 0; + for (String projectName: projectNames) { + String projectPrefix = "projects." + projectIndex; + Project project = projects.get(projectName); + projectProperties.setProperty("projects." + projectPrefix + ".name", project.getName()); + projectIndex++; + } + File projectFile = new File(directoryFile, "projects.properties"); + OutputStream projectOutputStream = null; + try { + projectOutputStream = new FileOutputStream(projectFile); + projectProperties.store(projectOutputStream, "jSite projects"); + } finally { + Closer.close(projectOutputStream); + } + } + +} diff --git a/src/net/pterodactylus/jsite/main/Main.java b/src/net/pterodactylus/jsite/main/Main.java index c465311..39024ec 100644 --- a/src/net/pterodactylus/jsite/main/Main.java +++ b/src/net/pterodactylus/jsite/main/Main.java @@ -19,7 +19,10 @@ package net.pterodactylus.jsite.main; +import java.io.File; + import net.pterodactylus.jsite.core.Core; +import net.pterodactylus.jsite.core.ProjectManager; import net.pterodactylus.jsite.gui.SwingInterface; /** @@ -46,6 +49,9 @@ public class Main { private void start() { Core core = new Core(); + ProjectManager projectManager = new ProjectManager(System.getProperty("user.home") + File.pathSeparator + ".jSite"); + core.setProjectManager(projectManager); + SwingInterface swingInterface = new SwingInterface(core); core.addCoreListener(swingInterface); swingInterface.start();