实现编辑器的Undo Redo功能用Java来

 
 

    用 java 实现 编辑器 的Undo Redo 功能 ,非常的方便,下面是一个实现这个功能的类, 
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;
  }
}


使用的时候,只需要将你创建的JEditorPane作为对象传入UndoWrapper中即可。使用方式如下 
new UndoWrapper(editorPane); 
OK这样你的编辑器就具有了Undo Redo功能,而且是次数不收限制的。

 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值