From: David ‘Bombe’ Roden Date: Fri, 22 Jan 2016 22:07:19 +0000 (+0100) Subject: Simplify file scanner listener handling X-Git-Tag: 0.13^2~17 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=4ce9a8e2c9430dc1dbc4d3e639e12d0ebce692fd;p=jSite.git Simplify file scanner listener handling --- diff --git a/src/main/java/de/todesbaum/jsite/application/ProjectInserter.java b/src/main/java/de/todesbaum/jsite/application/ProjectInserter.java index dd85bf3..f20c3d0 100644 --- a/src/main/java/de/todesbaum/jsite/application/ProjectInserter.java +++ b/src/main/java/de/todesbaum/jsite/application/ProjectInserter.java @@ -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); } /** diff --git a/src/main/java/de/todesbaum/jsite/gui/FileScanner.java b/src/main/java/de/todesbaum/jsite/gui/FileScanner.java index 9135073..1377604 100644 --- a/src/main/java/de/todesbaum/jsite/gui/FileScanner.java +++ b/src/main/java/de/todesbaum/jsite/gui/FileScanner.java @@ -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 fileScannerListeners = new ArrayList(); + 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(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); } /** diff --git a/src/main/java/de/todesbaum/jsite/gui/ProjectFilesPage.java b/src/main/java/de/todesbaum/jsite/gui/ProjectFilesPage.java index 922a772..47b79d3 100644 --- a/src/main/java/de/todesbaum/jsite/gui/ProjectFilesPage.java +++ b/src/main/java/de/todesbaum/jsite/gui/ProjectFilesPage.java @@ -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"));