001 package net.sourceforge.mp3dings.utils;
002
003 /* Mp3dings - manage mp3 meta-information
004 * Copyright (C) 2006 Moritz Ringler
005 * $Id: FindFilesTask.java,v 1.2 2006/04/05 09:36:40 ringler Exp $
006 *
007 * This program is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU General Public License
009 * as published by the Free Software Foundation; either version 2
010 * of the License, or (at your option) any later version.
011 *
012 * This program is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015 * GNU General Public License for more details.
016 *
017 * You should have received a copy of the GNU General Public License
018 * along with this program; if not, write to the Free Software
019 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020 */
021 import java.awt.Frame;
022 import java.io.File;
023 import java.io.FileFilter;
024 import java.util.List;
025 import java.util.ArrayList;
026 import java.util.Arrays;
027 import net.sourceforge.mp3dings.Globals;
028
029 public class FindFilesTask extends TimeIntensiveTask {
030
031 File[] f;
032 FileFilter ff;
033 boolean running = false;
034
035 /**
036 * Creates a new FindFilesTask object.
037 *
038 * @param owner DOCUMENT ME!
039 * @param startFiles DOCUMENT ME!
040 * @param ff DOCUMENT ME!
041 */
042 public FindFilesTask(Frame owner, File[] startFiles, FileFilter ff) {
043 super(owner, Globals.getString("DATA_ADD_FILES_PROGRESS"), "");
044 this.ff = ff;
045 f = startFiles;
046 }
047
048 /**
049 * @return startFiles while isRunning(), list of found files otherwise
050 */
051 public File[] getFiles() {
052 return f;
053 }
054
055 /**
056 * DOCUMENT ME!
057 *
058 * @return DOCUMENT ME!
059 */
060 public boolean isRunning() {
061 return running;
062 }
063
064 /**
065 * DOCUMENT ME!
066 */
067 public void run() {
068 running = true;
069 getMonitor().setProgress(1);
070 if(f == null){
071 f = new File[0];
072 return;
073 }
074 List vFiles = new ArrayList(f.length);
075 vFiles.addAll(Arrays.asList(f));
076 int i = 0;
077 int fileCount = 0;
078 int totalFiles = vFiles.size();
079
080 //int progress=0;
081 while (i < vFiles.size()) {
082 if (getMonitor().isCanceled()) {
083 break;
084 }
085
086 File fi = (File) vFiles.get(i);
087 getMonitor().setNote(fi.getPath());
088
089 boolean removedCorrectFile;
090
091 /* directories: add contents (mp3s and dirs) to vFiles
092 and remove directory from file list*/
093 if (fi.isDirectory()) {
094 removedCorrectFile = (fi == vFiles.remove(i));
095
096 assert removedCorrectFile;
097 File[] dir = fi.listFiles(ff);
098
099 // catch IO errors by testing for null
100 if (dir != null) {
101 for (int j = 0; j < dir.length; j++) {
102 vFiles.add((File) dir[j]);
103 totalFiles++;
104 }
105 }
106
107 /* mp3 files: keep file in list and continue with next file */
108 } else if (ff.accept(fi)) {
109 i++;
110
111 /* no dir and no mp3: remove file from list */
112 } else {
113 removedCorrectFile = (fi == vFiles.remove(i));
114
115 assert removedCorrectFile;
116 }
117 fileCount++;
118
119 /* calculate progress in integer arithmetic */
120 getMonitor().setProgress(1 + fileCount * 99 / totalFiles);
121 }
122 File[] xf = (File[]) vFiles.toArray(new File[vFiles.size()]);
123 if(i < vFiles.size()){
124 File[] yf = new File[i];
125 System.arraycopy(xf, 0, yf, 0, i);
126 xf = yf;
127 }
128 f = xf;
129 running = false;
130 }
131 }