1 17 package org.eclipse.emf.mapping.command; 18 19 20 import java.util.Collection ; 21 import java.util.Iterator ; 22 23 import org.eclipse.emf.common.command.AbstractCommand; 24 import org.eclipse.emf.common.command.Command; 25 import org.eclipse.emf.common.command.CompoundCommand; 26 import org.eclipse.emf.edit.command.RemoveCommand; 27 import org.eclipse.emf.edit.command.SetCommand; 28 import org.eclipse.emf.mapping.MappedObjectState; 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 SetOverrideCommand extends AbstractCommand 39 { 40 43 protected MappingDomain mappingDomain; 44 45 48 protected SetCommand setCommand; 49 50 53 protected Command mapCommand; 54 55 58 public SetOverrideCommand(MappingDomain domain, SetCommand setCommand) 59 { 60 super(setCommand.doGetLabel(), setCommand.doGetDescription()); 61 62 this.mappingDomain = domain; 63 this.setCommand = setCommand; 64 } 65 66 protected boolean prepare() 67 { 68 return setCommand.doCanExecute(); 69 } 70 71 public void execute() 72 { 73 setCommand.doExecute(); 74 75 MappingRoot mappingRoot = mappingDomain.getMappingRoot(); 76 CompoundCommand subcommands = new CompoundCommand(); 77 78 if (setCommand.getOldValue() != null) 79 { 80 Object oldValue = setCommand.getOldValue(); 81 for (Iterator objects = mappingDomain.treeIterator(oldValue); objects.hasNext(); ) 82 { 83 Object object = objects.next(); 84 for (Iterator mappings = mappingRoot.getMappings(object).iterator(); mappings.hasNext(); ) 85 { 86 Mapping mapping = (Mapping)mappings.next(); 87 Collection outputs = mapping.getOutputs(); 88 if (outputs.size() == 1 && outputs.iterator().next() == object) 89 { 90 subcommands.append(RemoveMappingCommand.create(mappingDomain, mapping)); 91 } 92 else 93 { 94 subcommands.append( 95 (RemoveCommand.create(mappingDomain, mapping, MappingPackage.eINSTANCE.getMapping_Outputs(), object))); 97 } 98 } 99 } 100 } 101 102 if (setCommand.getValue() != null) 103 { 104 Object value = setCommand.getValue(); 105 for (Iterator objects = mappingDomain.treeIterator(value); objects.hasNext(); ) 106 { 107 Object object = objects.next(); 108 MappedObjectState mappedObjectState = mappingRoot.getMappedObjectState(object); 109 Object originatingInput = mappedObjectState.getOriginatingInput(); 110 111 if (originatingInput == null) 115 { 116 mappedObjectState.setOutput(); 117 } 118 else if (mappingRoot.isAttachedObject(object)) 119 { 120 subcommands.append(CreateMappingCommand.create(mappingDomain, originatingInput, object)); 121 } 122 } 123 } 124 125 mapCommand = subcommands.unwrap(); 126 if (mapCommand.canExecute()) 127 { 128 mapCommand.execute(); 129 } 130 else 131 { 132 mapCommand.dispose(); 133 mapCommand = null; 134 } 135 } 136 137 public void undo() 138 { 139 if (mapCommand != null) 140 { 141 mapCommand.undo(); 142 } 143 setCommand.doUndo(); 144 } 145 146 public void redo() 147 { 148 setCommand.doRedo(); 149 if (mapCommand != null) 150 { 151 mapCommand.redo(); 152 } 153 } 154 155 public void dispose() 156 { 157 if (mapCommand != null) 158 { 159 mapCommand.dispose(); 160 } 161 setCommand.doDispose(); 162 } 163 164 public Collection getResult() 165 { 166 return setCommand.doGetResult(); 167 } 168 169 public Collection getAffectedObjects() 170 { 171 return setCommand.doGetAffectedObjects(); 172 } 173 174 178 public String toString() 179 { 180 StringBuffer result = new StringBuffer (super.toString()); 181 result.append(" (mappingDomain: " + mappingDomain + ")"); 182 result.append(" (mapCommand: " + mapCommand + ")"); 183 184 return result.toString(); 185 } 186 } 187 | Popular Tags |