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 小平 閱讀(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());才好使  回復  更多評論
            
          <2007年7月>
          24252627282930
          1234567
          891011121314
          15161718192021
          22232425262728
          2930311234

          常用鏈接

          留言簿(2)

          隨筆分類

          隨筆檔案

          framework

          j2me

          java

          linux

          web

          其他

          友情鏈接

          素材

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 堆龙德庆县| 和林格尔县| 礼泉县| 兴业县| 合江县| 南陵县| 尼勒克县| 西林县| 乌拉特后旗| 正定县| 漳平市| 玉溪市| 广宗县| 延庆县| 天门市| 溆浦县| 六盘水市| 西盟| 平定县| 安新县| 贵德县| 通榆县| 梅河口市| 苏尼特左旗| 哈尔滨市| 隆尧县| 边坝县| 尤溪县| 阳城县| 茂名市| 黄山市| 武威市| 义马市| 大洼县| 会东县| 同仁县| 体育| 扶沟县| 宁强县| 临猗县| 金门县|