Move starting file scanner in background to file scanner class
[jSite.git] / src / main / java / de / todesbaum / jsite / gui / FileScanner.java
index 75dafe0..9135073 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * jSite - FileScanner.java - Copyright © 2006–2012 David Roden
+ * jSite - FileScanner.java - Copyright © 2006–2014 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
@@ -23,7 +23,6 @@ import java.io.FileFilter;
 import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.InputStream;
-import java.io.OutputStream;
 import java.security.DigestOutputStream;
 import java.security.MessageDigest;
 import java.security.NoSuchAlgorithmException;
@@ -34,9 +33,10 @@ import java.util.logging.Level;
 import java.util.logging.Logger;
 
 import net.pterodactylus.util.io.Closer;
+import net.pterodactylus.util.io.NullOutputStream;
+import net.pterodactylus.util.io.StreamCopier;
 import de.todesbaum.jsite.application.Project;
 import de.todesbaum.jsite.i18n.I18n;
-import de.todesbaum.util.io.StreamCopier;
 
 /**
  * Scans the local path of a project anychronously and returns the list of found
@@ -63,6 +63,9 @@ 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.
         *
@@ -103,6 +106,20 @@ public class FileScanner implements Runnable {
        }
 
        /**
+        * Returns the name of the last file scanned.
+        *
+        * @return The name of the last file scanned, or {@code null} if there was
+        *         no file scanned yet
+        */
+       public String getLastFilename() {
+               return lastFilename;
+       }
+
+       public void startInBackground() {
+               new Thread(this).start();
+       }
+
+       /**
         * {@inheritDoc}
         * <p>
         * Scans all available files in the project’s local path and emits an event
@@ -110,9 +127,11 @@ public class FileScanner implements Runnable {
         *
         * @see FileScannerListener#fileScannerFinished(FileScanner)
         */
+       @Override
        public void run() {
                files = new ArrayList<ScannedFile>();
                error = false;
+               lastFilename = null;
                try {
                        scanFiles(new File(project.getLocalPath()), files);
                        Collections.sort(files);
@@ -154,6 +173,7 @@ public class FileScanner implements Runnable {
        private void scanFiles(File rootDir, List<ScannedFile> fileList) throws IOException {
                File[] files = rootDir.listFiles(new FileFilter() {
 
+                       @Override
                        @SuppressWarnings("synthetic-access")
                        public boolean accept(File file) {
                                return !project.isIgnoreHiddenFiles() || !file.isHidden();
@@ -170,6 +190,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;
                }
        }
 
@@ -182,7 +203,6 @@ public class FileScanner implements Runnable {
         *            The name of the file, relative to the project path
         * @return The hash of the file
         */
-       @SuppressWarnings("synthetic-access")
        private static String hashFile(String path, String filename) {
                InputStream fileInputStream = null;
                DigestOutputStream digestOutputStream = null;
@@ -219,39 +239,6 @@ public class FileScanner implements Runnable {
        }
 
        /**
-        * {@link OutputStream} that discards all written bytes.
-        *
-        * @author David ‘Bombe’ Roden &lt;bombe@freenetproject.org&gt;
-        */
-       private static class NullOutputStream extends OutputStream {
-
-               /**
-                * {@inheritDoc}
-                */
-               @Override
-               public void write(int b) {
-                       /* do nothing. */
-               }
-
-               /**
-                * {@inheritDoc}
-                */
-               @Override
-               public void write(byte[] b) {
-                       /* do nothing. */
-               }
-
-               /**
-                * {@inheritDoc}
-                */
-               @Override
-               public void write(byte[] b, int off, int len) {
-                       /* do nothing. */
-               }
-
-       }
-
-       /**
         * Container for a scanned file, consisting of the name of the file and its
         * hash.
         *
@@ -335,6 +322,7 @@ public class FileScanner implements Runnable {
                /**
                 * {@inheritDoc}
                 */
+               @Override
                public int compareTo(ScannedFile scannedFile) {
                        return filename.compareTo(scannedFile.filename);
                }