KickJava   Java API By Example, From Geeks To Geeks.

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


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: RemoveMappingCommand.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 RemoveMappingCommand extends AbstractCommand
42 {
43   /**
44    * This creates a command that removes the mapping from the mapping root.
45    */

46   public static Command create(MappingDomain domain, Mapping mapping)
47   {
48     return create(domain, Collections.singleton(mapping));
49   }
50
51   /**
52    * This creates a command that removes the mappings in the collection from the mapping root.
53    */

54   public static Command create(MappingDomain domain, Collection JavaDoc collection)
55   {
56     return
57       domain.createCommand
58         (RemoveMappingCommand.class,
59          new CommandParameter(domain.getMappingRoot(), null, collection));
60   }
61
62   /**
63    * This caches the label.
64    */

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

70   protected static final String JavaDoc DESCRIPTION = MappingPlugin.getPlugin().getString("_UI_RemoveMappingCommand_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   /**
89    * This creates a command instance that removes the mappings in the collection from the mapping root.
90    */

91   public RemoveMappingCommand(MappingDomain domain, Collection JavaDoc collection)
92   {
93     super(LABEL, DESCRIPTION);
94
95     this.domain = domain;
96     this.collection = collection;
97   }
98
99   protected boolean prepare()
100   {
101     boolean result = true;
102
103     if (domain == null || collection == null || collection.isEmpty())
104     {
105       result = false;
106     }
107     else
108     {
109       for (Iterator JavaDoc objects = collection.iterator(); objects.hasNext(); )
110       {
111         Object JavaDoc object = objects.next();
112         if (!(object instanceof Mapping))
113         {
114           result = false;
115           break;
116         }
117         else
118         {
119           Mapping mapping = (Mapping)object;
120           result = domain.getMappingRoot().canRemoveMapping(mapping);
121         }
122       }
123     }
124
125     return result;
126   }
127
128   public void execute()
129   {
130     // This will deal with all the subcommands to modifying the root mapping tree.
131
//
132
CompoundCommand subcommands = new CompoundCommand();
133
134     // For each mapping being removed...
135
//
136
for (Iterator JavaDoc mappings = collection.iterator(); mappings.hasNext(); )
137     {
138       Mapping mapping = (Mapping)mappings.next();
139       Mapping parentMapping = mapping.getNestedIn();
140
141       // Make sure the back pointers to this mapping from the mapped objects is set.
142
//
143
domain.getMappingRoot().deregister(mapping);
144
145       // Create a command to do parentMapping.getNested().remove(mapping).
146
//
147
//subcommands.appendAndExecute(new RemoveCommand(domain, parentMapping, parentMapping.ePackageMapping().getMapping_Nested(), mapping));
148
subcommands.appendAndExecute(new RemoveCommand(domain, parentMapping, MappingPackage.eINSTANCE.getMapping_Nested(), mapping));
149    
150       Collection JavaDoc nestedMappings = new ArrayList JavaDoc(mapping.getNested());
151       if (!nestedMappings.isEmpty())
152       {
153         //subcommands.appendAndExecute(new RemoveCommand(domain, mapping, mapping.ePackageMapping().getMapping_Nested(), nestedMappings));
154
//subcommands.appendAndExecute(new AddCommand(domain, parentMapping, parentMapping.ePackageMapping().getMapping_Nested(), nestedMappings));
155
subcommands.appendAndExecute(new RemoveCommand(domain, mapping, MappingPackage.eINSTANCE.getMapping_Nested(), nestedMappings));
156         subcommands.appendAndExecute(new AddCommand(domain, parentMapping, MappingPackage.eINSTANCE.getMapping_Nested(), nestedMappings));
157       }
158     }
159
160     subcommand = subcommands.unwrap();
161   }
162
163   public void undo()
164   {
165     for (Iterator JavaDoc objects = collection.iterator(); objects.hasNext(); )
166     {
167       Mapping mapping = (Mapping)objects.next();
168       domain.getMappingRoot().register(mapping);
169     }
170
171     subcommand.undo();
172   }
173
174   public void redo()
175   {
176     for (Iterator JavaDoc objects = collection.iterator(); objects.hasNext(); )
177     {
178       Mapping mapping = (Mapping)objects.next();
179       domain.getMappingRoot().deregister(mapping);
180     }
181
182     subcommand.redo();
183   }
184
185   public Collection JavaDoc getResult()
186   {
187     return collection;
188   }
189
190   public void dispose()
191   {
192     if (subcommand != null)
193     {
194       subcommand.dispose();
195     }
196     super.dispose();
197   }
198
199   /**
200    * This gives an abbreviated name using this object's own class' name, without package qualification,
201    * followed by a space separated list of <tt>field:value</tt> pairs.
202    */

203   public String JavaDoc toString()
204   {
205     StringBuffer JavaDoc result = new StringBuffer JavaDoc(super.toString());
206     result.append(" (domain: " + domain + ")");
207     result.append(" (collection: " + collection + ")");
208     result.append(" (subcommand: " + subcommand + ")");
209
210     return result.toString();
211   }
212 }
213
Popular Tags