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.List;
27 import net.pterodactylus.util.beans.AbstractBean;
30 * Container for project information. A Project is capable of notifying
31 * {@link PropertyChangeListener}s if any of the contained properties change.
33 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
35 public class Project extends AbstractBean {
37 /** Name of the “name” property. */
38 public static final String PROPERTY_NAME = "name";
40 /** Name of the “description” property. */
41 public static final String PROPERTY_DESCRIPTION = "description";
43 /** Name of the “public key” property. */
44 public static final String PROPERTY_PUBLIC_KEY = "publicKey";
46 /** Name of the “private key” property. */
47 public static final String PROPERTY_PRIVATE_KEY = "privateKey";
49 /** Name of the “base path” property. */
50 public static final String PROPERTY_BASE_PATH = "basePath";
52 /** Name of the “base path entries” property. */
53 public static final String PROPERTY_BASE_PATH_ENTRIES = "basePathEntries";
58 /** The name of the project. */
61 /** The description of the project. */
62 private String description;
64 /** The public key. */
65 private String publicKey;
67 /** The private key. */
68 private String privateKey;
70 /** The base path of the project. */
71 private String basePath;
73 /** The list of files from the base path. */
74 private List<Entry> basePathEntries = new ArrayList<Entry>();
76 /** The list of virtual files. */
77 private List<Entry> virtualEntries = new ArrayList<Entry>();
80 * Creates a new project.
87 * Clones the given project.
91 Project(Project project) {
92 this.name = project.name;
93 this.description = project.description;
94 this.publicKey = project.publicKey;
95 this.privateKey = project.privateKey;
96 this.basePath = project.basePath;
97 this.basePathEntries.addAll(project.basePathEntries);
98 this.virtualEntries.addAll(project.virtualEntries);
102 * Returns the internal ID.
104 * @return The internal ID
111 * Sets the internal ID.
116 void setId(String id) {
121 * Returns the name of the project.
123 * @return The name of the project
125 public String getName() {
130 * Sets the name of the project.
133 * The name of the project
135 public void setName(String name) {
136 String oldName = this.name;
138 fireIfPropertyChanged(PROPERTY_NAME, oldName, name);
142 * Returns the description of the project.
144 * @return The description of the project
146 public String getDescription() {
151 * Sets the description of the project
154 * The description of the project
156 public void setDescription(String description) {
157 String oldDescription = this.description;
158 this.description = description;
159 fireIfPropertyChanged(PROPERTY_DESCRIPTION, oldDescription, description);
163 * Returns the public key of the project.
165 * @return The public key of the project
167 public String getPublicKey() {
172 * Sets the public key of the project.
175 * The public key of the project
177 void setPublicKey(String publicKey) {
178 String oldPublicKey = this.publicKey;
179 this.publicKey = publicKey;
180 fireIfPropertyChanged(PROPERTY_PUBLIC_KEY, oldPublicKey, publicKey);
184 * Returns the private key of the project.
186 * @return The private key of the project
188 public String getPrivateKey() {
193 * Sets the private key of the project.
196 * The private key of the project
198 void setPrivateKey(String privateKey) {
199 String oldPrivateKey = this.privateKey;
200 this.privateKey = privateKey;
201 fireIfPropertyChanged(PROPERTY_PRIVATE_KEY, oldPrivateKey, privateKey);
205 * Returns the base path of the project.
207 * @return The base path of the project
209 public String getBasePath() {
214 * Sets the base path of the project.
217 * The base path of the project
219 public void setBasePath(String basePath) {
220 String oldBasePath = this.basePath;
221 this.basePath = basePath;
222 fireIfPropertyChanged(PROPERTY_BASE_PATH, oldBasePath, basePath);
226 * Rescans the base path for new or changed files.
228 public void rescanBasePath() {
229 List<Entry> entries = new ArrayList<Entry>();
230 scanPath("", entries);
231 basePathEntries.clear();
232 basePathEntries.addAll(entries);
233 firePropertyChange(PROPERTY_BASE_PATH_ENTRIES, null, null);
237 * Returns the list of files from the base path.
239 * @return The list of files from the base path
241 public List<Entry> getBasePathEntries() {
242 return basePathEntries;
246 * Returns the list of visual entries.
248 * @return The visual entries
250 public List<Entry> getVirtualEntries() {
251 return virtualEntries;
255 * Adds a virtual entry that redirects to the given target.
258 * The name of the entry
260 * The content type of the entry, or <code>null</code> for
263 * The target URI of the redirect
265 public void addVirtualEntry(String name, String contentType, String target) {
266 RedirectEntry redirectEntry = new RedirectEntry();
267 redirectEntry.setName(name);
268 redirectEntry.setContentType(contentType);
269 redirectEntry.setTarget(target);
270 redirectEntry.setInsert(true);
278 * Scans the given path relative to {@link #basePath} for files and adds
279 * them to the given list of entries.
282 * The current path, relative to the base path
284 * The list of entries
286 private void scanPath(String currentPath, List<Entry> entries) {
287 File currentDirectory = new File(basePath + File.separatorChar + currentPath);
288 if (!currentDirectory.isDirectory()) {
291 for (File file: currentDirectory.listFiles()) {
292 String fileName = currentPath + file.getName();
293 if (file.isDirectory()) {
294 scanPath(fileName + File.separatorChar, entries);
297 PhysicalEntry entry = new PhysicalEntry();
298 entry.setName(fileName);
299 entry.setPath(file.getPath());
300 entry.setInsert(!file.isHidden());