Undoing and Redoing Multiple Edits

Fortunately, QTJ offers a unique opportunity to combine Swing’s thoughtfully designed undo API, javax.swing.undo, with QuickTime’s support for reverting a movie to a previous state. Combined, these features provide the ability to support a long trail of undoes and redoes.

How do I do that?

RedoableQTEditor again builds on BasicQTEditor, adding a Swing UndoManager that is used by both the doUndo( ) and doRedo( ) methods:

Note

Compile and run this example with ant run-ch03-redoableqteditor.

public void doUndo( ) throws QTException {
  if (! undoanager.canUndo( )) {
      System.out.println ("can't undo");
      return;
  }
  undoManager.undo( );
}
 
public void doRedo( ) throws QTException {
  if (! undoManager.canRedo( )) {
      System.out.println ("can't redo");
      return;
  }
  undoManager.redo( );
}

The information about a destructive edit is encapsulated by an inner class called QTEdit :

class QTEdit extends AbstractUndoableEdit { MovieEditState previousState; MovieEditState newState; String name; public QTEdit (MovieEditState pState, MovieEditState nState, String n) { previousState = pState; newState = nState; this.name = n; } public String getPresentationName( ) { return name; } public void redo( ) throws CannotRedoException { super.redo( ); try { movie.useEditState (newState); controller.movieChanged( ); } catch (QTException qte) { qte.printStackTrace( ); } } public void undo ( ) throws CannotUndoException { super.undo( ); try { movie.useEditState (previousState); controller.movieChanged( ...

Get QuickTime for Java: A Developer's Notebook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.