2 * jSite - a tool for uploading websites into Freenet
3 * Copyright (C) 2006 David Roden
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.
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.
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.
20 package de.todesbaum.jsite.gui;
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;
29 import de.todesbaum.jsite.application.Project;
30 import de.todesbaum.jsite.i18n.I18n;
33 * Scans the local path of a project anychronously and returns the list of found
36 * @see Project#getLocalPath()
37 * @see FileScannerListener#fileScannerFinished(FileScanner)
38 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
40 public class FileScanner implements Runnable {
42 /** The list of listeners. */
43 private final List<FileScannerListener> fileScannerListeners = new ArrayList<FileScannerListener>();
45 /** The project to scan. */
46 private final Project project;
48 /** The list of found files. */
49 private List<String> files;
51 /** Wether there was an error. */
52 private boolean error = false;
55 * Creates a new file scanner for the given project.
58 * The project whose files to scan
60 public FileScanner(Project project) {
61 this.project = project;
65 * Adds the given listener to the list of listeners.
67 * @param fileScannerListener
70 public void addFileScannerListener(FileScannerListener fileScannerListener) {
71 fileScannerListeners.add(fileScannerListener);
75 * Removes the given listener from the list of listeners.
77 * @param fileScannerListener
78 * The listener to remove
80 public void removeFileScannerListener(FileScannerListener fileScannerListener) {
81 fileScannerListeners.remove(fileScannerListener);
85 * Notifies all listeners that the file scan finished.
87 protected void fireFileScannerFinished() {
88 for (FileScannerListener fileScannerListener : new ArrayList<FileScannerListener>(fileScannerListeners)) {
89 fileScannerListener.fileScannerFinished(this);
96 * Scans all available files in the project’s local path and emits an event
99 * @see FileScannerListener#fileScannerFinished(FileScanner)
102 files = new ArrayList<String>();
105 scanFiles(new File(project.getLocalPath()), files);
106 Collections.sort(files);
107 } catch (IOException ioe1) {
110 fireFileScannerFinished();
114 * Returns whether there was an error scanning for files.
116 * @return <code>true</code> if there was an error, <code>false</code>
119 public boolean isError() {
124 * Returns the list of found files.
126 * @return The list of found files
128 public List<String> getFiles() {
133 * Recursively scans a directory and adds all found files to the given list.
136 * The directory to scan
138 * The list to which to add the found files
139 * @throws IOException
140 * if an I/O error occurs
142 private void scanFiles(File rootDir, List<String> fileList) throws IOException {
143 File[] files = rootDir.listFiles(new FileFilter() {
145 public boolean accept(File file) {
146 return !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);