add default insert flag
[jSite2.git] / src / net / pterodactylus / jsite / project / Project.java
index 2c6609f..5c58763 100644 (file)
@@ -20,6 +20,9 @@
 package net.pterodactylus.jsite.project;
 
 import java.beans.PropertyChangeListener;
+import java.io.File;
+import java.util.ArrayList;
+import java.util.List;
 
 import net.pterodactylus.util.beans.AbstractBean;
 
@@ -28,7 +31,6 @@ import net.pterodactylus.util.beans.AbstractBean;
  * {@link PropertyChangeListener}s if any of the contained properties change.
  * 
  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
- * @version $Id$
  */
 public class Project extends AbstractBean {
 
@@ -44,8 +46,14 @@ public class Project extends AbstractBean {
        /** Name of the “private key” property. */
        public static final String PROPERTY_PRIVATE_KEY = "privateKey";
 
-       /** Name of the “local path” property. */
-       public static final String PROPERTY_LOCAL_PATH = "localPath";
+       /** 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";
+
+       /** Internal ID. */
+       private String id;
 
        /** The name of the project. */
        private String name;
@@ -62,9 +70,52 @@ public class Project extends AbstractBean {
        /** The base path of the project. */
        private String basePath;
 
-       //
-       // EVENT MANAGEMENT
-       //
+       /** The list of files from the base path. */
+       private List<Entry> basePathEntries = new ArrayList<Entry>();
+
+       /** 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;
+               this.basePathEntries.addAll(project.basePathEntries);
+               this.virtualEntries.addAll(project.virtualEntries);
+       }
+
+       /**
+        * Returns the internal ID.
+        * 
+        * @return The internal ID
+        */
+       String getId() {
+               return id;
+       }
+
+       /**
+        * Sets the internal ID.
+        * 
+        * @param id
+        *            The internal ID
+        */
+       void setId(String id) {
+               this.id = id;
+       }
 
        /**
         * Returns the name of the project.
@@ -84,9 +135,7 @@ public class Project extends AbstractBean {
        public void setName(String name) {
                String oldName = this.name;
                this.name = name;
-               if (!equal(oldName, name)) {
-                       firePropertyChange(PROPERTY_NAME, oldName, name);
-               }
+               fireIfPropertyChanged(PROPERTY_NAME, oldName, name);
        }
 
        /**
@@ -107,9 +156,7 @@ public class Project extends AbstractBean {
        public void setDescription(String description) {
                String oldDescription = this.description;
                this.description = description;
-               if (!equal(oldDescription, description)) {
-                       firePropertyChange(PROPERTY_DESCRIPTION, oldDescription, description);
-               }
+               fireIfPropertyChanged(PROPERTY_DESCRIPTION, oldDescription, description);
        }
 
        /**
@@ -127,12 +174,10 @@ public class Project extends AbstractBean {
         * @param publicKey
         *            The public key of the project
         */
-       public void setPublicKey(String publicKey) {
+       void setPublicKey(String publicKey) {
                String oldPublicKey = this.publicKey;
                this.publicKey = publicKey;
-               if (!equal(oldPublicKey, publicKey)) {
-                       firePropertyChange(PROPERTY_PUBLIC_KEY, oldPublicKey, publicKey);
-               }
+               fireIfPropertyChanged(PROPERTY_PUBLIC_KEY, oldPublicKey, publicKey);
        }
 
        /**
@@ -150,12 +195,10 @@ public class Project extends AbstractBean {
         * @param privateKey
         *            The private key of the project
         */
-       public void setPrivateKey(String privateKey) {
+       void setPrivateKey(String privateKey) {
                String oldPrivateKey = this.privateKey;
                this.privateKey = privateKey;
-               if (!equal(oldPrivateKey, privateKey)) {
-                       firePropertyChange(PROPERTY_PRIVATE_KEY, oldPrivateKey, privateKey);
-               }
+               fireIfPropertyChanged(PROPERTY_PRIVATE_KEY, oldPrivateKey, privateKey);
        }
 
        /**
@@ -176,7 +219,88 @@ public class Project extends AbstractBean {
        public void setBasePath(String basePath) {
                String oldBasePath = this.basePath;
                this.basePath = basePath;
-               firePropertyChange(PROPERTY_LOCAL_PATH, oldBasePath, basePath);
+               fireIfPropertyChanged(PROPERTY_BASE_PATH, oldBasePath, basePath);
+       }
+
+       /**
+        * Rescans the base path for new or changed files.
+        */
+       public void rescanBasePath() {
+               List<Entry> entries = new ArrayList<Entry>();
+               scanPath("", entries);
+               basePathEntries.clear();
+               basePathEntries.addAll(entries);
+               firePropertyChange(PROPERTY_BASE_PATH_ENTRIES, null, null);
+       }
+
+       /**
+        * Returns the list of files from the base path.
+        * 
+        * @return The list of files from the base path
+        */
+       public List<Entry> getBasePathEntries() {
+               return basePathEntries;
+       }
+
+       /**
+        * Returns the list of visual entries.
+        * 
+        * @return The visual entries
+        */
+       public List<Entry> getVirtualEntries() {
+               return virtualEntries;
+       }
+
+       /**
+        * 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
+        */
+       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);
+       }
+
+       //
+       // 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
+        */
+       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.setDefaultInsert(!file.isHidden());
+                       entry.setInsert(!file.isHidden());
+                       entries.add(entry);
+               }
        }
 
 }