1 11 package org.eclipse.jdt.ui.actions; 12 13 import java.lang.reflect.InvocationTargetException ; 14 import java.net.URL ; 15 16 import org.eclipse.core.runtime.CoreException; 17 18 import org.eclipse.swt.widgets.Display; 19 import org.eclipse.swt.widgets.Shell; 20 21 import org.eclipse.jface.dialogs.MessageDialog; 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.IJavaElement; 31 import org.eclipse.jdt.core.IPackageFragmentRoot; 32 33 import org.eclipse.jdt.internal.corext.util.JavaModelUtil; 34 import org.eclipse.jdt.internal.corext.util.Messages; 35 36 import org.eclipse.jdt.ui.JavaElementLabels; 37 import org.eclipse.jdt.ui.JavaUI; 38 39 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds; 40 import org.eclipse.jdt.internal.ui.JavaPlugin; 41 import org.eclipse.jdt.internal.ui.actions.ActionMessages; 42 import org.eclipse.jdt.internal.ui.actions.ActionUtil; 43 import org.eclipse.jdt.internal.ui.actions.OpenBrowserUtil; 44 import org.eclipse.jdt.internal.ui.actions.SelectionConverter; 45 import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor; 46 import org.eclipse.jdt.internal.ui.util.ExceptionHandler; 47 48 61 public class OpenExternalJavadocAction extends SelectionDispatchAction { 62 63 private JavaEditor fEditor; 64 65 72 public OpenExternalJavadocAction(IWorkbenchSite site) { 73 super(site); 74 setText(ActionMessages.OpenExternalJavadocAction_label); 75 setDescription(ActionMessages.OpenExternalJavadocAction_description); 76 setToolTipText(ActionMessages.OpenExternalJavadocAction_tooltip); 77 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.OPEN_EXTERNAL_JAVADOC_ACTION); 78 } 79 80 94 public OpenExternalJavadocAction(IWorkbenchSite site, ISelectionProvider provider) { 95 this(site); 96 setSpecialSelectionProvider(provider); 97 } 98 99 100 104 public OpenExternalJavadocAction(JavaEditor editor) { 105 this(editor.getEditorSite()); 106 fEditor= editor; 107 setEnabled(SelectionConverter.canOperateOn(fEditor)); 108 } 109 110 113 public void selectionChanged(ITextSelection selection) { 114 } 115 116 119 public void selectionChanged(IStructuredSelection selection) { 120 setEnabled(checkEnabled(selection)); 121 } 122 123 private boolean checkEnabled(IStructuredSelection selection) { 124 if (selection.size() != 1) 125 return false; 126 return selection.getFirstElement() instanceof IJavaElement; 127 } 128 129 132 public void run(ITextSelection selection) { 133 IJavaElement element= SelectionConverter.getInput(fEditor); 134 if (!ActionUtil.isProcessable(getShell(), element)) 135 return; 136 137 try { 138 IJavaElement[] elements= SelectionConverter.codeResolveOrInputForked(fEditor); 139 if (elements == null || elements.length == 0) 140 return; 141 IJavaElement candidate= elements[0]; 142 if (elements.length > 1) { 143 candidate= SelectionConverter.selectJavaElement(elements, getShell(), getDialogTitle(), ActionMessages.OpenExternalJavadocAction_select_element); 144 } 145 if (candidate != null) { 146 run(candidate); 147 } 148 } catch (InvocationTargetException e) { 149 ExceptionHandler.handle(e, getShell(), getDialogTitle(), ActionMessages.OpenExternalJavadocAction_code_resolve_failed); 150 } catch (InterruptedException e) { 151 } 153 } 154 155 158 public void run(IStructuredSelection selection) { 159 if (!checkEnabled(selection)) 160 return; 161 IJavaElement element= (IJavaElement)selection.getFirstElement(); 162 if (!ActionUtil.isProcessable(getShell(), element)) 163 return; 164 run(element); 165 } 166 167 171 public void run(IJavaElement element) { 172 if (element == null) 173 return; 174 Shell shell= getShell(); 175 try { 176 String labelName= JavaElementLabels.getElementLabel(element, JavaElementLabels.ALL_DEFAULT); 177 178 URL baseURL= JavaUI.getJavadocBaseLocation(element); 179 if (baseURL == null) { 180 IPackageFragmentRoot root= JavaModelUtil.getPackageFragmentRoot(element); 181 if (root != null && root.getKind() == IPackageFragmentRoot.K_BINARY) { 182 String message= ActionMessages.OpenExternalJavadocAction_libraries_no_location; 183 showMessage(shell, Messages.format(message, new String [] { labelName, root.getElementName() }), false); 184 } else { 185 IJavaElement annotatedElement= element.getJavaProject(); 186 String message= ActionMessages.OpenExternalJavadocAction_source_no_location; 187 showMessage(shell, Messages.format(message, new String [] { labelName, annotatedElement.getElementName() }), false); 188 } 189 return; 190 } 191 URL url= JavaUI.getJavadocLocation(element, true); 192 if (url != null) { 193 OpenBrowserUtil.open(url, shell.getDisplay(), getTitle()); 194 } 195 } catch (CoreException e) { 196 JavaPlugin.log(e); 197 showMessage(shell, ActionMessages.OpenExternalJavadocAction_opening_failed, true); 198 } 199 } 200 201 private static void showMessage(final Shell shell, final String message, final boolean isError) { 202 Display.getDefault().asyncExec(new Runnable () { 203 public void run() { 204 if (isError) { 205 MessageDialog.openError(shell, getTitle(), message); 206 } else { 207 MessageDialog.openInformation(shell, getTitle(), message); 208 } 209 } 210 }); 211 } 212 213 private static String getTitle() { 214 return ActionMessages.OpenExternalJavadocAction_dialog_title; 215 } 216 217 222 protected String getDialogTitle() { 223 return getTitle(); 224 } 225 } 226 | Popular Tags |