1 11 package org.eclipse.jdt.ui.actions; 12 13 import java.lang.reflect.InvocationTargetException ; 14 import java.util.ArrayList ; 15 import java.util.Arrays ; 16 import java.util.List ; 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 64 public class OpenTypeHierarchyAction extends SelectionDispatchAction { 65 66 private JavaEditor fEditor; 67 68 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 97 public OpenTypeHierarchyAction(IWorkbenchSite site, ISelectionProvider provider) { 98 this(site); 99 setSpecialSelectionProvider(provider); 100 } 101 102 103 107 public OpenTypeHierarchyAction(JavaEditor editor) { 108 this(editor.getEditorSite()); 109 fEditor= editor; 110 setEnabled(SelectionConverter.canOperateOn(fEditor)); 111 } 112 113 116 public void selectionChanged(ITextSelection selection) { 117 } 118 119 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 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 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 candidates= new ArrayList (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 e) { 177 ExceptionHandler.handle(e, getShell(), getDialogTitle(), ActionMessages.SelectionConverter_codeResolve_failed); 178 } catch (InterruptedException e) { 179 } 181 } 182 183 186 public void run(IStructuredSelection selection) { 187 if (selection.size() != 1) 188 return; 189 Object 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 result= new ArrayList (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 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 getDialogTitle() { 229 return ActionMessages.OpenTypeHierarchyAction_dialog_title; 230 } 231 232 private static IStatus compileCandidates(List result, IJavaElement elem) { 233 IStatus ok= new Status(IStatus.OK, JavaPlugin.getPluginId(), 0, "", null); 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 message) { 284 return new Status(IStatus.INFO, JavaPlugin.getPluginId(), IJavaStatusConstants.INTERNAL_ERROR, message, null); 285 } 286 } 287 | Popular Tags |