1 17 package org.eclipse.emf.edit.command; 18 19 20 import java.util.Collection ; 21 import java.util.Collections ; 22 23 import org.eclipse.emf.common.command.Command; 24 import org.eclipse.emf.common.util.EList; 25 import org.eclipse.emf.ecore.EObject; 26 import org.eclipse.emf.ecore.EStructuralFeature; 27 import org.eclipse.emf.edit.EMFEditPlugin; 28 import org.eclipse.emf.edit.domain.EditingDomain; 29 30 31 57 public class MoveCommand extends AbstractOverrideableCommand 58 { 59 63 public static Command create(EditingDomain domain, Object owner, Object feature, Object value, int index) 64 { 65 return domain.createCommand(MoveCommand.class, new CommandParameter(owner, feature, value, index)); 66 } 67 68 69 72 protected static final String LABEL = EMFEditPlugin.INSTANCE.getString("_UI_MoveCommand_label"); 73 74 77 protected static final String DESCRIPTION = EMFEditPlugin.INSTANCE.getString("_UI_MoveCommand_description"); 78 79 82 protected static final String DESCRIPTION_FOR_LIST = EMFEditPlugin.INSTANCE.getString("_UI_MoveCommand_description_for_list"); 83 84 88 protected EObject owner; 89 90 94 protected EStructuralFeature feature; 95 96 99 protected EList ownerList; 100 101 104 protected Object value; 105 106 109 protected int index; 110 111 114 protected int oldIndex; 115 116 120 public MoveCommand(EditingDomain domain, EObject owner, EStructuralFeature feature, Object value, int index) 121 { 122 super (domain, LABEL, DESCRIPTION); 123 124 this.owner = owner; 125 this.feature = feature; 126 this.value = value; 127 this.index = index; 128 129 ownerList = getOwnerList(this.owner, feature); 130 } 131 132 135 public MoveCommand(EditingDomain domain, EList list, Object value, int index) 136 { 137 super(domain, LABEL, DESCRIPTION_FOR_LIST); 138 139 this.value = value; 140 this.index = index; 141 142 ownerList = list; 143 } 144 145 149 public EObject getOwner() 150 { 151 return owner; 152 } 153 154 158 public EStructuralFeature getFeature() 159 { 160 return feature; 161 } 162 163 166 public EList getOwnerList() 167 { 168 return ownerList; 169 } 170 171 174 public Object getValue() 175 { 176 return value; 177 } 178 179 182 public int getIndex() 183 { 184 return index; 185 } 186 187 190 public int getOldIndex() 191 { 192 return oldIndex; 193 } 194 195 protected boolean prepare() 196 { 197 boolean result = 200 ownerList != null && 201 ownerList.contains(value) && 202 index >= 0 && 203 index < ownerList.size() && 204 (owner == null || !domain.isReadOnly(owner.eResource())); 205 206 return result; 207 } 208 209 public void doExecute() 210 { 211 oldIndex = ownerList.indexOf(value); 212 ownerList.move(index, value); 213 } 214 215 public void doUndo() 216 { 217 ownerList.move(oldIndex, value); 218 } 219 220 public void doRedo() 221 { 222 ownerList.move(index, value); 223 } 224 225 public Collection doGetResult() 226 { 227 return Collections.singleton(value); 228 } 229 230 public Collection doGetAffectedObjects() 231 { 232 return Collections.singleton(value); 233 } 234 235 239 public String toString() 240 { 241 StringBuffer result = new StringBuffer (super.toString()); 242 result.append(" (owner: " + owner + ")"); 243 result.append(" (feature: " + feature + ")"); 244 result.append(" (ownerList: " + ownerList + ")"); 245 result.append(" (value: " + value + ")"); 246 result.append(" (index: " + index + ")"); 247 result.append(" (oldIndex: " + oldIndex + ")"); 248 249 return result.toString(); 250 } 251 } 252 | Popular Tags |