KickJava   Java API By Example, From Geeks To Geeks.

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


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.net.URL JavaDoc;
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 /**
49  * This action opens the selected element's Javadoc in an external
50  * browser.
51  * <p>
52  * The action is applicable to selections containing elements of
53  * type <code>IJavaElement</code>.
54  *
55  * <p>
56  * This class may be instantiated; it is not intended to be subclassed.
57  * </p>
58  *
59  * @since 2.0
60  */

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

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     /**
81      * Creates a new <code>OpenExternalJavadocAction</code>. The action requires
82      * that the selection provided by the given selection provider is of type <code>
83      * org.eclipse.jface.viewers.IStructuredSelection</code>.
84      *
85      * @param site the site providing additional context information for this action
86      * @param provider a special selection provider which is used instead
87      * of the site's selection provider or <code>null</code> to use the site's
88      * selection provider
89      *
90      * @since 3.2
91      * @deprecated Use {@link #setSpecialSelectionProvider(ISelectionProvider)} instead. This API will be
92      * removed after 3.2 M5.
93      */

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

104     public OpenExternalJavadocAction(JavaEditor editor) {
105         this(editor.getEditorSite());
106         fEditor= editor;
107         setEnabled(SelectionConverter.canOperateOn(fEditor));
108     }
109     
110     /* (non-Javadoc)
111      * Method declared on SelectionDispatchAction.
112      */

113     public void selectionChanged(ITextSelection selection) {
114     }
115
116     /* (non-Javadoc)
117      * Method declared on SelectionDispatchAction.
118      */

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     /* (non-Javadoc)
130      * Method declared on SelectionDispatchAction.
131      */

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 JavaDoc e) {
149             ExceptionHandler.handle(e, getShell(), getDialogTitle(), ActionMessages.OpenExternalJavadocAction_code_resolve_failed);
150         } catch (InterruptedException JavaDoc e) {
151             // cancelled
152
}
153     }
154     
155     /* (non-Javadoc)
156      * Method declared on SelectionDispatchAction.
157      */

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     /*
168      * No Javadoc since the method isn't meant to be public but is
169      * since the beginning
170      */

171     public void run(IJavaElement element) {
172         if (element == null)
173             return;
174         Shell shell= getShell();
175         try {
176             String JavaDoc labelName= JavaElementLabels.getElementLabel(element, JavaElementLabels.ALL_DEFAULT);
177             
178             URL JavaDoc 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 JavaDoc message= ActionMessages.OpenExternalJavadocAction_libraries_no_location;
183                     showMessage(shell, Messages.format(message, new String JavaDoc[] { labelName, root.getElementName() }), false);
184                 } else {
185                     IJavaElement annotatedElement= element.getJavaProject();
186                     String JavaDoc message= ActionMessages.OpenExternalJavadocAction_source_no_location;
187                     showMessage(shell, Messages.format(message, new String JavaDoc[] { labelName, annotatedElement.getElementName() }), false);
188                 }
189                 return;
190             }
191             URL JavaDoc 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 JavaDoc message, final boolean isError) {
202         Display.getDefault().asyncExec(new Runnable JavaDoc() {
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 JavaDoc getTitle() {
214         return ActionMessages.OpenExternalJavadocAction_dialog_title;
215     }
216     
217     /**
218      * Note: this method is for internal use only. Clients should not call this method.
219      *
220      * @return the dialog default title
221      */

222     protected String JavaDoc getDialogTitle() {
223         return getTitle();
224     }
225 }
226
Popular Tags