001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 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.lang.reflect.InvocationTargetException;
013: import java.util.List;
014:
015: import org.eclipse.core.runtime.IProgressMonitor;
016: import org.eclipse.core.runtime.IStatus;
017: import org.eclipse.jface.operation.IRunnableWithProgress;
018: import org.eclipse.jface.viewers.IStructuredSelection;
019: import org.eclipse.jface.viewers.StructuredSelection;
020: import org.eclipse.ui.PlatformUI;
021: import org.eclipse.ui.internal.ide.StatusUtil;
022: import org.eclipse.ui.statushandlers.StatusManager;
023: import org.eclipse.ui.views.markers.internal.MarkerAdapter.MarkerCategory;
024:
025: /**
026: * The ActionSelectAll is the action for selecting all of the entries.
027: *
028: */
029: public class ActionSelectAll extends MarkerSelectionProviderAction {
030:
031: private MarkerView view;
032:
033: /**
034: * Create a new instance of the receiver with the supplied
035: *
036: * @param markerView
037: */
038: public ActionSelectAll(MarkerView markerView) {
039: super (markerView.getViewer(),
040: MarkerMessages.selectAllAction_title);
041: setEnabled(true);
042: view = markerView;
043: }
044:
045: /*
046: * (non-Javadoc)
047: *
048: * @see org.eclipse.jface.action.Action#run()
049: */
050: public void run() {
051:
052: if (view.getMarkerAdapter().isBuilding())
053: return;
054:
055: IRunnableWithProgress selectionRunnableWithProgress = new IRunnableWithProgress() {
056: /*
057: * (non-Javadoc)
058: *
059: * @see org.eclipse.jface.operation.IRunnableWithProgress#run(org.eclipse.core.runtime.IProgressMonitor)
060: */
061: public void run(IProgressMonitor monitor) {
062:
063: try {
064: monitor.beginTask(
065: MarkerMessages.selectAllAction_title, 100);
066: monitor
067: .subTask(MarkerMessages.selectAllAction_calculating);
068: if (view.getMarkerAdapter().isShowingHierarchy()) {
069:
070: if (monitor.isCanceled())
071: return;
072:
073: monitor.worked(10);
074: PlatformUI.getWorkbench().getDisplay()
075: .readAndDispatch();
076: MarkerCategory[] categories = view
077: .getMarkerAdapter().getCategories();
078: int totalSize = 0;
079: for (int i = 0; i < categories.length; i++) {
080: MarkerCategory markerCategory = categories[i];
081: totalSize += markerCategory
082: .getDisplayedSize();
083: }
084: monitor.worked(10);
085: PlatformUI.getWorkbench().getDisplay()
086: .readAndDispatch();
087: Object[] selection = new Object[totalSize];
088: int index = 0;
089: for (int i = 0; i < categories.length; i++) {
090: MarkerNode[] children = categories[i]
091: .getChildren();
092: System.arraycopy(children, 0, selection,
093: index, children.length);
094: index += children.length;
095: }
096: monitor.worked(10);
097: if (monitor.isCanceled())
098: return;
099: PlatformUI.getWorkbench().getDisplay()
100: .readAndDispatch();
101: monitor
102: .subTask(MarkerMessages.selectAllAction_applying);
103: getSelectionProvider().setSelection(
104: new StructuredSelection(selection));
105: } else {
106: if (monitor.isCanceled())
107: return;
108: monitor.worked(10);
109: List selection = view.getMarkerAdapter()
110: .getCurrentMarkers().asList();
111: monitor.worked(10);
112: monitor
113: .subTask(MarkerMessages.selectAllAction_applying);
114: PlatformUI.getWorkbench().getDisplay()
115: .readAndDispatch();
116: getSelectionProvider().setSelection(
117: new StructuredSelection(selection));
118: }
119: } finally {
120: monitor.done();
121: }
122:
123: }
124: };
125:
126: try {
127: PlatformUI.getWorkbench().getProgressService().run(false,
128: false, selectionRunnableWithProgress);
129: } catch (InvocationTargetException e) {
130: StatusManager.getManager().handle(
131: StatusUtil.newStatus(IStatus.ERROR, e
132: .getLocalizedMessage(), e),
133: StatusManager.LOG);
134: } catch (InterruptedException e) {
135: StatusManager.getManager().handle(
136: StatusUtil.newStatus(IStatus.ERROR, e
137: .getLocalizedMessage(), e),
138: StatusManager.LOG);
139: }
140:
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(!selection.isEmpty());
150: }
151: }
|