KickJava   Java API By Example, From Geeks To Geeks.

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


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: AddOverrideCommand.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.AbstractCommand;
25 import org.eclipse.emf.common.command.Command;
26 import org.eclipse.emf.common.command.CompoundCommand;
27 import org.eclipse.emf.edit.command.AddCommand;
28 import org.eclipse.emf.mapping.MappedObjectState;
29 import org.eclipse.emf.mapping.MappingRoot;
30 import org.eclipse.emf.mapping.domain.MappingDomain;
31
32
33 /**
34  * This command overrides the AddCommand to additionally map the added object(s) to corresponding inputs.
35  */

36 public class AddOverrideCommand extends AbstractCommand
37 {
38   /**
39    * This keeps track of the mapping domain in which the command operates.
40    */

41   protected MappingDomain mappingDomain;
42
43   /**
44    * This keeps track of the AddCommand we're overriding
45    */

46   protected AddCommand addCommand;
47
48   /**
49    * This keeps track of the create mapping command(s) used to implement this command.
50    */

51   protected Command mapCommand;
52
53   /**
54    * This creates a command instance that adds and then maps the added object(s).
55    */

56   public AddOverrideCommand(MappingDomain domain, AddCommand addCommand)
57   {
58     super(addCommand.doGetLabel(), addCommand.doGetDescription());
59
60     this.mappingDomain = domain;
61     this.addCommand = addCommand;
62   }
63
64   protected boolean prepare()
65   {
66     return addCommand.doCanExecute();
67   }
68
69   public void execute()
70   {
71     addCommand.doExecute();
72
73     MappingRoot mappingRoot = mappingDomain.getMappingRoot();
74     CompoundCommand subcommands = new CompoundCommand();
75     for (Iterator JavaDoc additions = addCommand.getCollection().iterator(); additions.hasNext(); )
76     {
77       for (Iterator JavaDoc objects = mappingDomain.treeIterator(additions.next()); objects.hasNext(); )
78       {
79         Object JavaDoc object = objects.next();
80         MappedObjectState mappedObjectState = mappingRoot.getMappedObjectState(object);
81         Object JavaDoc originatingInput = mappedObjectState.getOriginatingInput();
82
83         // This is tricky and is done for each object here rather than once for the owner of the addCommnd.
84
// We want to make sure the object is really part of the conceptual tree induced by the domain.
85
//
86
if (originatingInput == null)
87         {
88           mappedObjectState.setOutput();
89         }
90         else if (mappingRoot.isAttachedObject(object))
91         {
92           mapOutputObject(object, originatingInput, subcommands);
93         }
94       }
95     }
96     mapCommand = !subcommands.isEmpty() ? subcommands.unwrap() : null;
97   }
98
99   protected void mapOutputObject(Object JavaDoc outputObject, Object JavaDoc originatingInput, CompoundCommand subcommands)
100   {
101     subcommands.appendAndExecute(CreateMappingCommand.create(mappingDomain, originatingInput, outputObject));
102   }
103
104   public void undo()
105   {
106     if (mapCommand != null)
107     {
108       mapCommand.undo();
109     }
110     addCommand.doUndo();
111   }
112
113   public void redo()
114   {
115     addCommand.doRedo();
116     if (mapCommand != null)
117     {
118       mapCommand.redo();
119     }
120   }
121
122   public void dispose()
123   {
124     if (mapCommand != null)
125     {
126       mapCommand.dispose();
127     }
128     addCommand.doDispose();
129   }
130
131   public Collection JavaDoc getResult()
132   {
133     Collection JavaDoc result = new ArrayList JavaDoc();
134     result.addAll(addCommand.doGetResult());
135     if (mapCommand != null)
136     {
137       result.addAll(mapCommand.getResult());
138     }
139     return result;
140   }
141
142   public Collection JavaDoc getAffectedObjects()
143   {
144     Collection JavaDoc result = new ArrayList JavaDoc();
145     result.addAll(addCommand.doGetAffectedObjects());
146     if (mapCommand != null)
147     {
148       result.addAll(mapCommand.getAffectedObjects());
149     }
150     return result;
151   }
152
153   /**
154    * This gives an abbreviated name using this object's own class' name, without package qualification,
155    * followed by a space separated list of <tt>field:value</tt> pairs.
156    */

157   public String JavaDoc toString()
158   {
159     StringBuffer JavaDoc result = new StringBuffer JavaDoc(super.toString());
160     result.append(" (mappingDomain: " + mappingDomain + ")");
161     result.append(" (mapCommand: " + mapCommand + ")");
162
163     return result.toString();
164   }
165 }
166
Popular Tags