1 17 package org.eclipse.emf.mapping.command; 18 19 20 import java.util.ArrayList ; 21 import java.util.Collection ; 22 import java.util.Iterator ; 23 import java.util.ListIterator ; 24 25 import org.eclipse.emf.common.command.AbstractCommand; 26 import org.eclipse.emf.common.command.Command; 27 import org.eclipse.emf.common.command.CompoundCommand; 28 import org.eclipse.emf.edit.command.RemoveCommand; 29 import org.eclipse.emf.mapping.Mapping; 30 import org.eclipse.emf.mapping.MappingPackage; 31 import org.eclipse.emf.mapping.MappingRoot; 32 import org.eclipse.emf.mapping.domain.MappingDomain; 33 34 35 38 public class RemoveOverrideCommand extends AbstractCommand 39 { 40 43 protected MappingDomain mappingDomain; 44 45 48 protected RemoveCommand removeCommand; 49 50 53 protected Command mapCommand; 54 55 58 public RemoveOverrideCommand(MappingDomain domain, RemoveCommand removeCommand) 59 { 60 super(removeCommand.doGetLabel(), removeCommand.doGetDescription()); 61 62 this.mappingDomain = domain; 63 this.removeCommand = removeCommand; 64 } 65 66 protected boolean prepare() 67 { 68 return removeCommand.doCanExecute(); 69 } 70 71 public void execute() 72 { 73 MappingRoot mappingRoot = mappingDomain.getMappingRoot(); 74 CompoundCommand subcommands = new CompoundCommand(); 75 for (Iterator removals = removeCommand.getCollection().iterator(); removals.hasNext(); ) 76 { 77 ArrayList commandList = new ArrayList (); 78 for (Iterator objects = mappingDomain.treeIterator(removals.next()); objects.hasNext(); ) 79 { 80 Object object = objects.next(); 81 for (Iterator mappings = mappingRoot.getMappings(object).iterator(); mappings.hasNext(); ) 82 { 83 Mapping mapping = (Mapping)mappings.next(); 84 Collection outputs = mapping.getOutputs(); 85 if (outputs.size() == 1 && outputs.iterator().next() == object) 86 { 87 commandList.add(RemoveMappingCommand.create(mappingDomain, mapping)); 88 } 89 else 90 { 91 commandList.add(RemoveCommand.create(mappingDomain, mapping, MappingPackage.eINSTANCE.getMapping_Outputs(), object)); 93 } 94 } 95 } 96 for (ListIterator commands = commandList.listIterator(commandList.size()); commands.hasPrevious(); ) 97 { 98 subcommands.appendAndExecute((Command)commands.previous()); 99 } 100 } 101 mapCommand = !subcommands.isEmpty() ? subcommands.unwrap() : null; 102 removeCommand.doExecute(); 103 } 104 105 public void undo() 106 { 107 removeCommand.doUndo(); 108 if (mapCommand != null) 109 { 110 mapCommand.undo(); 111 } 112 } 113 114 public void redo() 115 { 116 if (mapCommand != null) 117 { 118 mapCommand.redo(); 119 } 120 removeCommand.doRedo(); 121 } 122 123 public void dispose() 124 { 125 if (mapCommand != null) 126 { 127 mapCommand.dispose(); 128 } 129 removeCommand.doDispose(); 130 } 131 132 public Collection getResult() 133 { 134 return removeCommand.doGetResult(); 135 } 136 137 public Collection getAffectedObjects() 138 { 139 return removeCommand.doGetAffectedObjects(); 140 } 141 142 146 public String toString() 147 { 148 StringBuffer result = new StringBuffer (super.toString()); 149 result.append(" (mappingDomain: " + mappingDomain + ")"); 150 result.append(" (mapCommand: " + mapCommand + ")"); 151 152 return result.toString(); 153 } 154 } 155 | Popular Tags |