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: *******************************************************************************/package org.eclipse.ui.views.markers.internal;
011:
012: import java.util.ArrayList;
013: import java.util.List;
014:
015: import org.eclipse.core.commands.operations.IUndoableOperation;
016: import org.eclipse.core.resources.IMarker;
017: import org.eclipse.jface.dialogs.MessageDialog;
018: import org.eclipse.jface.viewers.ISelectionProvider;
019: import org.eclipse.jface.viewers.IStructuredSelection;
020: import org.eclipse.osgi.util.NLS;
021: import org.eclipse.ui.ide.undo.DeleteMarkersOperation;
022: import org.eclipse.ui.ide.undo.WorkspaceUndoUtil;
023:
024: /**
025: * ActionDeleteCompleted is the action for deleting completed markers.
026: *
027: */
028: public class ActionDeleteCompleted extends
029: MarkerSelectionProviderAction {
030:
031: private TaskView part;
032:
033: /**
034: * Constructs an ActionDeleteCompleted instance
035: *
036: * @param part
037: * @param provider
038: */
039: public ActionDeleteCompleted(TaskView part,
040: ISelectionProvider provider) {
041: super (provider, MarkerMessages.deleteCompletedAction_title);
042: this .part = part;
043: setEnabled(false);
044: }
045:
046: /*
047: * (non-Javadoc)
048: *
049: * @see org.eclipse.jface.action.Action#run()
050: */
051: public void run() {
052: final List completed = getCompletedTasks();
053: // Check if there is anything to do
054: if (completed.size() == 0) {
055: MessageDialog.openInformation(part.getSite().getShell(),
056: MarkerMessages.deleteCompletedTasks_dialogTitle,
057: MarkerMessages.deleteCompletedTasks_noneCompleted);
058: return;
059: }
060: String message;
061: if (completed.size() == 1) {
062: message = MarkerMessages.deleteCompletedTasks_permanentSingular;
063: } else {
064: message = NLS
065: .bind(
066: MarkerMessages.deleteCompletedTasks_permanentPlural,
067: String.valueOf(completed.size()));
068: }
069: // Verify.
070: if (!MessageDialog.openConfirm(part.getSite().getShell(),
071: MarkerMessages.deleteCompletedTasks_dialogTitle,
072: message)) {
073: return;
074: }
075: IMarker[] markers = (IMarker[]) completed
076: .toArray(new IMarker[completed.size()]);
077: IUndoableOperation op = new DeleteMarkersOperation(markers,
078: getText());
079: execute(op, MarkerMessages.deleteCompletedTasks_errorMessage,
080: null, WorkspaceUndoUtil.getUIInfoAdapter(part.getSite()
081: .getShell()));
082: }
083:
084: private List getCompletedTasks() {
085: List completed = new ArrayList();
086:
087: MarkerList markerList = part.getVisibleMarkers();
088:
089: ConcreteMarker[] markers = markerList.toArray();
090:
091: for (int i = 0; i < markers.length; i++) {
092: ConcreteMarker marker = markers[i];
093: if (marker instanceof TaskMarker) {
094: TaskMarker taskMarker = (TaskMarker) marker;
095:
096: if (taskMarker.getDone() == 1) {
097: completed.add(taskMarker.getMarker());
098: }
099: }
100: }
101:
102: return completed;
103: }
104:
105: /*
106: * (non-Javadoc)
107: *
108: * @see org.eclipse.ui.actions.SelectionProviderAction#selectionChanged(org.eclipse.jface.viewers.IStructuredSelection)
109: */
110: public void selectionChanged(IStructuredSelection selection) {
111: setEnabled(!selection.isEmpty());
112: }
113:
114: }
|