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 小平 閱讀(1871) 評論(3)  編輯  收藏 所屬分類: eclipse plugIn


          FeedBack:
          # re: 為org.eclipse.jface.text.TextViewer添加undo、redo 并添加Ctrl+z,與Ctrl+y功能
          2008-04-16 14:47 | 張凱
          不錯,學(xué)習(xí)了  回復(fù)  更多評論
            
          # re: 為org.eclipse.jface.text.TextViewer添加undo、redo 并添加Ctrl+z,與Ctrl+y功能
          2013-06-11 21:43 | yumin_999@163.com
          反復(fù)測試,還是不好使,奇怪。yumin_999@163.com  回復(fù)  更多評論
            
          # 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ù)  更多評論
            

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


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

          常用鏈接

          留言簿(2)

          隨筆分類

          隨筆檔案

          framework

          j2me

          java

          linux

          web

          其他

          友情鏈接

          素材

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 廉江市| 石城县| 富川| 金沙县| 砚山县| 定襄县| 乌鲁木齐市| 沙坪坝区| 庆安县| 哈尔滨市| 双城市| 梨树县| 繁峙县| 辽中县| 探索| 广南县| 信阳市| 徐汇区| 顺义区| 屯昌县| 麻城市| 神池县| 毕节市| 秦皇岛市| 彰化市| 淳安县| 清徐县| 光泽县| 曲松县| 东城区| 卫辉市| 阿拉尔市| 浦城县| 阳山县| 富源县| 大兴区| 衡水市| 西盟| 平塘县| 汉阴县| 湟中县|