add default file
[jSite2.git] / src / net / pterodactylus / jsite / project / Project.java
index ab0b363..e6f732b 100644 (file)
 package net.pterodactylus.jsite.project;
 
 import java.beans.PropertyChangeListener;
-import java.io.File;
-import java.util.ArrayList;
-import java.util.List;
+import java.util.HashMap;
+import java.util.Map;
 
 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>
- * @version $Id$
  */
 public class Project extends AbstractBean {
 
@@ -50,8 +48,8 @@ public class Project extends AbstractBean {
        /** Name of the “base path” property. */
        public static final String PROPERTY_BASE_PATH = "basePath";
 
-       /** Name of the “base path entries” property. */
-       public static final String PROPERTY_BASE_PATH_ENTRIES = "basePathEntries";
+       /** Name of the “default file” property. */
+       public static final String PROPERTY_DEFAULT_FILE = "defaultFile";
 
        /** Internal ID. */
        private String id;
@@ -71,15 +69,35 @@ public class Project extends AbstractBean {
        /** The base path of the project. */
        private String basePath;
 
-       /** The list of files from the base path. */
-       private List<Entry> basePathEntries = new ArrayList<Entry>();
+       /** The default file. */
+       private String defaultFile;
+
+       /** The overrides. */
+       private final Map<String, Override> overrides = new HashMap<String, Override>();
 
-       /** The list of virtual files. */
-       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;
+       }
 
        /**
         * Returns the internal ID.
-        * 
+        *
         * @return The internal ID
         */
        String getId() {
@@ -88,7 +106,7 @@ public class Project extends AbstractBean {
 
        /**
         * Sets the internal ID.
-        * 
+        *
         * @param id
         *            The internal ID
         */
@@ -98,7 +116,7 @@ public class Project extends AbstractBean {
 
        /**
         * Returns the name of the project.
-        * 
+        *
         * @return The name of the project
         */
        public String getName() {
@@ -107,7 +125,7 @@ public class Project extends AbstractBean {
 
        /**
         * Sets the name of the project.
-        * 
+        *
         * @param name
         *            The name of the project
         */
@@ -119,7 +137,7 @@ public class Project extends AbstractBean {
 
        /**
         * Returns the description of the project.
-        * 
+        *
         * @return The description of the project
         */
        public String getDescription() {
@@ -128,7 +146,7 @@ public class Project extends AbstractBean {
 
        /**
         * Sets the description of the project
-        * 
+        *
         * @param description
         *            The description of the project
         */
@@ -140,7 +158,7 @@ public class Project extends AbstractBean {
 
        /**
         * Returns the public key of the project.
-        * 
+        *
         * @return The public key of the project
         */
        public String getPublicKey() {
@@ -149,7 +167,7 @@ public class Project extends AbstractBean {
 
        /**
         * Sets the public key of the project.
-        * 
+        *
         * @param publicKey
         *            The public key of the project
         */
@@ -161,7 +179,7 @@ public class Project extends AbstractBean {
 
        /**
         * Returns the private key of the project.
-        * 
+        *
         * @return The private key of the project
         */
        public String getPrivateKey() {
@@ -170,7 +188,7 @@ public class Project extends AbstractBean {
 
        /**
         * Sets the private key of the project.
-        * 
+        *
         * @param privateKey
         *            The private key of the project
         */
@@ -182,7 +200,7 @@ public class Project extends AbstractBean {
 
        /**
         * Returns the base path of the project.
-        * 
+        *
         * @return The base path of the project
         */
        public String getBasePath() {
@@ -191,7 +209,7 @@ public class Project extends AbstractBean {
 
        /**
         * Sets the base path of the project.
-        * 
+        *
         * @param basePath
         *            The base path of the project
         */
@@ -202,83 +220,55 @@ public class Project extends AbstractBean {
        }
 
        /**
-        * Rescans the base path for new or changed files.
+        * Returns the default file.
+        *
+        * @return The default file
         */
-       public void rescanBasePath() {
-               List<Entry> entries = new ArrayList<Entry>();
-               scanPath("", entries);
-               basePathEntries.clear();
-               basePathEntries.addAll(entries);
-               firePropertyChange(PROPERTY_BASE_PATH_ENTRIES, null, null);
+       public String getDefaultFile() {
+               return defaultFile;
        }
 
        /**
-        * Returns the list of files from the base path.
-        * 
-        * @return The list of files from the base path
+        * Sets the default file.
+        *
+        * @param defaultFile
+        *            The default file
         */
-       public List<Entry> getBasePathEntries() {
-               return basePathEntries;
+       public void setDefaultFile(String defaultFile) {
+               String oldDefaultFile = this.defaultFile;
+               this.defaultFile = defaultFile;
+               fireIfPropertyChanged(PROPERTY_DEFAULT_FILE, oldDefaultFile, defaultFile);
        }
 
        /**
-        * Returns the list of visual entries.
-        * 
-        * @return The visual entries
+        * Adds an override for the given file.
+        *
+        * @param filePath
+        *            The file path
+        * @param override
+        *            The override for the file
         */
-       public List<Entry> getVirtualEntries() {
-               return virtualEntries;
+       public void addOverride(String filePath, Override override) {
+               overrides.put(filePath, override);
        }
 
        /**
-        * Adds a virtual entry that redirects to the given target.
-        * 
-        * @param name
-        *            The name of the entry
-        * @param contentType
-        *            The content type of the entry, or <code>null</code> for
-        *            auto-detection
-        * @param target
-        *            The target URI of the redirect
+        * Removes the override for the given file.
+        *
+        * @param filePath
+        *            The file path for which to remove the override
         */
-       public void addVirtualEntry(String name, String contentType, String target) {
-               RedirectEntry redirectEntry = new RedirectEntry();
-               redirectEntry.setName(name);
-               redirectEntry.setContentType(contentType);
-               redirectEntry.setTarget(target);
-               redirectEntry.setInsert(true);
+       public void removeOverride(String filePath) {
+               overrides.remove(filePath);
        }
 
-       //
-       // PRIVATE METHODS
-       //
-
        /**
-        * Scans the given path relative to {@link #basePath} for files and adds
-        * them to the given list of entries.
-        * 
-        * @param currentPath
-        *            The current path, relative to the base path
-        * @param entries
-        *            The list of entries
+        * Returns the list of {@link Override}s.
+        *
+        * @return All overrides
         */
-       private void scanPath(String currentPath, List<Entry> entries) {
-               File currentDirectory = new File(basePath + File.separatorChar + currentPath);
-               if (!currentDirectory.isDirectory()) {
-                       return;
-               }
-               for (File file: currentDirectory.listFiles()) {
-                       String fileName = currentPath + file.getName();
-                       if (file.isDirectory()) {
-                               scanPath(fileName + File.separatorChar, entries);
-                               continue;
-                       }
-                       PhysicalEntry entry = new PhysicalEntry();
-                       entry.setName(fileName);
-                       entry.setPath(file.getPath());
-                       entry.setInsert(!file.isHidden());
-                       entries.add(entry);
-               }
+       public Map<String, Override> getOverrides() {
+               return overrides;
        }
 
 }