1 /*******************************************************************************
2 * Copyright (c) 2000, 2011 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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package org.eclipse.jdt.ui.actions;
13 import java.lang.reflect.InvocationTargetException;
14 import java.util.ArrayList;
15 import java.util.Iterator;
16 import java.util.List;
18 import org.eclipse.core.runtime.CoreException;
19 import org.eclipse.core.runtime.IStatus;
20 import org.eclipse.core.runtime.MultiStatus;
21 import org.eclipse.core.runtime.Status;
23 import org.eclipse.core.resources.IFile;
25 import org.eclipse.jface.dialogs.ErrorDialog;
26 import org.eclipse.jface.util.OpenStrategy;
27 import org.eclipse.jface.viewers.ISelection;
28 import org.eclipse.jface.viewers.ISelectionProvider;
29 import org.eclipse.jface.viewers.IStructuredSelection;
31 import org.eclipse.jface.text.IRegion;
32 import org.eclipse.jface.text.ITextSelection;
33 import org.eclipse.jface.text.Region;
35 import org.eclipse.ui.IActionBars;
36 import org.eclipse.ui.IEditorPart;
37 import org.eclipse.ui.IWorkbenchSite;
38 import org.eclipse.ui.PartInitException;
39 import org.eclipse.ui.PlatformUI;
41 import org.eclipse.ui.texteditor.IEditorStatusLine;
43 import org.eclipse.jdt.core.IJavaElement;
44 import org.eclipse.jdt.core.ISourceReference;
45 import org.eclipse.jdt.core.ITypeRoot;
46 import org.eclipse.jdt.core.JavaModelException;
48 import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
49 import org.eclipse.jdt.internal.corext.util.Messages;
51 import org.eclipse.jdt.ui.JavaElementLabels;
52 import org.eclipse.jdt.ui.JavaUI;
54 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
55 import org.eclipse.jdt.internal.ui.JavaPlugin;
56 import org.eclipse.jdt.internal.ui.actions.ActionMessages;
57 import org.eclipse.jdt.internal.ui.actions.ActionUtil;
58 import org.eclipse.jdt.internal.ui.actions.SelectionConverter;
59 import org.eclipse.jdt.internal.ui.javaeditor.EditorUtility;
60 import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor;
61 import org.eclipse.jdt.internal.ui.javaeditor.JavaElementHyperlinkDetector;
62 import org.eclipse.jdt.internal.ui.search.IOccurrencesFinder.OccurrenceLocation;
63 import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
67 * This action opens a Java editor on a Java element or file.
69 * The action is applicable to selections containing elements of
70 * type <code>ICompilationUnit</code>, <code>IMember</code>
71 * or <code>IFile</code>.
74 * This class may be instantiated; it is not intended to be subclassed.
79 * @noextend This class is not intended to be subclassed by clients.
81 public class OpenAction extends SelectionDispatchAction {
83 public JavaEditor fEditor;
86 * Creates a new <code>OpenAction</code>. The action requires
87 * that the selection provided by the site's selection provider is of type <code>
88 * org.eclipse.jface.viewers.IStructuredSelection</code>.
90 * @param site the site providing context information for this action
92 public OpenAction(IWorkbenchSite site) {
94 setText(ActionMessages.OpenAction_label);
95 setToolTipText(ActionMessages.OpenAction_tooltip);
96 setDescription(ActionMessages.OpenAction_description);
97 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.OPEN_ACTION);
101 * Note: This constructor is for internal use only. Clients should not call this constructor.
102 * @param editor the Java editor
104 * @noreference This constructor is not intended to be referenced by clients.
106 public OpenAction(JavaEditor editor) {
107 this(editor.getEditorSite());
109 setText(ActionMessages.OpenAction_declaration_label);
110 setEnabled(EditorUtility.getEditorInputJavaElement(fEditor, false) != null);
114 * Method declared on SelectionDispatchAction.
117 public void selectionChanged(ITextSelection selection) {
121 * Method declared on SelectionDispatchAction.
124 public void selectionChanged(IStructuredSelection selection) {
125 setEnabled(checkEnabled(selection));
128 private boolean checkEnabled(IStructuredSelection selection) {
129 if (selection.isEmpty())
131 for (Iterator<?> iter= selection.iterator(); iter.hasNext();) {
132 Object element= iter.next();
133 if (element instanceof ISourceReference)
135 if (element instanceof IFile)
137 if (JavaModelUtil.isOpenableStorage(element))
145 * Method declared on SelectionDispatchAction.
148 public void run(ITextSelection selection) {
149 ITypeRoot input= EditorUtility.getEditorInputJavaElement(fEditor, false);
151 setStatusLineMessage();
154 IRegion region= new Region(selection.getOffset(), selection.getLength());
155 OccurrenceLocation location= JavaElementHyperlinkDetector.findBreakOrContinueTarget(input, region);
156 if (location != null) {
157 location.generated_147830987560627848(this);
161 IJavaElement[] elements= SelectionConverter.codeResolveForked(fEditor, false);
162 elements= selectOpenableElements(elements);
163 if (elements == null || elements.length == 0) {
164 if (!ActionUtil.isProcessable(fEditor))
166 setStatusLineMessage();
170 IJavaElement element= elements[0];
171 if (elements.length > 1) {
172 element= SelectionConverter.selectJavaElement(elements, getShell(), getDialogTitle(), ActionMessages.OpenAction_select_element);
177 run(new Object[] {element} );
178 } catch (InvocationTargetException e) {
179 ExceptionHandler.handle(e, getShell(), getDialogTitle(), ActionMessages.OpenAction_error_message);
180 } catch (InterruptedException e) {
186 * Sets the error message in the status line.
190 private void setStatusLineMessage() {
191 IEditorStatusLine statusLine= (IEditorStatusLine) fEditor.getAdapter(IEditorStatusLine.class);
192 if (statusLine != null)
193 statusLine.setMessage(true, ActionMessages.OpenAction_error_messageBadSelection, null);
194 getShell().getDisplay().beep();
199 * Selects the openable elements out of the given ones.
201 * @param elements the elements to filter
202 * @return the openable elements
205 private IJavaElement[] selectOpenableElements(IJavaElement[] elements) {
206 List<IJavaElement> result= new ArrayList<IJavaElement>(elements.length);
207 for (int i= 0; i < elements.length; i++) {
208 IJavaElement element= elements[i];
209 switch (element.getElementType()) {
210 case IJavaElement.PACKAGE_DECLARATION:
211 case IJavaElement.PACKAGE_FRAGMENT:
212 case IJavaElement.PACKAGE_FRAGMENT_ROOT:
213 case IJavaElement.JAVA_PROJECT:
214 case IJavaElement.JAVA_MODEL:
221 return result.toArray(new IJavaElement[result.size()]);
225 * Method declared on SelectionDispatchAction.
228 public void run(IStructuredSelection selection) {
229 if (!checkEnabled(selection))
231 run(selection.toArray());
235 * Note: this method is for internal use only. Clients should not call this method.
237 * @param elements the elements to process
239 * @noreference This method is not intended to be referenced by clients.
241 public void run(Object[] elements) {
242 if (elements == null)
245 MultiStatus status= new MultiStatus(JavaUI.ID_PLUGIN, IStatus.OK, ActionMessages.OpenAction_multistatus_message, null);
247 for (int i= 0; i < elements.length; i++) {
248 Object element= elements[i];
250 element= getElementToOpen(element);
251 boolean activateOnOpen= fEditor != null ? true : OpenStrategy.activateOnOpen();
252 IEditorPart part= EditorUtility.openInEditor(element, activateOnOpen);
253 if (part != null && element instanceof IJavaElement)
254 JavaUI.revealInEditor(part, (IJavaElement)element);
255 } catch (PartInitException e) {
256 String message= Messages.format(ActionMessages.OpenAction_error_problem_opening_editor, new String[] { JavaElementLabels.getTextLabel(element, JavaElementLabels.ALL_DEFAULT), e.getStatus().getMessage() });
257 status.add(new Status(IStatus.ERROR, JavaUI.ID_PLUGIN, IStatus.ERROR, message, null));
258 } catch (CoreException e) {
259 String message= Messages.format(ActionMessages.OpenAction_error_problem_opening_editor, new String[] { JavaElementLabels.getTextLabel(element, JavaElementLabels.ALL_DEFAULT), e.getStatus().getMessage() });
260 status.add(new Status(IStatus.ERROR, JavaUI.ID_PLUGIN, IStatus.ERROR, message, null));
264 if (!status.isOK()) {
265 IStatus[] children= status.getChildren();
266 ErrorDialog.openError(getShell(), getDialogTitle(), ActionMessages.OpenAction_error_message, children.length == 1 ? children[0] : status);
271 * Note: this method is for internal use only. Clients should not call this method.
273 * @param object the element to open
274 * @return the real element to open
275 * @throws JavaModelException if an error occurs while accessing the Java model
277 * @noreference This method is not intended to be referenced by clients.
279 public Object getElementToOpen(Object object) throws JavaModelException {
283 private String getDialogTitle() {
284 return ActionMessages.OpenAction_error_title;
287 public void generated_5795278149633683358(IActionBars actionBars) {
288 actionBars.setGlobalActionHandler(JdtActionConstants.OPEN, this);
289 setActionDefinitionId(IJavaEditorActionDefinitionIds.OPEN_EDITOR);
292 public void generated_7931678020130717635(OpenEditorActionGroup openeditoractiongroup, ISelectionProvider specialSelectionProvider) {
293 setActionDefinitionId(IJavaEditorActionDefinitionIds.OPEN_EDITOR);
294 openeditoractiongroup.fSelectionProvider= specialSelectionProvider == null ? openeditoractiongroup.fSite.getSelectionProvider() : specialSelectionProvider;
295 openeditoractiongroup.initialize();
296 if (specialSelectionProvider != null)
297 setSpecialSelectionProvider(specialSelectionProvider);
300 public void generated_344836486837402147(OpenEditorActionGroup openeditoractiongroup) {
301 ISelection selection= openeditoractiongroup.fSelectionProvider.getSelection();
303 if (!openeditoractiongroup.fIsEditorOwner) {
304 openeditoractiongroup.fSelectionProvider.addSelectionChangedListener(this);