package net.pterodactylus.jsite.project;
import java.beans.PropertyChangeListener;
+import java.io.File;
import java.util.ArrayList;
+import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Container for project information. A Project is capable of notifying
* {@link PropertyChangeListener}s if any of the contained properties change.
- *
+ *
* @author David ‘Bombe’ Roden <bombe@freenetproject.org>
*/
public class Project extends AbstractBean {
/**
* Clones the given project.
- *
+ *
* @param project
*/
Project(Project project) {
/**
* Returns the internal ID.
- *
+ *
* @return The internal ID
*/
String getId() {
/**
* Sets the internal ID.
- *
+ *
* @param id
* The internal ID
*/
/**
* Returns the name of the project.
- *
+ *
* @return The name of the project
*/
public String getName() {
/**
* Sets the name of the project.
- *
+ *
* @param name
* The name of the project
*/
/**
* Returns the description of the project.
- *
+ *
* @return The description of the project
*/
public String getDescription() {
/**
* Sets the description of the project
- *
+ *
* @param description
* The description of the project
*/
/**
* Returns the public key of the project.
- *
+ *
* @return The public key of the project
*/
public String getPublicKey() {
/**
* Sets the public key of the project.
- *
+ *
* @param publicKey
* The public key of the project
*/
/**
* Returns the private key of the project.
- *
+ *
* @return The private key of the project
*/
public String getPrivateKey() {
/**
* Sets the private key of the project.
- *
+ *
* @param privateKey
* The private key of the project
*/
/**
* Returns the base path of the project.
- *
+ *
* @return The base path of the project
*/
public String getBasePath() {
/**
* Sets the base path of the project.
- *
+ *
* @param basePath
* The base path of the project
*/
/**
* Returns the default file.
- *
+ *
* @return The default file
*/
public String getDefaultFile() {
/**
* Sets the default file.
- *
+ *
* @param defaultFile
* The default file
*/
/**
* Adds an override for the given file.
- *
+ *
* @param filePath
* The file path
* @param override
/**
* Removes the override for the given file.
- *
+ *
* @param filePath
* The file path for which to remove the override
*/
/**
* Returns the list of {@link Override}s.
- *
+ *
* @return All overrides
*/
public Map<String, Override> getOverrides() {
/**
* Scans the base path for files and returns the {@link ProjectFile} for the
* base path. From this file it is possible to reach all files in the base
- * path.
- *
- * This method is disk-intensive and may take some time on larger
+ * path. This method is disk-intensive and may take some time on larger
* directories!
- *
+ *
* @return The file for the base path
*/
public ProjectFile getBaseFile() {
+ ProjectFileImpl rootProjectFile = new ProjectFileImpl(null, "", true, false);
+ scanDirectory(new File(basePath), rootProjectFile);
+ return rootProjectFile;
+ }
+ //
+ // PRIVATE METHODS
+ //
+
+ /**
+ * Scans the given directory and recreates the file and directory structure
+ * in the given project file.
+ *
+ * @param directory
+ * The directory to scan
+ * @param projectFile
+ * The project file in which to recreate the directory and file
+ * structure
+ */
+ private void scanDirectory(File directory, ProjectFileImpl projectFile) {
+ if (!directory.isDirectory()) {
+ return;
+ }
+ for (File file: directory.listFiles()) {
+ ProjectFileImpl projectFileChild = projectFile.addFile(file.getName(), file.isDirectory(), file.isHidden());
+ if (file.isDirectory()) {
+ scanDirectory(file, projectFileChild);
+ }
+ }
+ projectFile.sort();
}
/**
* Implementation of a {@link ProjectFile}.
- *
+ *
* @author David ‘Bombe’ Roden <bombe@freenetproject.org>
*/
- private static class ProjectFileImpl implements ProjectFile {
+ private static class ProjectFileImpl implements ProjectFile, Comparable<ProjectFileImpl> {
+ /** The parent of this project file. */
private final ProjectFileImpl parentProjectFile;
+
+ /** The name of this project file. */
private final String name;
+
+ /** Whether this project file is a directory. */
private final boolean directory;
- private final boolean hidden;
- private List<ProjectFileImpl> childProjectFiles = new ArrayList<ProjectFileImpl>();
+ /** Whether this file is hidden. */
+ private final boolean hidden;
+ /** This project file’s children. */
+ private List<ProjectFileImpl> childProjectFiles = new ArrayList<ProjectFileImpl>();
- public ProjectFileImpl(ProjectFileImpl parentProjectFile, String name, boolean isDirectory, boolean isHidden) {
+ /**
+ * Creates a new project fie.
+ *
+ * @param parentProjectFile
+ * The parent of the project file, or <code>null</code> if
+ * the new project file does not have a parent
+ * @param name
+ * The name of the project file
+ * @param isDirectory
+ * <code>true</code> if this project file is a directory,
+ * <code>false</code> otherwise
+ * @param isHidden
+ * <code>true</code> if this project file is hidden,
+ * <code>false</code> otherwise
+ */
+ ProjectFileImpl(ProjectFileImpl parentProjectFile, String name, boolean isDirectory, boolean isHidden) {
this.parentProjectFile = parentProjectFile;
this.name = name;
this.directory = isDirectory;
* @see net.pterodactylus.jsite.project.ProjectFile#isFile()
*/
public boolean isFile() {
- // TODO Auto-generated method stub
- return false;
+ return !directory;
}
/**
* @see net.pterodactylus.jsite.project.ProjectFile#isDirectory()
*/
public boolean isDirectory() {
- // TODO Auto-generated method stub
- return false;
+ return directory;
}
/**
* @see net.pterodactylus.jsite.project.ProjectFile#isHidden()
*/
public boolean isHidden() {
- // TODO Auto-generated method stub
- return false;
+ return hidden;
}
/**
* @see net.pterodactylus.jsite.project.ProjectFile#getFiles()
*/
public List<ProjectFile> getFiles() {
- // TODO Auto-generated method stub
- return null;
+ List<ProjectFile> projectFiles = new ArrayList<ProjectFile>(childProjectFiles);
+ return projectFiles;
}
//
- // PRIVATE METHODS
+ // ACTIONS
//
- private ProjectFileImpl getParent() {
- return parentProjectFile;
+ /**
+ * Adds a new project file as child to this project file.
+ *
+ * @param name
+ * The name of the file
+ * @param isDirectory
+ * <code>true</code> if the new file is a directory,
+ * <code>false</code> otherwise
+ * @param isHidden
+ * <code>true</code> if the new file is hidden,
+ * <code>false</code> otherwise
+ * @return The created project file
+ */
+ public ProjectFileImpl addFile(String name, boolean isDirectory, boolean isHidden) {
+ ProjectFileImpl newProjectFile = new ProjectFileImpl(this, name, isDirectory, isHidden);
+ childProjectFiles.add(newProjectFile);
+ return newProjectFile;
+ }
+
+ /**
+ * Sorts the children of this file.
+ */
+ public void sort() {
+ Collections.sort(childProjectFiles);
}
+ //
+ // INTERFACE Comparable
+ //
+
+ /**
+ * {@inheritDoc}
+ */
+ public int compareTo(ProjectFileImpl otherProjectFileImpl) {
+ return name.compareTo(otherProjectFileImpl.name);
+ }
}
* 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.project;
import java.util.List;
* Abstraction for a that exists on the machine {@link Core} is being run on.
* This abstraction layer exists to make it possible to run jSite as a daemon
* and only connect to it via network.
- *
+ *
* @author David ‘Bombe’ Roden <bombe@freenetproject.org>
*/
public interface ProjectFile {
/**
* Returns the name of the file.
- *
+ *
* @return The name of the file
*/
public String getName();
/**
* Returns all parent files of this file. This file is the last file in the
* returned list.
- *
+ *
* @return A list of all parents of this file and this file itself
*/
public List<ProjectFile> getParents();
/**
* Returns whether this file is a directory.
- *
+ *
* @return <code>true</code> if this file is a directory,
* <code>false</code> otherwise
*/
/**
* Returns whether this file is a file (as opposed to being a directory).
- *
+ *
* @return <code>true</code> if this file is a file, <code>false</code>
* otherwise
*/
/**
* Returns whether this file is hidden.
- *
+ *
* @return <code>true</code> if this file is hidden
*/
public boolean isHidden();
/**
* If this file is a directory, returns all files in this directory.
- *
+ *
* @see #isDirectory()
* @return All files in this directory if this file is a directory, or
* <code>null</code> if this file is not a directory