KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > ui > actions > OpenTypeHierarchyAction


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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 JavaDoc;
14 import java.util.ArrayList JavaDoc;
15 import java.util.Arrays JavaDoc;
16 import java.util.List JavaDoc;
17
18 import org.eclipse.core.runtime.IStatus;
19 import org.eclipse.core.runtime.Status;
20
21 import org.eclipse.jface.dialogs.ErrorDialog;
22 import org.eclipse.jface.viewers.ISelectionProvider;
23 import org.eclipse.jface.viewers.IStructuredSelection;
24
25 import org.eclipse.jface.text.ITextSelection;
26
27 import org.eclipse.ui.IWorkbenchSite;
28 import org.eclipse.ui.PlatformUI;
29
30 import org.eclipse.jdt.core.IClassFile;
31 import org.eclipse.jdt.core.ICompilationUnit;
32 import org.eclipse.jdt.core.IImportDeclaration;
33 import org.eclipse.jdt.core.IJavaElement;
34 import org.eclipse.jdt.core.IPackageFragment;
35 import org.eclipse.jdt.core.IType;
36 import org.eclipse.jdt.core.JavaModelException;
37 import org.eclipse.jdt.core.Signature;
38
39 import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
40
41 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
42 import org.eclipse.jdt.internal.ui.IJavaStatusConstants;
43 import org.eclipse.jdt.internal.ui.JavaPlugin;
44 import org.eclipse.jdt.internal.ui.actions.ActionMessages;
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.browsing.LogicalPackage;
48 import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor;
49 import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
50 import org.eclipse.jdt.internal.ui.util.OpenTypeHierarchyUtil;
51
52 /**
53  * This action opens a type hierarchy on the selected type.
54  * <p>
55  * The action is applicable to selections containing elements of type
56  * <code>IType</code>.
57  *
58  * <p>
59  * This class may be instantiated; it is not intended to be subclassed.
60  * </p>
61  *
62  * @since 2.0
63  */

64 public class OpenTypeHierarchyAction extends SelectionDispatchAction {
65     
66     private JavaEditor fEditor;
67     
68     /**
69      * Creates a new <code>OpenTypeHierarchyAction</code>. The action requires
70      * that the selection provided by the site's selection provider is of type <code>
71      * org.eclipse.jface.viewers.IStructuredSelection</code>.
72      *
73      * @param site the site providing context information for this action
74      */

75     public OpenTypeHierarchyAction(IWorkbenchSite site) {
76         super(site);
77         setText(ActionMessages.OpenTypeHierarchyAction_label);
78         setToolTipText(ActionMessages.OpenTypeHierarchyAction_tooltip);
79         setDescription(ActionMessages.OpenTypeHierarchyAction_description);
80         PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.OPEN_TYPE_HIERARCHY_ACTION);
81     }
82     
83     /**
84      * Creates a new <code>OpenTypeHierarchyAction</code>. The action requires
85      * that the selection provided by the given selection provider is of type <code>
86      * org.eclipse.jface.viewers.IStructuredSelection</code>.
87      *
88      * @param site the site providing context information for this action
89      * @param provider a special selection provider which is used instead
90      * of the site's selection provider or <code>null</code> to use the site's
91      * selection provider
92      *
93      * @since 3.2
94      * @deprecated Use {@link #setSpecialSelectionProvider(ISelectionProvider)} instead. This API will be
95      * removed after 3.2 M5.
96      */

97     public OpenTypeHierarchyAction(IWorkbenchSite site, ISelectionProvider provider) {
98         this(site);
99         setSpecialSelectionProvider(provider);
100     }
101
102     
103     /**
104      * Note: This constructor is for internal use only. Clients should not call this constructor.
105      * @param editor the Java editor
106      */

107     public OpenTypeHierarchyAction(JavaEditor editor) {
108         this(editor.getEditorSite());
109         fEditor= editor;
110         setEnabled(SelectionConverter.canOperateOn(fEditor));
111     }
112     
113     /* (non-Javadoc)
114      * Method declared on SelectionDispatchAction.
115      */

116     public void selectionChanged(ITextSelection selection) {
117     }
118
119     /* (non-Javadoc)
120      * Method declared on SelectionDispatchAction.
121      */

122     public void selectionChanged(IStructuredSelection selection) {
123         setEnabled(isEnabled(selection));
124     }
125     
126     private boolean isEnabled(IStructuredSelection selection) {
127         if (selection.size() != 1)
128             return false;
129         Object JavaDoc input= selection.getFirstElement();
130         
131         
132         if (input instanceof LogicalPackage)
133             return true;
134         
135         if (!(input instanceof IJavaElement))
136             return false;
137         switch (((IJavaElement)input).getElementType()) {
138             case IJavaElement.INITIALIZER:
139             case IJavaElement.METHOD:
140             case IJavaElement.FIELD:
141             case IJavaElement.TYPE:
142                 return true;
143             case IJavaElement.PACKAGE_FRAGMENT_ROOT:
144             case IJavaElement.JAVA_PROJECT:
145             case IJavaElement.PACKAGE_FRAGMENT:
146             case IJavaElement.PACKAGE_DECLARATION:
147             case IJavaElement.IMPORT_DECLARATION:
148             case IJavaElement.CLASS_FILE:
149             case IJavaElement.COMPILATION_UNIT:
150                 return true;
151             case IJavaElement.LOCAL_VARIABLE:
152             default:
153                 return false;
154         }
155     }
156     
157     /* (non-Javadoc)
158      * Method declared on SelectionDispatchAction.
159      */

