add method to return complete path of a project file
[jSite2.git] / src / net / pterodactylus / jsite / project / Project.java
index b80c293..7a6ee10 100644 (file)
 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;
 
 import net.pterodactylus.util.beans.AbstractBean;
@@ -28,7 +32,7 @@ import net.pterodactylus.util.beans.AbstractBean;
 /**
  * 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 {
@@ -48,6 +52,9 @@ public class Project extends AbstractBean {
        /** Name of the “base path” property. */
        public static final String PROPERTY_BASE_PATH = "basePath";
 
+       /** Name of the “default file” property. */
+       public static final String PROPERTY_DEFAULT_FILE = "defaultFile";
+
        /** Internal ID. */
        private String id;
 
@@ -66,6 +73,9 @@ public class Project extends AbstractBean {
        /** The base path of the project. */
        private String basePath;
 
+       /** The default file. */
+       private String defaultFile;
+
        /** The overrides. */
        private final Map<String, Override> overrides = new HashMap<String, Override>();
 
@@ -78,7 +88,7 @@ public class Project extends AbstractBean {
 
        /**
         * Clones the given project.
-        *
+        * 
         * @param project
         */
        Project(Project project) {
@@ -91,7 +101,7 @@ public class Project extends AbstractBean {
 
        /**
         * Returns the internal ID.
-        *
+        * 
         * @return The internal ID
         */
        String getId() {
@@ -100,7 +110,7 @@ public class Project extends AbstractBean {
 
        /**
         * Sets the internal ID.
-        *
+        * 
         * @param id
         *            The internal ID
         */
@@ -110,7 +120,7 @@ public class Project extends AbstractBean {
 
        /**
         * Returns the name of the project.
-        *
+        * 
         * @return The name of the project
         */
        public String getName() {
@@ -119,7 +129,7 @@ public class Project extends AbstractBean {
 
        /**
         * Sets the name of the project.
-        *
+        * 
         * @param name
         *            The name of the project
         */
@@ -131,7 +141,7 @@ public class Project extends AbstractBean {
 
        /**
         * Returns the description of the project.
-        *
+        * 
         * @return The description of the project
         */
        public String getDescription() {
@@ -140,7 +150,7 @@ public class Project extends AbstractBean {
 
        /**
         * Sets the description of the project
-        *
+        * 
         * @param description
         *            The description of the project
         */
@@ -152,7 +162,7 @@ public class Project extends AbstractBean {
 
        /**
         * Returns the public key of the project.
-        *
+        * 
         * @return The public key of the project
         */
        public String getPublicKey() {
@@ -161,7 +171,7 @@ public class Project extends AbstractBean {
 
        /**
         * Sets the public key of the project.
-        *
+        * 
         * @param publicKey
         *            The public key of the project
         */
@@ -173,7 +183,7 @@ public class Project extends AbstractBean {
 
        /**
         * Returns the private key of the project.
-        *
+        * 
         * @return The private key of the project
         */
        public String getPrivateKey() {
@@ -182,7 +192,7 @@ public class Project extends AbstractBean {
 
        /**
         * Sets the private key of the project.
-        *
+        * 
         * @param privateKey
         *            The private key of the project
         */
@@ -194,7 +204,7 @@ public class Project extends AbstractBean {
 
        /**
         * Returns the base path of the project.
-        *
+        * 
         * @return The base path of the project
         */
        public String getBasePath() {
@@ -203,7 +213,7 @@ public class Project extends AbstractBean {
 
        /**
         * Sets the base path of the project.
-        *
+        * 
         * @param basePath
         *            The base path of the project
         */
@@ -214,8 +224,29 @@ public class Project extends AbstractBean {
        }
 
        /**
+        * Returns the default file.
+        * 
+        * @return The default file
+        */
+       public String getDefaultFile() {
+               return defaultFile;
+       }
+
+       /**
+        * Sets the default file.
+        * 
+        * @param defaultFile
+        *            The default file
+        */
+       public void setDefaultFile(String defaultFile) {
+               String oldDefaultFile = this.defaultFile;
+               this.defaultFile = defaultFile;
+               fireIfPropertyChanged(PROPERTY_DEFAULT_FILE, oldDefaultFile, defaultFile);
+       }
+
+       /**
         * Adds an override for the given file.
-        *
+        * 
         * @param filePath
         *            The file path
         * @param override
@@ -227,7 +258,7 @@ public class Project extends AbstractBean {
 
        /**
         * Removes the override for the given file.
-        *
+        * 
         * @param filePath
         *            The file path for which to remove the override
         */
@@ -237,11 +268,209 @@ public class Project extends AbstractBean {
 
        /**
         * Returns the list of {@link Override}s.
-        *
+        * 
         * @return All overrides
         */
        public Map<String, Override> getOverrides() {
                return overrides;
        }
 
+       /**
+        * 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
+        * directories!
+        * 
+        * @return The file for the base path, or <code>null</code> if the base
+        *         path does not denote an existing directory
+        */
+       public ProjectFile getBaseFile() {
+               File basePathFile = new File(basePath);
+               if (!basePathFile.exists() || !basePathFile.isDirectory()) {
+                       return null;
+               }
+               ProjectFileImpl rootProjectFile = new ProjectFileImpl(null, "", true, false);
+               scanDirectory(basePathFile, 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 &lt;bombe@freenetproject.org&gt;
+        */
+       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;
+
+               /** Whether this file is hidden. */
+               private final boolean hidden;
+
+               /** This project file’s children. */
+               private List<ProjectFileImpl> childProjectFiles = new ArrayList<ProjectFileImpl>();
+
+               /**
+                * 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;
+                       this.hidden = isHidden;
+               }
+
+               //
+               // INTERFACE ProjectFile
+               //
+
+               /**
+                * @see net.pterodactylus.jsite.project.ProjectFile#getName()
+                */
+               public String getName() {
+                       return name;
+               }
+
+               /**
+                * @see net.pterodactylus.jsite.project.ProjectFile#getParents()
+                */
+               public List<ProjectFile> getParents() {
+                       List<ProjectFile> parentProjectFiles = new ArrayList<ProjectFile>();
+                       ProjectFileImpl currentProjectFile = this;
+                       do {
+                               parentProjectFiles.add(0, currentProjectFile);
+                       } while ((currentProjectFile = currentProjectFile.parentProjectFile) != null);
+                       return parentProjectFiles;
+               }
+
+               /**
+                * {@inheritDoc}
+                */
+               /* TODO - caching? */
+               public String getCompletePath() {
+                       StringBuilder completePath = new StringBuilder();
+                       ProjectFileImpl currentProjectFile = this;
+                       do {
+                               completePath.insert(0, File.separatorChar).insert(0, this.getName());
+                       } while ((currentProjectFile = currentProjectFile.parentProjectFile) != null);
+                       return completePath.substring(1);
+               }
+
+               /**
+                * @see net.pterodactylus.jsite.project.ProjectFile#isFile()
+                */
+               public boolean isFile() {
+                       return !directory;
+               }
+
+               /**
+                * @see net.pterodactylus.jsite.project.ProjectFile#isDirectory()
+                */
+               public boolean isDirectory() {
+                       return directory;
+               }
+
+               /**
+                * @see net.pterodactylus.jsite.project.ProjectFile#isHidden()
+                */
+               public boolean isHidden() {
+                       return hidden;
+               }
+
+               /**
+                * @see net.pterodactylus.jsite.project.ProjectFile#getFiles()
+                */
+               public List<ProjectFile> getFiles() {
+                       List<ProjectFile> projectFiles = new ArrayList<ProjectFile>(childProjectFiles);
+                       return projectFiles;
+               }
+
+               //
+               // ACTIONS
+               //
+
+               /**
+                * 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);
+               }
+
+       }
+
 }