1 11 12 package org.eclipse.ui.ide.undo; 13 14 import java.util.HashMap ; 15 import java.util.Iterator ; 16 import java.util.Map ; 17 18 import org.eclipse.core.resources.IMarker; 19 import org.eclipse.core.resources.IResource; 20 import org.eclipse.core.runtime.CoreException; 21 import org.eclipse.core.runtime.IProgressMonitor; 22 import org.eclipse.core.runtime.IStatus; 23 import org.eclipse.core.runtime.Status; 24 import org.eclipse.core.runtime.jobs.ISchedulingRule; 25 import org.eclipse.core.runtime.jobs.MultiRule; 26 import org.eclipse.ui.internal.ide.undo.MarkerDescription; 27 import org.eclipse.ui.internal.ide.undo.UndoMessages; 28 29 40 abstract class AbstractMarkersOperation extends AbstractWorkspaceOperation { 41 42 MarkerDescription[] markerDescriptions; 43 44 IMarker[] markers; 45 46 Map [] attributes; 47 48 65 AbstractMarkersOperation(IMarker[] markers, 66 MarkerDescription[] markerDescriptions, Map attributes, String name) { 67 super(name); 68 this.markers = markers; 69 this.attributes = null; 70 if (attributes != null && markers != null) { 76 if (markers.length > 1) { 77 this.attributes = new Map [markers.length]; 78 for (int i = 0; i < markers.length; i++) { 79 Map copiedAttributes = new HashMap (); 80 copiedAttributes.putAll(attributes); 81 this.attributes[i] = copiedAttributes; 82 } 83 } else { 84 this.attributes = new Map [] { attributes }; 85 } 86 } 87 setMarkerDescriptions(markerDescriptions); 88 } 89 90 102 protected void deleteMarkers(int work, IProgressMonitor monitor) 103 throws CoreException { 104 if (markers == null || markers.length == 0) { 105 monitor.worked(work); 106 return; 107 } 108 int markerWork = work / markers.length; 109 markerDescriptions = new MarkerDescription[markers.length]; 110 for (int i = 0; i < markers.length; i++) { 111 markerDescriptions[i] = new MarkerDescription(markers[i]); 112 markers[i].delete(); 113 monitor.worked(markerWork); 114 } 115 markers = new IMarker[0]; 116 } 117 118 128 protected void createMarkers(int work, IProgressMonitor monitor) 129 throws CoreException { 130 if (markerDescriptions == null || markerDescriptions.length == 0) { 131 monitor.worked(work); 132 return; 133 } 134 int markerWork = work / markerDescriptions.length; 135 markers = new IMarker[markerDescriptions.length]; 136 137 for (int i = 0; i < markerDescriptions.length; i++) { 139 markers[i] = markerDescriptions[i].createMarker(); 140 monitor.worked(markerWork); 141 } 142 } 143 144 159 protected void updateMarkers(int work, IProgressMonitor monitor, 160 boolean mergeAttributes) throws CoreException { 161 if (attributes == null || markers == null 162 || attributes.length != markers.length || markers.length == 0) { 163 monitor.worked(work); 164 return; 165 } 166 int markerWork = work / markers.length; 167 for (int i = 0; i < markers.length; i++) { 168 if (mergeAttributes) { 169 Map oldAttributes = markers[i].getAttributes(); 170 int increment = markerWork / attributes[i].size(); 171 Map replacedAttributes = new HashMap (); 172 173 for (Iterator iter = attributes[i].keySet().iterator(); iter 174 .hasNext();) { 175 String key = (String ) iter.next(); 176 Object val = attributes[i].get(key); 177 markers[i].setAttribute(key, val); 178 replacedAttributes.put(key, oldAttributes.get(key)); 179 monitor.worked(increment); 180 } 181 attributes[i] = replacedAttributes; 182 } else { 183 Map oldAttributes = markers[i].getAttributes(); 185 markers[i].setAttributes(attributes[i]); 186 attributes[i] = oldAttributes; 187 } 188 } 189 } 190 191 197 protected void setMarkerDescriptions(MarkerDescription[] descriptions) { 198 markerDescriptions = descriptions; 199 addUndoContexts(); 200 updateTargetResources(); 201 } 202 203 207 208 private void updateTargetResources() { 209 IResource[] resources = null; 210 if (markers == null) { 211 if (markerDescriptions != null) { 212 resources = new IResource[markerDescriptions.length]; 213 for (int i = 0; i < markerDescriptions.length; i++) { 214 resources[i] = markerDescriptions[i].getResource(); 215 } 216 } 217 } else { 218 resources = new IResource[markers.length]; 219 for (int i = 0; i < markers.length; i++) { 220 resources[i] = markers[i].getResource(); 221 } 222 } 223 setTargetResources(resources); 224 } 225 226 236 237 private void addUndoContexts() { 238 String [] types = null; 239 if (markers == null) { 240 if (markerDescriptions != null) { 241 types = new String [markerDescriptions.length]; 242 for (int i = 0; i < markerDescriptions.length; i++) { 243 types[i] = markerDescriptions[i].getType(); 244 } 245 } 246 } else { 247 types = new String [markers.length]; 248 for (int i = 0; i < markers.length; i++) { 249 try { 250 types[i] = markers[i].getType(); 251 } catch (CoreException e) { 252 } 253 254 } 255 } 256 if (types != null) { 257 for (int i = 0; i < types.length; i++) { 258 if (types[i] != null) { 262 if (types[i].equals(IMarker.BOOKMARK)) { 263 addContext(WorkspaceUndoUtil.getBookmarksUndoContext()); 264 } else if (types[i].equals(IMarker.TASK)) { 265 addContext(WorkspaceUndoUtil.getTasksUndoContext()); 266 } else if (types[i] != null) { 267 addContext(WorkspaceUndoUtil.getWorkspaceUndoContext()); 269 } 270 } 271 } 272 } 273 } 274 275 281 public IMarker[] getMarkers() { 282 return markers; 283 } 284 285 292 protected boolean markersExist() { 293 if (markers == null || markers.length == 0) { 294 return false; 295 } 296 for (int i = 0; i < markers.length; i++) { 297 if (!markers[i].exists()) { 298 return false; 299 } 300 } 301 return true; 302 303 } 304 305 312 protected abstract IStatus getBasicUndoStatus(); 313 314 321 protected abstract IStatus getBasicRedoStatus(); 322 323 328 public IStatus computeExecutionStatus(IProgressMonitor monitor) { 329 IStatus status = getBasicRedoStatus(); 330 if (status.isOK()) { 331 return super.computeExecutionStatus(monitor); 332 } 333 if (status.getSeverity() == IStatus.ERROR) { 334 markInvalid(); 335 } 336 return status; 337 } 338 339 344 public IStatus computeUndoableStatus(IProgressMonitor monitor) { 345 IStatus status = getBasicUndoStatus(); 346 if (status.isOK()) { 347 return super.computeUndoableStatus(monitor); 348 } 349 if (status.getSeverity() == IStatus.ERROR) { 350 markInvalid(); 351 } 352 return status; 353 } 354 355 360 public IStatus computeRedoableStatus(IProgressMonitor monitor) { 361 IStatus status = getBasicRedoStatus(); 362 if (status.isOK()) { 363 return super.computeRedoableStatus(monitor); 364 } 365 if (status.getSeverity() == IStatus.ERROR) { 366 markInvalid(); 367 } 368 return status; 369 } 370 371 382 protected IStatus getMarkerDeletionStatus() { 383 if (markersExist()) { 384 return Status.OK_STATUS; 385 } 386 return getErrorStatus(UndoMessages.MarkerOperation_MarkerDoesNotExist); 387 } 388 389 400 protected IStatus getMarkerCreationStatus() { 401 if (!resourcesExist()) { 402 return getErrorStatus(UndoMessages.MarkerOperation_ResourceDoesNotExist); 403 } else if (markerDescriptions == null) { 404 return getErrorStatus(UndoMessages.MarkerOperation_NotEnoughInfo); 405 } 406 return Status.OK_STATUS; 407 } 408 409 420 protected IStatus getMarkerUpdateStatus() { 421 if (!markersExist()) { 422 return getErrorStatus(UndoMessages.MarkerOperation_MarkerDoesNotExist); 423 } else if (attributes == null) { 424 return getErrorStatus(UndoMessages.MarkerOperation_NotEnoughInfo); 425 } 426 return Status.OK_STATUS; 427 } 428 429 434 protected ISchedulingRule getExecuteSchedulingRule() { 435 ISchedulingRule[] ruleArray = new ISchedulingRule[resources.length]; 436 for (int i = 0; i < resources.length; i++) { 437 ruleArray[i] = getWorkspaceRuleFactory().markerRule(resources[i]); 438 } 439 return MultiRule.combine(ruleArray); 440 } 441 442 447 protected ISchedulingRule getUndoSchedulingRule() { 448 return getExecuteSchedulingRule(); 449 } 450 451 456 protected void appendDescriptiveText(StringBuffer text) { 457 super.appendDescriptiveText(text); 458 text.append(" markers: "); text.append(markers); 460 text.append('\''); 461 text.append(" markerDescriptions: "); text.append(markerDescriptions); 463 text.append('\''); 464 text.append(" attributes: "); text.append(attributes); 466 text.append('\''); 467 } 468 } 469 | Popular Tags |