160     public void run(ITextSelection selection) {
161         IJavaElement input= SelectionConverter.getInput(fEditor);
162         if (!ActionUtil.isProcessable(getShell(), input))
163             return;
164         
165         try {
166             IJavaElement[] elements= SelectionConverter.codeResolveOrInputForked(fEditor);
167             if (elements == null)
168                 return;
169             List JavaDoc candidates= new ArrayList JavaDoc(elements.length);
170             for (int i= 0; i < elements.length; i++) {
171                 IJavaElement[] resolvedElements= OpenTypeHierarchyUtil.getCandidates(elements[i]);
172                 if (resolvedElements != null)
173                     candidates.addAll(Arrays.asList(resolvedElements));
174             }
175             run((IJavaElement[])candidates.toArray(new IJavaElement[candidates.size()]));
176         } catch (InvocationTargetException JavaDoc e) {
177             ExceptionHandler.handle(e, getShell(), getDialogTitle(), ActionMessages.SelectionConverter_codeResolve_failed);
178         } catch (InterruptedException JavaDoc e) {
179             // cancelled
180
}
181     }
182     
183     /* (non-Javadoc)
184      * Method declared on SelectionDispatchAction.
185      */

186     public void run(IStructuredSelection selection) {
187         if (selection.size() != 1)
188             return;
189         Object JavaDoc input= selection.getFirstElement();
190         
191         if (input instanceof LogicalPackage) {
192             IPackageFragment[] fragments= ((LogicalPackage)input).getFragments();
193             if (fragments.length == 0)
194                 return;
195             input= fragments[0];
196         }
197
198         if (!(input instanceof IJavaElement)) {
199             IStatus status= createStatus(ActionMessages.OpenTypeHierarchyAction_messages_no_java_element);
200             ErrorDialog.openError(getShell(), getDialogTitle(), ActionMessages.OpenTypeHierarchyAction_messages_title, status);
201             return;
202         }
203         IJavaElement element= (IJavaElement) input;
204         if (!ActionUtil.isProcessable(getShell(), element))
205             return;
206
207         List JavaDoc result= new ArrayList JavaDoc(1);
208         IStatus status= compileCandidates(result, element);
209         if (status.isOK()) {
210             run((IJavaElement[]) result.toArray(new IJavaElement[result.size()]));
211         } else {
212             ErrorDialog.openError(getShell(), getDialogTitle(), ActionMessages.OpenTypeHierarchyAction_messages_title, status);
213         }
214     }
215
216     /*
217      * No Javadoc since the method isn't meant to be public but is
218      * since the beginning
219      */

220     public void run(IJavaElement[] elements) {
221         if (elements.length == 0) {
222             getShell().getDisplay().beep();
223             return;
224         }
225         OpenTypeHierarchyUtil.open(elements, getSite().getWorkbenchWindow());
226     }
227     
228     private static String JavaDoc getDialogTitle() {
229         return ActionMessages.OpenTypeHierarchyAction_dialog_title;
230     }
231     
232     private static IStatus compileCandidates(List JavaDoc result, IJavaElement elem) {
233         IStatus ok= new Status(IStatus.OK, JavaPlugin.getPluginId(), 0, "", null); //$NON-NLS-1$
234
try {
235             switch (elem.getElementType()) {
236                 case IJavaElement.INITIALIZER:
237                 case IJavaElement.METHOD:
238                 case IJavaElement.FIELD:
239                 case IJavaElement.TYPE:
240                 case IJavaElement.PACKAGE_FRAGMENT_ROOT:
241                 case IJavaElement.JAVA_PROJECT:
242                     result.add(elem);
243                     return ok;
244                 case IJavaElement.PACKAGE_FRAGMENT:
245                     if (((IPackageFragment)elem).containsJavaResources()) {
246                         result.add(elem);
247                         return ok;
248                     }
249                     return createStatus(ActionMessages.OpenTypeHierarchyAction_messages_no_java_resources);
250                 case IJavaElement.PACKAGE_DECLARATION:
251                     result.add(elem.getAncestor(IJavaElement.PACKAGE_FRAGMENT));
252                     return ok;
253                 case IJavaElement.IMPORT_DECLARATION:
254                     IImportDeclaration decl= (IImportDeclaration) elem;
255                     if (decl.isOnDemand()) {
256                         elem= JavaModelUtil.findTypeContainer(elem.getJavaProject(), Signature.getQualifier(elem.getElementName()));
257                     } else {
258                         elem= elem.getJavaProject().findType(elem.getElementName());
259                     }
260                     if (elem != null) {
261                         result.add(elem);
262                         return ok;
263                     }
264                     return createStatus(ActionMessages.OpenTypeHierarchyAction_messages_unknown_import_decl);
265                 case IJavaElement.CLASS_FILE:
266                     result.add(((IClassFile)elem).getType());
267                     return ok;
268                 case IJavaElement.COMPILATION_UNIT:
269                     ICompilationUnit cu= (ICompilationUnit)elem;
270                     IType[] types= cu.getTypes();
271                     if (types.length > 0) {
272                         result.addAll(Arrays.asList(types));
273                         return ok;
274                     }
275                     return createStatus(ActionMessages.OpenTypeHierarchyAction_messages_no_types);
276             }
277         } catch (JavaModelException e) {
278             return e.getStatus();
279         }
280         return createStatus(ActionMessages.OpenTypeHierarchyAction_messages_no_valid_java_element);
281     }
282     
283     private static IStatus createStatus(String JavaDoc message) {
284         return new Status(IStatus.INFO, JavaPlugin.getPluginId(), IJavaStatusConstants.INTERNAL_ERROR, message, null);
285     }
286 }
287
Popular Tags