1 19 20 21 package org.netbeans.modules.search; 22 23 import java.awt.EventQueue ; 24 import java.util.ArrayList ; 25 import java.util.Collections ; 26 import java.util.Iterator ; 27 import java.util.List ; 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 46 public final class SearchPerformer extends NodeAction { 47 48 50 public HelpCtx getHelpCtx() { 51 return HelpCtx.DEFAULT_HELP; } 53 54 56 public String getName() { 57 return NbBundle.getMessage(SearchPerformer.class, 58 "TEXT_FIND_ACTION_IMPL_NAME"); } 60 61 73 static List getTypes(Node[] nodes) { 74 if (nodes == null || nodes.length == 0) { 75 return Collections.EMPTY_LIST; 76 } 77 Iterator it = Utils.getSearchTypes().iterator(); 78 if (!it.hasNext()) { 79 return Collections.EMPTY_LIST; 80 } 81 List result = new ArrayList (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 105 public boolean enable(Node[] nodes) { 106 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 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 133 private static boolean canSearch(Node node) { 134 Lookup nodeLookup = node.getLookup(); 135 136 137 SearchInfo searchInfo = (SearchInfo) 138 nodeLookup.lookup(SearchInfo.class); 139 if (searchInfo != null) { 140 return searchInfo.canSearch(); 141 } 142 143 144 return nodeLookup.lookup(DataObject.Container.class) != null; 145 } 146 147 149 protected boolean asynchronous() { 150 return false; 151 } 152 153 159 public void performAction(Node[] nodes) { 160 assert EventQueue.isDispatchThread(); 161 162 String msg = Manager.getInstance().mayStartSearching(); 163 if (msg != null) { 164 168 DialogDisplayer.getDefault().notify( 169 new NotifyDescriptor.Message( 170 msg, 171 NotifyDescriptor.INFORMATION_MESSAGE)); 172 return; 173 } 174 175 176 List searchTypeList = getTypes(nodes); 177 if (searchTypeList.isEmpty()) { 178 return; 179 } 180 181 182 List clonedSearchTypeList = new ArrayList (searchTypeList.size()); 183 for (Iterator it = searchTypeList.iterator(); it.hasNext(); ) { 184 clonedSearchTypeList.add(((SearchType) it.next()).clone()); 185 } 186 187 SearchPanel searchPanel = new SearchPanel(clonedSearchTypeList); 188 189 190 if (nodes.length >= 1) { 191 Object title = nodes[0].getValue(SearchPanel.PROP_DIALOG_TITLE); 192 if (title != null && title instanceof String ) { 193 searchPanel.setTitle((String ) 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 |