Simplify file scanner listener handling
authorDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Fri, 22 Jan 2016 22:07:19 +0000 (23:07 +0100)
committerDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Fri, 22 Jan 2016 22:28:12 +0000 (23:28 +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/ProjectFilesPage.java

index dd85bf3..f20c3d0 100644 (file)
@@ -171,8 +171,7 @@ public class ProjectInserter implements FileScannerListener, Runnable {
        public void start(ProgressListener progressListener) {
                cancelled = false;
                this.progressListener = progressListener;
-               fileScanner = new FileScanner(project);
-               fileScanner.addFileScannerListener(this);
+               fileScanner = new FileScanner(project, this);
                fileScanner.startInBackground();
        }
 
@@ -288,9 +287,8 @@ public class ProjectInserter implements FileScannerListener, Runnable {
                        }
                }
                long totalSize = 0;
-               FileScanner fileScanner = new FileScanner(project);
                final CountDownLatch completionLatch = new CountDownLatch(1);
-               fileScanner.addFileScannerListener(new FileScannerListener() {
+               FileScanner fileScanner = new FileScanner(project, new FileScannerListener() {
 
                        @Override
                        public void fileScannerFinished(FileScanner fileScanner) {
@@ -440,7 +438,6 @@ public class ProjectInserter implements FileScannerListener, Runnable {
                } else {
                        projectInsertListeners.fireProjectInsertFinished(project, false, null);
                }
-               fileScanner.removeFileScannerListener(this);
        }
 
        /**
index 9135073..1377604 100644 (file)
@@ -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;
 
@@ -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<FileScannerListener> fileScannerListeners = new ArrayList<FileScannerListener>();
+       private final FileScannerListener fileScannerListener;
 
        /** The project to scan. */
        private final Project project;
@@ -72,37 +73,9 @@ public class FileScanner implements Runnable {
         * @param project
         *            The project whose files to scan
         */
-       public FileScanner(Project project) {
+       public FileScanner(Project project, FileScannerListener fileScannerListener) {
                this.project = project;
-       }
-
-       /**
-        * Adds the given listener to the list of listeners.
-        *
-        * @param fileScannerListener
-        *            The listener to add
-        */
-       public void addFileScannerListener(FileScannerListener fileScannerListener) {
-               fileScannerListeners.add(fileScannerListener);
-       }
-
-       /**
-        * 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<FileScannerListener>(fileScannerListeners)) {
-                       fileScannerListener.fileScannerFinished(this);
-               }
+               this.fileScannerListener = Objects.requireNonNull(fileScannerListener);
        }
 
        /**
@@ -138,7 +111,7 @@ public class FileScanner implements Runnable {
                } catch (IOException ioe1) {
                        error = true;
                }
-               fireFileScannerFinished();
+               fileScannerListener.fileScannerFinished(this);
        }
 
        /**
index 922a772..47b79d3 100644 (file)
@@ -182,8 +182,7 @@ public class ProjectFilesPage extends TWizardPage implements ActionListener, Lis
        @Override
        public void pageAdded(TWizard wizard) {
                /* create file scanner. */
-               fileScanner = new FileScanner(project);
-               fileScanner.addFileScannerListener(this);
+               fileScanner = new FileScanner(project, this);
 
                actionScan();
                this.wizard.setPreviousName(I18n.getMessage("jsite.wizard.previous"));