1 19 20 21 package org.netbeans.modules.search; 22 23 24 import java.beans.PropertyChangeEvent ; 25 import java.beans.PropertyChangeListener ; 26 import java.util.List ; 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 46 final class SearchTask implements Runnable , Cancellable { 47 48 49 private final Node[] nodes; 50 51 private final SearchType[] customizedSearchTypes; 52 53 private final List searchTypeList; 54 55 private ResultModel resultModel; 56 57 private SearchGroup searchGroup; 58 62 private PropertyChangeListener searchGroupListener; 63 64 private boolean notifyWhenFinished = true; 65 66 private volatile boolean interrupted = false; 67 68 private volatile boolean finished = false; 69 70 private final String replaceString; 71 72 73 80 public SearchTask(final Node[] nodes, 81 final List 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 97 private static String 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 108 private boolean isSearchAndReplace() { 109 return (replaceString != null); 110 } 111 112 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 123 ensureResultModelExists(); 124 if (searchGroup == null) { 125 return; 126 } 127 128 searchGroup.addPropertyChangeListener(WeakListeners.propertyChange( 130 searchGroupListener = new PropertyChangeListener () { 131 public void propertyChange(PropertyChangeEvent 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 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 157 ResultModel getResultModel() { 158 ensureResultModelExists(); 159 return resultModel; 160 } 161 162 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 182 private void matchingObjectFound(Object object) { 183 boolean canContinue = resultModel.objectFound(object); 184 if (!canContinue) { 185 searchGroup.stopSearch(); 186 } 187 } 188 189 199 void stop(boolean notifyWhenFinished) { 200 if (notifyWhenFinished == false) { this.notifyWhenFinished = notifyWhenFinished; 202 } 203 stop(); 204 } 205 206 211 void stop() { 212 if (!finished) { 213 interrupted = true; 214 } 215 if (searchGroup != null) { 216 searchGroup.stopSearch(); 217 } 218 } 219 220 227 public boolean cancel() { 228 stop(); 229 return true; 230 } 231 232 237 boolean notifyWhenFinished() { 238 return notifyWhenFinished; 239 } 240 241 248 boolean wasInterrupted() { 249 return interrupted; 250 } 251 252 } 253 | Popular Tags |