dda7a5c39c9b25c2060451eed7cd2c8f2c2af495
[jSite2.git] / src / net / pterodactylus / jsite / project / Project.java
1 /*
2  * jSite2 - Project.java -
3  * Copyright © 2008 David Roden
4  *
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.
9  *
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.
14  *
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.
18  */
19
20 package net.pterodactylus.jsite.project;
21
22 import java.beans.PropertyChangeListener;
23 import java.util.ArrayList;
24 import java.util.HashMap;
25 import java.util.List;
26 import java.util.Map;
27
28 import net.pterodactylus.util.beans.AbstractBean;
29
30 /**
31  * Container for project information. A Project is capable of notifying
32  * {@link PropertyChangeListener}s if any of the contained properties change.
33  *
34  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
35  */
36 public class Project extends AbstractBean {
37
38         /** Name of the “name” property. */
39         public static final String PROPERTY_NAME = "name";
40
41         /** Name of the “description” property. */
42         public static final String PROPERTY_DESCRIPTION = "description";
43
44         /** Name of the “public key” property. */
45         public static final String PROPERTY_PUBLIC_KEY = "publicKey";
46
47         /** Name of the “private key” property. */
48         public static final String PROPERTY_PRIVATE_KEY = "privateKey";
49
50         /** Name of the “base path” property. */
51         public static final String PROPERTY_BASE_PATH = "basePath";
52
53         /** Name of the “default file” property. */
54         public static final String PROPERTY_DEFAULT_FILE = "defaultFile";
55
56         /** Internal ID. */
57         private String id;
58
59         /** The name of the project. */
60         private String name;
61
62         /** The description of the project. */
63         private String description;
64
65         /** The public key. */
66         private String publicKey;
67
68         /** The private key. */
69         private String privateKey;
70
71         /** The base path of the project. */
72         private String basePath;
73
74         /** The default file. */
75         private String defaultFile;
76
77         /** The overrides. */
78         private final Map<String, Override> overrides = new HashMap<String, Override>();
79
80         /**
81          * Creates a new project.
82          */
83         public Project() {
84                 /* do nothing. */
85         }
86
87         /**
88          * Clones the given project.
89          *
90          * @param project
91          */
92         Project(Project project) {
93                 this.name = project.name;
94                 this.description = project.description;
95                 this.publicKey = project.publicKey;
96                 this.privateKey = project.privateKey;
97                 this.basePath = project.basePath;
98         }
99
100         /**
101          * Returns the internal ID.
102          *
103          * @return The internal ID
104          */
105         String getId() {
106                 return id;
107         }
108
109         /**
110          * Sets the internal ID.
111          *
112          * @param id
113          *            The internal ID
114          */
115         void setId(String id) {
116                 this.id = id;
117         }
118
119         /**
120          * Returns the name of the project.
121          *
122          * @return The name of the project
123          */
124         public String getName() {
125                 return name;
126         }
127
128         /**
129          * Sets the name of the project.
130          *
131          * @param name
132          *            The name of the project
133          */
134         public void setName(String name) {
135                 String oldName = this.name;
136                 this.name = name;
137                 fireIfPropertyChanged(PROPERTY_NAME, oldName, name);
138         }
139
140         /**
141          * Returns the description of the project.
142          *
143          * @return The description of the project
144          */
145         public String getDescription() {
146                 return description;
147         }
148
149         /**
150          * Sets the description of the project
151          *
152          * @param description
153          *            The description of the project
154          */
155         public void setDescription(String description) {
156                 String oldDescription = this.description;
157                 this.description = description;
158                 fireIfPropertyChanged(PROPERTY_DESCRIPTION, oldDescription, description);
159         }
160
161         /**
162          * Returns the public key of the project.
163          *
164          * @return The public key of the project
165          */
166         public String getPublicKey() {
167                 return publicKey;
168         }
169
170         /**
171          * Sets the public key of the project.
172          *
173          * @param publicKey
174          *            The public key of the project
175          */
176         void setPublicKey(String publicKey) {
177                 String oldPublicKey = this.publicKey;
178                 this.publicKey = publicKey;
179                 fireIfPropertyChanged(PROPERTY_PUBLIC_KEY, oldPublicKey, publicKey);
180         }
181
182         /**
183          * Returns the private key of the project.
184          *
185          * @return The private key of the project
186          */
187         public String getPrivateKey() {
188                 return privateKey;
189         }
190
191         /**
192          * Sets the private key of the project.
193          *
194          * @param privateKey
195          *            The private key of the project
196          */
197         void setPrivateKey(String privateKey) {
198                 String oldPrivateKey = this.privateKey;
199                 this.privateKey = privateKey;
200                 fireIfPropertyChanged(PROPERTY_PRIVATE_KEY, oldPrivateKey, privateKey);
201         }
202
203         /**
204          * Returns the base path of the project.
205          *
206          * @return The base path of the project
207          */
208         public String getBasePath() {
209                 return basePath;
210         }
211
212         /**
213          * Sets the base path of the project.
214          *
215          * @param basePath
216          *            The base path of the project
217          */
218         public void setBasePath(String basePath) {
219                 String oldBasePath = this.basePath;
220                 this.basePath = basePath;
221                 fireIfPropertyChanged(PROPERTY_BASE_PATH, oldBasePath, basePath);
222         }
223
224         /**
225          * Returns the default file.
226          *
227          * @return The default file
228          */
229         public String getDefaultFile() {
230                 return defaultFile;
231         }
232
233         /**
234          * Sets the default file.
235          *
236          * @param defaultFile
237          *            The default file
238          */
239         public void setDefaultFile(String defaultFile) {
240                 String oldDefaultFile = this.defaultFile;
241                 this.defaultFile = defaultFile;
242                 fireIfPropertyChanged(PROPERTY_DEFAULT_FILE, oldDefaultFile, defaultFile);
243         }
244
245         /**
246          * Adds an override for the given file.
247          *
248          * @param filePath
249          *            The file path
250          * @param override
251          *            The override for the file
252          */
253         public void addOverride(String filePath, Override override) {
254                 overrides.put(filePath, override);
255         }
256
257         /**
258          * Removes the override for the given file.
259          *
260          * @param filePath
261          *            The file path for which to remove the override
262          */
263         public void removeOverride(String filePath) {
264                 overrides.remove(filePath);
265         }
266
267         /**
268          * Returns the list of {@link Override}s.
269          *
270          * @return All overrides
271          */
272         public Map<String, Override> getOverrides() {
273                 return overrides;
274         }
275
276         /**
277          * Scans the base path for files and returns the {@link ProjectFile} for the
278          * base path. From this file it is possible to reach all files in the base
279          * path.
280          *
281          * This method is disk-intensive and may take some time on larger
282          * directories!
283          *
284          * @return The file for the base path
285          */
286         public ProjectFile getBaseFile() {
287
288         }
289
290         /**
291          * Implementation of a {@link ProjectFile}.
292          *
293          * @author David ‘Bombe’ Roden &lt;bombe@freenetproject.org&gt;
294          */
295         private static class ProjectFileImpl implements ProjectFile {
296
297                 private final ProjectFileImpl parentProjectFile;
298                 private final String name;
299                 private final boolean directory;
300                 private final boolean hidden;
301                 private List<ProjectFileImpl> childProjectFiles = new ArrayList<ProjectFileImpl>();
302
303
304
305                 public ProjectFileImpl(ProjectFileImpl parentProjectFile, String name, boolean isDirectory, boolean isHidden) {
306                         this.parentProjectFile = parentProjectFile;
307                         this.name = name;
308                         this.directory = isDirectory;
309                         this.hidden = isHidden;
310                 }
311
312                 //
313                 // INTERFACE ProjectFile
314                 //
315
316                 /**
317                  * @see net.pterodactylus.jsite.project.ProjectFile#getName()
318                  */
319                 public String getName() {
320                         return name;
321                 }
322
323                 /**
324                  * @see net.pterodactylus.jsite.project.ProjectFile#getParents()
325                  */
326                 public List<ProjectFile> getParents() {
327                         List<ProjectFile> parentProjectFiles = new ArrayList<ProjectFile>();
328                         ProjectFileImpl currentProjectFile = this;
329                         do {
330                                 parentProjectFiles.add(0, currentProjectFile);
331                         } while ((currentProjectFile = currentProjectFile.parentProjectFile) != null);
332                         return parentProjectFiles;
333                 }
334
335                 /**
336                  * @see net.pterodactylus.jsite.project.ProjectFile#isFile()
337                  */
338                 public boolean isFile() {
339                         // TODO Auto-generated method stub
340                         return false;
341                 }
342
343                 /**
344                  * @see net.pterodactylus.jsite.project.ProjectFile#isDirectory()
345                  */
346                 public boolean isDirectory() {
347                         // TODO Auto-generated method stub
348                         return false;
349                 }
350
351                 /**
352                  * @see net.pterodactylus.jsite.project.ProjectFile#isHidden()
353                  */
354                 public boolean isHidden() {
355                         // TODO Auto-generated method stub
356                         return false;
357                 }
358
359                 /**
360                  * @see net.pterodactylus.jsite.project.ProjectFile#getFiles()
361                  */
362                 public List<ProjectFile> getFiles() {
363                         // TODO Auto-generated method stub
364                         return null;
365                 }
366
367                 //
368                 // PRIVATE METHODS
369                 //
370
371                 private ProjectFileImpl getParent() {
372                         return parentProjectFile;
373                 }
374
375
376         }
377
378 }