add size
[jSite2.git] / src / net / pterodactylus / jsite / project / Project.java
index f6314cb..265eeff 100644 (file)
@@ -281,11 +281,16 @@ public class Project extends AbstractBean {
         * path. This method is disk-intensive and may take some time on larger
         * directories!
         * 
-        * @return The file for the base path
+        * @return The file for the base path, or <code>null</code> if the base
+        *         path does not denote an existing directory
         */
        public ProjectFile getBaseFile() {
-               ProjectFileImpl rootProjectFile = new ProjectFileImpl(null, "", true, false);
-               scanDirectory(new File(basePath), rootProjectFile);
+               File basePathFile = new File(basePath);
+               if (!basePathFile.exists() || !basePathFile.isDirectory()) {
+                       return null;
+               }
+               ProjectFileImpl rootProjectFile = new ProjectFileImpl(null, "", 0, true, false);
+               scanDirectory(basePathFile, rootProjectFile);
                return rootProjectFile;
        }
 
@@ -308,7 +313,7 @@ public class Project extends AbstractBean {
                        return;
                }
                for (File file: directory.listFiles()) {
-                       ProjectFileImpl projectFileChild = projectFile.addFile(file.getName(), file.isDirectory(), file.isHidden());
+                       ProjectFileImpl projectFileChild = projectFile.addFile(file.getName(), file.length(), file.isDirectory(), file.isHidden());
                        if (file.isDirectory()) {
                                scanDirectory(file, projectFileChild);
                        }
@@ -329,6 +334,9 @@ public class Project extends AbstractBean {
                /** The name of this project file. */
                private final String name;
 
+               /** The size of the file. */
+               private final long size;
+
                /** Whether this project file is a directory. */
                private final boolean directory;
 
@@ -346,6 +354,8 @@ public class Project extends AbstractBean {
                 *            the new project file does not have a parent
                 * @param name
                 *            The name of the project file
+                * @param size
+                *            The size of the file
                 * @param isDirectory
                 *            <code>true</code> if this project file is a directory,
                 *            <code>false</code> otherwise
@@ -353,9 +363,10 @@ public class Project extends AbstractBean {
                 *            <code>true</code> if this project file is hidden,
                 *            <code>false</code> otherwise
                 */
-               ProjectFileImpl(ProjectFileImpl parentProjectFile, String name, boolean isDirectory, boolean isHidden) {
+               ProjectFileImpl(ProjectFileImpl parentProjectFile, String name, long size, boolean isDirectory, boolean isHidden) {
                        this.parentProjectFile = parentProjectFile;
                        this.name = name;
+                       this.size = size;
                        this.directory = isDirectory;
                        this.hidden = isHidden;
                }
@@ -372,6 +383,13 @@ public class Project extends AbstractBean {
                }
 
                /**
+                * {@inheritDoc}
+                */
+               public long getSize() {
+                       return size;
+               }
+
+               /**
                 * @see net.pterodactylus.jsite.project.ProjectFile#getParents()
                 */
                public List<ProjectFile> getParents() {
@@ -384,6 +402,19 @@ public class Project extends AbstractBean {
                }
 
                /**
+                * {@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() {
@@ -421,6 +452,8 @@ public class Project extends AbstractBean {
                 * 
                 * @param name
                 *            The name of the file
+                * @param size
+                *            The size of the file
                 * @param isDirectory
                 *            <code>true</code> if the new file is a directory,
                 *            <code>false</code> otherwise
@@ -429,8 +462,8 @@ public class Project extends AbstractBean {
                 *            <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);
+               public ProjectFileImpl addFile(String name, long size, boolean isDirectory, boolean isHidden) {
+                       ProjectFileImpl newProjectFile = new ProjectFileImpl(this, name, size, isDirectory, isHidden);
                        childProjectFiles.add(newProjectFile);
                        return newProjectFile;
                }