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.StrictCompoundCommand; 28 import org.eclipse.emf.edit.command.CommandParameter; 29 import org.eclipse.emf.mapping.Mapping; 30 import org.eclipse.emf.mapping.MappingPlugin; 31 import org.eclipse.emf.mapping.domain.MappingDomain; 32 33 34 38 public class CreateMappingCommand extends AbstractCommand 39 { 40 43 public static final int ENABLE_MULTIPLE_INPUTS = 0x0001; 44 47 public static final int ENABLE_MULTIPLE_OUTPUTS = 0x0002; 48 51 public static final int ENABLE_MAPPED_INPUTS = 0x0004; 52 55 public static final int ENABLE_MAPPED_OUTPUTS = 0x0008; 56 59 public static final int ENABLE_INCOMPATIBLE_METAOBJECTS = 0x0010; 60 63 public static final int ENABLE_INCOMPATIBLE_TYPE_CLASSIFIERS = 0x0020; 64 67 public static final int ENABLE_EMPTY_INPUTS = 0x0040; 68 71 public static final int ENABLE_EMPTY_OUTPUTS = 0x0080; 72 75 public static final int ENABLE_UNMAPPED_PARENTS = 0x0100; 76 79 public static final int ENABLE_ALL = 0xFFFF; 80 81 84 public static Command create(MappingDomain domain, Collection collection) 85 { 86 return 87 domain.createCommand 88 (CreateMappingCommand.class, 89 new CommandParameter(domain.getMappingRoot(), null, collection)); 90 } 91 92 95 public static Command create(MappingDomain domain, Object input, Object output) 96 { 97 Collection collection = new ArrayList (); 98 collection.add(input); 99 collection.add(output); 100 return create(domain, collection); 101 } 102 103 106 public static Command create(MappingDomain domain, Collection inputs, Collection outputs) 107 { 108 Collection collection = new ArrayList (); 109 collection.addAll(inputs); 110 collection.addAll(outputs); 111 return create(domain, collection); 112 } 113 114 117 public static Command create(MappingDomain domain, Collection inputs, Object output) 118 { 119 Collection collection = new ArrayList (); 120 collection.addAll(inputs); 121 collection.add(output); 122 return create(domain, collection); 123 } 124 125 128 public static Command create(MappingDomain domain, Object input, Collection outputs) 129 { 130 Collection collection = new ArrayList (); 131 collection.add(input); 132 collection.addAll(outputs); 133 return create(domain, collection); 134 } 135 136 139 protected static final String LABEL = MappingPlugin.getPlugin().getString("_UI_CreateMappingCommand_label"); 140 141 144 protected static final String DESCRIPTION = MappingPlugin.getPlugin().getString("_UI_CreateMappingCommand_description"); 145 146 149 protected MappingDomain domain; 150 151 154 protected Collection inputs; 155 156 159 protected Collection outputs; 160 161 164 protected Mapping newMapping; 165 166 169 protected Command subcommand; 170 171 174 public CreateMappingCommand(MappingDomain domain, Collection collection, int enablementFlags) 175 { 176 this(domain, collection); 177 } 178 179 182 public CreateMappingCommand(MappingDomain domain, Collection collection) 183 { 184 super(LABEL, DESCRIPTION); 185 186 this.domain = domain; 187 188 inputs = new ArrayList (); 189 outputs = new ArrayList (); 190 for (Iterator objects = collection.iterator(); objects.hasNext(); ) 191 { 192 Object object = objects.next(); 193 if (domain.getMappingRoot().isInputObject(object)) 194 { 195 inputs.add(object); 196 } 197 else if (domain.getMappingRoot().isOutputObject(object)) 198 { 199 outputs.add(object); 200 } 201 else 202 { 203 inputs = outputs = null; 204 break; 205 } 206 } 207 } 208 209 protected boolean prepare() 210 { 211 boolean result = 212 domain != null && 213 inputs != null && 214 outputs != null && 215 domain.getMappingRoot().canCreateMapping(inputs, outputs, null); 216 217 return result; 218 } 219 220 public void execute() 221 { 222 newMapping = domain.getMappingRoot().createMapping(inputs, outputs); 223 224 StrictCompoundCommand subcommands = new StrictCompoundCommand(); 225 subcommands.appendAndExecute(AddMappingCommand.create(domain, newMapping)); 226 subcommand = subcommands.unwrap(); 227 } 228 229 public void undo() 230 { 231 subcommand.undo(); 233 } 234 235 public void redo() 236 { 237 subcommand.redo(); 238 } 239 240 public Collection getResult() 241 { 242 return Collections.singleton(newMapping); 243 } 244 245 public Collection getAffectedObjects() 246 { 247 return Collections.singleton(newMapping); 248 } 249 250 public void dispose() 251 { 252 if (subcommand != null) 253 { 254 subcommand.dispose(); 255 } 256 super.dispose(); 257 } 258 259 public String getLabel() 260 { 261 if (inputs == null || inputs.isEmpty() || outputs == null || outputs.isEmpty()) 262 { 263 return MappingPlugin.getPlugin().getString("_UI_CreateMappingCommand_onesided_label"); 264 } 265 else 266 { 267 return super.getLabel(); 268 } 269 } 270 271 public String getDescription() 272 { 273 if (inputs == null || inputs.isEmpty() || outputs == null || outputs.isEmpty()) 274 { 275 return MappingPlugin.getPlugin().getString("_UI_CreateMappingCommand_onesided_description"); 276 } 277 else 278 { 279 return super.getDescription(); 280 } 281 } 282 283 287 public String toString() 288 { 289 StringBuffer result = new StringBuffer (super.toString()); 290 result.append(" (domain: " + domain + ")"); 291 result.append(" (inputs: " + inputs + ")"); 292 result.append(" (outputs: " + outputs + ")"); 293 result.append(" (newMapping: " + newMapping + ")"); 294 result.append(" (subcommand: " + subcommand + ")"); 295 296 return result.toString(); 297 } 298 } 299 | Popular Tags |