工作小驛

          Ninja!

          BlogJava 首頁 新隨筆 聯系 聚合 管理
            103 Posts :: 0 Stories :: 36 Comments :: 0 Trackbacks
          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;
          }
          }
              
          posted on 2007-08-14 12:15 王君 閱讀(468) 評論(0)  編輯  收藏 所屬分類: J2SE
          主站蜘蛛池模板: 福清市| 巴林左旗| 淅川县| 高淳县| 略阳县| 青河县| 金溪县| 阿拉善盟| 汝州市| 朝阳市| 新晃| 扎囊县| 卓尼县| 金乡县| 鹤岗市| 车险| 资溪县| 托里县| 厦门市| 台江县| 德清县| 陈巴尔虎旗| 富阳市| 宁强县| 永和县| 沾化县| 灵川县| 岐山县| 门头沟区| 海城市| 柘城县| 诸城市| 苍南县| 桐庐县| 阿鲁科尔沁旗| 济南市| 乌兰浩特市| 建湖县| 南宫市| 曲麻莱县| 建水县|