KickJava   Java API By Example, From Geeks To Geeks.

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


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: CreateMappingCommand.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.Collections JavaDoc;
23 import java.util.Iterator JavaDoc;
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 /**
35  * The create mapping command creates a new mapping in a {@link MappingDomain}
36  * from a set of the domain's input and output objects.
37  */

38 public class CreateMappingCommand extends AbstractCommand
39 {
40   /**
41    * @deprecated - use MappingDomain.ENABLE_MULTIPLE_INPUTS
42    */

43   public static final int ENABLE_MULTIPLE_INPUTS = 0x0001;
44   /**
45    * @deprecated - use MappingDomain.ENABLE_MULTIPLE_OUTPUTS
46    */

47   public static final int ENABLE_MULTIPLE_OUTPUTS = 0x0002;
48   /**
49    * @deprecated - use MappingDomain.ENABLE_MULTIPLE_INPUT_MAPPINGS
50    */

51   public static final int ENABLE_MAPPED_INPUTS = 0x0004;
52   /**
53    * @deprecated - use MappingDomain.ENABLE_MULTIPLE_OUTPUT_MAPPINGS
54    */

55   public static final int ENABLE_MAPPED_OUTPUTS = 0x0008;
56   /**
57    * @deprecated - use MappingDomain.ENABLE_INCOMPATIBLE_METAOBJECTS
58    */

59   public static final int ENABLE_INCOMPATIBLE_METAOBJECTS = 0x0010;
60   /**
61    * @deprecated - use MappingDomain.ENABLE_INCOMPATIBLE_TYPE_CLASSIFIERS
62    */

63   public static final int ENABLE_INCOMPATIBLE_TYPE_CLASSIFIERS = 0x0020;
64   /**
65    * @deprecated - use MappingDomain.ENABLE_EMPTY_INPUTS
66    */

67   public static final int ENABLE_EMPTY_INPUTS = 0x0040;
68   /**
69    * @deprecated - use MappingDomain.ENABLE_EMPTY_OUTPUTS
70    */

71   public static final int ENABLE_EMPTY_OUTPUTS = 0x0080;
72   /**
73    * @deprecated - use MappingDomain.ENABLE_UNMAPPED_PARENTS
74    */

75   public static final int ENABLE_UNMAPPED_PARENTS = 0x0100;
76   /**
77    * @deprecated - use MappingDomain.ENABLE_ALL
78    */

79   public static final int ENABLE_ALL = 0xFFFF;
80
81   /**
82    * This creates a command that creates a new mapping involving the given domain's collection of input and output objects.
83    */

84   public static Command create(MappingDomain domain, Collection JavaDoc collection)
85   {
86     return
87       domain.createCommand
88         (CreateMappingCommand.class,
89          new CommandParameter(domain.getMappingRoot(), null, collection));
90   }
91
92   /**
93    * This creates a command that creates a new mapping between the given input and output.
94    */

95   public static Command create(MappingDomain domain, Object JavaDoc input, Object JavaDoc output)
96   {
97     Collection JavaDoc collection = new ArrayList JavaDoc();
98     collection.add(input);
99     collection.add(output);
100     return create(domain, collection);
101   }
102  
103   /**
104    * This creates a command that creates a new mapping with the given collections of inputs and outputs.
105    */

106   public static Command create(MappingDomain domain, Collection JavaDoc inputs, Collection JavaDoc outputs)
107   {
108     Collection JavaDoc collection = new ArrayList JavaDoc();
109     collection.addAll(inputs);
110     collection.addAll(outputs);
111     return create(domain, collection);
112   }
113
114   /**
115    * This creates a command that creates a new mapping with the given collection of inputs and output.
116    */

117   public static Command create(MappingDomain domain, Collection JavaDoc inputs, Object JavaDoc output)
118   {
119     Collection JavaDoc collection = new ArrayList JavaDoc();
120     collection.addAll(inputs);
121     collection.add(output);
122     return create(domain, collection);
123   }
124
125   /**
126    * This creates a command that creates a new mapping with the given input and collection of outputs.
127    */

128   public static Command create(MappingDomain domain, Object JavaDoc input, Collection JavaDoc outputs)
129   {
130     Collection JavaDoc collection = new ArrayList JavaDoc();
131     collection.add(input);
132     collection.addAll(outputs);
133     return create(domain, collection);
134   }
135
136   /**
137    * This caches the label.
138    */

139   protected static final String JavaDoc LABEL = MappingPlugin.getPlugin().getString("_UI_CreateMappingCommand_label");
140
141   /**
142    * This cachaes the description.
143    */

144   protected static final String JavaDoc DESCRIPTION = MappingPlugin.getPlugin().getString("_UI_CreateMappingCommand_description");
145
146   /**
147    * This keeps track of the mapping domain in which the command operates.
148    */

149   protected MappingDomain domain;
150
151   /**
152    * This keeps track of the input objects that are to be mapped.
153    */

154   protected Collection JavaDoc inputs;
155
156   /**
157    * This keeps track of the output objects that are to be mapped.
158    */

159   protected Collection JavaDoc outputs;
160
161   /**
162    * This is set during {@link #execute} to record the new mapping that is created.
163    */

164   protected Mapping newMapping;
165
166   /**
167    * This is set during {@link #execute} to record the command used to add the newly created mapping to the mapping root.
168    */

169   protected Command subcommand;
170
171   /**
172    * @deprecated
173    */

174   public CreateMappingCommand(MappingDomain domain, Collection JavaDoc collection, int enablementFlags)
175   {
176     this(domain, collection);
177   }
178
179   /**
180    * This creates a command that creates a new mapping involving the given domain's collection of input and output objects.
181    */

182   public CreateMappingCommand(MappingDomain domain, Collection JavaDoc collection)
183   {
184     super(LABEL, DESCRIPTION);
185
186     this.domain = domain;
187
188     inputs = new ArrayList JavaDoc();
189     outputs = new ArrayList JavaDoc();
190     for (Iterator JavaDoc objects = collection.iterator(); objects.hasNext(); )
191     {
192       Object JavaDoc 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     //domain.getMappingRoot().removeMapping(newMapping);
232
subcommand.undo();
233   }
234
235   public void redo()
236   {
237     subcommand.redo();
238   }
239
240   public Collection JavaDoc getResult()
241   {
242     return Collections.singleton(newMapping);
243   }
244
245   public Collection JavaDoc 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 JavaDoc 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 JavaDoc 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   /**
284    * This gives an abbreviated name using this object's own class' name, without package qualification,
285    * followed by a space separated list of <tt>field:value</tt> pairs.
286    */

287   public String JavaDoc toString()
288   {
289     StringBuffer JavaDoc result = new StringBuffer JavaDoc(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