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.Command; 25 import org.eclipse.emf.common.command.CompoundCommand; 26 import org.eclipse.emf.mapping.Mapping; 27 import org.eclipse.emf.mapping.MappingPlugin; 28 import org.eclipse.emf.mapping.MappingRoot; 29 import org.eclipse.emf.mapping.domain.MappingDomain; 30 31 32 public abstract class MatchMappingCommand extends CompoundCommand 33 { 34 37 protected MappingDomain domain; 38 39 42 protected Mapping mapping; 43 44 47 protected Collection mappedInputs; 48 49 52 protected static final String LABEL = MappingPlugin.getPlugin().getString("_UI_MatchMappingCommand_label"); 53 54 57 protected static final String DESCRIPTION = MappingPlugin.getPlugin().getString("_UI_MatchMappingCommand_description"); 58 59 public MatchMappingCommand(MappingDomain domain, Mapping mapping) 60 { 61 super(LABEL, DESCRIPTION); 62 63 this.domain = domain; 64 this.mapping = mapping; 65 } 66 67 protected boolean prepare() 68 { 69 if (domain != null && mapping != null) 70 { 71 Collection inputChildren = new ArrayList (); 72 for (Iterator inputs = mapping.getSenders().iterator(); inputs.hasNext(); ) 73 { 74 inputChildren.addAll(domain.getChildren(inputs.next())); 75 } 76 77 Collection outputChildren = new ArrayList (); 78 for (Iterator outputs = mapping.getReceivers().iterator(); outputs.hasNext(); ) 79 { 80 outputChildren.addAll(domain.getChildren(outputs.next())); 81 } 82 83 matchChildren(inputChildren, outputChildren); 84 } 85 86 boolean result = super.prepare(); 89 return result; 90 } 91 92 protected void matchChildren(Collection inputChildren, Collection outputChildren) 93 { 94 mappedInputs = new ArrayList (); 95 MappingRoot mappingRoot = domain.getMappingRoot(); 96 boolean multipleMatchesAllowed = (domain.getMappingEnablementFlags() & MappingDomain.ENABLE_MULTIPLE_INPUT_MAPPINGS) != 0; 97 98 for (Iterator childOutputs = outputChildren.iterator(); childOutputs.hasNext(); ) 99 { 100 Object childOutput = childOutputs.next(); 101 if (mappingRoot.getMappings(childOutput).isEmpty()) 102 { 103 Collection mappedObjects = new ArrayList (); 104 105 for (Iterator childInputs = inputChildren.iterator(); childInputs.hasNext(); ) 106 { 107 Object childInput = childInputs.next(); 108 boolean canCreateMapping = 109 multipleMatchesAllowed || (!mappedInputs.contains(childInput) && mappingRoot.getMappings(childInput).isEmpty()); 110 if (canCreateMapping && match(childInput, childOutput, mappedObjects)) 111 { 112 break; 113 } 114 } 115 116 if (!mappedObjects.isEmpty()) 117 { 118 mappedInputs.addAll(mappedObjects); 119 mappedObjects.add(childOutput); 120 121 Command mapCommand = CreateMappingCommand.create(domain, mappedObjects); 122 appendIfCanExecute(mapCommand); 123 } 124 } 125 } 126 } 127 128 protected abstract boolean match(Object inputObject, Object outputObject, Collection mappedObjects); 129 130 134 public String toString() 135 { 136 StringBuffer result = new StringBuffer (super.toString()); 137 result.append(" (domain: " + domain + ")"); 138 result.append(" (mapping: " + mapping + ")"); 139 140 return result.toString(); 141 } 142 } 143 | Popular Tags |