2 * jSite - FileScanner.java - Copyright © 2006–2011 David Roden
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.
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.
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.
19 package de.todesbaum.jsite.gui;
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;
28 import de.todesbaum.jsite.application.Project;
29 import de.todesbaum.jsite.i18n.I18n;
32 * Scans the local path of a project anychronously and returns the list of found
35 * @see Project#getLocalPath()
36 * @see FileScannerListener#fileScannerFinished(FileScanner)
37 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
39 public class FileScanner implements Runnable {
41 /** The list of listeners. */
42 private final List<FileScannerListener> fileScannerListeners = new ArrayList<FileScannerListener>();
44 /** The project to scan. */
45 private final Project project;
47 /** The list of found files. */
48 private List<String> files;
50 /** Wether there was an error. */
51 private boolean error = false;
54 * Creates a new file scanner for the given project.
57 * The project whose files to scan
59 public FileScanner(Project project) {
60 this.project = project;
64 * Adds the given listener to the list of listeners.
66 * @param fileScannerListener
69 public void addFileScannerListener(FileScannerListener fileScannerListener) {
70 fileScannerListeners.add(fileScannerListener);
74 * Removes the given listener from the list of listeners.
76 * @param fileScannerListener
77 * The listener to remove
79 public void removeFileScannerListener(FileScannerListener fileScannerListener) {
80 fileScannerListeners.remove(fileScannerListener);
84 * Notifies all listeners that the file scan finished.
86 protected void fireFileScannerFinished() {
87 for (FileScannerListener fileScannerListener : new ArrayList<FileScannerListener>(fileScannerListeners)) {
88 fileScannerListener.fileScannerFinished(this);
95 * Scans all available files in the project’s local path and emits an event
98 * @see FileScannerListener#fileScannerFinished(FileScanner)
101 files = new ArrayList<String>();
104 scanFiles(new File(project.getLocalPath()), files);
105 Collections.sort(files);
106 } catch (IOException ioe1) {
109 fireFileScannerFinished();
113 * Returns whether there was an error scanning for files.
115 * @return <code>true</code> if there was an error, <code>false</code>
118 public boolean isError() {
123 * Returns the list of found files.
125 * @return The list of found files
127 public List<String> getFiles() {
132 * Recursively scans a directory and adds all found files to the given list.
135 * The directory to scan
137 * The list to which to add the found files
138 * @throws IOException
139 * if an I/O error occurs
141 private void scanFiles(File rootDir, List<String> fileList) throws IOException {
142 File[] files = rootDir.listFiles(new FileFilter() {
144 @SuppressWarnings("synthetic-access")
145 public boolean accept(File file) {
146 return !project.isIgnoreHiddenFiles() || !file.isHidden();
150 throw new IOException(I18n.getMessage("jsite.file-scanner.can-not-read-directory"));
152 for (File file : files) {
153 if (file.isDirectory()) {
154 scanFiles(file, fileList);
157 String filename = project.shortenFilename(file);
158 filename = filename.replace('\\', '/');
159 fileList.add(filename);