1 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 36 public class RestoreInitialStateCommand extends AbstractCommand 37 { 38 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 51 protected static final String LABEL = MappingPlugin.getPlugin().getString("_UI_RestoreInitialStateCommand_label"); 52 53 56 protected static final String DESCRIPTION = MappingPlugin.getPlugin().getString("_UI_RestoreInitialStateCommand_description"); 57 58 61 protected MappingDomain domain; 62 63 66 Command removeCommand; 67 68 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 MappingRoot mappingRoot = domain.getMappingRoot(); 98 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 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 148 public String toString() 149 { 150 StringBuffer result = new StringBuffer (super.toString()); 151 result.append(" (domain: " + domain + ")"); 152 result.append(" (removeCommand: " + removeCommand + ")"); 153 154 return result.toString(); 155 } 156 } 157 | Popular Tags |