1 /*******************************************************************************
2 * Copyright (c) 2000, 2011 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package org.eclipse.jdt.ui.actions;
13 import java.lang.reflect.InvocationTargetException;
15 import org.eclipse.core.runtime.IAdaptable;
17 import org.eclipse.jface.action.IMenuManager;
18 import org.eclipse.jface.dialogs.MessageDialog;
19 import org.eclipse.jface.viewers.IStructuredSelection;
20 import org.eclipse.jface.window.Window;
22 import org.eclipse.jface.text.ITextSelection;
24 import org.eclipse.ui.IWorkbenchSite;
25 import org.eclipse.ui.dialogs.ElementListSelectionDialog;
27 import org.eclipse.jdt.core.IClassFile;
28 import org.eclipse.jdt.core.ICompilationUnit;
29 import org.eclipse.jdt.core.IJavaElement;
30 import org.eclipse.jdt.core.ILocalVariable;
31 import org.eclipse.jdt.core.IMember;
32 import org.eclipse.jdt.core.IPackageFragment;
33 import org.eclipse.jdt.core.IType;
34 import org.eclipse.jdt.core.JavaCore;
35 import org.eclipse.jdt.core.JavaModelException;
36 import org.eclipse.jdt.core.search.IJavaSearchScope;
38 import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
40 import org.eclipse.jdt.ui.JavaElementLabelProvider;
41 import org.eclipse.jdt.ui.search.ElementQuerySpecification;
42 import org.eclipse.jdt.ui.search.QuerySpecification;
44 import org.eclipse.jdt.internal.ui.JavaPlugin;
45 import org.eclipse.jdt.internal.ui.actions.ActionUtil;
46 import org.eclipse.jdt.internal.ui.actions.SelectionConverter;
47 import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor;
48 import org.eclipse.jdt.internal.ui.search.JavaSearchQuery;
49 import org.eclipse.jdt.internal.ui.search.JavaSearchScopeFactory;
50 import org.eclipse.jdt.internal.ui.search.SearchMessages;
51 import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
54 * Abstract class for Java search actions.
56 * Note: This class is for internal use only. Clients should not use this class.
61 * @noextend This class is not intended to be subclassed by clients.
63 public abstract class FindAction extends SelectionDispatchAction {
65 // A dummy which can't be selected in the UI
66 private static final IJavaElement RETURN_WITHOUT_BEEP= JavaCore.create(JavaPlugin.getWorkspace().getRoot());
68 private Class<?>[] fValidTypes;
69 private JavaEditor fEditor;
72 FindAction(IWorkbenchSite site) {
74 fValidTypes= getValidTypes();
78 FindAction(JavaEditor editor) {
79 this(editor.getEditorSite());
81 setEnabled(SelectionConverter.canOperateOn(fEditor));
85 * Called once by the constructors to initialize label, tooltip, image and help support of the action.
86 * To be overridden by implementors of this action.
91 * Called once by the constructors to get the list of the valid input types of the action.
92 * To be overridden by implementors of this action.
93 * @return the valid input types of the action
95 abstract Class<?>[] getValidTypes();
97 private boolean canOperateOn(IStructuredSelection sel) {
98 return sel != null && !sel.isEmpty() && canOperateOn(getJavaElement(sel, true));
101 boolean canOperateOn(IJavaElement element) {
102 if (element == null || fValidTypes == null || fValidTypes.length == 0 || !ActionUtil.isOnBuildPath(element))
105 for (int i= 0; i < fValidTypes.length; i++) {
106 if (fValidTypes[i].isInstance(element)) {
107 if (element.getElementType() == IJavaElement.PACKAGE_FRAGMENT)
108 return hasChildren((IPackageFragment)element);
116 private boolean hasChildren(IPackageFragment packageFragment) {
118 return packageFragment.hasChildren();
119 } catch (JavaModelException ex) {
124 private IJavaElement getTypeIfPossible(IJavaElement o, boolean silent) {
125 switch (o.getElementType()) {
126 case IJavaElement.COMPILATION_UNIT:
130 return findType((ICompilationUnit)o, silent);
131 case IJavaElement.CLASS_FILE:
132 return ((IClassFile)o).getType();
138 IJavaElement getJavaElement(IStructuredSelection selection, boolean silent) {
139 if (selection.size() == 1) {
140 Object firstElement= selection.getFirstElement();
141 IJavaElement elem= null;
142 if (firstElement instanceof IJavaElement)
143 elem= (IJavaElement) firstElement;
144 else if (firstElement instanceof IAdaptable)
145 elem= (IJavaElement) ((IAdaptable) firstElement).getAdapter(IJavaElement.class);
147 return getTypeIfPossible(elem, silent);
154 private void showOperationUnavailableDialog() {
155 MessageDialog.openInformation(getShell(), SearchMessages.JavaElementAction_operationUnavailable_title, getOperationUnavailableMessage());
158 String getOperationUnavailableMessage() {
159 return SearchMessages.JavaElementAction_operationUnavailable_generic;
162 private IJavaElement findType(ICompilationUnit cu, boolean silent) {
165 types= cu.getAllTypes();
166 } catch (JavaModelException ex) {
167 if (JavaModelUtil.isExceptionToBeLogged(ex))
168 ExceptionHandler.log(ex, SearchMessages.JavaElementAction_error_open_message);
170 return RETURN_WITHOUT_BEEP;
174 if (types.length == 1 || (silent && types.length > 0))
177 return RETURN_WITHOUT_BEEP;
178 if (types.length == 0)
180 String title= SearchMessages.JavaElementAction_typeSelectionDialog_title;
181 String message = SearchMessages.JavaElementAction_typeSelectionDialog_message;
182 int flags= (JavaElementLabelProvider.SHOW_DEFAULT);
184 ElementListSelectionDialog dialog= new ElementListSelectionDialog(getShell(), new JavaElementLabelProvider(flags));
185 dialog.setTitle(title);
186 dialog.setMessage(message);
187 dialog.setElements(types);
189 if (dialog.open() == Window.OK)
190 return (IType)dialog.getFirstResult();
192 return RETURN_WITHOUT_BEEP;
196 * Method declared on SelectionChangedAction.
199 public void run(IStructuredSelection selection) {
200 IJavaElement element= getJavaElement(selection, false);
201 if (element == null || !element.exists()) {
202 showOperationUnavailableDialog();
205 else if (element == RETURN_WITHOUT_BEEP)
212 * Method declared on SelectionChangedAction.
215 public void run(ITextSelection selection) {
216 if (!ActionUtil.isProcessable(fEditor))
219 String title= SearchMessages.SearchElementSelectionDialog_title;
220 String message= SearchMessages.SearchElementSelectionDialog_message;
222 IJavaElement[] elements= SelectionConverter.codeResolveForked(fEditor, true);
223 if (elements.length > 0 && canOperateOn(elements[0])) {
224 IJavaElement element= elements[0];
225 if (elements.length > 1)
226 element= SelectionConverter.selectJavaElement(elements, getShell(), title, message);
231 showOperationUnavailableDialog();
232 } catch (InvocationTargetException ex) {
233 String title= SearchMessages.Search_Error_search_title;
234 String message= SearchMessages.Search_Error_codeResolve;
235 ExceptionHandler.handle(ex, getShell(), title, message);
236 } catch (InterruptedException e) {
242 * Method declared on SelectionChangedAction.
245 public void selectionChanged(IStructuredSelection selection) {
246 setEnabled(canOperateOn(selection));
250 * Method declared on SelectionChangedAction.
253 public void selectionChanged(ITextSelection selection) {
257 * Executes this action for the given java element.
258 * @param element The java element to be found.
260 public void run(IJavaElement element) {
262 if (!ActionUtil.isProcessable(getShell(), element))
265 // will return true except for debugging purposes.
267 performNewSearch(element);
268 } catch (JavaModelException ex) {
269 ExceptionHandler.handle(ex, getShell(), SearchMessages.Search_Error_search_notsuccessful_title, SearchMessages.Search_Error_search_notsuccessful_message);
270 } catch (InterruptedException e) {
275 private void performNewSearch(IJavaElement element) throws JavaModelException, InterruptedException {
276 JavaSearchQuery query= new JavaSearchQuery(createQuery(element));
277 query.generated_1531289937401678903(this);
281 * Creates a query for the given element. Subclasses reimplement this method.
283 * @param element the element to create a query for
285 * @return returns the query
286 * @throws JavaModelException thrown when accessing the element failed
287 * @throws InterruptedException thrown when the user interrupted the query selection
289 QuerySpecification createQuery(IJavaElement element) throws JavaModelException, InterruptedException {
290 JavaSearchScopeFactory factory= JavaSearchScopeFactory.getInstance();
291 IJavaSearchScope scope= factory.createWorkspaceScope(true);
292 String description= factory.getWorkspaceScopeDescription(true);
293 return new ElementQuerySpecification(element, getLimitTo(), scope, description);
296 abstract int getLimitTo();
298 IType getType(IJavaElement element) {
303 if (element.getElementType() == IJavaElement.TYPE)
304 type= (IType)element;
305 else if (element instanceof IMember)
306 type= ((IMember)element).getDeclaringType();
307 else if (element instanceof ILocalVariable) {
308 type= (IType)element.getAncestor(IJavaElement.TYPE);
313 JavaEditor getEditor() {
317 public void generated_7671403876970365707(IMenuManager manager, ReadReferencesSearchGroup readreferencessearchgroup) {
318 update(readreferencessearchgroup.getContext().getSelection());
319 readreferencessearchgroup.addAction(this, manager);
322 public void generated_2054578905786438163(IMenuManager manager, WriteReferencesSearchGroup writereferencessearchgroup) {
323 update(writereferencessearchgroup.getContext().getSelection());
324 writereferencessearchgroup.addAction(this, manager);
327 public void generated_7641763968768104972(IMenuManager manager, ImplementorsSearchGroup implementorssearchgroup) {
328 update(implementorssearchgroup.getContext().getSelection());
329 implementorssearchgroup.addAction(this, manager);
332 public void generated_8844095363273067869(IMenuManager manager, DeclarationsSearchGroup declarationssearchgroup) {
333 update(declarationssearchgroup.getContext().getSelection());
334 declarationssearchgroup.addAction(this, manager);
337 public void generated_7002754455068930980(IMenuManager manager, ReferencesSearchGroup referencessearchgroup) {
338 update(referencessearchgroup.getContext().getSelection());
339 referencessearchgroup.addAction(this, manager);
342 public void generated_5153504316989025399(WorkingSetFindAction workingsetfindaction) {
343 workingsetfindaction.setImageDescriptor(getImageDescriptor());
344 workingsetfindaction.setToolTipText(getToolTipText());