1 /*
2 * Scope: a generic MVC framework.
3 * Copyright (c) 2000-2002, The Scope team
4 * All rights reserved.
5 *
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * Neither the name "Scope" nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
27 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 *
35 *
36 * $Id: SearchViewModel.java,v 1.7 2002/09/12 18:26:02 ludovicc Exp $
37 */
38 package samples.filefind;
39
40
41 import java.io.File;
42 import java.io.FileFilter;
43 import java.io.FilenameFilter;
44 import java.util.ArrayList;
45 import java.util.Date;
46 import java.util.Iterator;
47 import java.util.List;
48 import org.apache.commons.logging.Log;
49 import org.apache.commons.logging.LogFactory;
50 import org.apache.oro.io.Perl5FilenameFilter;
51 import org.scopemvc.core.Selector;
52 import org.scopemvc.model.basic.BasicModel;
53 import org.scopemvc.model.collection.ListModel;
54
55 /***
56 * ***** Should fire a change event intermittently during a long search
57 *
58 * @author <A HREF="mailto:daniel.michalik@autel.cz">Daniel Michalik</A>
59 * @author <A HREF="mailto:smeyfroi@users.sourceforge.net">Steve Meyfroidt</A>
60 * @created 12 September 2002
61 * @version $Revision: 1.7 $ $Date: 2002/09/12 18:26:02 $
62 */
63 public final class SearchViewModel extends BasicModel {
64
65 /***
66 * TODO: describe of the Field
67 */
68 public static final Selector DATE_CRITERIA_ENABLED =
69 Selector.fromString("dateCriteriaEnabled");
70 /***
71 * TODO: describe of the Field
72 */
73 public static final Selector FILE_NAME_PATTERN =
74 Selector.fromString("fileNamePattern");
75 /***
76 * TODO: describe of the Field
77 */
78 public static final Selector DATE_CRITERIA =
79 Selector.fromString("dateCriteria");
80
81 private static final Log LOG = LogFactory.getLog(SearchViewModel.class);
82
83 private static final int SEARCH_THREAD_PRIORITY = Thread.MIN_PRIORITY;
84
85 private boolean dateCriteriaEnabled = true;
86 private String fileNamePattern = ".*";
87 private DateCriteriaModel dateCriteria;
88 private FSRootsModel fsRootsModel = new FSRootsModel();
89
90
91 /***
92 * Constructor for the SearchViewModel object
93 */
94 public SearchViewModel() {
95 dateCriteria = new DateCriteriaModel();
96 dateCriteria.addModelChangeListener(this);
97 // must listen to submodels for event propagation
98 }
99
100
101 /***
102 * Gets the fs roots
103 *
104 * @return The fsRoots value
105 */
106 public FSRootsModel getFsRoots() {
107 if (LOG.isDebugEnabled()) {
108 LOG.debug("getFSRootsModel: " + fsRootsModel);
109 }
110 return fsRootsModel;
111 }
112
113
114 /***
115 * Gets the date criteria enabled
116 *
117 * @return The dateCriteriaEnabled value
118 */
119 public boolean isDateCriteriaEnabled() {
120 return dateCriteriaEnabled;
121 }
122
123
124 /***
125 * Gets the file name pattern
126 *
127 * @return The fileNamePattern value
128 */
129 public String getFileNamePattern() {
130 return fileNamePattern;
131 }
132
133
134 /***
135 * Gets the date criteria
136 *
137 * @return The dateCriteria value
138 */
139 public DateCriteriaModel getDateCriteria() {
140 return dateCriteria;
141 }
142
143
144 /***
145 * Sets the date criteria enabled
146 *
147 * @param dateCriteriaEnabled The new dateCriteriaEnabled value
148 */
149 public void setDateCriteriaEnabled(boolean dateCriteriaEnabled) {
150 this.dateCriteriaEnabled = dateCriteriaEnabled;
151 fireModelChange(VALUE_CHANGED, DATE_CRITERIA_ENABLED);
152 }
153
154
155 /***
156 * Sets the file name pattern
157 *
158 * @param fileNamePattern The new fileNamePattern value
159 */
160 public void setFileNamePattern(String fileNamePattern) {
161 this.fileNamePattern = fileNamePattern;
162 fireModelChange(VALUE_CHANGED, FILE_NAME_PATTERN);
163 }
164
165
166 /***
167 * Creates background thread and search files.
168 *
169 * @return TODO: Describe the Return Value
170 */
171 public List search() {
172 List files = new ListModel(new ArrayList());
173 FilenameFilter nameFilter = new Perl5FilenameFilter(getFileNamePattern());
174 ComplexFileFilter filter = new ComplexFileFilter();
175 filter.nameFilter = nameFilter;
176 if (isDateCriteriaEnabled()) {
177 dateCriteria.prepareFilter(filter);
178 }
179 new SearchThread(files, roots(), filter).start();
180 return files;
181 }
182
183
184 private List roots() {
185 return fsRootsModel.getSelectedRoots();
186 }
187
188
189 private void search0(List files, List roots, FileFilter filter) {
190 for (Iterator i = roots.iterator(); i.hasNext(); ) {
191 File root = (File) i.next();
192 search(root, filter, files);
193 }
194 }
195
196
197 private void search(File dir, FileFilter filter, List result) {
198 // disable event firing completely while we do this
199 ((BasicModel) result).makeActive(false);
200 makeActive(false);
201
202 try {
203 File files[];
204 try {
205 files = dir.listFiles(filter);
206 } catch (SecurityException ex) {
207 LOG.error("Cannot open directory " + dir.getAbsolutePath(), ex);
208 return;
209 }
210
211 if (files == null) {
212 return;
213 }
214
215 List dirs = new ArrayList();
216 for (int i = 0; i < files.length; i++) {
217 if (files[i].isDirectory()) {
218 dirs.add(files[i]);
219 } else {
220 result.add(new FileProperties(files[i]));
221 }
222 }
223
224 for (Iterator i = dirs.iterator(); i.hasNext(); ) {
225 search((File) i.next(), filter, result);
226 }
227
228 } finally {
229 // reenable event firing and tell the world about the change
230 ((BasicModel) result).makeActive(true);
231 makeActive(true);
232 ((BasicModel) result).fireModelChange(VALUE_CHANGED, null);
233 }
234 }
235
236
237 static class ComplexFileFilter implements FileFilter {
238 FilenameFilter nameFilter;
239
240 Date dateFrom;
241 // if null, dates are ignored
242 Date dateTo;
243
244 /***
245 * TODO: document the method
246 *
247 * @param file TODO: Describe the Parameter
248 * @return TODO: Describe the Return Value
249 */
250 public boolean accept(File file) {
251 if (file.isDirectory()) {
252 return true;
253 }
254 if (!nameFilter.accept(file.getParentFile(), file.getName())) {
255 return false;
256 }
257 if (dateFrom != null) {
258 if (dateFrom.getTime() > file.lastModified()) {
259 return false;
260 }
261 }
262 if (dateTo != null) {
263 if (dateTo.getTime() < file.lastModified()) {
264 return false;
265 }
266 }
267 return true;
268 }
269 }
270
271
272 class SearchThread extends Thread {
273 FileFilter fileFilter;
274 List roots;
275 List files;
276
277 /***
278 * Constructor for the SearchThread object
279 *
280 * @param f TODO: Describe the Parameter
281 * @param r TODO: Describe the Parameter
282 * @param filter TODO: Describe the Parameter
283 */
284 SearchThread(List f, List r, FileFilter filter) {
285 roots = r;
286 fileFilter = filter;
287 files = f;
288 }
289
290 /***
291 * Main processing method for the SearchThread object
292 */
293 public void run() {
294 setPriority(SEARCH_THREAD_PRIORITY);
295 search0(files, roots, fileFilter);
296 files = null;
297 roots = null;
298 fileFilter = null;
299 }
300 }
301 }
This page was automatically generated by Maven