001: /*******************************************************************************
002: * Copyright (c) 2006, 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.ide.undo;
011:
012: import java.util.Map;
013:
014: import org.eclipse.core.resources.IMarker;
015: import org.eclipse.core.runtime.CoreException;
016: import org.eclipse.core.runtime.IAdaptable;
017: import org.eclipse.core.runtime.IProgressMonitor;
018: import org.eclipse.core.runtime.IStatus;
019: import org.eclipse.core.runtime.NullProgressMonitor;
020: import org.eclipse.ui.internal.ide.undo.UndoMessages;
021:
022: /**
023: * An UpdateMarkersOperation represents an undoable operation for updating one
024: * or more markers in the workspace with one or more sets of attributes. Clients
025: * may call the public API from a background thread.
026: *
027: * This class is intended to be instantiated and used by clients. It is not
028: * intended to be subclassed by clients.
029: *
030: * @since 3.3
031: *
032: */
033: public class UpdateMarkersOperation extends AbstractMarkersOperation {
034:
035: // Whether attributes should be merged with existing attributes when
036: // updated, or considered to be complete replacements.
037: private boolean mergeAttributes;
038:
039: /**
040: * Create an undoable operation that can update the specified marker with
041: * the specified attributes.
042: *
043: * @param marker
044: * the marker to be updated
045: * @param attributes
046: * the map of attributes to be assigned to the marker. This map
047: * does not replace the attribute map of the marker, but instead,
048: * each attribute in the map is added or updated with the current
049: * value in the map. In other words
050: * @param name
051: * the name used to describe this operation
052: * @param mergeAttributes
053: * <code>true</code> if the specified map of attributes for the
054: * marker is to be merged with the attributes already specified
055: * for the marker, or <code>false</code> if the specified map
056: * of attributes is to be considered a complete replacement of
057: * all attributes of the marker
058: */
059: public UpdateMarkersOperation(IMarker marker, Map attributes,
060: String name, boolean mergeAttributes) {
061: super (new IMarker[] { marker }, null, attributes, name);
062: this .mergeAttributes = mergeAttributes;
063: }
064:
065: /**
066: * Create an undoable operation that updates many markers to have the same
067: * set of attributes.
068: *
069: * @param markers
070: * the markers to be updated
071: * @param attributes
072: * the map of attributes to be assigned to each marker
073: * @param name
074: * the name used to describe this operation
075: * @param mergeAttributes
076: * <code>true</code> if the specified map of attributes for
077: * each marker is to be merged with the attributes already
078: * specified for that marker, or <code>false</code> if the
079: * specified map of attributes is to be considered a complete
080: * replacement of all attributes for each marker
081: */
082: public UpdateMarkersOperation(IMarker[] markers, Map attributes,
083: String name, boolean mergeAttributes) {
084: super (markers, null, attributes, name);
085: this .mergeAttributes = mergeAttributes;
086: }
087:
088: /*
089: * (non-Javadoc)
090: *
091: * Map execution to updating the markers.
092: *
093: * @see org.eclipse.ui.ide.undo.AbstractWorkspaceOperation#doExecute(org.eclipse.core.runtime.IProgressMonitor,
094: * org.eclipse.core.runtime.IAdaptable)
095: */
096: protected void doExecute(IProgressMonitor monitor, IAdaptable info)
097: throws CoreException {
098: if (monitor == null) {
099: monitor = new NullProgressMonitor();
100: }
101: monitor.beginTask("", 100); //$NON-NLS-1$
102: monitor
103: .setTaskName(UndoMessages.MarkerOperation_UpdateProgress);
104: updateMarkers(100, monitor, mergeAttributes);
105: monitor.done();
106: }
107:
108: /*
109: * (non-Javadoc)
110: *
111: * Map undo to execute (since both operations update the markers).
112: *
113: * @see org.eclipse.ui.ide.undo.AbstractWorkspaceOperation#doUndo(org.eclipse.core.runtime.IProgressMonitor,
114: * org.eclipse.core.runtime.IAdaptable)
115: */
116: protected void doUndo(IProgressMonitor monitor, IAdaptable info)
117: throws CoreException {
118: // doExecute simply swaps the current and remembered attributes,
119: // so it can also be used for undo
120: doExecute(monitor, info);
121: }
122:
123: /*
124: * (non-Javadoc)
125: *
126: * Map undo status to marker update status.
127: *
128: * @see org.eclipse.ui.ide.undo.AbstractMarkersOperation#getBasicUndoStatus()
129: */
130: protected IStatus getBasicUndoStatus() {
131: return getMarkerUpdateStatus();
132: }
133:
134: /*
135: * (non-Javadoc)
136: *
137: * Map redo status to marker update status.
138: *
139: * @see org.eclipse.ui.ide.undo.AbstractMarkersOperation#getBasicRedoStatus()
140: */
141: protected IStatus getBasicRedoStatus() {
142: return getMarkerUpdateStatus();
143: }
144: }
|