Exemplo n.º 1
0
  /**
   * Attempts to execute a command and adds it to the undo stack if the command executes
   * successfully.
   *
   * @param command The command to execute.
   */
  void executeCommand(Command command) {
    boolean success = command.execDo();

    // Only mess with the command stacks if the command was executed
    // successfully.
    if (success) {
      // If the command invalidates the command stacks then
      // clear the command stacks.
      if (command.invalidates()) {
        undoStack.clear();
        redoStack.clear();
      }

      // If the command is undoable, it must be added to the undo
      // stack and the redo stack must be cleared.
      if (command.isUndoable()) {
        undoStack.addLast(command);

        if (undoStack.size() > mThreshold) {
          undoStack.removeFirst();
        }

        redoStack.clear();
      }

      // Necessary so that the undo/redo menu gets an update
      // AFTER the command has successfully executed.
      publisher.publishEvent(new Event());
    }
  }
Exemplo n.º 2
0
  /**
   * Attempts to redo the first command in the redo stack, and adds it to the undo stack if
   * successful.
   */
  void redoCommand() {
    if (!redoStack.isEmpty()) {
      Command command = redoStack.removeLast();

      boolean success = command.execDo();

      if (success) {
        undoStack.addLast(command);
      }

      // Necessary so that the undo/redo menu gets an update
      // AFTER the command has successfully executed.
      publisher.publishEvent(new Event());
    }
  }