X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fde%2Ftodesbaum%2Fjsite%2Fgui%2FFileScanner.java;h=c3e4fd2be678ebadb95efa001e2952822e75ae2c;hb=8db42d2121e8ee465ab8380a66febde1949a0106;hp=cfb0c238a286685fe9106caec9bbd22432dc2f0d;hpb=967018cd81182ec2fd4f941e271137aadbaa6d81;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 cfb0c23..c3e4fd2 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–2019 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 @@ -29,6 +29,7 @@ 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; @@ -43,7 +44,7 @@ import de.todesbaum.jsite.i18n.I18n; * files as an event. * * @see Project#getLocalPath() - * @see FileScannerListener#fileScannerFinished(FileScanner) + * @see FileScannerListener#fileScannerFinished(boolean, java.util.Collection) * @author David ‘Bombe’ Roden <bombe@freenetproject.org> */ public class FileScanner implements Runnable { @@ -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(); } /** @@ -108,19 +98,20 @@ public class FileScanner implements Runnable { * Scans all available files in the project’s local path and emits an event * when finished. * - * @see FileScannerListener#fileScannerFinished(FileScanner) + * @see FileScannerListener#fileScannerFinished(boolean, java.util.Collection) */ @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(error, files); } /** @@ -172,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; } } @@ -219,95 +211,4 @@ public class FileScanner implements Runnable { return hexString.toString(); } - /** - * Container for a scanned file, consisting of the name of the file and its - * hash. - * - * @author David ‘Bombe’ Roden <bombe@freenetproject.org> - */ - public static class ScannedFile implements Comparable { - - /** The name of the file. */ - private final String filename; - - /** The hash of the file. */ - private final String hash; - - /** - * Creates a new scanned file. - * - * @param filename - * The name of the file - * @param hash - * The hash of the file - */ - public ScannedFile(String filename, String hash) { - this.filename = filename; - this.hash = hash; - } - - // - // ACCESSORS - // - - /** - * Returns the name of the file. - * - * @return The name of the file - */ - public String getFilename() { - return filename; - } - - /** - * Returns the hash of the file. - * - * @return The hash of the file - */ - public String getHash() { - return hash; - } - - // - // OBJECT METHODS - // - - /** - * {@inheritDoc} - */ - @Override - public int hashCode() { - return filename.hashCode(); - } - - /** - * {@inheritDoc} - */ - @Override - public boolean equals(Object obj) { - return filename.equals(obj); - } - - /** - * {@inheritDoc} - */ - @Override - public String toString() { - return filename; - } - - // - // COMPARABLE METHODS - // - - /** - * {@inheritDoc} - */ - @Override - public int compareTo(ScannedFile scannedFile) { - return filename.compareTo(scannedFile.filename); - } - - } - }