科技行者

行者学院 转型私董会 科技行者专题报道 网红大战科技行者

知识库

知识库 安全导航

至顶网软件频道基础软件使用Java来实现编辑器的Undo Redo功能

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

  • 扫一扫
    分享文章到微信

  • 扫一扫
    关注官方公众号
    至顶头条

本文给出了Java实现编辑器的Undo Redo功能类的源代码,供大家参考!

作者:赛迪网 来源:赛迪网 2007年9月3日

关键字: java 编辑器 Undo Redo

  • 评论
  • 分享微博
  • 分享邮件

用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功能,而且是次数不受限制的。

查看本文来源

    • 评论
    • 分享微博
    • 分享邮件
    邮件订阅

    如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。

    重磅专题
    往期文章
    最新文章