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 RemoveMappingCommand extends AbstractCommand 42 { 43 46 public static Command create(MappingDomain domain, Mapping mapping) 47 { 48 return create(domain, Collections.singleton(mapping)); 49 } 50 51 54 public static Command create(MappingDomain domain, Collection collection) 55 { 56 return 57 domain.createCommand 58 (RemoveMappingCommand.class, 59 new CommandParameter(domain.getMappingRoot(), null, collection)); 60 } 61 62 65 protected static final String LABEL = MappingPlugin.getPlugin().getString("_UI_RemoveMappingCommand_label"); 66 67 70 protected static final String DESCRIPTION = MappingPlugin.getPlugin().getString("_UI_RemoveMappingCommand_description"); 71 72 75 protected MappingDomain domain; 76 77 80 protected Collection collection; 81 82 85 Command subcommand; 86 87 88 91 public RemoveMappingCommand(MappingDomain domain, Collection collection) 92 { 93 super(LABEL, DESCRIPTION); 94 95 this.domain = domain; 96 this.collection = collection; 97 } 98 99 protected boolean prepare() 100 { 101 boolean result = true; 102 103 if (domain == null || collection == null || collection.isEmpty()) 104 { 105 result = false; 106 } 107 else 108 { 109 for (Iterator objects = collection.iterator(); objects.hasNext(); ) 110 { 111 Object object = objects.next(); 112 if (!(object instanceof Mapping)) 113 { 114 result = false; 115 break; 116 } 117 else 118 { 119 Mapping mapping = (Mapping)object; 120 result = domain.getMappingRoot().canRemoveMapping(mapping); 121 } 122 } 123 } 124 125 return result; 126 } 127 128 public void execute() 129 { 130 CompoundCommand subcommands = new CompoundCommand(); 133 134 for (Iterator mappings = collection.iterator(); mappings.hasNext(); ) 137 { 138 Mapping mapping = (Mapping)mappings.next(); 139 Mapping parentMapping = mapping.getNestedIn(); 140 141 domain.getMappingRoot().deregister(mapping); 144 145 subcommands.appendAndExecute(new RemoveCommand(domain, parentMapping, MappingPackage.eINSTANCE.getMapping_Nested(), mapping)); 149 150 Collection nestedMappings = new ArrayList (mapping.getNested()); 151 if (!nestedMappings.isEmpty()) 152 { 153 subcommands.appendAndExecute(new RemoveCommand(domain, mapping, MappingPackage.eINSTANCE.getMapping_Nested(), nestedMappings)); 156 subcommands.appendAndExecute(new AddCommand(domain, parentMapping, MappingPackage.eINSTANCE.getMapping_Nested(), nestedMappings)); 157 } 158 } 159 160 subcommand = subcommands.unwrap(); 161 } 162 163 public void undo() 164 { 165 for (Iterator objects = collection.iterator(); objects.hasNext(); ) 166 { 167 Mapping mapping = (Mapping)objects.next(); 168 domain.getMappingRoot().register(mapping); 169 } 170 171 subcommand.undo(); 172 } 173 174 public void redo() 175 { 176 for (Iterator objects = collection.iterator(); objects.hasNext(); ) 177 { 178 Mapping mapping = (Mapping)objects.next(); 179 domain.getMappingRoot().deregister(mapping); 180 } 181 182 subcommand.redo(); 183 } 184 185 public Collection getResult() 186 { 187 return collection; 188 } 189 190 public void dispose() 191 { 192 if (subcommand != null) 193 { 194 subcommand.dispose(); 195 } 196 super.dispose(); 197 } 198 199 203 public String toString() 204 { 205 StringBuffer result = new StringBuffer (super.toString()); 206 result.append(" (domain: " + domain + ")"); 207 result.append(" (collection: " + collection + ")"); 208 result.append(" (subcommand: " + subcommand + ")"); 209 210 return result.toString(); 211 } 212 } 213 | Popular Tags |