KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > emf > mapping > command > MatchMappingCommand


1 /**
2  * <copyright>
3  *
4  * Copyright (c) 2002-2004 IBM Corporation and others.
5  * All rights reserved. This program and the accompanying materials
6  * are made available under the terms of the Eclipse Public License v1.0
7  * which accompanies this distribution, and is available at
8  * http://www.eclipse.org/legal/epl-v10.html
9  *
10  * Contributors:
11  * IBM - Initial API and implementation
12  *
13  * </copyright>
14  *
15  * $Id: MatchMappingCommand.java,v 1.2 2005/06/08 06:21:43 nickb Exp $
16  */

17 package org.eclipse.emf.mapping.command;
18
19
20 import java.util.ArrayList JavaDoc;
21 import java.util.Collection JavaDoc;
22 import java.util.Iterator JavaDoc;
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   /**
35    * This keeps track of the mapping domain in which the command operates.
36    */

37   protected MappingDomain domain;
38
39   /**
40    * This is the mapping that is being recursively matched.
41    */

42   protected Mapping mapping;
43
44   /**
45    * This is the collection of inputs that have been matched by this command
46    */

47   protected Collection JavaDoc mappedInputs;
48
49   /**
50    * This caches the label.
51    */

52   protected static final String JavaDoc LABEL = MappingPlugin.getPlugin().getString("_UI_MatchMappingCommand_label");
53
54   /**
55    * This cachaes the description.
56    */

57   protected static final String JavaDoc 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 JavaDoc inputChildren = new ArrayList JavaDoc();
72       for (Iterator JavaDoc inputs = mapping.getSenders().iterator(); inputs.hasNext(); )
73       {
74         inputChildren.addAll(domain.getChildren(inputs.next()));
75       }
76
77       Collection JavaDoc outputChildren = new ArrayList JavaDoc();
78       for (Iterator JavaDoc outputs = mapping.getReceivers().iterator(); outputs.hasNext(); )
79       {
80         outputChildren.addAll(domain.getChildren(outputs.next()));
81       }
82
83       matchChildren(inputChildren, outputChildren);
84     }
85
86     // We have done our preparation. Now ask the super to validate.
87
//
88
boolean result = super.prepare();
89     return result;
90   }
91
92   protected void matchChildren(Collection JavaDoc inputChildren, Collection JavaDoc outputChildren)
93   {
94     mappedInputs = new ArrayList JavaDoc();
95     MappingRoot mappingRoot = domain.getMappingRoot();
96     boolean multipleMatchesAllowed = (domain.getMappingEnablementFlags() & MappingDomain.ENABLE_MULTIPLE_INPUT_MAPPINGS) != 0;
97
98     for (Iterator JavaDoc childOutputs = outputChildren.iterator(); childOutputs.hasNext(); )
99     {
100       Object JavaDoc childOutput = childOutputs.next();
101       if (mappingRoot.getMappings(childOutput).isEmpty())
102       {
103         Collection JavaDoc mappedObjects = new ArrayList JavaDoc();
104
105         for (Iterator JavaDoc childInputs = inputChildren.iterator(); childInputs.hasNext(); )
106         {
107           Object JavaDoc 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 JavaDoc inputObject, Object JavaDoc outputObject, Collection JavaDoc mappedObjects);
129
130   /**
131    * This gives an abbreviated name using this object's own class' name, without package qualification,
132    * followed by a space separated list of <tt>field:value</tt> pairs.
133    */

134   public String JavaDoc toString()
135   {
136     StringBuffer JavaDoc result = new StringBuffer JavaDoc(super.toString());
137     result.append(" (domain: " + domain + ")");
138     result.append(" (mapping: " + mapping + ")");
139
140     return result.toString();
141   }
142 }
143
Popular Tags