KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > views > markers > internal > ActionOpenMarker


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  * Sebastian Davids <sdavids@gmx.de> - Fix for Bug 73612
11  * [Markers] "Open All" does not work with multi-select in the bookmarks view
12  *******************************************************************************/

13
14 package org.eclipse.ui.views.markers.internal;
15
16 import org.eclipse.core.resources.IFile;
17 import org.eclipse.core.resources.IMarker;
18 import org.eclipse.core.runtime.CoreException;
19 import org.eclipse.core.runtime.IStatus;
20 import org.eclipse.core.runtime.Status;
21 import org.eclipse.jface.util.OpenStrategy;
22 import org.eclipse.jface.viewers.ISelectionProvider;
23 import org.eclipse.jface.viewers.IStructuredSelection;
24 import org.eclipse.ui.IEditorInput;
25 import org.eclipse.ui.IEditorPart;
26 import org.eclipse.ui.IWorkbenchPart;
27 import org.eclipse.ui.PartInitException;
28 import org.eclipse.ui.ide.IDE;
29 import org.eclipse.ui.ide.ResourceUtil;
30 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin;
31 import org.eclipse.ui.statushandlers.StatusAdapter;
32 import org.eclipse.ui.statushandlers.StatusManager;
33
34 /**
35  * Action to open an editor on the selected bookmarks.
36  */

37 public class ActionOpenMarker extends MarkerSelectionProviderAction {
38
39     private final String JavaDoc IMAGE_PATH = "elcl16/gotoobj_tsk.gif"; //$NON-NLS-1$
40

41     private final String JavaDoc DISABLED_IMAGE_PATH = "dlcl16/gotoobj_tsk.gif"; //$NON-NLS-1$
42

43     protected IWorkbenchPart part;
44
45     /**
46      * Create a new instance of the receiver.
47      *
48      * @param part
49      * @param provider
50      */

51     public ActionOpenMarker(IWorkbenchPart part, ISelectionProvider provider) {
52         super(provider, MarkerMessages.openAction_title);
53         this.part = part;
54         setImageDescriptor(IDEWorkbenchPlugin.getIDEImageDescriptor(IMAGE_PATH));
55         setDisabledImageDescriptor(IDEWorkbenchPlugin
56                 .getIDEImageDescriptor(DISABLED_IMAGE_PATH));
57         setEnabled(false);
58     }
59
60     /*
61      * (non-Javadoc)
62      *
63      * @see org.eclipse.jface.action.Action#run()
64      */

65     public void run() {
66         IMarker[] markers = getSelectedMarkers();
67         for (int i = 0; i < markers.length; i++) {
68             IMarker marker = markers[i];
69
70             // optimization: if the active editor has the same input as the
71
// selected marker then
72
// RevealMarkerAction would have been run and we only need to
73
// activate the editor
74
IEditorPart editor = part.getSite().getPage().getActiveEditor();
75             if (editor != null) {
76                 IEditorInput input = editor.getEditorInput();
77                 IFile file = ResourceUtil.getFile(input);
78                 if (file != null) {
79                     if (marker.getResource().equals(file)) {
80                         part.getSite().getPage().activate(editor);
81                     }
82                 }
83             }
84
85             if (marker.getResource() instanceof IFile) {
86                 try {
87                     IFile file = (IFile) marker.getResource();
88                     if (file.getLocation() == null
89                             || file.getLocationURI() == null)
90                         return; // Abort if it cannot be opened
91
IDE.openEditor(part.getSite().getPage(), marker,
92                             OpenStrategy.activateOnOpen());
93                 } catch (PartInitException e) {
94                     // Open an error style dialog for PartInitException by
95
// including any extra information from the nested
96
// CoreException if present.
97

98                     // Check for a nested CoreException
99
CoreException nestedException = null;
100                     IStatus status = e.getStatus();
101                     if (status != null
102                             && status.getException() instanceof CoreException) {
103                         nestedException = (CoreException) status.getException();
104                     }
105
106                     if (nestedException != null) {
107                         // Open an error dialog and include the extra
108
// status information from the nested CoreException
109
reportStatus(nestedException.getStatus());
110                     } else {
111                         // Open a regular error dialog since there is no
112
// extra information to display
113
reportError(e.getLocalizedMessage());
114                     }
115                 }
116             }
117         }
118     }
119
120     /**
121      * Report an error message
122      *
123      * @param message
124      */

125     private void reportError(String JavaDoc message) {
126         IStatus status = new Status(IStatus.ERROR,
127                 IDEWorkbenchPlugin.IDE_WORKBENCH, message);
128         reportStatus(status);
129     }
130
131     /**
132      * Report the status
133      *
134      * @param status
135      */

136     private void reportStatus(IStatus status) {
137         StatusAdapter adapter = new StatusAdapter(status);
138         adapter.setProperty(StatusAdapter.TITLE_PROPERTY,
139                 MarkerMessages.OpenMarker_errorTitle);
140         StatusManager.getManager().handle(adapter, StatusManager.SHOW);
141     }
142
143     /*
144      * (non-Javadoc)
145      *
146      * @see org.eclipse.ui.actions.SelectionProviderAction#selectionChanged(org.eclipse.jface.viewers.IStructuredSelection)
147      */

148     public void selectionChanged(IStructuredSelection selection) {
149         setEnabled(Util.allConcreteSelection(selection));
150     }
151 }
152
Popular Tags