2 * jSite2 - Project.java -
3 * Copyright © 2008 David Roden
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 package net.pterodactylus.jsite.project;
22 import java.beans.PropertyChangeListener;
24 import java.util.ArrayList;
25 import java.util.Collections;
26 import java.util.HashMap;
27 import java.util.List;
30 import net.pterodactylus.util.beans.AbstractBean;
33 * Container for project information. A Project is capable of notifying
34 * {@link PropertyChangeListener}s if any of the contained properties change.
36 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
38 public class Project extends AbstractBean {
40 /** Name of the “name” property. */
41 public static final String PROPERTY_NAME = "name";
43 /** Name of the “description” property. */
44 public static final String PROPERTY_DESCRIPTION = "description";
46 /** Name of the “public key” property. */
47 public static final String PROPERTY_PUBLIC_KEY = "publicKey";
49 /** Name of the “private key” property. */
50 public static final String PROPERTY_PRIVATE_KEY = "privateKey";
52 /** Name of the “base path” property. */
53 public static final String PROPERTY_BASE_PATH = "basePath";
55 /** Name of the “default file” property. */
56 public static final String PROPERTY_DEFAULT_FILE = "defaultFile";
61 /** The name of the project. */
64 /** The description of the project. */
65 private String description;
67 /** The public key. */
68 private String publicKey;
70 /** The private key. */
71 private String privateKey;
73 /** The base path of the project. */
74 private String basePath;
76 /** The default file. */
77 private String defaultFile;
80 private final Map<String, FileOverride> fileOverrides = new HashMap<String, FileOverride>();
83 * Creates a new project.
90 * Clones the given project.
94 Project(Project project) {
95 this.name = project.name;
96 this.description = project.description;
97 this.publicKey = project.publicKey;
98 this.privateKey = project.privateKey;
99 this.basePath = project.basePath;
103 * Returns the internal ID.
105 * @return The internal ID
112 * Sets the internal ID.
117 void setId(String id) {
122 * Returns the name of the project.
124 * @return The name of the project
126 public String getName() {
131 * Sets the name of the project.
134 * The name of the project
136 public void setName(String name) {
137 String oldName = this.name;
139 fireIfPropertyChanged(PROPERTY_NAME, oldName, name);
143 * Returns the description of the project.
145 * @return The description of the project
147 public String getDescription() {
152 * Sets the description of the project
155 * The description of the project
157 public void setDescription(String description) {
158 String oldDescription = this.description;
159 this.description = description;
160 fireIfPropertyChanged(PROPERTY_DESCRIPTION, oldDescription, description);
164 * Returns the public key of the project.
166 * @return The public key of the project
168 public String getPublicKey() {
173 * Sets the public key of the project.
176 * The public key of the project
178 void setPublicKey(String publicKey) {
179 String oldPublicKey = this.publicKey;
180 this.publicKey = publicKey;
181 fireIfPropertyChanged(PROPERTY_PUBLIC_KEY, oldPublicKey, publicKey);
185 * Returns the private key of the project.
187 * @return The private key of the project
189 public String getPrivateKey() {
194 * Sets the private key of the project.
197 * The private key of the project
199 void setPrivateKey(String privateKey) {
200 String oldPrivateKey = this.privateKey;
201 this.privateKey = privateKey;
202 fireIfPropertyChanged(PROPERTY_PRIVATE_KEY, oldPrivateKey, privateKey);
206 * Returns the base path of the project.
208 * @return The base path of the project
210 public String getBasePath() {
215 * Sets the base path of the project.
218 * The base path of the project
220 public void setBasePath(String basePath) {
221 String oldBasePath = this.basePath;
222 this.basePath = basePath;
223 fireIfPropertyChanged(PROPERTY_BASE_PATH, oldBasePath, basePath);
227 * Returns the default file.
229 * @return The default file
231 public String getDefaultFile() {
236 * Sets the default file.
241 public void setDefaultFile(String defaultFile) {
242 String oldDefaultFile = this.defaultFile;
243 this.defaultFile = defaultFile;
244 fireIfPropertyChanged(PROPERTY_DEFAULT_FILE, oldDefaultFile, defaultFile);
248 * Adds a file override for the given file.
253 * The override for the file
255 public void addFileOverride(String filePath, FileOverride override) {
256 fileOverrides.put(filePath, override);
260 * Removes the file override for the given file.
263 * The file path for which to remove the override
265 public void removeFileOverride(String filePath) {
266 fileOverrides.remove(filePath);
270 * Returns the list of {@link FileOverride}s.
272 * @return All file overrides
274 public Map<String, FileOverride> getFileOverrides() {
275 return fileOverrides;
279 * Scans the base path for files and returns the {@link ProjectFile} for the
280 * base path. From this file it is possible to reach all files in the base
281 * path. This method is disk-intensive and may take some time on larger
284 * @return The file for the base path, or <code>null</code> if the base
285 * path does not denote an existing directory
287 public ProjectFile getBaseFile() {
288 File basePathFile = new File(basePath);
289 if (!basePathFile.exists() || !basePathFile.isDirectory()) {
292 ProjectFileImpl rootProjectFile = new ProjectFileImpl(null, "", 0, true, false);
293 scanDirectory(basePathFile, rootProjectFile);
294 return rootProjectFile;
302 * Scans the given directory and recreates the file and directory structure
303 * in the given project file.
306 * The directory to scan
308 * The project file in which to recreate the directory and file
311 private void scanDirectory(File directory, ProjectFileImpl projectFile) {
312 if (!directory.isDirectory()) {
315 for (File file : directory.listFiles()) {
316 ProjectFileImpl projectFileChild = projectFile.addFile(file.getName(), file.length(), file.isDirectory(), file.isHidden());
317 if (file.isDirectory()) {
318 scanDirectory(file, projectFileChild);
325 * Implementation of a {@link ProjectFile}.
327 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
329 private static class ProjectFileImpl implements ProjectFile, Comparable<ProjectFileImpl> {
331 /** The parent of this project file. */
332 private final ProjectFileImpl parentProjectFile;
334 /** The name of this project file. */
335 private final String name;
337 /** The size of the file. */
338 private final long size;
340 /** Whether this project file is a directory. */
341 private final boolean directory;
343 /** Whether this file is hidden. */
344 private final boolean hidden;
346 /** This project file’s children. */
347 private List<ProjectFileImpl> childProjectFiles = new ArrayList<ProjectFileImpl>();
350 * Creates a new project fie.
352 * @param parentProjectFile
353 * The parent of the project file, or <code>null</code> if
354 * the new project file does not have a parent
356 * The name of the project file
358 * The size of the file
360 * <code>true</code> if this project file is a directory,
361 * <code>false</code> otherwise
363 * <code>true</code> if this project file is hidden,
364 * <code>false</code> otherwise
366 ProjectFileImpl(ProjectFileImpl parentProjectFile, String name, long size, boolean isDirectory, boolean isHidden) {
367 this.parentProjectFile = parentProjectFile;
370 this.directory = isDirectory;
371 this.hidden = isHidden;
375 // INTERFACE ProjectFile
379 * @see net.pterodactylus.jsite.project.ProjectFile#getName()
381 public String getName() {
386 * @see net.pterodactylus.jsite.project.ProjectFile#getParent()
388 public ProjectFile getParent() {
389 return parentProjectFile;
395 public long getSize() {
400 * @see net.pterodactylus.jsite.project.ProjectFile#getParents()
402 public List<ProjectFile> getParents() {
403 List<ProjectFile> parentProjectFiles = new ArrayList<ProjectFile>();
404 ProjectFileImpl currentProjectFile = this;
406 parentProjectFiles.add(0, currentProjectFile);
407 } while ((currentProjectFile = currentProjectFile.parentProjectFile) != null);
408 return parentProjectFiles;
414 /* TODO - caching? */
415 public String getCompletePath() {
416 StringBuilder completePath = new StringBuilder();
417 ProjectFileImpl currentProjectFile = this;
419 completePath.insert(0, File.separatorChar).insert(0, this.getName());
420 } while ((currentProjectFile = currentProjectFile.parentProjectFile) != null);
421 return completePath.substring(1);
425 * @see net.pterodactylus.jsite.project.ProjectFile#isFile()
427 public boolean isFile() {
432 * @see net.pterodactylus.jsite.project.ProjectFile#isDirectory()
434 public boolean isDirectory() {
439 * @see net.pterodactylus.jsite.project.ProjectFile#isHidden()
441 public boolean isHidden() {
446 * @see net.pterodactylus.jsite.project.ProjectFile#getFiles()
448 public List<ProjectFile> getFiles() {
449 List<ProjectFile> projectFiles = new ArrayList<ProjectFile>(childProjectFiles);
458 * Adds a new project file as child to this project file.
461 * The name of the file
463 * The size of the file
465 * <code>true</code> if the new file is a directory,
466 * <code>false</code> otherwise
468 * <code>true</code> if the new file is hidden,
469 * <code>false</code> otherwise
470 * @return The created project file
472 public ProjectFileImpl addFile(String name, long size, boolean isDirectory, boolean isHidden) {
473 ProjectFileImpl newProjectFile = new ProjectFileImpl(this, name, size, isDirectory, isHidden);
474 childProjectFiles.add(newProjectFile);
475 return newProjectFile;
479 * Sorts the children of this file.
482 Collections.sort(childProjectFiles);
486 // INTERFACE Comparable
492 public int compareTo(ProjectFileImpl otherProjectFileImpl) {
493 return name.compareTo(otherProjectFileImpl.name);