1 11 package org.eclipse.jdt.ui.actions; 12 13 import org.eclipse.swt.widgets.Shell; 14 15 import org.eclipse.jface.action.IStatusLineManager; 16 import org.eclipse.jface.viewers.IStructuredSelection; 17 18 import org.eclipse.jface.text.ITextSelection; 19 20 import org.eclipse.ui.IActionBars; 21 import org.eclipse.ui.IEditorInput; 22 import org.eclipse.ui.IEditorSite; 23 import org.eclipse.ui.IViewPart; 24 import org.eclipse.ui.IViewSite; 25 import org.eclipse.ui.IWorkbenchSite; 26 import org.eclipse.ui.PlatformUI; 27 import org.eclipse.ui.part.IPageSite; 28 import org.eclipse.ui.part.Page; 29 import org.eclipse.ui.texteditor.IEditorStatusLine; 30 31 import org.eclipse.jdt.core.IClassFile; 32 import org.eclipse.jdt.core.IJavaElement; 33 import org.eclipse.jdt.core.IMember; 34 import org.eclipse.jdt.core.ISourceRange; 35 import org.eclipse.jdt.core.JavaModelException; 36 37 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds; 38 import org.eclipse.jdt.internal.ui.JavaPlugin; 39 import org.eclipse.jdt.internal.ui.actions.ActionUtil; 40 import org.eclipse.jdt.internal.ui.javaeditor.IClassFileEditorInput; 41 import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor; 42 import org.eclipse.jdt.internal.ui.search.FindOccurrencesEngine; 43 import org.eclipse.jdt.internal.ui.search.OccurrencesFinder; 44 import org.eclipse.jdt.internal.ui.search.SearchMessages; 45 46 59 public class FindOccurrencesInFileAction extends SelectionDispatchAction { 60 61 private JavaEditor fEditor; 62 private IActionBars fActionBars; 63 64 71 public FindOccurrencesInFileAction(IViewPart part) { 72 this(part.getSite()); 73 } 74 75 82 public FindOccurrencesInFileAction(Page page) { 83 this(page.getSite()); 84 } 85 86 90 public FindOccurrencesInFileAction(JavaEditor editor) { 91 this(editor.getEditorSite()); 92 fEditor= editor; 93 setEnabled(getEditorInput(editor) != null); 94 } 95 96 104 public FindOccurrencesInFileAction(IWorkbenchSite site) { 105 super(site); 106 107 if (site instanceof IViewSite) 108 fActionBars= ((IViewSite)site).getActionBars(); 109 else if (site instanceof IEditorSite) 110 fActionBars= ((IEditorSite)site).getActionBars(); 111 else if (site instanceof IPageSite) 112 fActionBars= ((IPageSite)site).getActionBars(); 113 114 setText(SearchMessages.Search_FindOccurrencesInFile_label); 115 setToolTipText(SearchMessages.Search_FindOccurrencesInFile_tooltip); 116 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.FIND_OCCURRENCES_IN_FILE_ACTION); 117 } 118 119 121 124 public void selectionChanged(IStructuredSelection selection) { 125 setEnabled(getMember(selection) != null); 126 } 127 128 131 private IMember getMember(IStructuredSelection selection) { 132 if (selection.size() != 1) 133 return null; 134 Object o= selection.getFirstElement(); 135 if (o instanceof IMember) { 136 IMember member= (IMember)o; 137 try { 138 if (member.getNameRange() == null) 139 return null; 140 } catch (JavaModelException ex) { 141 return null; 142 } 143 144 IClassFile file= member.getClassFile(); 145 if (file != null) { 146 try { 147 if (file.getSourceRange() != null) 148 return member; 149 } catch (JavaModelException e) { 150 return null; 151 } 152 } 153 return member; 154 } 155 return null; 156 } 157 158 public void run(IStructuredSelection selection) { 159 IMember member= getMember(selection); 160 if (!ActionUtil.isProcessable(getShell(), member)) 161 return; 162 FindOccurrencesEngine engine= FindOccurrencesEngine.create(member, new OccurrencesFinder()); 163 try { 164 ISourceRange range= member.getNameRange(); 165 String result= engine.run(range.getOffset(), range.getLength()); 166 if (result != null) 167 showMessage(getShell(), fActionBars, result); 168 } catch (JavaModelException e) { 169 JavaPlugin.log(e); 170 } 171 } 172 173 private static void showMessage(Shell shell, IActionBars actionBars, String msg) { 174 if (actionBars != null) { 175 IStatusLineManager statusLine= actionBars.getStatusLineManager(); 176 if (statusLine != null) 177 statusLine.setMessage(msg); 178 } 179 shell.getDisplay().beep(); 180 } 181 182 184 187 public void selectionChanged(ITextSelection selection) { 188 } 189 190 193 public final void run(ITextSelection ts) { 194 IJavaElement input= getEditorInput(fEditor); 195 if (!ActionUtil.isProcessable(getShell(), input)) 196 return; 197 FindOccurrencesEngine engine= FindOccurrencesEngine.create(input, new OccurrencesFinder()); 198 try { 199 String result= engine.run(ts.getOffset(), ts.getLength()); 200 if (result != null) 201 showMessage(getShell(), fEditor, result); 202 } catch (JavaModelException e) { 203 JavaPlugin.log(e); 204 } 205 } 206 207 private static IJavaElement getEditorInput(JavaEditor editor) { 208 IEditorInput input= editor.getEditorInput(); 209 if (input instanceof IClassFileEditorInput) 210 return ((IClassFileEditorInput)input).getClassFile(); 211 return JavaPlugin.getDefault().getWorkingCopyManager().getWorkingCopy(input); 212 } 213 214 private static void showMessage(Shell shell, JavaEditor editor, String msg) { 215 IEditorStatusLine statusLine= (IEditorStatusLine) editor.getAdapter(IEditorStatusLine.class); 216 if (statusLine != null) 217 statusLine.setMessage(true, msg, null); 218 shell.getDisplay().beep(); 219 } 220 } 221 | Popular Tags |