]> git.uio.no Git - ifi-stolz-refaktor.git/blob - case-study/jdt-after/ui/org/eclipse/jdt/ui/actions/OpenAction.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-after / ui / org / eclipse / jdt / ui / actions / OpenAction.java
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
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;
14 import java.util.ArrayList;
15 import java.util.Iterator;
16 import java.util.List;
17
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;
22
23 import org.eclipse.core.resources.IFile;
24
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;
30
31 import org.eclipse.jface.text.IRegion;
32 import org.eclipse.jface.text.ITextSelection;
33 import org.eclipse.jface.text.Region;
34
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;
40
41 import org.eclipse.ui.texteditor.IEditorStatusLine;
42
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;
47
48 import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
49 import org.eclipse.jdt.internal.corext.util.Messages;
50
51 import org.eclipse.jdt.ui.JavaElementLabels;
52 import org.eclipse.jdt.ui.JavaUI;
53
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;
64
65
66 /**
67  * This action opens a Java editor on a Java element or file.
68  * <p>
69  * The action is applicable to selections containing elements of
70  * type <code>ICompilationUnit</code>, <code>IMember</code>
71  * or <code>IFile</code>.
72  *
73  * <p>
74  * This class may be instantiated; it is not intended to be subclassed.
75  * </p>
76  *
77  * @since 2.0
78  *
79  * @noextend This class is not intended to be subclassed by clients.
80  */
81 public class OpenAction extends SelectionDispatchAction {
82
83         public JavaEditor fEditor;
84
85         /**
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>.
89          *
90          * @param site the site providing context information for this action
91          */
92         public OpenAction(IWorkbenchSite site) {
93                 super(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);
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          * @noreference This constructor is not intended to be referenced by clients.
105          */
106         public OpenAction(JavaEditor editor) {
107                 this(editor.getEditorSite());
108                 fEditor= editor;
109                 setText(ActionMessages.OpenAction_declaration_label);
110                 setEnabled(EditorUtility.getEditorInputJavaElement(fEditor, false) != null);
111         }
112
113         /* (non-Javadoc)
114          * Method declared on SelectionDispatchAction.
115          */
116         @Override
117         public void selectionChanged(ITextSelection selection) {
118         }
119
120         /* (non-Javadoc)
121          * Method declared on SelectionDispatchAction.
122          */
123         @Override
124         public void selectionChanged(IStructuredSelection selection) {
125                 setEnabled(checkEnabled(selection));
126         }
127
128         private boolean checkEnabled(IStructuredSelection selection) {
129                 if (selection.isEmpty())
130                         return false;
131                 for (Iterator<?> iter= selection.iterator(); iter.hasNext();) {
132                         Object element= iter.next();
133                         if (element instanceof ISourceReference)
134                                 continue;
135                         if (element instanceof IFile)
136                                 continue;
137                         if (JavaModelUtil.isOpenableStorage(element))
138                                 continue;
139                         return false;
140                 }
141                 return true;
142         }
143
144         /* (non-Javadoc)
145          * Method declared on SelectionDispatchAction.
146          */
147         @Override
148         public void run(ITextSelection selection) {
149                 ITypeRoot input= EditorUtility.getEditorInputJavaElement(fEditor, false);
150                 if (input == null) {
151                         setStatusLineMessage();
152                         return;
153                 }
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);
158                         return;
159                 }
160                 try {
161                         IJavaElement[] elements= SelectionConverter.codeResolveForked(fEditor, false);
162                         elements= selectOpenableElements(elements);
163                         if (elements == null || elements.length == 0) {
164                                 if (!ActionUtil.isProcessable(fEditor))
165                                         return;
166                                 setStatusLineMessage();
167                                 return;
168                         }
169
170                         IJavaElement element= elements[0];
171                         if (elements.length > 1) {
172                                 element= SelectionConverter.selectJavaElement(elements, getShell(), getDialogTitle(), ActionMessages.OpenAction_select_element);
173                                 if (element == null)
174                                         return;
175                         }
176
177                         run(new Object[] {element} );
178                 } catch (InvocationTargetException e) {
179                         ExceptionHandler.handle(e, getShell(), getDialogTitle(), ActionMessages.OpenAction_error_message);
180                 } catch (InterruptedException e) {
181                         // ignore
182                 }
183         }
184
185         /**
186          * Sets the error message in the status line.
187          * 
188          * @since 3.7
189          */
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();
195                 return;
196         }
197
198         /**
199          * Selects the openable elements out of the given ones.
200          *
201          * @param elements the elements to filter
202          * @return the openable elements
203          * @since 3.4
204          */
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:
215                                         break;
216                                 default:
217                                         result.add(element);
218                                         break;
219                         }
220                 }
221                 return result.toArray(new IJavaElement[result.size()]);
222         }
223
224         /* (non-Javadoc)
225          * Method declared on SelectionDispatchAction.
226          */
227         @Override
228         public void run(IStructuredSelection selection) {
229                 if (!checkEnabled(selection))
230                         return;
231                 run(selection.toArray());
232         }
233
234         /**
235          * Note: this method is for internal use only. Clients should not call this method.
236          *
237          * @param elements the elements to process
238          *
239          * @noreference This method is not intended to be referenced by clients.
240          */
241         public void run(Object[] elements) {
242                 if (elements == null)
243                         return;
244
245                 MultiStatus status= new MultiStatus(JavaUI.ID_PLUGIN, IStatus.OK, ActionMessages.OpenAction_multistatus_message, null);
246
247                 for (int i= 0; i < elements.length; i++) {
248                         Object element= elements[i];
249                         try {
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));
261                                 JavaPlugin.log(e);
262                         }
263                 }
264                 if (!status.isOK()) {
265                         IStatus[] children= status.getChildren();
266                         ErrorDialog.openError(getShell(), getDialogTitle(), ActionMessages.OpenAction_error_message, children.length == 1 ? children[0] : status);
267                 }
268         }
269
270         /**
271          * Note: this method is for internal use only. Clients should not call this method.
272          *
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
276          *
277          * @noreference This method is not intended to be referenced by clients.
278          */
279         public Object getElementToOpen(Object object) throws JavaModelException {
280                 return object;
281         }
282
283         private String getDialogTitle() {
284                 return ActionMessages.OpenAction_error_title;
285         }
286
287         public void generated_5795278149633683358(IActionBars actionBars) {
288                 actionBars.setGlobalActionHandler(JdtActionConstants.OPEN, this);
289                 setActionDefinitionId(IJavaEditorActionDefinitionIds.OPEN_EDITOR);
290         }
291
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);
298         }
299
300         public void generated_344836486837402147(OpenEditorActionGroup openeditoractiongroup) {
301                 ISelection selection= openeditoractiongroup.fSelectionProvider.getSelection();
302                 update(selection);
303                 if (!openeditoractiongroup.fIsEditorOwner) {
304                         openeditoractiongroup.fSelectionProvider.addSelectionChangedListener(this);
305                 }
306         }
307 }