65108053757d1bcb85816e886ad873ba98734502
[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
24 import net.pterodactylus.util.beans.AbstractBean;
25
26 /**
27  * Container for project information. A Project is capable of notifying
28  * {@link PropertyChangeListener}s if any of the contained properties change.
29  * 
30  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
31  * @version $Id$
32  */
33 public class Project extends AbstractBean {
34
35         /** Name of the “name” property. */
36         public static final String PROPERTY_NAME = "name";
37
38         /** Name of the “description” property. */
39         public static final String PROPERTY_DESCRIPTION = "description";
40
41         /** Name of the “public key” property. */
42         public static final String PROPERTY_PUBLIC_KEY = "publicKey";
43
44         /** Name of the “private key” property. */
45         public static final String PROPERTY_PRIVATE_KEY = "privateKey";
46
47         /** Name of the “local path” property. */
48         public static final String PROPERTY_BASE_PATH = "basePath";
49
50         /** Internal ID. */
51         private String id;
52
53         /** The name of the project. */
54         private String name;
55
56         /** The description of the project. */
57         private String description;
58
59         /** The public key. */
60         private String publicKey;
61
62         /** The private key. */
63         private String privateKey;
64
65         /** The base path of the project. */
66         private String basePath;
67
68         /** The list of files from the base path. */
69         private List<Entry> basePathEntries = new ArrayList<Entry>();
70
71         /**
72          * Returns the internal ID.
73          * 
74          * @return The internal ID
75          */
76         String getId() {
77                 return id;
78         }
79
80         /**
81          * Sets the internal ID.
82          * 
83          * @param id
84          *            The internal ID
85          */
86         void setId(String id) {
87                 this.id = id;
88         }
89
90         /**
91          * Returns the name of the project.
92          * 
93          * @return The name of the project
94          */
95         public String getName() {
96                 return name;
97         }
98
99         /**
100          * Sets the name of the project.
101          * 
102          * @param name
103          *            The name of the project
104          */
105         public void setName(String name) {
106                 String oldName = this.name;
107                 this.name = name;
108                 fireIfPropertyChanged(PROPERTY_NAME, oldName, name);
109         }
110
111         /**
112          * Returns the description of the project.
113          * 
114          * @return The description of the project
115          */
116         public String getDescription() {
117                 return description;
118         }
119
120         /**
121          * Sets the description of the project
122          * 
123          * @param description
124          *            The description of the project
125          */
126         public void setDescription(String description) {
127                 String oldDescription = this.description;
128                 this.description = description;
129                 fireIfPropertyChanged(PROPERTY_DESCRIPTION, oldDescription, description);
130         }
131
132         /**
133          * Returns the public key of the project.
134          * 
135          * @return The public key of the project
136          */
137         public String getPublicKey() {
138                 return publicKey;
139         }
140
141         /**
142          * Sets the public key of the project.
143          * 
144          * @param publicKey
145          *            The public key of the project
146          */
147         void setPublicKey(String publicKey) {
148                 String oldPublicKey = this.publicKey;
149                 this.publicKey = publicKey;
150                 fireIfPropertyChanged(PROPERTY_PUBLIC_KEY, oldPublicKey, publicKey);
151         }
152
153         /**
154          * Returns the private key of the project.
155          * 
156          * @return The private key of the project
157          */
158         public String getPrivateKey() {
159                 return privateKey;
160         }
161
162         /**
163          * Sets the private key of the project.
164          * 
165          * @param privateKey
166          *            The private key of the project
167          */
168         void setPrivateKey(String privateKey) {
169                 String oldPrivateKey = this.privateKey;
170                 this.privateKey = privateKey;
171                 fireIfPropertyChanged(PROPERTY_PRIVATE_KEY, oldPrivateKey, privateKey);
172         }
173
174         /**
175          * Returns the base path of the project.
176          * 
177          * @return The base path of the project
178          */
179         public String getBasePath() {
180                 return basePath;
181         }
182
183         /**
184          * Sets the base path of the project.
185          * 
186          * @param basePath
187          *            The base path of the project
188          */
189         public void setBasePath(String basePath) {
190                 String oldBasePath = this.basePath;
191                 this.basePath = basePath;
192                 fireIfPropertyChanged(PROPERTY_BASE_PATH, oldBasePath, basePath);
193         }
194
195         /**
196          * Rescans the base path for new or changed files.
197          */
198         public void rescanBasePath() {
199                 List<Entry> entries = new ArrayList<Entry>();
200                 scanPath("", entries);
201         }
202  
203         /**
204          * Returns the list of files from the base path.
205          * 
206          * @return The list of files from the base path
207          */
208         public List<Entry> getBasePathEntries() {
209                 return basePathEntries;
210         }
211  
212         //
213         // PRIVATE METHODS
214         //
215  
216         /**
217          * Scans the given path relative to {@link #basePath} for files and adds
218          * them to the given list of entries.
219          * 
220          * @param currentPath
221          *            The current path, relative to the base path
222          * @param entries
223          *            The list of entries
224          */
225         private void scanPath(String currentPath, List<Entry> entries) {
226                 File currentDirectory = new File(basePath + File.separatorChar + currentPath);
227                 if (!currentDirectory.isDirectory()) {
228                         return;
229                 }
230                 
231                 for (File file: currentDirectory.listFiles()) {
232                         String fileName = currentPath + File.separatorChar + file.getName();
233                         if (file.isDirectory()) {
234                                 scanPath(fileName, entries);
235                                 continue;
236                         }
237                         PhysicalEntry entry = new PhysicalEntry();
238                         entry.setName(fileName);
239                         entry.setPath(file.getPath());
240                         entry.setInsert(!file.isHidden());
241                         entries.add(entry);
242                 }
243         }
244
245 }