1 17 package org.eclipse.emf.mapping.command; 18 19 20 import java.util.ArrayList ; 21 import java.util.Collection ; 22 import java.util.Collections ; 23 import java.util.Iterator ; 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.AddCommand; 29 import org.eclipse.emf.edit.command.CommandParameter; 30 import org.eclipse.emf.edit.command.RemoveCommand; 31 import org.eclipse.emf.mapping.Mapping; 32 import org.eclipse.emf.mapping.MappingPackage; 33 import org.eclipse.emf.mapping.MappingPlugin; 34 import org.eclipse.emf.mapping.domain.MappingDomain; 35 36 37 41 public class AddMappingCommand extends AbstractCommand 42 { 43 46 public static Command create(MappingDomain domain, Collection collection) 47 { 48 return 49 domain.createCommand 50 (AddMappingCommand.class, 51 new CommandParameter(domain.getMappingRoot(), null, collection)); 52 } 53 54 57 public static Command create(MappingDomain domain, Mapping mapping) 58 { 59 return create(domain, Collections.singleton(mapping)); 60 } 61 62 65 protected static final String LABEL = MappingPlugin.getPlugin().getString("_UI_AddMappingCommand_label"); 66 67 70 protected static final String DESCRIPTION = MappingPlugin.getPlugin().getString("_UI_AddMappingCommand_description"); 71 72 75 protected MappingDomain domain; 76 77 80 protected Collection collection; 81 82 85 Command subcommand; 86 87 90 public AddMappingCommand(MappingDomain domain, Collection collection) 91 { 92 super(LABEL, DESCRIPTION); 93 94 this.domain = domain; 95 this.collection = collection; 96 } 97 98 protected boolean prepare() 99 { 100 boolean result = domain != null; 101 for (Iterator objects = collection.iterator(); objects.hasNext(); ) 102 { 103 Object object = objects.next(); 104 if (!(object instanceof Mapping)) 105 { 106 result = false; 107 break; 108 } 109 } 110 111 return result; 112 } 113 114 public void execute() 115 { 116 CompoundCommand subcommands = new CompoundCommand(); 119 120 for (Iterator mappings = collection.iterator(); mappings.hasNext(); ) 123 { 124 Mapping mapping = (Mapping)mappings.next(); 125 126 Mapping parentMapping = domain.getMappingRoot().getParentMapping(mapping.getMappedObjects()); 129 130 domain.getMappingRoot().register(mapping); 133 134 if (subcommands.appendAndExecute(new AddCommand(domain, parentMapping, MappingPackage.eINSTANCE.getMapping_Nested(), mapping))) 138 { 139 Collection siblingsToReparent = new ArrayList (); 143 for (Iterator i = parentMapping.getNested().iterator(); i.hasNext(); ) 144 { 145 Mapping siblingMapping = (Mapping)i.next(); 146 if (siblingMapping != mapping) 147 { 148 if (domain.getMappingRoot().getParentMapping(siblingMapping.getMappedObjects()) == mapping) 149 { 150 siblingsToReparent.add(siblingMapping); 151 } 152 } 153 } 154 155 if (!siblingsToReparent.isEmpty()) 158 { 159 subcommands.appendAndExecute 162 (new RemoveCommand(domain, parentMapping, MappingPackage.eINSTANCE.getMapping_Nested(), siblingsToReparent)); 164 165 subcommands.appendAndExecute 168 (new AddCommand(domain, mapping, MappingPackage.eINSTANCE.getMapping_Nested(), siblingsToReparent)); 170 } 171 } 172 } 173 174 subcommand = subcommands.unwrap(); 175 } 176 177 public void undo() 178 { 179 for (Iterator objects = collection.iterator(); objects.hasNext(); ) 180 { 181 Mapping mapping = (Mapping)objects.next(); 182 domain.getMappingRoot().deregister(mapping); 183 } 184 185 subcommand.undo(); 186 } 187 188 public void redo() 189 { 190 for (Iterator objects = collection.iterator(); objects.hasNext(); ) 191 { 192 Mapping mapping = (Mapping)objects.next(); 193 domain.getMappingRoot().register(mapping); 194 } 195 196 subcommand.redo(); 197 } 198 199 public Collection getResult() 200 { 201 return collection; 202 } 203 204 public Collection getAffectedObjects() 205 { 206 return collection; 207 } 208 209 public void dispose() 210 { 211 if (subcommand != null) 212 { 213 subcommand.dispose(); 214 } 215 super.dispose(); 216 } 217 218 222 public String toString() 223 { 224 StringBuffer result = new StringBuffer (super.toString()); 225 result.append(" (domain: " + domain + ")"); 226 result.append(" (collection: " + collection + ")"); 227 result.append(" (subcommand: " + subcommand + ")"); 228 229 return result.toString(); 230 } 231 } 232 | Popular Tags |