KickJava   Java API By Example, From Geeks To Geeks.

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


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: AddMappingCommand.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.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 /**
38  * The create mapping command creates a new mapping in a {@link MappingDomain}
39  * from a set of the domain's input and output objects.
40  */

41 public class AddMappingCommand extends AbstractCommand
42 {
43   /**
44    * This creates a command that adds the new mappings in the collection into the appropriate place in the mapping root's.
45    */

46   public static Command create(MappingDomain domain, Collection JavaDoc collection)
47   {
48     return
49       domain.createCommand
50         (AddMappingCommand.class,
51          new CommandParameter(domain.getMappingRoot(), null, collection));
52   }
53
54   /**
55    * This creates a command that adds the new mappings in the collection into the appropriate place in the mapping root's.
56    */

57   public static Command create(MappingDomain domain, Mapping mapping)
58   {
59     return create(domain, Collections.singleton(mapping));
60   }
61
62   /**
63    * This caches the label.
64    */

65   protected static final String JavaDoc LABEL = MappingPlugin.getPlugin().getString("_UI_AddMappingCommand_label");
66
67   /**
68    * This cachaes the description.
69    */

70   protected static final String JavaDoc DESCRIPTION = MappingPlugin.getPlugin().getString("_UI_AddMappingCommand_description");
71
72   /**
73    * This keeps track of the mapping domain in which the command operates.
74    */

75   protected MappingDomain domain;
76
77   /**
78    * This keeps track of the input and output objects that are to be mapped.
79    */

80   protected Collection JavaDoc collection;
81
82   /**
83    * This keeps track of all the subcommand(s) use to implement this command.
84    */

85   Command subcommand;
86
87   /**
88    * This creates a command instance that adds the new mappings in the collection into the appropriate place in the mapping root's.
89    */

90   public AddMappingCommand(MappingDomain domain, Collection JavaDoc collection)
91   {
92     super(LABEL, DESCRIPTION);
93
94     this.domain = domain;
95     this.collection = collection;
96   }
97
98   protected boolean prepare()
99   {
100     boolean result = domain != null;
101     for (Iterator JavaDoc objects = collection.iterator(); objects.hasNext(); )
102     {
103       Object JavaDoc object = objects.next();
104       if (!(object instanceof Mapping))
105       {
106         result = false;
107         break;
108       }
109     }
110
111     return result;
112   }
113
114   public void execute()
115   {
116     // This will deal with all the subcommands to modifying the root mapping tree.
117
//
118
CompoundCommand subcommands = new CompoundCommand();
119
120     // For each mapping being added...
121
//
122
for (Iterator JavaDoc mappings = collection.iterator(); mappings.hasNext(); )
123     {
124       Mapping mapping = (Mapping)mappings.next();
125
126       // Find the appropriate parent mapping, which at the very least, should be the mapping root itself.
127
//
128
Mapping parentMapping = domain.getMappingRoot().getParentMapping(mapping.getMappedObjects());
129
130       // Make sure the back pointers to this mapping from the mapped objects is set.
131
//
132
domain.getMappingRoot().register(mapping);
133
134       // Create a command to do parentMapping.getNested().add(mapping).
135
//
136
//if (subcommands.appendAndExecute(new AddCommand(domain, parentMapping, parentMapping.ePackageMapping().getMapping_Nested(), mapping)))
137
if (subcommands.appendAndExecute(new AddCommand(domain, parentMapping, MappingPackage.eINSTANCE.getMapping_Nested(), mapping)))
138       {
139         // Check all the siblings to see which if any should now be nested under this new mapping.
140
// The are accumulated into a list so that they can be removed as a single command with a single notification.
141
//
142
Collection JavaDoc siblingsToReparent = new ArrayList JavaDoc();
143         for (Iterator JavaDoc i = parentMapping.getNested().iterator(); i.hasNext(); )
144         {
145           Mapping siblingMapping = (Mapping)i.next();
146           if (siblingMapping != mapping)
147           {
148             if (domain.getMappingRoot().getParentMapping(siblingMapping.getMappedObjects()) == mapping)
149             {
150               siblingsToReparent.add(siblingMapping);
151             }
152           }
153         }
154
155         // If there are siblings that need to be reparented.
156
//
157
if (!siblingsToReparent.isEmpty())
158         {
159           // Create commands to do parentMapping.getNested().removeAll(siblingsToReparent).
160
//
161
subcommands.appendAndExecute
162             //(new RemoveCommand(domain, parentMapping, parentMapping.ePackageMapping().getMapping_Nested(), siblingsToReparent));
163
(new RemoveCommand(domain, parentMapping, MappingPackage.eINSTANCE.getMapping_Nested(), siblingsToReparent));
164
165           // Create commands to do mapping.getNested().addAll(siblingsToReparent).
166
//
167
subcommands.appendAndExecute
168             //(new AddCommand(domain, mapping, mapping.ePackageMapping().getMapping_Nested(), siblingsToReparent));
169
(new AddCommand(domain, mapping, MappingPackage.eINSTANCE.getMapping_Nested(), siblingsToReparent));
170         }
171       }
172     }
173
174     subcommand = subcommands.unwrap();
175   }
176
177   public void undo()
178   {
179     for (Iterator JavaDoc objects = collection.iterator(); objects.hasNext(); )
180     {
181       Mapping mapping = (Mapping)objects.next();
182       domain.getMappingRoot().deregister(mapping);
183     }
184
185     subcommand.undo();
186   }
187
188   public void redo()
189   {
190     for (Iterator JavaDoc objects = collection.iterator(); objects.hasNext(); )
191     {
192       Mapping mapping = (Mapping)objects.next();
193       domain.getMappingRoot().register(mapping);
194     }
195
196     subcommand.redo();
197   }
198
199   public Collection JavaDoc getResult()
200   {
201     return collection;
202   }
203
204   public Collection JavaDoc getAffectedObjects()
205   {
206     return collection;
207   }
208
209   public void dispose()
210   {
211     if (subcommand != null)
212     {
213       subcommand.dispose();
214     }
215     super.dispose();
216   }
217
218   /**
219    * This gives an abbreviated name using this object's own class' name, without package qualification,
220    * followed by a space separated list of <tt>field:value</tt> pairs.
221    */

222   public String JavaDoc toString()
223   {
224     StringBuffer JavaDoc result = new StringBuffer JavaDoc(super.toString());
225     result.append(" (domain: " + domain + ")");
226     result.append(" (collection: " + collection + ")");
227     result.append(" (subcommand: " + subcommand + ")");
228
229     return result.toString();
230   }
231 }
232
Popular Tags