KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2005, 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  *******************************************************************************/

11 package org.eclipse.ui.views.markers.internal;
12
13 import java.util.ArrayList JavaDoc;
14
15 import org.eclipse.core.commands.ExecutionException;
16 import org.eclipse.core.commands.operations.IUndoableOperation;
17 import org.eclipse.core.resources.IMarker;
18 import org.eclipse.core.runtime.CoreException;
19 import org.eclipse.core.runtime.IAdaptable;
20 import org.eclipse.core.runtime.IProgressMonitor;
21 import org.eclipse.jface.dialogs.ErrorDialog;
22 import org.eclipse.jface.viewers.ISelectionProvider;
23 import org.eclipse.jface.viewers.IStructuredSelection;
24 import org.eclipse.ui.PlatformUI;
25 import org.eclipse.ui.actions.SelectionProviderAction;
26 import org.eclipse.ui.ide.undo.WorkspaceUndoUtil;
27 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin;
28
29 /**
30  * MarkerSelectionProviderAction is the abstract super class of the selection
31  * provider actions used by marker views.
32  *
33  */

34 public abstract class MarkerSelectionProviderAction extends
35         SelectionProviderAction {
36
37     /**
38      * Create a new instance of the receiver.
39      *
40      * @param provider
41      * @param text
42      */

43     public MarkerSelectionProviderAction(ISelectionProvider provider,
44             String JavaDoc text) {
45         super(provider, text);
46
47     }
48
49     /**
50      * Get the selected markers in the receiver.
51      *
52      * @return IMarker[]
53      */

54     IMarker[] getSelectedMarkers() {
55
56         return getSelectedMarkers(getStructuredSelection());
57     }
58
59     /**
60      * Return the selected markers for the structured selection.
61      *
62      * @param structured
63      * IStructuredSelection
64      * @return IMarker[]
65      */

66     IMarker[] getSelectedMarkers(IStructuredSelection structured) {
67         Object JavaDoc[] selection = structured.toArray();
68         ArrayList JavaDoc markers = new ArrayList JavaDoc();
69         for (int i = 0; i < selection.length; i++) {
70             Object JavaDoc object = selection[i];
71             if (!(object instanceof MarkerNode)) {
72                 return new IMarker[0];// still pending
73
}
74             MarkerNode marker = (MarkerNode) object;
75             if (marker.isConcrete()) {
76                 markers.add(((ConcreteMarker) object).getMarker());
77             }
78         }
79
80         return (IMarker[]) markers.toArray(new IMarker[markers.size()]);
81     }
82
83     /**
84      * Get the selected marker in the receiver.
85      *
86      * @return IMarker
87      */

88     IMarker getSelectedMarker() {
89
90         ConcreteMarker selection = (ConcreteMarker) getStructuredSelection()
91                 .getFirstElement();
92         return selection.getMarker();
93     }
94
95     /**
96      * Execute the specified undoable operation
97      */

98     void execute(IUndoableOperation operation, String JavaDoc title,
99             IProgressMonitor monitor, IAdaptable uiInfo) {
100         try {
101             PlatformUI.getWorkbench().getOperationSupport()
102                     .getOperationHistory().execute(operation, monitor, uiInfo);
103         } catch (ExecutionException e) {
104             if (e.getCause() instanceof CoreException) {
105                 ErrorDialog
106                         .openError(WorkspaceUndoUtil.getShell(uiInfo), title,
107                                 null, ((CoreException) e.getCause())
108                                         .getStatus());
109             } else {
110                 IDEWorkbenchPlugin.log(title, e);
111             }
112         }
113     }
114 }
115
Popular Tags