KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > search > SearchTask


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20
21 package org.netbeans.modules.search;
22
23
24 import java.beans.PropertyChangeEvent JavaDoc;
25 import java.beans.PropertyChangeListener JavaDoc;
26 import java.util.List JavaDoc;
27 import org.netbeans.api.progress.ProgressHandle;
28 import org.netbeans.api.progress.ProgressHandleFactory;
29 import org.netbeans.modules.search.types.FullTextType;
30 import org.openide.LifecycleManager;
31
32 import org.openide.nodes.Node;
33 import org.openide.util.Cancellable;
34 import org.openide.util.NbBundle;
35 import org.openide.util.WeakListeners;
36 import org.openidex.search.SearchGroup;
37 import org.openidex.search.SearchType;
38
39
40 /**
41  * Task performing search.
42  *
43  * @author Peter Zavadsky
44  * @author Marian Petras
45  */

46 final class SearchTask implements Runnable JavaDoc, Cancellable {
47
48     /** nodes to search */
49     private final Node[] nodes;
50     /** */
51     private final SearchType[] customizedSearchTypes;
52     /** */
53     private final List JavaDoc searchTypeList;
54     /** ResultModel result model. */
55     private ResultModel resultModel;
56     /** <code>SearchGroup</code> to search on. */
57     private SearchGroup searchGroup;
58     /**
59      * listener which listens for the search group's notifications of found
60      * objects
61      */

62     private PropertyChangeListener JavaDoc searchGroupListener;
63     /** attribute used by class <code>Manager</code> */
64     private boolean notifyWhenFinished = true;
65     /** */
66     private volatile boolean interrupted = false;
67     /** */
68     private volatile boolean finished = false;
69     /** */
70     private final String JavaDoc replaceString;
71     
72     
73     /**
74      * Creates a new <code>SearchTask</code>.
75      *
76      * @param
77      * @param
78      * @param
79      */

80     public SearchTask(final Node[] nodes,
81                       final List JavaDoc searchTypeList,
82                       final SearchType[] customizedSearchTypes) {
83         this.nodes = nodes;
84         this.searchTypeList = searchTypeList;
85         this.customizedSearchTypes = customizedSearchTypes;
86         
87         this.replaceString = getReplaceString(customizedSearchTypes);
88     }
89     
90     /**
91      * Checks whether we are about to do search &amp; replace.
92      *
93      * @param customizedSearchTypes array of applied search types
94      * @return replace string set in the full text search,
95      * or {@code null} if no replacing should be performed
96      */

97     private static String JavaDoc getReplaceString(SearchType[] searchTypes) {
98         for (SearchType searchType : searchTypes) {
99             if (searchType.getClass() == FullTextType.class) {
100                 return ((FullTextType) searchType).getReplaceString();
101             }
102         }
103         return null;
104     }
105     
106     /**
107      */

108     private boolean isSearchAndReplace() {
109         return (replaceString != null);
110     }
111     
112     /** Runs the search task. */
113     public void run() {
114         if (isSearchAndReplace()) {
115             LifecycleManager.getDefault().saveAll();
116         }
117         
118         ProgressHandle progressHandle = ProgressHandleFactory.createHandle(
119                 NbBundle.getMessage(ResultView.class,"TEXT_SEARCHING___"), this);
120         progressHandle.start();
121         
122         /* Start the actual search: */
123         ensureResultModelExists();
124         if (searchGroup == null) {
125             return;
126         }
127
128         //Set of search types to be used able to search on the same object type.
129
searchGroup.addPropertyChangeListener(WeakListeners.propertyChange(
130             searchGroupListener = new PropertyChangeListener JavaDoc() {
131                 public void propertyChange(PropertyChangeEvent JavaDoc evt) {
132                     if (SearchGroup.PROP_FOUND.equals(evt.getPropertyName())) {
133                         matchingObjectFound(evt.getNewValue());
134                     }
135                 }
136             }, searchGroup)
137         );
138
139         searchGroup.setSearchRootNodes(nodes);
140         try {
141             searchGroup.search();
142         } catch (RuntimeException JavaDoc ex) {
143             resultModel.searchException(ex);
144             ex.printStackTrace();
145         } finally {
146             finished = true;
147             progressHandle.finish();
148         }
149     }
150     
151     SearchTask createNewGeneration() {
152         return new SearchTask(nodes, searchTypeList, customizedSearchTypes);
153     }
154
155     /**
156      */

157     ResultModel getResultModel() {
158         ensureResultModelExists();
159         return resultModel;
160     }
161     
162     /**
163      */

164     private void ensureResultModelExists() {
165         if (resultModel == null) {
166             SearchGroup[] groups
167                     = SearchGroup.createSearchGroups(customizedSearchTypes);
168             searchGroup = (groups.length != 0) ? groups[0] : null;
169             resultModel = new ResultModel(searchTypeList,
170                                           searchGroup,
171                                           replaceString);
172         }
173     }
174
175     /**
176      * Called when a matching object is found by the <code>SearchGroup</code>.
177      * Notifies the result model of the found object and stops searching
178      * if number of the found objects reached the limit.
179      *
180      * @param object found matching object
181      */

182     private void matchingObjectFound(Object JavaDoc object) {
183         boolean canContinue = resultModel.objectFound(object);
184         if (!canContinue) {
185             searchGroup.stopSearch();
186         }
187     }
188     
189     /**
190      * Stops this search task.
191      * This method also sets a value of attribute
192      * <code>notifyWhenFinished</code>. This method may be called multiple
193      * times (even if this task is already stopped) to change the value
194      * of the attribute.
195      *
196      * @param notifyWhenFinished new value of attribute
197      * <code>notifyWhenFinished</code>
198      */

199     void stop(boolean notifyWhenFinished) {
200         if (notifyWhenFinished == false) { //allow only change true -> false
201
this.notifyWhenFinished = notifyWhenFinished;
202         }
203         stop();
204     }
205     
206     /**
207      * Stops this search task.
208      *
209      * @see #stop(boolean)
210      */

211     void stop() {
212         if (!finished) {
213             interrupted = true;
214         }
215         if (searchGroup != null) {
216             searchGroup.stopSearch();
217         }
218     }
219     
220     /**
221      * Cancel processing of the task.
222      *
223      * @return true if the task was succesfully cancelled, false if job
224      * can't be cancelled for some reason
225      * @see org.openide.util.Cancellable#cancel
226      */

227     public boolean cancel() {
228         stop();
229         return true;
230     }
231
232     /**
233      * Returns value of attribute <code>notifyWhenFinished</code>.
234      *
235      * @return current value of the attribute
236      */

237     boolean notifyWhenFinished() {
238         return notifyWhenFinished;
239     }
240     
241     /**
242      * Was this search task interrupted?
243      *
244      * @return <code>true</code> if this method has been interrupted
245      * by calling {@link #stop()} or {@link #stop(boolean)}
246      * during the search; <code>false</code> otherwise
247      */

248     boolean wasInterrupted() {
249         return interrupted;
250     }
251
252 }
253
Popular Tags