add javadoc
[jSite.git] / src / de / todesbaum / jsite / gui / FileScanner.java
1 /*
2  * jSite - a tool for uploading websites into Freenet
3  * Copyright (C) 2006 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 de.todesbaum.jsite.gui;
21
22 import java.io.File;
23 import java.io.FileFilter;
24 import java.io.IOException;
25 import java.util.ArrayList;
26 import java.util.Collections;
27 import java.util.List;
28
29 import de.todesbaum.jsite.application.Project;
30 import de.todesbaum.jsite.i18n.I18n;
31
32 /**
33  * Scans the local path of a project anychronously and returns the list of found
34  * files as an event.
35  * 
36  * @see Project#getLocalPath()
37  * @see FileScannerListener#fileScannerFinished(FileScanner)
38  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
39  */
40 public class FileScanner implements Runnable {
41
42         /** The list of listeners. */
43         private final List<FileScannerListener> fileScannerListeners = new ArrayList<FileScannerListener>();
44
45         /** The project to scan. */
46         private final Project project;
47
48         /** The list of found files. */
49         private List<String> files;
50
51         /** Wether there was an error. */
52         private boolean error = false;
53
54         /**
55          * Creates a new file scanner for the given project.
56          * 
57          * @param project
58          *            The project whose files to scan
59          */
60         public FileScanner(Project project) {
61                 this.project = project;
62         }
63
64         /**
65          * Adds the given listener to the list of listeners.
66          * 
67          * @param fileScannerListener
68          *            The listener to add
69          */
70         public void addFileScannerListener(FileScannerListener fileScannerListener) {
71                 fileScannerListeners.add(fileScannerListener);
72         }
73
74         /**
75          * Removes the given listener from the list of listeners.
76          * 
77          * @param fileScannerListener
78          *            The listener to remove
79          */
80         public void removeFileScannerListener(FileScannerListener fileScannerListener) {
81                 fileScannerListeners.remove(fileScannerListener);
82         }
83
84         /**
85          * Notifies all listeners that the file scan finished.
86          */
87         protected void fireFileScannerFinished() {
88                 for (FileScannerListener fileScannerListener : new ArrayList<FileScannerListener>(fileScannerListeners)) {
89                         fileScannerListener.fileScannerFinished(this);
90                 }
91         }
92
93         /**
94          * {@inheritDoc}
95          * <p>
96          * Scans all available files in the project’s local path and emits an event
97          * when finished.
98          * 
99          * @see FileScannerListener#fileScannerFinished(FileScanner)
100          */
101         public void run() {
102                 files = new ArrayList<String>();
103                 error = false;
104                 try {
105                         scanFiles(new File(project.getLocalPath()), files);
106                         Collections.sort(files);
107                 } catch (IOException ioe1) {
108                         error = true;
109                 }
110                 fireFileScannerFinished();
111         }
112
113         /**
114          * Returns whether there was an error scanning for files.
115          * 
116          * @return <code>true</code> if there was an error, <code>false</code>
117          *         otherwise
118          */
119         public boolean isError() {
120                 return error;
121         }
122
123         /**
124          * Returns the list of found files.
125          * 
126          * @return The list of found files
127          */
128         public List<String> getFiles() {
129                 return files;
130         }
131
132         /**
133          * Recursively scans a directory and adds all found files to the given list.
134          * 
135          * @param rootDir
136          *            The directory to scan
137          * @param fileList
138          *            The list to which to add the found files
139          * @throws IOException
140          *             if an I/O error occurs
141          */
142         private void scanFiles(File rootDir, List<String> fileList) throws IOException {
143                 File[] files = rootDir.listFiles(new FileFilter() {
144
145                         public boolean accept(File file) {
146                                 return !file.isHidden();
147                         }
148                 });
149                 if (files == null) {
150                         throw new IOException(I18n.getMessage("jsite.file-scanner.can-not-read-directory"));
151                 }
152                 for (File file : files) {
153                         if (file.isDirectory()) {
154                                 scanFiles(file, fileList);
155                                 continue;
156                         }
157                         String filename = project.shortenFilename(file);
158                         filename = filename.replace('\\', '/');
159                         fileList.add(filename);
160                 }
161         }
162
163 }