posts - 31,  comments - 31,  trackbacks - 0

                 前兩天遇到為org.eclipse.jface.text.TextViewer添加undo、redo 并添加Ctrl+z,與Ctrl+y功能這個問題,搜遍了國內(nèi)網(wǎng)站,也沒有個好結(jié)果,終于多天后在一個國外的svn服務(wù)器上找到了一段代碼,解決了問題
          1、為TextViewer添加TextViewerUndoManager來管理記錄

          protected TextViewer textViewer;
               
          protected TextViewerUndoManager undoManager;
               
                
          //20是保存記錄的數(shù)量。
               undoManager = new TextViewerUndoManager(20);
               
               
          //綁定對textViewer控件的數(shù)據(jù)進行管理
               undoManager.connect(textViewer);
               textViewer.setUndoManager(undoManager);


          2、添加Ctrl+z,與Ctrl+y事件

          StyledText styledText = textViewer.getTextWidget();
          styledText.addKeyListener(
          new KeyListener( ) {
                      
          public void keyPressed( KeyEvent e )
                      
          {
                          
          if ( isUndoKeyPress( e ) )
                          
          {
                              textViewer.doOperation( ITextOperationTarget.UNDO );
                          }

                          
          else if ( isRedoKeyPress( e ) )
                          
          {
                              textViewer.doOperation( ITextOperationTarget.REDO );
                          }

                      }

                      
          private boolean isUndoKeyPress( KeyEvent e )
                      
          {
                          
          // CTRL + z
                          return ( ( e.stateMask & SWT.CONTROL ) > 0 )
                          
          && ( ( e.keyCode == 'z' ) || ( e.keyCode == 'Z' ) );
                      }

                      
          private boolean isRedoKeyPress( KeyEvent e )
                      
          {
                          
          // CTRL + y
                          return ( ( e.stateMask & SWT.CONTROL ) > 0 )
                          
          && ( ( e.keyCode == 'y' ) || ( e.keyCode == 'Y' ) );
                      }

                      
          public void keyReleased( KeyEvent e )
                      
          {
                          
          // do nothing
                      }

                  }
          );
          posted on 2007-07-16 10:49 小平 閱讀(1875) 評論(3)  編輯  收藏 所屬分類: eclipse plugIn


          FeedBack:
          # re: 為org.eclipse.jface.text.TextViewer添加undo、redo 并添加Ctrl+z,與Ctrl+y功能
          2008-04-16 14:47 | 張凱
          不錯,學習了  回復  更多評論
            
          # re: 為org.eclipse.jface.text.TextViewer添加undo、redo 并添加Ctrl+z,與Ctrl+y功能
          2013-06-11 21:43 | yumin_999@163.com
          反復測試,還是不好使,奇怪。yumin_999@163.com  回復  更多評論
            
          # re: 為org.eclipse.jface.text.TextViewer添加undo、redo 并添加Ctrl+z,與Ctrl+y功能
          2013-06-11 22:15 | yumin_999@163.com
          找到原因了,必須加textViewer.setDocument(new Document());才好使  回復  更多評論
            

          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導航:
           
          <2013年6月>
          2627282930311
          2345678
          9101112131415
          16171819202122
          23242526272829
          30123456

          常用鏈接

          留言簿(2)

          隨筆分類

          隨筆檔案

          framework

          j2me

          java

          linux

          web

          其他

          友情鏈接

          素材

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 弋阳县| 新平| 武平县| 车致| 普兰店市| 武川县| 灵寿县| 漯河市| 迁安市| 南岸区| 开化县| 茶陵县| 偃师市| 普兰县| 阆中市| 平陆县| 碌曲县| 镇原县| 舞阳县| 安达市| 屏南县| 达日县| 华亭县| 龙胜| 临邑县| 乌拉特前旗| 丹寨县| 新民市| 富蕴县| 贺兰县| 淮阳县| 星座| 沅陵县| 林口县| 壶关县| 河池市| 达尔| 金门县| 河东区| 晋宁县| 碌曲县|