From: David ‘Bombe’ Roden Date: Wed, 10 Feb 2016 18:42:48 +0000 (+0100) Subject: Refactor file scanner listener interface X-Git-Tag: 0.13^2~12 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=cd351fe39bcbc8933187c3864e79e3dc896a374c;p=jSite.git Refactor file scanner listener interface --- diff --git a/src/main/java/de/todesbaum/jsite/application/ProjectInserter.java b/src/main/java/de/todesbaum/jsite/application/ProjectInserter.java index 53b0726..c7dad3b 100644 --- a/src/main/java/de/todesbaum/jsite/application/ProjectInserter.java +++ b/src/main/java/de/todesbaum/jsite/application/ProjectInserter.java @@ -25,6 +25,7 @@ import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collection; import java.util.HashSet; import java.util.Iterator; import java.util.List; @@ -40,7 +41,7 @@ import java.util.logging.Logger; import net.pterodactylus.util.io.StreamCopier.ProgressListener; import de.todesbaum.jsite.gui.FileScanner; -import de.todesbaum.jsite.gui.FileScanner.ScannedFile; +import de.todesbaum.jsite.gui.ScannedFile; import de.todesbaum.jsite.gui.FileScannerListener; import de.todesbaum.util.freenet.fcp2.Client; import de.todesbaum.util.freenet.fcp2.ClientPutComplexDir; @@ -288,13 +289,7 @@ public class ProjectInserter implements FileScannerListener, Runnable { } long totalSize = 0; final CountDownLatch completionLatch = new CountDownLatch(1); - FileScanner fileScanner = new FileScanner(project, new FileScannerListener() { - - @Override - public void fileScannerFinished(FileScanner fileScanner) { - completionLatch.countDown(); - } - }); + FileScanner fileScanner = new FileScanner(project, (error, files) -> completionLatch.countDown()); fileScanner.startInBackground(); while (completionLatch.getCount() > 0) { try { @@ -432,8 +427,8 @@ public class ProjectInserter implements FileScannerListener, Runnable { * {@inheritDoc} */ @Override - public void fileScannerFinished(FileScanner fileScanner) { - if (!fileScanner.isError()) { + public void fileScannerFinished(boolean error, Collection files) { + if (!error) { new Thread(this).start(); } else { projectInsertListeners.fireProjectInsertFinished(project, false, null); diff --git a/src/main/java/de/todesbaum/jsite/gui/FileScanner.java b/src/main/java/de/todesbaum/jsite/gui/FileScanner.java index 1377604..04705b5 100644 --- a/src/main/java/de/todesbaum/jsite/gui/FileScanner.java +++ b/src/main/java/de/todesbaum/jsite/gui/FileScanner.java @@ -44,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 { @@ -98,7 +98,7 @@ 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() { @@ -111,7 +111,7 @@ public class FileScanner implements Runnable { } catch (IOException ioe1) { error = true; } - fileScannerListener.fileScannerFinished(this); + fileScannerListener.fileScannerFinished(error, files); } /** @@ -211,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); - } - - } - } diff --git a/src/main/java/de/todesbaum/jsite/gui/FileScannerListener.java b/src/main/java/de/todesbaum/jsite/gui/FileScannerListener.java index d3d5145..4088697 100644 --- a/src/main/java/de/todesbaum/jsite/gui/FileScannerListener.java +++ b/src/main/java/de/todesbaum/jsite/gui/FileScannerListener.java @@ -18,6 +18,7 @@ package de.todesbaum.jsite.gui; +import java.util.Collection; import java.util.EventListener; /** @@ -29,12 +30,6 @@ import java.util.EventListener; */ public interface FileScannerListener extends EventListener { - /** - * Notifies a listener that scanning a project’s local path has finished. - * - * @param fileScanner - * The file scanner that finished - */ - public void fileScannerFinished(FileScanner fileScanner); + void fileScannerFinished(boolean error, Collection files); -} \ No newline at end of file +} diff --git a/src/main/java/de/todesbaum/jsite/gui/ProjectFilesPage.java b/src/main/java/de/todesbaum/jsite/gui/ProjectFilesPage.java index eaae3bd..c7a0a97 100644 --- a/src/main/java/de/todesbaum/jsite/gui/ProjectFilesPage.java +++ b/src/main/java/de/todesbaum/jsite/gui/ProjectFilesPage.java @@ -29,9 +29,9 @@ import java.awt.event.KeyEvent; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.text.MessageFormat; +import java.util.Collection; import java.util.HashSet; import java.util.Iterator; -import java.util.List; import java.util.Set; import javax.swing.AbstractAction; @@ -65,7 +65,6 @@ import net.pterodactylus.util.swing.SwingUtils; import net.pterodactylus.util.thread.StoppableDelay; import de.todesbaum.jsite.application.FileOption; import de.todesbaum.jsite.application.Project; -import de.todesbaum.jsite.gui.FileScanner.ScannedFile; import de.todesbaum.jsite.i18n.I18n; import de.todesbaum.jsite.i18n.I18nContainer; import de.todesbaum.util.swing.TLabel; @@ -469,11 +468,9 @@ public class ProjectFilesPage extends TWizardPage implements ActionListener, Lis * Updates the file list. */ @Override - public void fileScannerFinished(FileScanner fileScanner) { + public void fileScannerFinished(boolean error, Collection files) { delayedNotification.finish(); - final boolean error = fileScanner.isError(); if (!error) { - final List files = fileScanner.getFiles(); SwingUtilities.invokeLater(new Runnable() { @Override diff --git a/src/main/java/de/todesbaum/jsite/gui/ScannedFile.java b/src/main/java/de/todesbaum/jsite/gui/ScannedFile.java new file mode 100644 index 0000000..00a659e --- /dev/null +++ b/src/main/java/de/todesbaum/jsite/gui/ScannedFile.java @@ -0,0 +1,92 @@ +package de.todesbaum.jsite.gui; + +/** + * Container for a scanned file, consisting of the name of the file and its + * hash. + * + * @author David ‘Bombe’ Roden <bombe@freenetproject.org> + */ +public 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); + } + +}