1 11 package org.eclipse.jdt.ui.actions; 12 13 import org.eclipse.jdt.core.IJavaElement; 14 import org.eclipse.jdt.core.IOpenable; 15 import org.eclipse.jdt.core.JavaModelException; 16 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds; 17 import org.eclipse.jdt.internal.ui.JavaPlugin; 18 import org.eclipse.jdt.internal.ui.actions.ActionMessages; 19 import org.eclipse.jdt.internal.ui.actions.SelectionConverter; 20 import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor; 21 import org.eclipse.jdt.internal.ui.packageview.PackageExplorerPart; 22 import org.eclipse.jface.dialogs.ErrorDialog; 23 import org.eclipse.jface.text.ITextSelection; 24 import org.eclipse.jface.viewers.IStructuredSelection; 25 import org.eclipse.ui.IWorkbenchSite; 26 import org.eclipse.ui.PlatformUI; 27 39 public class ShowInPackageViewAction extends SelectionDispatchAction { 40 41 private JavaEditor fEditor; 42 43 50 public ShowInPackageViewAction(IWorkbenchSite site) { 51 super(site); 52 setText(ActionMessages.ShowInPackageViewAction_label); 53 setDescription(ActionMessages.ShowInPackageViewAction_description); 54 setToolTipText(ActionMessages.ShowInPackageViewAction_tooltip); 55 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.SHOW_IN_PACKAGEVIEW_ACTION); 56 } 57 58 62 public ShowInPackageViewAction(JavaEditor editor) { 63 this(editor.getEditorSite()); 64 fEditor= editor; 65 setEnabled(SelectionConverter.canOperateOn(fEditor)); 66 } 67 68 71 public void selectionChanged(ITextSelection selection) { 72 } 73 74 77 public void selectionChanged(IStructuredSelection selection) { 78 setEnabled(checkEnabled(selection)); 79 } 80 81 private boolean checkEnabled(IStructuredSelection selection) { 82 if (selection.size() != 1) 83 return false; 84 return selection.getFirstElement() instanceof IJavaElement; 85 } 86 87 90 public void run(ITextSelection selection) { 91 try { 92 IJavaElement element= SelectionConverter.getElementAtOffset(fEditor); 93 if (element != null) 94 run(element); 95 } catch (JavaModelException e) { 96 JavaPlugin.log(e); 97 String message= ActionMessages.ShowInPackageViewAction_error_message; 98 ErrorDialog.openError(getShell(), getDialogTitle(), message, e.getStatus()); 99 } 100 } 101 102 105 public void run(IStructuredSelection selection) { 106 if (!checkEnabled(selection)) 107 return; 108 run((IJavaElement)selection.getFirstElement()); 109 } 110 111 115 public void run(IJavaElement element) { 116 if (element == null) 117 return; 118 119 IOpenable openable= element.getOpenable(); 121 if (openable instanceof IJavaElement) 122 element= (IJavaElement)openable; 123 124 PackageExplorerPart view= PackageExplorerPart.openInActivePerspective(); 125 view.tryToReveal(element); 126 } 127 128 private static String getDialogTitle() { 129 return ActionMessages.ShowInPackageViewAction_dialog_title; 130 } 131 } 132 | Popular Tags |