X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fde%2Ftodesbaum%2Fjsite%2Fgui%2FFileScanner.java;h=1377604d4adec932a1dd16b530dcc85d10bd8cde;hb=4ce9a8e2c9430dc1dbc4d3e639e12d0ebce692fd;hp=1790fbfc60a9eb1021c6a66b84c9b71dea278059;hpb=0e88169c3e8decfcd99f39f5ecf3a85df50c3fca;p=jSite.git diff --git a/src/main/java/de/todesbaum/jsite/gui/FileScanner.java b/src/main/java/de/todesbaum/jsite/gui/FileScanner.java index 1790fbf..1377604 100644 --- a/src/main/java/de/todesbaum/jsite/gui/FileScanner.java +++ b/src/main/java/de/todesbaum/jsite/gui/FileScanner.java @@ -1,5 +1,5 @@ /* - * jSite - FileScanner.java - Copyright © 2006–2012 David Roden + * jSite - FileScanner.java - Copyright © 2006–2014 David Roden * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -23,20 +23,21 @@ import java.io.FileFilter; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; -import java.io.OutputStream; import java.security.DigestOutputStream; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.ArrayList; import java.util.Collections; import java.util.List; +import java.util.Objects; import java.util.logging.Level; import java.util.logging.Logger; +import net.pterodactylus.util.io.Closer; +import net.pterodactylus.util.io.NullOutputStream; +import net.pterodactylus.util.io.StreamCopier; import de.todesbaum.jsite.application.Project; import de.todesbaum.jsite.i18n.I18n; -import de.todesbaum.util.io.Closer; -import de.todesbaum.util.io.StreamCopier; /** * Scans the local path of a project anychronously and returns the list of found @@ -52,7 +53,7 @@ public class FileScanner implements Runnable { private final static Logger logger = Logger.getLogger(FileScanner.class.getName()); /** The list of listeners. */ - private final List fileScannerListeners = new ArrayList(); + private final FileScannerListener fileScannerListener; /** The project to scan. */ private final Project project; @@ -63,43 +64,32 @@ public class FileScanner implements Runnable { /** Wether there was an error. */ private boolean error = false; + /** The name of the last file scanned. */ + private String lastFilename; + /** * Creates a new file scanner for the given project. * * @param project * The project whose files to scan */ - public FileScanner(Project project) { + public FileScanner(Project project, FileScannerListener fileScannerListener) { this.project = project; + this.fileScannerListener = Objects.requireNonNull(fileScannerListener); } /** - * Adds the given listener to the list of listeners. + * Returns the name of the last file scanned. * - * @param fileScannerListener - * The listener to add + * @return The name of the last file scanned, or {@code null} if there was + * no file scanned yet */ - public void addFileScannerListener(FileScannerListener fileScannerListener) { - fileScannerListeners.add(fileScannerListener); + public String getLastFilename() { + return lastFilename; } - /** - * Removes the given listener from the list of listeners. - * - * @param fileScannerListener - * The listener to remove - */ - public void removeFileScannerListener(FileScannerListener fileScannerListener) { - fileScannerListeners.remove(fileScannerListener); - } - - /** - * Notifies all listeners that the file scan finished. - */ - protected void fireFileScannerFinished() { - for (FileScannerListener fileScannerListener : new ArrayList(fileScannerListeners)) { - fileScannerListener.fileScannerFinished(this); - } + public void startInBackground() { + new Thread(this).start(); } /** @@ -110,16 +100,18 @@ public class FileScanner implements Runnable { * * @see FileScannerListener#fileScannerFinished(FileScanner) */ + @Override public void run() { files = new ArrayList(); error = false; + lastFilename = null; try { scanFiles(new File(project.getLocalPath()), files); Collections.sort(files); } catch (IOException ioe1) { error = true; } - fireFileScannerFinished(); + fileScannerListener.fileScannerFinished(this); } /** @@ -154,6 +146,7 @@ public class FileScanner implements Runnable { private void scanFiles(File rootDir, List fileList) throws IOException { File[] files = rootDir.listFiles(new FileFilter() { + @Override @SuppressWarnings("synthetic-access") public boolean accept(File file) { return !project.isIgnoreHiddenFiles() || !file.isHidden(); @@ -170,6 +163,7 @@ public class FileScanner implements Runnable { String filename = project.shortenFilename(file).replace('\\', '/'); String hash = hashFile(project.getLocalPath(), filename); fileList.add(new ScannedFile(filename, hash)); + lastFilename = filename; } } @@ -182,7 +176,6 @@ public class FileScanner implements Runnable { * The name of the file, relative to the project path * @return The hash of the file */ - @SuppressWarnings("synthetic-access") private static String hashFile(String path, String filename) { InputStream fileInputStream = null; DigestOutputStream digestOutputStream = null; @@ -219,39 +212,6 @@ public class FileScanner implements Runnable { } /** - * {@link OutputStream} that discards all written bytes. - * - * @author David ‘Bombe’ Roden <bombe@freenetproject.org> - */ - private static class NullOutputStream extends OutputStream { - - /** - * {@inheritDoc} - */ - @Override - public void write(int b) { - /* do nothing. */ - } - - /** - * {@inheritDoc} - */ - @Override - public void write(byte[] b) { - /* do nothing. */ - } - - /** - * {@inheritDoc} - */ - @Override - public void write(byte[] b, int off, int len) { - /* do nothing. */ - } - - } - - /** * Container for a scanned file, consisting of the name of the file and its * hash. * @@ -335,6 +295,7 @@ public class FileScanner implements Runnable { /** * {@inheritDoc} */ + @Override public int compareTo(ScannedFile scannedFile) { return filename.compareTo(scannedFile.filename); }