KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.util.Iterator JavaDoc;
15
16 import org.eclipse.core.runtime.CoreException;
17 import org.eclipse.core.runtime.IStatus;
18 import org.eclipse.core.runtime.MultiStatus;
19 import org.eclipse.core.runtime.Status;
20
21 import org.eclipse.core.resources.IFile;
22
23 import org.eclipse.jface.dialogs.ErrorDialog;
24 import org.eclipse.jface.util.OpenStrategy;
25 import org.eclipse.jface.viewers.IStructuredSelection;
26
27 import org.eclipse.jface.text.ITextSelection;
28
29 import org.eclipse.ui.IEditorPart;
30 import org.eclipse.ui.IWorkbenchSite;
31 import org.eclipse.ui.PartInitException;
32 import org.eclipse.ui.PlatformUI;
33 import org.eclipse.ui.texteditor.IEditorStatusLine;
34
35 import org.eclipse.jdt.core.ICompilationUnit;
36 import org.eclipse.jdt.core.IJavaElement;
37 import org.eclipse.jdt.core.ISourceReference;
38 import org.eclipse.jdt.core.JavaModelException;
39
40 import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
41 import org.eclipse.jdt.internal.corext.util.Messages;
42
43 import org.eclipse.jdt.ui.JavaUI;
44
45 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
46 import org.eclipse.jdt.internal.ui.JavaPlugin;
47 import org.eclipse.jdt.internal.ui.actions.ActionMessages;
48 import org.eclipse.jdt.internal.ui.actions.ActionUtil;
49 import org.eclipse.jdt.internal.ui.actions.SelectionConverter;
50 import org.eclipse.jdt.internal.ui.javaeditor.EditorUtility;
51 import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor;
52 import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
53 import org.eclipse.jdt.internal.ui.viewsupport.JavaUILabelProvider;
54
55 /**
56  * This action opens a Java editor on a Java element or file.
57  * <p>
58  * The action is applicable to selections containing elements of
59  * type <code>ICompilationUnit</code>, <code>IMember</code>
60  * or <code>IFile</code>.
61  *
62  * <p>
63  * This class may be instantiated; it is not intended to be subclassed.
64  * </p>
65  *
66  * @since 2.0
67  */

68 public class OpenAction extends SelectionDispatchAction {
69     
70     private JavaEditor fEditor;
71     
72     /**
73      * Creates a new <code>OpenAction</code>. The action requires
74      * that the selection provided by the site's selection provider is of type <code>
75      * org.eclipse.jface.viewers.IStructuredSelection</code>.
76      *
77      * @param site the site providing context information for this action
78      */

79     public OpenAction(IWorkbenchSite site) {
80         super(site);
81         setText(ActionMessages.OpenAction_label);
82         setToolTipText(ActionMessages.OpenAction_tooltip);
83         setDescription(ActionMessages.OpenAction_description);
84         PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.OPEN_ACTION);
85     }
86     
87     /**
88      * Note: This constructor is for internal use only. Clients should not call this constructor.
89      * @param editor the Java editor
90      */

91     public OpenAction(JavaEditor editor) {
92         this(editor.getEditorSite());
93         fEditor= editor;
94         setText(ActionMessages.OpenAction_declaration_label);
95         setEnabled(EditorUtility.getEditorInputJavaElement(fEditor, false) != null);
96     }
97     
98     /* (non-Javadoc)
99      * Method declared on SelectionDispatchAction.
100      */

101     public void selectionChanged(ITextSelection selection) {
102     }
103
104     /* (non-Javadoc)
105      * Method declared on SelectionDispatchAction.
106      */

107     public void selectionChanged(IStructuredSelection selection) {
108         setEnabled(checkEnabled(selection));
109     }
110     
111     private boolean checkEnabled(IStructuredSelection selection) {
112         if (selection.isEmpty())
113             return false;
114         for (Iterator JavaDoc iter= selection.iterator(); iter.hasNext();) {
115             Object JavaDoc element= iter.next();
116             if (element instanceof ISourceReference)
117                 continue;
118             if (element instanceof IFile)
119                 continue;
120             if (JavaModelUtil.isOpenableStorage(element))
121                 continue;
122             return false;
123         }
124         return true;
125     }
126     
127     /* (non-Javadoc)
128      * Method declared on SelectionDispatchAction.
129      */

