KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.awt.EventQueue JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Collections JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.List JavaDoc;
28 import org.openide.DialogDisplayer;
29 import org.openide.NotifyDescriptor;
30 import org.openide.loaders.DataObject;
31 import org.openide.nodes.Node;
32 import org.openide.util.HelpCtx;
33 import org.openide.util.Lookup;
34 import org.openide.util.NbBundle;
35 import org.openide.util.actions.NodeAction;
36 import org.openidex.search.SearchInfo;
37 import org.openidex.search.SearchType;
38
39
40 /**
41  *
42  * @author Petr Kuzel
43  * @author Marian Petras
44  * @see org.openide.actions.FindAction
45  */

46 public final class SearchPerformer extends NodeAction {
47     
48     /**
49      */

50     public HelpCtx getHelpCtx() {
51         return HelpCtx.DEFAULT_HELP; //PENDING
52
}
53     
54     /**
55      */

56     public String JavaDoc getName() {
57         return NbBundle.getMessage(SearchPerformer.class,
58                                    "TEXT_FIND_ACTION_IMPL_NAME"); //NOI18N
59
}
60
61     /**
62      * Collects a list of search types that are able to search the specified
63      * nodes.
64      *
65      * @param nodes nodes to be searched
66      * @return list of <code>SearchType</code>s being able to search
67      * the specified nodes, or an empty list if <code>nodes</code>
68      * is <code>null</code> or empty; or if no applicable search type
69      * is found for the specified set of nodes
70      * @see Utils#getSearchTypes()
71      * @see SearchType#enabled
72      */

73     static List JavaDoc getTypes(Node[] nodes) {
74         if (nodes == null || nodes.length == 0) {
75             return Collections.EMPTY_LIST;
76         }
77         Iterator JavaDoc it = Utils.getSearchTypes().iterator();
78         if (!it.hasNext()) {
79             return Collections.EMPTY_LIST;
80         }
81         List JavaDoc result = new ArrayList JavaDoc(5);
82         do {
83             SearchType searchType = (SearchType) it.next();
84             if (searchType.enabled(nodes) && !result.contains(searchType)) {
85                 result.add(searchType);
86             }
87         } while (it.hasNext());
88         return result;
89     }
90
91     /**
92      * Decides whether searching should be enabled with respect to a set
93      * of selected nodes.
94      * Searching is enabled if searching instructions
95      * (<code>SearchInfo</code> object) are available for all selected nodes
96      * and at least one registered search type is able to search all the
97      * selected nodes.
98      *
99      * @param nodes selected nodes
100      * @return <code>true</code> if searching the selected nodes should be
101      * enabled; <code>false</code> otherwise
102      * @see SearchInfo
103      * @see SearchType
104      */

105     public boolean enable(Node[] nodes) {
106         //System.out.println("enable(...)");
107
//System.out.println(" - nodes count: " + nodes.length);
108
if (nodes == null || nodes.length == 0) {
109             return false;
110         }
111
112         for (int i = 0; i < nodes.length; i++) {
113             if (!canSearch(nodes[i])) {
114                 return false;
115             }
116         }
117
118         Iterator JavaDoc it = Utils.getSearchTypes().iterator();
119         
120         while (it.hasNext()) {
121             SearchType searchType = (SearchType) it.next();
122
123             if (searchType.enabled(nodes)) {
124                 return true;
125             }
126         }
127
128         return false;
129     }
130
131     /**
132      */

133     private static boolean canSearch(Node node) {
134         Lookup nodeLookup = node.getLookup();
135         
136         /* 1st try - is the SearchInfo object in the node's lookup? */
137         SearchInfo searchInfo = (SearchInfo)
138                                 nodeLookup.lookup(SearchInfo.class);
139         if (searchInfo != null) {
140             return searchInfo.canSearch();
141         }
142     
143         /* 2nd try - does the node represent a DataObject.Container? */
144         return nodeLookup.lookup(DataObject.Container.class) != null;
145     }
146     
147     /**
148      */

149     protected boolean asynchronous() {
150         return false;
151     }
152
153     /**
154      * Performs search on nodes. Displays <code>CriteriaView</code> with predefined search types set.
155      *
156      * @param nodes currently selected nodes
157      * @see CriteriaView
158      */

159     public void performAction(Node[] nodes) {
160         assert EventQueue.isDispatchThread();
161         
162         String JavaDoc msg = Manager.getInstance().mayStartSearching();
163         if (msg != null) {
164             /*
165              * Search cannot be started, for example because the replace
166              * operation has not finished yet.
167              */

168             DialogDisplayer.getDefault().notify(
169                     new NotifyDescriptor.Message(
170                             msg,
171                             NotifyDescriptor.INFORMATION_MESSAGE));
172             return;
173         }
174
175         /* Get a list of applicable search types: */
176         List JavaDoc searchTypeList = getTypes(nodes);
177         if (searchTypeList.isEmpty()) {
178             return;
179         }
180         
181         /* Clone the list (deep copy): */
182         List JavaDoc clonedSearchTypeList = new ArrayList JavaDoc(searchTypeList.size());
183         for (Iterator JavaDoc it = searchTypeList.iterator(); it.hasNext(); ) {
184             clonedSearchTypeList.add(((SearchType) it.next()).clone());
185         }
186         
187         SearchPanel searchPanel = new SearchPanel(clonedSearchTypeList);
188         
189         /* a node may bear title of the Find dialog: */
190         if (nodes.length >= 1) {
191             Object JavaDoc title = nodes[0].getValue(SearchPanel.PROP_DIALOG_TITLE);
192             if (title != null && title instanceof String JavaDoc) {
193                 searchPanel.setTitle((String JavaDoc) title);
194             }
195         }
196
197         searchPanel.showDialog();
198         if (searchPanel.getReturnStatus() != SearchPanel.RET_OK) {
199             return;
200         }
201         
202         ResultView resultView = ResultView.getInstance();
203         resultView.rememberInput(nodes,
204                                  Utils.cloneSearchTypes(clonedSearchTypeList));
205         resultView.open();
206         resultView.requestActive();
207         
208         Manager.getInstance().scheduleSearchTask(
209                 new SearchTask(nodes,
210                                clonedSearchTypeList,
211                                searchPanel.getCustomizedSearchTypes()));
212     }
213     
214 }
215
Popular Tags