Refactor file scanner listener interface
authorDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Wed, 10 Feb 2016 18:42:48 +0000 (19:42 +0100)
committerDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Wed, 10 Feb 2016 18:42:48 +0000 (19:42 +0100)
src/main/java/de/todesbaum/jsite/application/ProjectInserter.java
src/main/java/de/todesbaum/jsite/gui/FileScanner.java
src/main/java/de/todesbaum/jsite/gui/FileScannerListener.java
src/main/java/de/todesbaum/jsite/gui/ProjectFilesPage.java
src/main/java/de/todesbaum/jsite/gui/ScannedFile.java [new file with mode: 0644]

index 53b0726..c7dad3b 100644 (file)
@@ -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<ScannedFile> files) {
+               if (!error) {
                        new Thread(this).start();
                } else {
                        projectInsertListeners.fireProjectInsertFinished(project, false, null);
index 1377604..04705b5 100644 (file)
@@ -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 &lt;bombe@freenetproject.org&gt;
  */
 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 &lt;bombe@freenetproject.org&gt;
-        */
-       public static class ScannedFile implements Comparable<ScannedFile> {
-
-               /** 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);
-               }
-
-       }
-
 }
index d3d5145..4088697 100644 (file)
@@ -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<ScannedFile> files);
 
-}
\ No newline at end of file
+}
index eaae3bd..c7a0a97 100644 (file)
@@ -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<ScannedFile> files) {
                delayedNotification.finish();
-               final boolean error = fileScanner.isError();
                if (!error) {
-                       final List<ScannedFile> 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 (file)
index 0000000..00a659e
--- /dev/null
@@ -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 &lt;bombe@freenetproject.org&gt;
+ */
+public class ScannedFile implements Comparable<ScannedFile> {
+
+       /** 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);
+       }
+
+}