posts - 31,  comments - 31,  trackbacks - 0

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

          protected TextViewer textViewer;
               
          protected TextViewerUndoManager undoManager;
               
                
          //20是保存記錄的數量。
               undoManager = new TextViewerUndoManager(20);
               
               
          //綁定對textViewer控件的數據進行管理
               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 小平 閱讀(1870) 評論(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());才好使  回復  更多評論
            
          <2013年6月>
          2627282930311
          2345678
          9101112131415
          16171819202122
          23242526272829
          30123456

          常用鏈接

          留言簿(2)

          隨筆分類

          隨筆檔案

          framework

          j2me

          java

          linux

          web

          其他

          友情鏈接

          素材

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 松阳县| 英吉沙县| 凤阳县| 平舆县| 兴业县| 中超| 土默特右旗| 孝昌县| 嵊州市| 松桃| 大关县| 会同县| 兰坪| 梁山县| 黎城县| 嘉祥县| 金寨县| 北流市| 丰台区| 登封市| 红河县| 景谷| 文成县| 靖西县| 丽江市| 鸡西市| 平湖市| 漳州市| 内江市| 岑巩县| 宽甸| 伊宁县| 清水县| 尉犁县| 南投县| 凌源市| 永昌县| 扶沟县| 长阳| 花垣县| 河西区|