KickJava   Java API By Example, From Geeks To Geeks.

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


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: RestoreInitialStateCommand.java,v 1.2 2005/06/08 06:21:43 nickb Exp $
16  */

17 package org.eclipse.emf.mapping.command;
18
19
20 import org.eclipse.emf.common.command.AbstractCommand;
21 import org.eclipse.emf.common.command.Command;
22 import org.eclipse.emf.common.command.CommandStack;
23 import org.eclipse.emf.edit.command.CommandParameter;
24 import org.eclipse.emf.edit.command.RemoveCommand;
25 import org.eclipse.emf.mapping.MappingPackage;
26 import org.eclipse.emf.mapping.MappingPlugin;
27 import org.eclipse.emf.mapping.MappingRoot;
28 import org.eclipse.emf.mapping.domain.MappingDomain;
29
30
31 /**
32  * The restore initial state command ensures that the mapping root is ready to begin a mapping session from a defined initial state.
33  * This implementation just deletes all the mappings, which is good for pure meet-in-the-middle mapping.
34  * For pure top down mapping, a derived implementation will want to reset the outputs.
35  */

36 public class RestoreInitialStateCommand extends AbstractCommand
37 {
38   /**
39    * This creates a command that removes the mapping from the mapping root.
40    */

41   public static Command create(MappingDomain domain)
42   {
43     return
44       domain.createCommand
45         (RestoreInitialStateCommand.class, new CommandParameter(domain.getMappingRoot(), null, null));
46   }
47
48   /**
49    * This caches the label.
50    */

51   protected static final String JavaDoc LABEL = MappingPlugin.getPlugin().getString("_UI_RestoreInitialStateCommand_label");
52
53   /**
54    * This cachaes the description.
55    */

56   protected static final String JavaDoc DESCRIPTION = MappingPlugin.getPlugin().getString("_UI_RestoreInitialStateCommand_description");
57
58   /**
59    * This keeps track of the mapping domain in which the command operates.
60    */

61   protected MappingDomain domain;
62
63   /**
64    * This keeps track of all the command used to implement removing all the mappings.
65    */

66   Command removeCommand;
67
68   /**
69    * This creates a command instance that removes the mappings in the collection from the mapping root.
70    */

71   public RestoreInitialStateCommand(MappingDomain domain)
72   {
73     super(LABEL, DESCRIPTION);
74
75     this.domain = domain;
76   }
77
78   protected boolean prepare()
79   {
80     boolean result = true;
81
82     CommandStack commandStack = domain.getCommandStack();
83     if (domain == null ||
84           !(commandStack instanceof PersistentCommandStack) ||
85           commandStack.getMostRecentCommand() != null)
86     {
87       result = false;
88     }
89
90     return result;
91   }
92
93   public void execute()
94   {
95     // Remove all the mappings from the root.
96
//
97
MappingRoot mappingRoot = domain.getMappingRoot();
98     //removeCommand = RemoveCommand.create(domain, mappingRoot, mappingRoot.ePackageMapping().getMapping_Nested(), mappingRoot.getNested());
99
removeCommand = RemoveCommand.create(domain, mappingRoot, MappingPackage.eINSTANCE.getMapping_Nested(), mappingRoot.getNested());
100
101     if (removeCommand.canExecute())
102     {
103       removeCommand.execute();
104     }
105     else
106     {
107       removeCommand.dispose();
108       removeCommand = null;
109     }
110
111     // This is the tricky part where we set the encoding
112
// that we'd like to have decoded as a series of commands to execute after this command has completed.
113
//
114
PersistentCommandStack commandStack = (PersistentCommandStack)domain.getCommandStack();
115     commandStack.setEncoding(domain, mappingRoot.getCommandStack());
116   }
117
118   public void undo()
119   {
120     if (removeCommand != null)
121     {
122       removeCommand.undo();
123     }
124   }
125
126   public void redo()
127   {
128     if (removeCommand != null)
129     {
130       removeCommand.redo();
131     }
132   }
133
134   public void dispose()
135   {
136     if (removeCommand != null)
137     {
138       removeCommand.dispose();
139     }
140
141     super.dispose();
142   }
143
144   /**
145    * This gives an abbreviated name using this object's own class' name, without package qualification,
146    * followed by a space separated list of <tt>field:value</tt> pairs.
147    */

148   public String JavaDoc toString()
149   {
150     StringBuffer JavaDoc result = new StringBuffer JavaDoc(super.toString());
151     result.append(" (domain: " + domain + ")");
152     result.append(" (removeCommand: " + removeCommand + ")");
153
154     return result.toString();
155   }
156 }
157
Popular Tags