130     public void run(ITextSelection selection) {
131         if (!isProcessable())
132             return;
133         try {
134             IJavaElement[] elements= SelectionConverter.codeResolveForked(fEditor, false);
135             if (elements == null || elements.length == 0) {
136                 IEditorStatusLine statusLine= (IEditorStatusLine) fEditor.getAdapter(IEditorStatusLine.class);
137                 if (statusLine != null)
138                     statusLine.setMessage(true, ActionMessages.OpenAction_error_messageBadSelection, null);
139                 getShell().getDisplay().beep();
140                 return;
141             }
142             IJavaElement element= elements[0];
143             if (elements.length > 1) {
144                 element= SelectionConverter.selectJavaElement(elements, getShell(), getDialogTitle(), ActionMessages.OpenAction_select_element);
145                 if (element == null)
146                     return;
147             }
148
149             int type= element.getElementType();
150             if (type == IJavaElement.JAVA_PROJECT || type == IJavaElement.PACKAGE_FRAGMENT_ROOT || type == IJavaElement.PACKAGE_FRAGMENT)
151                 element= EditorUtility.getEditorInputJavaElement(fEditor, false);
152             run(new Object JavaDoc[] {element} );
153         } catch (InvocationTargetException JavaDoc e) {
154             ExceptionHandler.handle(e, getShell(), getDialogTitle(), ActionMessages.OpenAction_error_message);
155         } catch (InterruptedException JavaDoc e) {
156             // ignore
157
}
158     }
159
160     private boolean isProcessable() {
161         if (fEditor != null) {
162             IJavaElement je= EditorUtility.getEditorInputJavaElement(fEditor, false);
163             if (je instanceof ICompilationUnit && !JavaModelUtil.isPrimary((ICompilationUnit)je))
164                 return true; // can process non-primary working copies
165
}
166         return ActionUtil.isProcessable(fEditor);
167     }
168     
169     /* (non-Javadoc)
170      * Method declared on SelectionDispatchAction.
171      */

172     public void run(IStructuredSelection selection) {
173         if (!checkEnabled(selection))
174             return;
175         run(selection.toArray());
176     }
177     
178     /**
179      * Note: this method is for internal use only. Clients should not call this method.
180      *
181      * @param elements the elements to process
182      */

183     public void run(Object JavaDoc[] elements) {
184         if (elements == null)
185             return;
186         
187         MultiStatus status= new MultiStatus(JavaUI.ID_PLUGIN, IStatus.OK, ActionMessages.OpenAction_multistatus_message, null);
188         
189         for (int i= 0; i < elements.length; i++) {
190             Object JavaDoc element= elements[i];
191             try {
192                 element= getElementToOpen(element);
193                 boolean activateOnOpen= fEditor != null ? true : OpenStrategy.activateOnOpen();
194                 IEditorPart part= EditorUtility.openInEditor(element, activateOnOpen);
195                 if (part != null && element instanceof IJavaElement)
196                     JavaUI.revealInEditor(part, (IJavaElement)element);
197             } catch (PartInitException e) {
198                 String JavaDoc message= Messages.format(ActionMessages.OpenAction_error_problem_opening_editor, new String JavaDoc[] { new JavaUILabelProvider().getText(element), e.getStatus().getMessage() });
199                 status.add(new Status(IStatus.ERROR, JavaUI.ID_PLUGIN, IStatus.ERROR, message, null));
200             } catch (CoreException e) {
201                 String JavaDoc message= Messages.format(ActionMessages.OpenAction_error_problem_opening_editor, new String JavaDoc[] { new JavaUILabelProvider().getText(element), e.getStatus().getMessage() });
202                 status.add(new Status(IStatus.ERROR, JavaUI.ID_PLUGIN, IStatus.ERROR, message, null));
203                 JavaPlugin.log(e);
204             }
205         }
206         if (!status.isOK()) {
207             IStatus[] children= status.getChildren();
208             ErrorDialog.openError(getShell(), getDialogTitle(), ActionMessages.OpenAction_error_message, children.length == 1 ? children[0] : status);
209         }
210     }
211     
212     /**
213      * Note: this method is for internal use only. Clients should not call this method.
214      *
215      * @param object the element to open
216      * @return the real element to open
217      * @throws JavaModelException if an error occurs while accessing the Java model
218      */

219     public Object JavaDoc getElementToOpen(Object JavaDoc object) throws JavaModelException {
220         return object;
221     }
222     
223     private String JavaDoc getDialogTitle() {
224         return ActionMessages.OpenAction_error_title;
225     }
226 }
227
Popular Tags