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