001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: * Sebastian Davids <sdavids@gmx.de> - Fix for Bug 73612
011: * [Markers] "Open All" does not work with multi-select in the bookmarks view
012: *******************************************************************************/package org.eclipse.ui.views.markers.internal;
013:
014: import org.eclipse.core.resources.IFile;
015: import org.eclipse.core.resources.IMarker;
016: import org.eclipse.core.runtime.CoreException;
017: import org.eclipse.core.runtime.IStatus;
018: import org.eclipse.core.runtime.Status;
019: import org.eclipse.jface.util.OpenStrategy;
020: import org.eclipse.jface.viewers.ISelectionProvider;
021: import org.eclipse.jface.viewers.IStructuredSelection;
022: import org.eclipse.ui.IEditorInput;
023: import org.eclipse.ui.IEditorPart;
024: import org.eclipse.ui.IWorkbenchPart;
025: import org.eclipse.ui.PartInitException;
026: import org.eclipse.ui.ide.IDE;
027: import org.eclipse.ui.ide.ResourceUtil;
028: import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin;
029: import org.eclipse.ui.statushandlers.StatusAdapter;
030: import org.eclipse.ui.statushandlers.StatusManager;
031:
032: /**
033: * Action to open an editor on the selected bookmarks.
034: */
035: public class ActionOpenMarker extends MarkerSelectionProviderAction {
036:
037: private final String IMAGE_PATH = "elcl16/gotoobj_tsk.gif"; //$NON-NLS-1$
038:
039: private final String DISABLED_IMAGE_PATH = "dlcl16/gotoobj_tsk.gif"; //$NON-NLS-1$
040:
041: protected IWorkbenchPart part;
042:
043: /**
044: * Create a new instance of the receiver.
045: *
046: * @param part
047: * @param provider
048: */
049: public ActionOpenMarker(IWorkbenchPart part,
050: ISelectionProvider provider) {
051: super (provider, MarkerMessages.openAction_title);
052: this .part = part;
053: setImageDescriptor(IDEWorkbenchPlugin
054: .getIDEImageDescriptor(IMAGE_PATH));
055: setDisabledImageDescriptor(IDEWorkbenchPlugin
056: .getIDEImageDescriptor(DISABLED_IMAGE_PATH));
057: setEnabled(false);
058: }
059:
060: /*
061: * (non-Javadoc)
062: *
063: * @see org.eclipse.jface.action.Action#run()
064: */
065: public void run() {
066: IMarker[] markers = getSelectedMarkers();
067: for (int i = 0; i < markers.length; i++) {
068: IMarker marker = markers[i];
069:
070: // optimization: if the active editor has the same input as the
071: // selected marker then
072: // RevealMarkerAction would have been run and we only need to
073: // activate the editor
074: IEditorPart editor = part.getSite().getPage()
075: .getActiveEditor();
076: if (editor != null) {
077: IEditorInput input = editor.getEditorInput();
078: IFile file = ResourceUtil.getFile(input);
079: if (file != null) {
080: if (marker.getResource().equals(file)) {
081: part.getSite().getPage().activate(editor);
082: }
083: }
084: }
085:
086: if (marker.getResource() instanceof IFile) {
087: try {
088: IFile file = (IFile) marker.getResource();
089: if (file.getLocation() == null
090: || file.getLocationURI() == null)
091: return; // Abort if it cannot be opened
092: IDE.openEditor(part.getSite().getPage(), marker,
093: OpenStrategy.activateOnOpen());
094: } catch (PartInitException e) {
095: // Open an error style dialog for PartInitException by
096: // including any extra information from the nested
097: // CoreException if present.
098:
099: // Check for a nested CoreException
100: CoreException nestedException = null;
101: IStatus status = e.getStatus();
102: if (status != null
103: && status.getException() instanceof CoreException) {
104: nestedException = (CoreException) status
105: .getException();
106: }
107:
108: if (nestedException != null) {
109: // Open an error dialog and include the extra
110: // status information from the nested CoreException
111: reportStatus(nestedException.getStatus());
112: } else {
113: // Open a regular error dialog since there is no
114: // extra information to display
115: reportError(e.getLocalizedMessage());
116: }
117: }
118: }
119: }
120: }
121:
122: /**
123: * Report an error message
124: *
125: * @param message
126: */
127: private void reportError(String message) {
128: IStatus status = new Status(IStatus.ERROR,
129: IDEWorkbenchPlugin.IDE_WORKBENCH, message);
130: reportStatus(status);
131: }
132:
133: /**
134: * Report the status
135: *
136: * @param status
137: */
138: private void reportStatus(IStatus status) {
139: StatusAdapter adapter = new StatusAdapter(status);
140: adapter.setProperty(StatusAdapter.TITLE_PROPERTY,
141: MarkerMessages.OpenMarker_errorTitle);
142: StatusManager.getManager().handle(adapter, StatusManager.SHOW);
143: }
144:
145: /*
146: * (non-Javadoc)
147: *
148: * @see org.eclipse.ui.actions.SelectionProviderAction#selectionChanged(org.eclipse.jface.viewers.IStructuredSelection)
149: */
150: public void selectionChanged(IStructuredSelection selection) {
151: setEnabled(Util.allConcreteSelection(selection));
152: }
153: }
|