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 24 import org.eclipse.emf.common.command.AbstractCommand; 25 import org.eclipse.emf.common.command.Command; 26 import org.eclipse.emf.common.command.CompoundCommand; 27 import org.eclipse.emf.edit.command.AddCommand; 28 import org.eclipse.emf.mapping.MappedObjectState; 29 import org.eclipse.emf.mapping.MappingRoot; 30 import org.eclipse.emf.mapping.domain.MappingDomain; 31 32 33 36 public class AddOverrideCommand extends AbstractCommand 37 { 38 41 protected MappingDomain mappingDomain; 42 43 46 protected AddCommand addCommand; 47 48 51 protected Command mapCommand; 52 53 56 public AddOverrideCommand(MappingDomain domain, AddCommand addCommand) 57 { 58 super(addCommand.doGetLabel(), addCommand.doGetDescription()); 59 60 this.mappingDomain = domain; 61 this.addCommand = addCommand; 62 } 63 64 protected boolean prepare() 65 { 66 return addCommand.doCanExecute(); 67 } 68 69 public void execute() 70 { 71 addCommand.doExecute(); 72 73 MappingRoot mappingRoot = mappingDomain.getMappingRoot(); 74 CompoundCommand subcommands = new CompoundCommand(); 75 for (Iterator additions = addCommand.getCollection().iterator(); additions.hasNext(); ) 76 { 77 for (Iterator objects = mappingDomain.treeIterator(additions.next()); objects.hasNext(); ) 78 { 79 Object object = objects.next(); 80 MappedObjectState mappedObjectState = mappingRoot.getMappedObjectState(object); 81 Object originatingInput = mappedObjectState.getOriginatingInput(); 82 83 if (originatingInput == null) 87 { 88 mappedObjectState.setOutput(); 89 } 90 else if (mappingRoot.isAttachedObject(object)) 91 { 92 mapOutputObject(object, originatingInput, subcommands); 93 } 94 } 95 } 96 mapCommand = !subcommands.isEmpty() ? subcommands.unwrap() : null; 97 } 98 99 protected void mapOutputObject(Object outputObject, Object originatingInput, CompoundCommand subcommands) 100 { 101 subcommands.appendAndExecute(CreateMappingCommand.create(mappingDomain, originatingInput, outputObject)); 102 } 103 104 public void undo() 105 { 106 if (mapCommand != null) 107 { 108 mapCommand.undo(); 109 } 110 addCommand.doUndo(); 111 } 112 113 public void redo() 114 { 115 addCommand.doRedo(); 116 if (mapCommand != null) 117 { 118 mapCommand.redo(); 119 } 120 } 121 122 public void dispose() 123 { 124 if (mapCommand != null) 125 { 126 mapCommand.dispose(); 127 } 128 addCommand.doDispose(); 129 } 130 131 public Collection getResult() 132 { 133 Collection result = new ArrayList (); 134 result.addAll(addCommand.doGetResult()); 135 if (mapCommand != null) 136 { 137 result.addAll(mapCommand.getResult()); 138 } 139 return result; 140 } 141 142 public Collection getAffectedObjects() 143 { 144 Collection result = new ArrayList (); 145 result.addAll(addCommand.doGetAffectedObjects()); 146 if (mapCommand != null) 147 { 148 result.addAll(mapCommand.getAffectedObjects()); 149 } 150 return result; 151 } 152 153 157 public String toString() 158 { 159 StringBuffer result = new StringBuffer (super.toString()); 160 result.append(" (mappingDomain: " + mappingDomain + ")"); 161 result.append(" (mapCommand: " + mapCommand + ")"); 162 163 return result.toString(); 164 } 165 } 166 | Popular Tags |