1 11 12 package org.eclipse.ui.texteditor; 13 14 15 import java.util.ArrayList ; 16 import java.util.List ; 17 import java.util.ResourceBundle ; 18 import java.util.regex.PatternSyntaxException ; 19 20 import org.eclipse.swt.graphics.Point; 21 import org.eclipse.swt.widgets.Shell; 22 23 import org.eclipse.jface.action.IStatusLineManager; 24 import org.eclipse.jface.dialogs.IDialogSettings; 25 26 import org.eclipse.jface.text.IFindReplaceTarget; 27 import org.eclipse.jface.text.IFindReplaceTargetExtension3; 28 import org.eclipse.jface.text.TextUtilities; 29 30 import org.eclipse.ui.IEditorPart; 31 import org.eclipse.ui.IWorkbenchPart; 32 import org.eclipse.ui.IWorkbenchWindow; 33 import org.eclipse.ui.internal.texteditor.TextEditorPlugin; 34 35 36 45 public class FindNextAction extends ResourceAction implements IUpdate { 46 47 48 private IFindReplaceTarget fTarget; 49 50 private IWorkbenchPart fWorkbenchPart; 51 52 private IWorkbenchWindow fWorkbenchWindow; 53 54 private IDialogSettings fDialogSettings; 55 56 private List fFindHistory= new ArrayList (); 57 58 private String fFindString; 59 60 private boolean fForward; 61 62 private boolean fWrapInit; 63 64 private boolean fCaseInit; 65 66 private boolean fWholeWordInit; 67 72 private boolean fRegExSearch; 73 78 private String fSelection; 79 80 93 public FindNextAction(ResourceBundle bundle, String prefix, IWorkbenchPart workbenchPart, boolean forward) { 94 super(bundle, prefix); 95 fWorkbenchPart= workbenchPart; 96 fForward= forward; 97 update(); 98 } 99 100 115 public FindNextAction(ResourceBundle bundle, String prefix, IWorkbenchWindow workbenchWindow, boolean forward) { 116 super(bundle, prefix); 117 fWorkbenchWindow= workbenchWindow; 118 fForward= forward; 119 update(); 120 } 121 122 126 private String getFindString() { 127 String string= getSelectionString(); 128 129 if ((string == null || fRegExSearch && string.equals(fSelection)) && !fFindHistory.isEmpty()) 130 string= (String ) fFindHistory.get(0); 131 132 return string; 133 } 134 135 139 private IStatusLineManager getStatusLineManager() { 140 IEditorPart editor= fWorkbenchPart.getSite().getPage().getActiveEditor(); 141 if (editor == null) 142 return null; 143 144 return editor.getEditorSite().getActionBars().getStatusLineManager(); 145 } 146 147 152 private void statusNotFound() { 153 fWorkbenchPart.getSite().getShell().getDisplay().beep(); 154 155 IStatusLineManager manager= getStatusLineManager(); 156 if (manager == null) 157 return; 158 159 manager.setMessage(EditorMessages.FindNext_Status_noMatch_label); 160 } 161 162 165 private void statusClear() { 166 IStatusLineManager manager= getStatusLineManager(); 167 if (manager == null) 168 return; 169 170 manager.setErrorMessage(""); manager.setMessage(""); } 173 174 177 public void run() { 178 if (fTarget != null) { 179 readConfiguration(); 180 181 fFindString= getFindString(); 182 if (fFindString == null) { 183 statusNotFound(); 184 return; 185 } 186 187 boolean wholeWord= fWholeWordInit && !fRegExSearch && isWord(fFindString); 188 189 statusClear(); 190 if (!findNext(fFindString, fForward, fCaseInit, fWrapInit, wholeWord, fRegExSearch)) 191 statusNotFound(); 192 193 writeConfiguration(); 194 } 195 } 196 197 205 private boolean isWord(String str) { 206 if (str == null || str.length() == 0) 207 return false; 208 209 for (int i= 0; i < str.length(); i++) { 210 if (!Character.isJavaIdentifierPart(str.charAt(i))) 211 return false; 212 } 213 return true; 214 } 215 216 219 public void update() { 220 221 if (fWorkbenchPart == null && fWorkbenchWindow != null) 222 fWorkbenchPart= fWorkbenchWindow.getPartService().getActivePart(); 223 224 if (fWorkbenchPart != null) 225 fTarget= (IFindReplaceTarget) fWorkbenchPart.getAdapter(IFindReplaceTarget.class); 226 else 227 fTarget= null; 228 229 setEnabled(fTarget != null && fTarget.canPerformFind()); 230 } 231 232 236 private int findIndex(String findString, int startPosition, boolean forwardSearch, boolean caseSensitive, boolean wrapSearch, boolean wholeWord, boolean regExSearch) { 237 238 if (forwardSearch) { 239 if (wrapSearch) { 240 int index= findAndSelect(startPosition, findString, true, caseSensitive, wholeWord, regExSearch); 241 if (index == -1) { 242 beep(); 243 index= findAndSelect(-1, findString, true, caseSensitive, wholeWord, regExSearch); 244 } 245 return index; 246 } 247 return findAndSelect(startPosition, findString, true, caseSensitive, wholeWord, regExSearch); 248 } 249 250 if (wrapSearch) { 252 int index= findAndSelect(startPosition - 1, findString, false, caseSensitive, wholeWord, regExSearch); 253 if (index == -1) { 254 beep(); 255 index= findAndSelect(-1, findString, false, caseSensitive, wholeWord, regExSearch); 256 } 257 return index; 258 } 259 return findAndSelect(startPosition - 1, findString, false, caseSensitive, wholeWord, regExSearch); 260 } 261 262 274 private boolean findNext(String findString, boolean forwardSearch, boolean caseSensitive, boolean wrapSearch, boolean wholeWord, boolean regExSearch) { 275 276 Point r= fTarget.getSelection(); 277 int findReplacePosition= r.x; 278 if (forwardSearch) 279 findReplacePosition += r.y; 280 281 int index= findIndex(findString, findReplacePosition, forwardSearch, caseSensitive, wrapSearch, wholeWord, regExSearch); 282 283 if (index != -1) 284 return true; 285 286 return false; 287 } 288 289 private void beep() { 290 Shell shell= null; 291 if (fWorkbenchPart != null) 292 shell= fWorkbenchPart.getSite().getShell(); 293 else if (fWorkbenchWindow != null) 294 shell= fWorkbenchWindow.getShell(); 295 296 if (shell != null && !shell.isDisposed()) 297 shell.getDisplay().beep(); 298 } 299 300 314 private int findAndSelect(int offset, String findString, boolean forwardSearch, boolean caseSensitive, boolean wholeWord, boolean regExSearch) { 315 if (fTarget instanceof IFindReplaceTargetExtension3) { 316 try { 317 return ((IFindReplaceTargetExtension3)fTarget).findAndSelect(offset, findString, forwardSearch, caseSensitive, wholeWord, regExSearch); 318 } catch (PatternSyntaxException ex) { 319 return -1; 320 } 321 } 322 return fTarget.findAndSelect(offset, findString, forwardSearch, caseSensitive, wholeWord); 323 } 324 325 327 333 private IDialogSettings getDialogSettings() { 334 IDialogSettings settings= TextEditorPlugin.getDefault().getDialogSettings(); 335 fDialogSettings= settings.getSection(FindReplaceDialog.class.getName()); 336 if (fDialogSettings == null) 337 fDialogSettings= settings.addNewSection(FindReplaceDialog.class.getName()); 338 return fDialogSettings; 339 } 340 341 345 private void readConfiguration() { 346 IDialogSettings s= getDialogSettings(); 347 348 fWrapInit= s.getBoolean("wrap"); fCaseInit= s.getBoolean("casesensitive"); fWholeWordInit= s.getBoolean("wholeword"); fRegExSearch= s.getBoolean("isRegEx"); fSelection= s.get("selection"); 354 String [] findHistory= s.getArray("findhistory"); if (findHistory != null) { 356 fFindHistory.clear(); 357 for (int i= 0; i < findHistory.length; i++) 358 fFindHistory.add(findHistory[i]); 359 } 360 } 361 362 365 private void writeConfiguration() { 366 if (fFindString == null) 367 return; 368 369 IDialogSettings s= getDialogSettings(); 370 371 String selection= fTarget.getSelectionText(); 372 if (selection == null) 373 selection= ""; s.put("selection", selection); 376 if (!fFindHistory.isEmpty() && fFindString.equals(fFindHistory.get(0))) 377 return; 378 379 int index= fFindHistory.indexOf(fFindString); 380 if (index != -1) 381 fFindHistory.remove(index); 382 fFindHistory.add(0, fFindString); 383 384 while (fFindHistory.size() > 8) 385 fFindHistory.remove(8); 386 String [] names= new String [fFindHistory.size()]; 387 fFindHistory.toArray(names); 388 s.put("findhistory", names); } 390 391 396 private String getSelectionString() { 397 398 402 String selection= fTarget.getSelectionText(); 403 if (selection != null && selection.length() > 0) { 404 int[] info= TextUtilities.indexOf(TextUtilities.DELIMITERS, selection, 0); 405 if (info[0] > 0) 406 return selection.substring(0, info[0]); 407 else if (info[0] == -1) 408 return selection; 409 } 410 return null; 411 } 412 } 413 | Popular Tags |