001: /*******************************************************************************
002: * Copyright (c) 2005, 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: *******************************************************************************/package org.eclipse.ui.views.markers.internal;
011:
012: import java.util.ArrayList;
013:
014: import org.eclipse.core.commands.ExecutionException;
015: import org.eclipse.core.commands.operations.IUndoableOperation;
016: import org.eclipse.core.resources.IMarker;
017: import org.eclipse.core.runtime.CoreException;
018: import org.eclipse.core.runtime.IAdaptable;
019: import org.eclipse.core.runtime.IProgressMonitor;
020: import org.eclipse.jface.dialogs.ErrorDialog;
021: import org.eclipse.jface.viewers.ISelectionProvider;
022: import org.eclipse.jface.viewers.IStructuredSelection;
023: import org.eclipse.ui.PlatformUI;
024: import org.eclipse.ui.actions.SelectionProviderAction;
025: import org.eclipse.ui.ide.undo.WorkspaceUndoUtil;
026: import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin;
027:
028: /**
029: * MarkerSelectionProviderAction is the abstract super class of the selection
030: * provider actions used by marker views.
031: *
032: */
033: public abstract class MarkerSelectionProviderAction extends
034: SelectionProviderAction {
035:
036: /**
037: * Create a new instance of the receiver.
038: *
039: * @param provider
040: * @param text
041: */
042: public MarkerSelectionProviderAction(ISelectionProvider provider,
043: String text) {
044: super (provider, text);
045:
046: }
047:
048: /**
049: * Get the selected markers in the receiver.
050: *
051: * @return IMarker[]
052: */
053: IMarker[] getSelectedMarkers() {
054:
055: return getSelectedMarkers(getStructuredSelection());
056: }
057:
058: /**
059: * Return the selected markers for the structured selection.
060: *
061: * @param structured
062: * IStructuredSelection
063: * @return IMarker[]
064: */
065: IMarker[] getSelectedMarkers(IStructuredSelection structured) {
066: Object[] selection = structured.toArray();
067: ArrayList markers = new ArrayList();
068: for (int i = 0; i < selection.length; i++) {
069: Object object = selection[i];
070: if (!(object instanceof MarkerNode)) {
071: return new IMarker[0];// still pending
072: }
073: MarkerNode marker = (MarkerNode) object;
074: if (marker.isConcrete()) {
075: markers.add(((ConcreteMarker) object).getMarker());
076: }
077: }
078:
079: return (IMarker[]) markers.toArray(new IMarker[markers.size()]);
080: }
081:
082: /**
083: * Get the selected marker in the receiver.
084: *
085: * @return IMarker
086: */
087: IMarker getSelectedMarker() {
088:
089: ConcreteMarker selection = (ConcreteMarker) getStructuredSelection()
090: .getFirstElement();
091: return selection.getMarker();
092: }
093:
094: /**
095: * Execute the specified undoable operation
096: */
097: void execute(IUndoableOperation operation, String title,
098: IProgressMonitor monitor, IAdaptable uiInfo) {
099: try {
100: PlatformUI.getWorkbench().getOperationSupport()
101: .getOperationHistory().execute(operation, monitor,
102: uiInfo);
103: } catch (ExecutionException e) {
104: if (e.getCause() instanceof CoreException) {
105: ErrorDialog.openError(WorkspaceUndoUtil
106: .getShell(uiInfo), title, null,
107: ((CoreException) e.getCause()).getStatus());
108: } else {
109: IDEWorkbenchPlugin.log(title, e);
110: }
111: }
112: }
113: }
|