1 17 package org.eclipse.emf.common.command; 18 19 20 import java.util.List ; 21 import java.util.ListIterator ; 22 23 import org.eclipse.emf.common.CommonPlugin; 24 import org.eclipse.emf.common.util.WrappedException; 25 26 27 64 public class StrictCompoundCommand extends CompoundCommand 65 { 66 69 protected boolean isUndoable; 70 71 74 protected boolean isPessimistic; 75 76 79 protected int rightMostExecutedCommandIndex = -1; 80 81 84 public StrictCompoundCommand() 85 { 86 super(); 87 resultIndex = LAST_COMMAND_ALL; 88 } 89 90 94 public StrictCompoundCommand(String label) 95 { 96 super(label); 97 resultIndex = LAST_COMMAND_ALL; 98 } 99 100 105 public StrictCompoundCommand(String label, String description) 106 { 107 super(label, description); 108 resultIndex = LAST_COMMAND_ALL; 109 } 110 111 115 public StrictCompoundCommand(List commandList) 116 { 117 super(commandList); 118 resultIndex = LAST_COMMAND_ALL; 119 } 120 121 126 public StrictCompoundCommand(String label, List commandList) 127 { 128 super(label, commandList); 129 resultIndex = LAST_COMMAND_ALL; 130 } 131 132 138 public StrictCompoundCommand(String label, String description, List commandList) 139 { 140 super(label, description, commandList); 141 resultIndex = LAST_COMMAND_ALL; 142 } 143 144 149 protected boolean prepare() 150 { 151 ListIterator commands = commandList.listIterator(); 154 155 if (commands.hasNext()) 158 { 159 boolean result = true; 160 161 for (;;) 164 { 165 Command command = (Command)commands.next(); 166 if (command.canExecute()) 167 { 168 if (commands.hasNext()) 169 { 170 if (command.canUndo()) 171 { 172 try 173 { 174 if (commands.previousIndex() <= rightMostExecutedCommandIndex) 175 { 176 command.redo(); 177 } 178 else 179 { 180 ++rightMostExecutedCommandIndex; 181 command.execute(); 182 } 183 } 184 catch (RuntimeException exception) 185 { 186 CommonPlugin.INSTANCE.log 187 (new WrappedException 188 (CommonPlugin.INSTANCE.getString("_UI_IgnoreException_exception"), exception).fillInStackTrace()); 189 190 result = false; 191 break; 192 } 193 } 194 else 195 { 196 result = false; 199 break; 200 } 201 } 202 else 203 { 204 isUndoable = command.canUndo(); 208 break; 209 } 210 } 211 else 212 { 213 result = false; 216 break; 217 } 218 } 219 220 if (isPessimistic) 223 { 224 commands.previous(); 227 228 while (commands.hasPrevious()) 231 { 232 Command command = (Command)commands.previous(); 233 command.undo(); 234 } 235 } 236 237 return result; 238 } 239 else 240 { 241 isUndoable = false; 242 return false; 243 } 244 } 245 246 252 public void execute() 253 { 254 if (isPessimistic) 255 { 256 for (ListIterator commands = commandList.listIterator(); commands.hasNext(); ) 257 { 258 try 259 { 260 Command command = (Command)commands.next(); 263 if (commands.previousIndex() <= rightMostExecutedCommandIndex) 264 { 265 command.redo(); 266 } 267 else 268 { 269 command.execute(); 270 } 271 } 272 catch (RuntimeException exception) 273 { 274 commands.previous(); 277 278 while (commands.hasPrevious()) 281 { 282 commands.previous(); 283 Command command = (Command)commands.previous(); 284 if (command.canUndo()) 285 { 286 command.undo(); 287 } 288 else 289 { 290 break; 291 } 292 } 293 294 throw exception; 295 } 296 } 297 } 298 else if (!commandList.isEmpty()) 299 { 300 Command command = (Command)commandList.get(commandList.size() - 1); 301 command.execute(); 302 } 303 } 304 305 310 public void undo() 311 { 312 if (isPessimistic) 313 { 314 super.undo(); 315 } 316 else if (!commandList.isEmpty()) 317 { 318 Command command = (Command)commandList.get(commandList.size() - 1); 319 command.undo(); 320 } 321 } 322 323 328 public void redo() 329 { 330 if (isPessimistic) 331 { 332 super.redo(); 333 } 334 else if (!commandList.isEmpty()) 335 { 336 Command command = (Command)commandList.get(commandList.size() - 1); 337 command.redo(); 338 } 339 } 340 341 386 public boolean appendAndExecute(Command command) 387 { 388 if (command != null) 389 { 390 if (!isPrepared) 391 { 392 if (commandList.isEmpty()) 393 { 394 isPrepared = true; 395 isExecutable = true; 396 } 397 else 398 { 399 isExecutable = prepare(); 400 isPrepared = true; 401 isPessimistic = true; 402 if (isExecutable) 403 { 404 execute(); 405 } 406 } 407 } 408 409 if (command.canExecute()) 410 { 411 try 412 { 413 command.execute(); 414 commandList.add(command); 415 ++rightMostExecutedCommandIndex; 416 isUndoable = command.canUndo(); 417 return true; 418 } 419 catch (RuntimeException exception) 420 { 421 CommonPlugin.INSTANCE.log 422 (new WrappedException 423 (CommonPlugin.INSTANCE.getString("_UI_IgnoreException_exception"), exception).fillInStackTrace()); 424 } 425 } 426 427 command.dispose(); 428 } 429 430 return false; 431 } 432 433 436 public String toString() 437 { 438 StringBuffer result = new StringBuffer (super.toString()); 439 result.append(" (isUndoable: " + isUndoable + ")"); 440 result.append(" (isPessimistic: " + isPessimistic + ")"); 441 result.append(" (rightMostExecutedCommandIndex: " + rightMostExecutedCommandIndex + ")"); 442 443 return result.toString(); 444 } 445 } 446 | Popular Tags |