KickJava   Java API By Example, From Geeks To Geeks.

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


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: RemoveOverrideCommand.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 import java.util.ListIterator 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.RemoveCommand;
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 RemoveCommand to additionally unmap the removed object(s).
37  */

38 public class RemoveOverrideCommand 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 RemoveCommand we're overriding
47    */

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

53   protected Command mapCommand;
54
55   /**
56    * This creates a command instance that removes and then unmaps the removed object(s).
57    */

58   public RemoveOverrideCommand(MappingDomain domain, RemoveCommand removeCommand)
59   {
60     super(removeCommand.doGetLabel(), removeCommand.doGetDescription());
61
62     this.mappingDomain = domain;
63     this.removeCommand = removeCommand;
64   }
65
66   protected boolean prepare()
67   {
68     return removeCommand.doCanExecute();
69   }
70
71   public void execute()
72   {
73     MappingRoot mappingRoot = mappingDomain.getMappingRoot();
74     CompoundCommand subcommands = new CompoundCommand();
75     for (Iterator JavaDoc removals = removeCommand.getCollection().iterator(); removals.hasNext(); )
76     {
77       ArrayList JavaDoc commandList = new ArrayList JavaDoc();
78       for (Iterator JavaDoc objects = mappingDomain.treeIterator(removals.next()); objects.hasNext(); )
79       {
80         Object JavaDoc object = objects.next();
81         for (Iterator JavaDoc mappings = mappingRoot.getMappings(object).iterator(); mappings.hasNext(); )
82         {
83           Mapping mapping = (Mapping)mappings.next();
84           Collection JavaDoc outputs = mapping.getOutputs();
85           if (outputs.size() == 1 && outputs.iterator().next() == object)
86           {
87             commandList.add(RemoveMappingCommand.create(mappingDomain, mapping));
88           }
89           else
90           {
91             //commandList.add(RemoveCommand.create(mappingDomain, mapping, mapping.ePackageMapping().getMapping_Outputs(), object));
92
commandList.add(RemoveCommand.create(mappingDomain, mapping, MappingPackage.eINSTANCE.getMapping_Outputs(), object));
93           }
94         }
95       }
96       for (ListIterator JavaDoc commands = commandList.listIterator(commandList.size()); commands.hasPrevious(); )
97       {
98         subcommands.appendAndExecute((Command)commands.previous());
99       }
100     }
101     mapCommand = !subcommands.isEmpty() ? subcommands.unwrap() : null;
102     removeCommand.doExecute();
103   }
104
105   public void undo()
106   {
107     removeCommand.doUndo();
108     if (mapCommand != null)
109     {
110       mapCommand.undo();
111     }
112   }
113
114   public void redo()
115   {
116     if (mapCommand != null)
117     {
118       mapCommand.redo();
119     }
120     removeCommand.doRedo();
121   }
122
123   public void dispose()
124   {
125     if (mapCommand != null)
126     {
127       mapCommand.dispose();
128     }
129     removeCommand.doDispose();
130   }
131
132   public Collection JavaDoc getResult()
133   {
134     return removeCommand.doGetResult();
135   }
136
137   public Collection JavaDoc getAffectedObjects()
138   {
139     return removeCommand.doGetAffectedObjects();
140   }
141
142   /**
143    * This gives an abbreviated name using this object's own class' name, without package qualification,
144    * followed by a space separated list of <tt>field:value</tt> pairs.
145    */

146   public String JavaDoc toString()
147   {
148     StringBuffer JavaDoc result = new StringBuffer JavaDoc(super.toString());
149     result.append(" (mappingDomain: " + mappingDomain + ")");
150     result.append(" (mapCommand: " + mapCommand + ")");
151
152     return result.toString();
153   }
154 }
155
Popular Tags