KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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
12 package org.eclipse.ui.views.markers.internal;
13
14 import java.lang.reflect.InvocationTargetException JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.core.runtime.IProgressMonitor;
18 import org.eclipse.core.runtime.IStatus;
19 import org.eclipse.jface.operation.IRunnableWithProgress;
20 import org.eclipse.jface.viewers.IStructuredSelection;
21 import org.eclipse.jface.viewers.StructuredSelection;
22 import org.eclipse.ui.PlatformUI;
23 import org.eclipse.ui.internal.ide.StatusUtil;
24 import org.eclipse.ui.statushandlers.StatusManager;
25 import org.eclipse.ui.views.markers.internal.MarkerAdapter.MarkerCategory;
26
27 /**
28  * The ActionSelectAll is the action for selecting all of the entries.
29  *
30  */

31 public class ActionSelectAll extends MarkerSelectionProviderAction {
32
33     private MarkerView view;
34
35     /**
36      * Create a new instance of the receiver with the supplied
37      *
38      * @param markerView
39      */

40     public ActionSelectAll(MarkerView markerView) {
41         super(markerView.getViewer(), MarkerMessages.selectAllAction_title);
42         setEnabled(true);
43         view = markerView;
44     }
45
46     /*
47      * (non-Javadoc)
48      *
49      * @see org.eclipse.jface.action.Action#run()
50      */

51     public void run() {
52
53         if (view.getMarkerAdapter().isBuilding())
54             return;
55
56         IRunnableWithProgress selectionRunnableWithProgress = new IRunnableWithProgress() {
57             /*
58              * (non-Javadoc)
59              *
60              * @see org.eclipse.jface.operation.IRunnableWithProgress#run(org.eclipse.core.runtime.IProgressMonitor)
61              */

62             public void run(IProgressMonitor monitor) {
63
64                 try {
65                     monitor.beginTask(MarkerMessages.selectAllAction_title,
66                              100);
67                     monitor.subTask(MarkerMessages.selectAllAction_calculating);
68                     if (view.getMarkerAdapter().isShowingHierarchy()) {
69
70                         if (monitor.isCanceled())
71                             return;
72
73                         monitor.worked(10);
74                         PlatformUI.getWorkbench().getDisplay()
75                                 .readAndDispatch();
76                         MarkerCategory[] categories = view.getMarkerAdapter()
77                                 .getCategories();
78                         int totalSize = 0;
79                         for (int i = 0; i < categories.length; i++) {
80                             MarkerCategory markerCategory = categories[i];
81                             totalSize += markerCategory.getDisplayedSize();
82                         }
83                         monitor.worked(10);
84                         PlatformUI.getWorkbench().getDisplay()
85                                 .readAndDispatch();
86                         Object JavaDoc[] selection = new Object JavaDoc[totalSize];
87                         int index = 0;
88                         for (int i = 0; i < categories.length; i++) {
89                             MarkerNode[] children = categories[i].getChildren();
90                             System.arraycopy(children, 0, selection, index,
91                                     children.length);
92                             index += children.length;
93                         }
94                         monitor.worked(10);
95                         if (monitor.isCanceled())
96                             return;
97                         PlatformUI.getWorkbench().getDisplay()
98                                 .readAndDispatch();
99                         monitor
100                                 .subTask(MarkerMessages.selectAllAction_applying);
101                         getSelectionProvider().setSelection(
102                                 new StructuredSelection(selection));
103                     } else {
104                         if (monitor.isCanceled())
105                             return;
106                         monitor.worked(10);
107                         List JavaDoc selection = view.getMarkerAdapter()
108                                 .getCurrentMarkers().asList();
109                         monitor.worked(10);
110                         monitor
111                                 .subTask(MarkerMessages.selectAllAction_applying);
112                         PlatformUI.getWorkbench().getDisplay()
113                                 .readAndDispatch();
114                         getSelectionProvider().setSelection(
115                                 new StructuredSelection(selection));
116                     }
117                 } finally {
118                     monitor.done();
119                 }
120
121             }
122         };
123
124         try {
125             PlatformUI.getWorkbench().getProgressService().run(false, false,
126                     selectionRunnableWithProgress);
127         } catch (InvocationTargetException JavaDoc e) {
128             StatusManager.getManager().handle(
129                     StatusUtil.newStatus(IStatus.ERROR,
130                             e.getLocalizedMessage(), e), StatusManager.LOG);
131         } catch (InterruptedException JavaDoc e) {
132             StatusManager.getManager().handle(
133                     StatusUtil.newStatus(IStatus.ERROR,
134                             e.getLocalizedMessage(), e), StatusManager.LOG);
135         }
136
137     }
138
139     /*
140      * (non-Javadoc)
141      *
142      * @see org.eclipse.ui.actions.SelectionProviderAction#selectionChanged(org.eclipse.jface.viewers.IStructuredSelection)
143      */

144     public void selectionChanged(IStructuredSelection selection) {
145         setEnabled(!selection.isEmpty());
146     }
147 }
148
Popular Tags