]> git.uio.no Git - ifi-stolz-refaktor.git/blob - case-study/jdt-after/ui/org/eclipse/jdt/ui/actions/FindAction.java
Some talks, mostly identical.
[ifi-stolz-refaktor.git] / case-study / jdt-after / ui / org / eclipse / jdt / ui / actions / FindAction.java
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
7  *
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package org.eclipse.jdt.ui.actions;
12
13 import java.lang.reflect.InvocationTargetException;
14
15 import org.eclipse.core.runtime.IAdaptable;
16
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;
21
22 import org.eclipse.jface.text.ITextSelection;
23
24 import org.eclipse.ui.IWorkbenchSite;
25 import org.eclipse.ui.dialogs.ElementListSelectionDialog;
26
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;
37
38 import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
39
40 import org.eclipse.jdt.ui.JavaElementLabelProvider;
41 import org.eclipse.jdt.ui.search.ElementQuerySpecification;
42 import org.eclipse.jdt.ui.search.QuerySpecification;
43
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;
52
53 /**
54  * Abstract class for Java search actions.
55  * <p>
56  * Note: This class is for internal use only. Clients should not use this class.
57  * </p>
58  *
59  * @since 2.0
60  *
61  * @noextend This class is not intended to be subclassed by clients.
62  */
63 public abstract class FindAction extends SelectionDispatchAction {
64
65         // A dummy which can't be selected in the UI
66         private static final IJavaElement RETURN_WITHOUT_BEEP= JavaCore.create(JavaPlugin.getWorkspace().getRoot());
67
68         private Class<?>[] fValidTypes;
69         private JavaEditor fEditor;
70
71
72         FindAction(IWorkbenchSite site) {
73                 super(site);
74                 fValidTypes= getValidTypes();
75                 init();
76         }
77
78         FindAction(JavaEditor editor) {
79                 this(editor.getEditorSite());
80                 fEditor= editor;
81                 setEnabled(SelectionConverter.canOperateOn(fEditor));
82         }
83
84         /**
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.
87          */
88         abstract void init();
89
90         /**
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
94          */
95         abstract Class<?>[] getValidTypes();
96
97         private boolean canOperateOn(IStructuredSelection sel) {
98                 return sel != null && !sel.isEmpty() && canOperateOn(getJavaElement(sel, true));
99         }
100
101         boolean canOperateOn(IJavaElement element) {
102                 if (element == null || fValidTypes == null || fValidTypes.length == 0 || !ActionUtil.isOnBuildPath(element))
103                         return false;
104
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);
109                                 else
110                                         return true;
111                         }
112                 }
113                 return false;
114         }
115
116         private boolean hasChildren(IPackageFragment packageFragment) {
117                 try {
118                         return packageFragment.hasChildren();
119                 } catch (JavaModelException ex) {
120                         return false;
121                 }
122         }
123
124         private IJavaElement getTypeIfPossible(IJavaElement o, boolean silent) {
125                 switch (o.getElementType()) {
126                         case IJavaElement.COMPILATION_UNIT:
127                                 if (silent)
128                                         return o;
129                                 else
130                                         return findType((ICompilationUnit)o, silent);
131                         case IJavaElement.CLASS_FILE:
132                                 return ((IClassFile)o).getType();
133                         default:
134                                 return o;
135                 }
136         }
137
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);
146                         if (elem != null) {
147                                 return getTypeIfPossible(elem, silent);
148                         }
149
150                 }
151                 return null;
152         }
153
154         private void showOperationUnavailableDialog() {
155                 MessageDialog.openInformation(getShell(), SearchMessages.JavaElementAction_operationUnavailable_title, getOperationUnavailableMessage());
156         }
157
158         String getOperationUnavailableMessage() {
159                 return SearchMessages.JavaElementAction_operationUnavailable_generic;
160         }
161
162         private IJavaElement findType(ICompilationUnit cu, boolean silent) {
163                 IType[] types= null;
164                 try {
165                         types= cu.getAllTypes();
166                 } catch (JavaModelException ex) {
167                         if (JavaModelUtil.isExceptionToBeLogged(ex))
168                                 ExceptionHandler.log(ex, SearchMessages.JavaElementAction_error_open_message);
169                         if (silent)
170                                 return RETURN_WITHOUT_BEEP;
171                         else
172                                 return null;
173                 }
174                 if (types.length == 1 || (silent && types.length > 0))
175                         return types[0];
176                 if (silent)
177                         return RETURN_WITHOUT_BEEP;
178                 if (types.length == 0)
179                         return null;
180                 String title= SearchMessages.JavaElementAction_typeSelectionDialog_title;
181                 String message = SearchMessages.JavaElementAction_typeSelectionDialog_message;
182                 int flags= (JavaElementLabelProvider.SHOW_DEFAULT);
183
184                 ElementListSelectionDialog dialog= new ElementListSelectionDialog(getShell(), new JavaElementLabelProvider(flags));
185                 dialog.setTitle(title);
186                 dialog.setMessage(message);
187                 dialog.setElements(types);
188
189                 if (dialog.open() == Window.OK)
190                         return (IType)dialog.getFirstResult();
191                 else
192                         return RETURN_WITHOUT_BEEP;
193         }
194
195         /*
196          * Method declared on SelectionChangedAction.
197          */
198         @Override
199         public void run(IStructuredSelection selection) {
200                 IJavaElement element= getJavaElement(selection, false);
201                 if (element == null || !element.exists()) {
202                         showOperationUnavailableDialog();
203                         return;
204                 }
205                 else if (element == RETURN_WITHOUT_BEEP)
206                         return;
207
208                 run(element);
209         }
210
211         /*
212          * Method declared on SelectionChangedAction.
213          */
214         @Override
215         public void run(ITextSelection selection) {
216                 if (!ActionUtil.isProcessable(fEditor))
217                         return;
218                 try {
219                         String title= SearchMessages.SearchElementSelectionDialog_title;
220                         String message= SearchMessages.SearchElementSelectionDialog_message;
221
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);
227                                 if (element != null)
228                                         run(element);
229                         }
230                         else
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) {
237                         // ignore
238                 }
239         }
240
241         /*
242          * Method declared on SelectionChangedAction.
243          */
244         @Override
245         public void selectionChanged(IStructuredSelection selection) {
246                 setEnabled(canOperateOn(selection));
247         }
248
249         /*
250          * Method declared on SelectionChangedAction.
251          */
252         @Override
253         public void selectionChanged(ITextSelection selection) {
254         }
255
256         /**
257          * Executes this action for the given java element.
258          * @param element The java element to be found.
259          */
260         public void run(IJavaElement element) {
261
262                 if (!ActionUtil.isProcessable(getShell(), element))
263                         return;
264
265                 // will return true except for debugging purposes.
266                 try {
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) {
271                         // cancelled
272                 }
273         }
274
275         private void performNewSearch(IJavaElement element) throws JavaModelException, InterruptedException {
276                 JavaSearchQuery query= new JavaSearchQuery(createQuery(element));
277                 query.generated_1531289937401678903(this);
278         }
279
280         /**
281          * Creates a query for the given element. Subclasses reimplement this method.
282          *
283          * @param element the element to create a query for
284          *
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
288          */
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);
294         }
295
296         abstract int getLimitTo();
297
298         IType getType(IJavaElement element) {
299                 if (element == null)
300                         return null;
301
302                 IType type= null;
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);
309                 }
310                 return type;
311         }
312
313         JavaEditor getEditor() {
314                 return fEditor;
315         }
316
317         public void generated_7671403876970365707(IMenuManager manager, ReadReferencesSearchGroup readreferencessearchgroup) {
318                 update(readreferencessearchgroup.getContext().getSelection());
319                 readreferencessearchgroup.addAction(this, manager);
320         }
321
322         public void generated_2054578905786438163(IMenuManager manager, WriteReferencesSearchGroup writereferencessearchgroup) {
323                 update(writereferencessearchgroup.getContext().getSelection());
324                 writereferencessearchgroup.addAction(this, manager);
325         }
326
327         public void generated_7641763968768104972(IMenuManager manager, ImplementorsSearchGroup implementorssearchgroup) {
328                 update(implementorssearchgroup.getContext().getSelection());
329                 implementorssearchgroup.addAction(this, manager);
330         }
331
332         public void generated_8844095363273067869(IMenuManager manager, DeclarationsSearchGroup declarationssearchgroup) {
333                 update(declarationssearchgroup.getContext().getSelection());
334                 declarationssearchgroup.addAction(this, manager);
335         }
336
337         public void generated_7002754455068930980(IMenuManager manager, ReferencesSearchGroup referencessearchgroup) {
338                 update(referencessearchgroup.getContext().getSelection());
339                 referencessearchgroup.addAction(this, manager);
340         }
341
342         public void generated_5153504316989025399(WorkingSetFindAction workingsetfindaction) {
343                 workingsetfindaction.setImageDescriptor(getImageDescriptor());
344                 workingsetfindaction.setToolTipText(getToolTipText());
345         }
346
347 }