Unify copyright file headers, update years to include 2011.
[jSite.git] / src / de / todesbaum / jsite / gui / FileScanner.java
1 /*
2  * jSite - FileScanner.java - Copyright © 2006–2011 David Roden
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17  */
18
19 package de.todesbaum.jsite.gui;
20
21 import java.io.File;
22 import java.io.FileFilter;
23 import java.io.IOException;
24 import java.util.ArrayList;
25 import java.util.Collections;
26 import java.util.List;
27
28 import de.todesbaum.jsite.application.Project;
29 import de.todesbaum.jsite.i18n.I18n;
30
31 /**
32  * Scans the local path of a project anychronously and returns the list of found
33  * files as an event.
34  *
35  * @see Project#getLocalPath()
36  * @see FileScannerListener#fileScannerFinished(FileScanner)
37  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
38  */
39 public class FileScanner implements Runnable {
40
41         /** The list of listeners. */
42         private final List<FileScannerListener> fileScannerListeners = new ArrayList<FileScannerListener>();
43
44         /** The project to scan. */
45         private final Project project;
46
47         /** The list of found files. */
48         private List<String> files;
49
50         /** Wether there was an error. */
51         private boolean error = false;
52
53         /**
54          * Creates a new file scanner for the given project.
55          *
56          * @param project
57          *            The project whose files to scan
58          */
59         public FileScanner(Project project) {
60                 this.project = project;
61         }
62
63         /**
64          * Adds the given listener to the list of listeners.
65          *
66          * @param fileScannerListener
67          *            The listener to add
68          */
69         public void addFileScannerListener(FileScannerListener fileScannerListener) {
70                 fileScannerListeners.add(fileScannerListener);
71         }
72
73         /**
74          * Removes the given listener from the list of listeners.
75          *
76          * @param fileScannerListener
77          *            The listener to remove
78          */
79         public void removeFileScannerListener(FileScannerListener fileScannerListener) {
80                 fileScannerListeners.remove(fileScannerListener);
81         }
82
83         /**
84          * Notifies all listeners that the file scan finished.
85          */
86         protected void fireFileScannerFinished() {
87                 for (FileScannerListener fileScannerListener : new ArrayList<FileScannerListener>(fileScannerListeners)) {
88                         fileScannerListener.fileScannerFinished(this);
89                 }
90         }
91
92         /**
93          * {@inheritDoc}
94          * <p>
95          * Scans all available files in the project’s local path and emits an event
96          * when finished.
97          *
98          * @see FileScannerListener#fileScannerFinished(FileScanner)
99          */
100         public void run() {
101                 files = new ArrayList<String>();
102                 error = false;
103                 try {
104                         scanFiles(new File(project.getLocalPath()), files);
105                         Collections.sort(files);
106                 } catch (IOException ioe1) {
107                         error = true;
108                 }
109                 fireFileScannerFinished();
110         }
111
112         /**
113          * Returns whether there was an error scanning for files.
114          *
115          * @return <code>true</code> if there was an error, <code>false</code>
116          *         otherwise
117          */
118         public boolean isError() {
119                 return error;
120         }
121
122         /**
123          * Returns the list of found files.
124          *
125          * @return The list of found files
126          */
127         public List<String> getFiles() {
128                 return files;
129         }
130
131         /**
132          * Recursively scans a directory and adds all found files to the given list.
133          *
134          * @param rootDir
135          *            The directory to scan
136          * @param fileList
137          *            The list to which to add the found files
138          * @throws IOException
139          *             if an I/O error occurs
140          */
141         private void scanFiles(File rootDir, List<String> fileList) throws IOException {
142                 File[] files = rootDir.listFiles(new FileFilter() {
143
144                         @SuppressWarnings("synthetic-access")
145                         public boolean accept(File file) {
146                                 return !project.isIgnoreHiddenFiles() || !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 }