import java.awt.event.actionevent;
import javax.swing.abstractaction;
import javax.swing.action;
import javax.swing.jeditorpane;
import javax.swing.keystroke;
import javax.swing.event.undoableeditevent;
import javax.swing.event.undoableeditlistener;
import javax.swing.text.jtextcomponent;
import javax.swing.undo.cannotredoexception;
import javax.swing.undo.cannotundoexception;
import javax.swing.undo.undomanager;
/**
* undowrapper is responsible for adding undo and redo support to text components.
* @author antonio vieiro (antonio@antonioshome.net), $author: $
* @version $revision: $
*/
public class undowrapper
implements undoableeditlistener
{
private undomanager undomanager;
private undoaction undoaction;
private redoaction redoaction;
private jeditorpane textcomponent;
/**
* creates a new instance of undowrapper
*/
public undowrapper( jeditorpane acomponent )
{
textcomponent = acomponent;
undomanager = new undomanager();
undoaction = new undoaction();
redoaction = new redoaction();
textcomponent.getdocument().addundoableeditlistener( this );
textcomponent.getinputmap().put( (keystroke) undoaction.getvalue(
action.accelerator_key), "undo" );
textcomponent.getinputmap().put( (keystroke) redoaction.getvalue(
action.accelerator_key), "redo" );
textcomponent.getactionmap().put( "undo", undoaction );
textcomponent.getactionmap().put( "redo", redoaction );
}
public void undoableedithappened(undoableeditevent e)
{
undomanager.addedit( e.getedit() );
undoaction.updateundostate();
redoaction.updateredostate();
}
/**
* undoaction is the action responsible for handling the undo operation.
*/
class undoaction
extends abstractaction
{
public undoaction()
{
super( "cannot undo" ); // todo: i18n
setenabled( false );
putvalue( action.accelerator_key, keystroke.getkeystroke("ctrl z") );
}
public void actionperformed(actionevent e)
{
try
{
undomanager.undo();
}
catch( cannotundoexception cue )
{
// todo: use logging?
cue.printstacktrace( system.err );
}
updateundostate();
redoaction.updateredostate();
}
void updateundostate()
{
if ( undomanager.canundo() )
{
setenabled( true );
putvalue( action.name, "undo" ); // todo i18n
}
else
{
setenabled( false );
putvalue( action.name, "cannot undo" ); // todo i18n
}
}
}
/**
* redoaction is the action responsible for handling the redo operation.
*/
class redoaction
extends abstractaction
{
public redoaction()
{
super( "cannot redo" ); // todo i18n
setenabled( false );
putvalue( action.accelerator_key, keystroke.getkeystroke("ctrl y") );
}
public void actionperformed(actionevent e)
{
try
{
undomanager.redo();
}
catch( cannotredoexception cre )
{
// todo: use logging?
cre.printstacktrace( system.err );
}
updateredostate();
undoaction.updateundostate();
}
void updateredostate()
{
if ( undomanager.canredo() )
{
setenabled( true );
putvalue( action.name, "redo" ); // todo i18n
}
else
{
setenabled( false );
putvalue( action.name, "cannot redo" ); // todo i18n
}
}
}
undoaction getundoaction()
{
return undoaction;
}
redoaction getredoaction()
{
return redoaction;
}
}
|