KickJava   Java API By Example, From Geeks To Geeks.

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


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: SetOverrideCommand.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.Collection JavaDoc;
21 import java.util.Iterator JavaDoc;
22
23 import org.eclipse.emf.common.command.AbstractCommand;
24 import org.eclipse.emf.common.command.Command;
25 import org.eclipse.emf.common.command.CompoundCommand;
26 import org.eclipse.emf.edit.command.RemoveCommand;
27 import org.eclipse.emf.edit.command.SetCommand;
28 import org.eclipse.emf.mapping.MappedObjectState;
29 import org.eclipse.emf.mapping.Mapping;
30 import org.eclipse.emf.mapping.MappingPackage;
31 import org.eclipse.emf.mapping.MappingRoot;
32 import org.eclipse.emf.mapping.domain.MappingDomain;
33
34
35 /**
36  * This command overrides the SetCommand to additionally unmap the removed object and map the new object.
37  */

38 public class SetOverrideCommand extends AbstractCommand
39 {
40   /**
41    * This keeps track of the mapping domain in which the command operates.
42    */

43   protected MappingDomain mappingDomain;
44
45   /**
46    * This keeps track of the SetCommand we're overriding
47    */

48   protected SetCommand setCommand;
49
50   /**
51    * This keeps track of the set mapping command(s) used to implement this command.
52    */

53   protected Command mapCommand;
54
55   /**
56    * This creates a command instance that removes, unmaps the removed object and then adds and maps the new object.
57    */

58   public SetOverrideCommand(MappingDomain domain, SetCommand setCommand)
59   {
60     super(setCommand.doGetLabel(), setCommand.doGetDescription());
61
62     this.mappingDomain = domain;
63     this.setCommand = setCommand;
64   }
65
66   protected boolean prepare()
67   {
68     return setCommand.doCanExecute();
69   }
70
71   public void execute()
72   {
73     setCommand.doExecute();
74
75     MappingRoot mappingRoot = mappingDomain.getMappingRoot();
76     CompoundCommand subcommands = new CompoundCommand();
77
78     if (setCommand.getOldValue() != null)
79     {
80       Object JavaDoc oldValue = setCommand.getOldValue();
81       for (Iterator JavaDoc objects = mappingDomain.treeIterator(oldValue); objects.hasNext(); )
82       {
83         Object JavaDoc object = objects.next();
84         for (Iterator JavaDoc mappings = mappingRoot.getMappings(object).iterator(); mappings.hasNext(); )
85         {
86           Mapping mapping = (Mapping)mappings.next();
87           Collection JavaDoc outputs = mapping.getOutputs();
88           if (outputs.size() == 1 && outputs.iterator().next() == object)
89           {
90             subcommands.append(RemoveMappingCommand.create(mappingDomain, mapping));
91           }
92           else
93           {
94             subcommands.append(
95               //(RemoveCommand.create(mappingDomain, mapping, mapping.ePackageMapping().getMapping_Outputs(), object)));
96
(RemoveCommand.create(mappingDomain, mapping, MappingPackage.eINSTANCE.getMapping_Outputs(), object)));
97           }
98         }
99       }
100     }
101
102     if (setCommand.getValue() != null)
103     {
104       Object JavaDoc value = setCommand.getValue();
105       for (Iterator JavaDoc objects = mappingDomain.treeIterator(value); objects.hasNext(); )
106       {
107         Object JavaDoc object = objects.next();
108         MappedObjectState mappedObjectState = mappingRoot.getMappedObjectState(object);
109         Object JavaDoc originatingInput = mappedObjectState.getOriginatingInput();
110  
111         // This is tricky and is done for each object here rather than once for the owner of the addCommnd.
112
// We want to make sure the object is really part of the conceptual tree induced by the domain.
113
//
114
if (originatingInput == null)
115         {
116           mappedObjectState.setOutput();
117         }
118         else if (mappingRoot.isAttachedObject(object))
119         {
120           subcommands.append(CreateMappingCommand.create(mappingDomain, originatingInput, object));
121         }
122       }
123     }
124
125     mapCommand = subcommands.unwrap();
126     if (mapCommand.canExecute())
127     {
128       mapCommand.execute();
129     }
130     else
131     {
132       mapCommand.dispose();
133       mapCommand = null;
134     }
135   }
136
137   public void undo()
138   {
139     if (mapCommand != null)
140     {
141       mapCommand.undo();
142     }
143     setCommand.doUndo();
144   }
145
146   public void redo()
147   {
148     setCommand.doRedo();
149     if (mapCommand != null)
150     {
151       mapCommand.redo();
152     }
153   }
154
155   public void dispose()
156   {
157     if (mapCommand != null)
158     {
159       mapCommand.dispose();
160     }
161     setCommand.doDispose();
162   }
163
164   public Collection JavaDoc getResult()
165   {
166     return setCommand.doGetResult();
167   }
168
169   public Collection JavaDoc getAffectedObjects()
170   {
171     return setCommand.doGetAffectedObjects();
172   }
173
174   /**
175    * This gives an abbreviated name using this object's own class' name, without package qualification,
176    * followed by a space separated list of <tt>field:value</tt> pairs.
177    */

178   public String JavaDoc toString()
179   {
180     StringBuffer JavaDoc result = new StringBuffer JavaDoc(super.toString());
181     result.append(" (mappingDomain: " + mappingDomain + ")");
182     result.append(" (mapCommand: " + mapCommand + ")");
183
184     return result.toString();
185   }
186 }
187
Popular Tags