子非魚

          BlogJava 首頁 新隨筆 聯系 聚合 管理
            21 Posts :: 0 Stories :: 1 Comments :: 0 Trackbacks

          參考:http://dev.eclipse.org/newslists/news.eclipse.platform.rcp/msg01745.html
          在非Editor中實現syntax coloring


          Rainer wrote:
          I hope this is the right place to ask this question:

          It's fine; eclipse.platform would have been right as well.

          I am working on a simple XML editing rich client. One of the main components is a JFace TextViewer, embedded in an Eclipse view (I can't use an Eclipse editor in my special case, unfortunately). I want to add syntax coloring, but can't seem to get the presentation reconciler to work.

          Here's the sample code:

          [...]

          reconciler= new PresentationReconciler();
          DefaultDamagerRepairer dr = new DefaultDamagerRepairer(new CodeRuleScanner());
          dr.setDocument(viewer.getDocument());
          reconciler.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE);
          reconciler.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE);
          reconciler.install(viewer);


          [..]

          The CodeRuleScanner (extends RuleBasedScanner) is configured with a set of rules and a default token. However, the PresentationReconciler seems to be totally inactive. Is there any further configuration necessary?

          No. If you look at the implementation of PresentationReconciler.install, you will see that it waits until the viewer's document is set (via an ITextInputListener). So, in order for the above to work, you have to set the document *after* installing the reconciler.


          You should also look at SourceViewer that adds more features and configures itself in the right order using a SourceViewerConfiguration.

          HTH, tom
           
          正確的做法:
           
          PresentationReconciler reconciler= new PresentationReconciler();
                  DefaultDamagerRepairer dr 
          = new DefaultDamagerRepairer(new PolicyCodeScanner(new ColorProvider()));
                  
                  reconciler.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE);
                  reconciler.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE);
                  reconciler.install(sourceViewer);
          //PresentationReconciler.install必須在setDoucument之前,切記
                  
                  
          //設置字體
                  sourceViewer.getTextWidget().setFont(new Font(sourceViewer.getControl().getDisplay(), 
                          
          new FontData("Courier"10, SWT.NORMAL)));
                  
          //設置當前編輯行的背景色
                  CursorLinePainter painter= new CursorLinePainter(sourceViewer);
                  painter.setHighlightColor(yellow);
                  sourceViewer.addPainter(painter);
                  
                  
          //設置文檔幫助
                  final ContentAssistant assistant = new ContentAssistant();
                  assistant.setContentAssistProcessor(
          new PolicyContentAssistProcessor(), IDocument.DEFAULT_CONTENT_TYPE);
                  assistant.install(sourceViewer);
                  
                  
          //設置文本內容
                  sourceViewer.setDocument(new Document(this.content));
                  
                  dr.setDocument(sourceViewer.getDocument());
                  
           
            
          另外:
          注入config:

          sourceViewer.configure(SourceViewerConfiguration configuration)


           

          posted on 2007-07-24 16:34 子非魚 閱讀(417) 評論(0)  編輯  收藏 所屬分類: eclipse
          主站蜘蛛池模板: 克拉玛依市| 芒康县| 霞浦县| 额尔古纳市| 章丘市| 乐昌市| 安达市| 邹城市| 仪征市| 资溪县| 永丰县| 西城区| 洛川县| 克什克腾旗| 曲阜市| 桂平市| 泗水县| 恩平市| 德兴市| 浮山县| 英超| 湘乡市| 桐乡市| 聂拉木县| 福贡县| 上蔡县| 酒泉市| 雷波县| 昌黎县| 潜江市| 嘉黎县| 昌平区| 江门市| 米易县| 丁青县| 彩票| 平凉市| 绍兴市| 阿尔山市| 奉新县| 普兰店市|