月蝕傳說

          浮躁讓人失去理智
          posts - 25, comments - 101, trackbacks - 0, articles - 0
            BlogJava :: 首頁 ::  :: 聯(lián)系 :: 聚合  :: 管理

          大家在使用Eclipse編寫Java代碼的時候,一定被Java代碼編輯器的強大功能所吸引:出色的錯誤提示,準(zhǔn)確的內(nèi)容幫助,文本的折疊等等。今天我以Eclipse插件中的XML Editor例子作為模板,為XML文本編輯器加入內(nèi)容幫助(Content Assis)。

          1.內(nèi)容幫助簡介

          在目前流行的IDE中,內(nèi)容提示幫助是必不可少的功能,可以說,如果沒有了內(nèi)容幫助,那IDE就不能稱為IDE。有了內(nèi)容幫助提示,能大大提高代碼編寫速度。

          請看下圖:當(dāng)我們在Java編輯器中輸入‘.’的時候,就會彈出一個菜單,里面列出了類所具有的方法以及屬性,并且在我們繼續(xù)輸入字符的時候,彈出的內(nèi)容會隨著我們的輸入進行過濾。



          下面我們以Eclipse的XML Editor Example為例,介紹一下內(nèi)容幫助如何實現(xiàn)的。

          2.創(chuàng)建XML Editor

          我們首先需要建立一個Plugin工程,然后在向?qū)ы撝羞x擇我們要生成的XML Editor例子:



          點Finish完成,這時候我們的工程便生成了,并且向?qū)н€為我們生成了XML Editor所需要的一些類,以及為我們的Plugin.xml實現(xiàn)了org.eclipse.ui.editors擴展點:



          3.簡單的內(nèi)容幫助

          在我們生成的類中,有一個名為XMLConfiguration的類,該類對XML Editor進行了一些設(shè)置,包括如何去為不同的文本區(qū)域顯示不同的顏色等,TextEditor所維護的SourceViewer就是通過它來進行設(shè)置的,但這不是我們所要討論的范圍,這里簡單地介紹一下即可。

          接下來我們需要復(fù)寫XMLConfiguration的一個方法:getContentAssistant。這個方法便是告訴我們的編輯器,我們所具有的內(nèi)容幫助是什么,在創(chuàng)建XML Editor的時候,默認(rèn)是不為我們生成這方面代碼的,所以我們需要自己復(fù)寫:

          ???? public ?IContentAssistant?getContentAssistant(ISourceViewer?sourceViewer)?{
          ????????
          // ?生成一個ContentAssistant
          ????????ContentAssistant?assistant? = ?? new ?ContentAssistant();
          ?
          ????????
          // ?設(shè)置幫組內(nèi)容彈出響應(yīng)時間
          ????????assistant.setAutoActivationDelay( 200 );
          ????????assistant.enableAutoActivation(
          true );
          ????????
          return ?assistant;
          ????}

          ContentAssistant并不是內(nèi)容幫助的提供者,它只是維護我們的內(nèi)容幫助,幫我們彈出菜單以及幫助內(nèi)容信息等作用。
          真正告訴ContentAssistant要顯示那些幫助內(nèi)容的,是IContentAssistProcessor接口類。讓我們創(chuàng)建一個名為StrutsContentAssisProcessor的類,并讓它實現(xiàn)IContentAssistProcessor接口:

          public?class?StrutsContentAssisProcessor?implements?IContentAssistProcessor?{

          ????
          public?ICompletionProposal[]?computeCompletionProposals(ITextViewer?viewer,
          ????????????
          int?offset)?{
          ????????
          return?null;
          ????}

          ????
          public?IContextInformation[]?computeContextInformation(ITextViewer?viewer,
          ????????????
          int?offset)?{
          ????????
          return?null;
          ????}

          ????
          public?char[]?getCompletionProposalAutoActivationCharacters()?{
          ????????
          return?null;
          ????}

          ????
          public?char[]?getContextInformationAutoActivationCharacters()?{
          ????????
          return?null;
          ????}

          ????
          public?String?getErrorMessage()?{
          ????????
          return?null;
          ????}

          ????
          public?IContextInformationValidator?getContextInformationValidator()?{
          ????????
          return?null;
          ????}

          }

          大家注意下computeCompletionProposals方法,這個方法便是返回我們的具體內(nèi)容幫助。所以我們需要為我們的編輯器創(chuàng)建所需要的內(nèi)容幫助:CompletionProposal

          先看一下這個類的構(gòu)造函數(shù)各個參數(shù)的含義:

          ???? *?@param replacementString?:選擇幫助信息后所要替代的文本內(nèi)容
          ?????*?@param?replacementOffset?:替代內(nèi)容輸入的位置
          ?????
          *?@param?replacementLength?:替代文本覆蓋原來文本的長度???
          ?? ??
          *?@param?cursorPosition?:完成內(nèi)容幫助的文本替代后,光標(biāo)所在位置
          ?????
          *?@param?image?:幫助內(nèi)容顯示的圖標(biāo)
          ?????
          *?@param?displayString?:幫助內(nèi)容的顯示字符串
          ?????*?@param?contextInformation?:幫助內(nèi)容的信息描述
          ?????*?@param?additionalProposalInfo?:附加信息

          在這幾個參數(shù)中image 、contextInformation、additionalProposalInfo我們可以設(shè)置為空。現(xiàn)在讓我們在computeCompletionProposals生成我們的幫助內(nèi)容:

          ?public?ICompletionProposal[]?computeCompletionProposals(ITextViewer?viewer,
          ????????????
          int?offset)?{
          ????????ICompletionProposal[]?proposals?
          =?new?ICompletionProposal[2];
          ????????
          ????????proposals[
          0]?=?new?CompletionProposal("替換文本1",?offset,?0,?new?String("替換文本1").length(),?null,?"幫組內(nèi)容1",?null,null)?;
          ????????proposals[
          1]?=?new?CompletionProposal("替換文本2",?offset,?0,?new?String("替換文本2").length(),?null,?"幫組內(nèi)容2",?null,null)?;
          ????????
          ????????
          return?proposals;
          ????}

          computeCompletionProposals輸入的參數(shù)中 offset是指當(dāng)內(nèi)容幫助彈出的時候,文本編輯器光標(biāo)所在位置。

          大家都知道,幫助內(nèi)容彈出的時候是需要一定條件的,也就是當(dāng)我們輸入了激活內(nèi)容幫助的字符的時候,它便會彈出來。IContentAssistProcessor?的getCompletionProposalAutoActivationCharacters方法便是讓我們返回激活幫助內(nèi)容字符的,假設(shè)當(dāng)我們輸入了‘<’時,彈出幫助內(nèi)容:

          ???public?char[]?getCompletionProposalAutoActivationCharacters()?{
          ????????
          return?new?String("<").toCharArray();
          ????}

          好了,我們的第一步已經(jīng)完成了,接下來就是在ContentAssis對象中設(shè)置我們所要返回的內(nèi)容幫助。
          返回到XMLConfiguration的getContentAssistant方法:

          ????public?IContentAssistant?getContentAssistant(ISourceViewer?sourceViewer)?{
          ????????
          //?生成一個ContentAssistant
          ????????ContentAssistant?assistant?=??new?ContentAssistant();
          ????????
          //?讓幫助內(nèi)容在XML的Tag標(biāo)簽范圍內(nèi)激%

          評論

          # re: Eclipse plugin開發(fā)之TextEditor —— 如何實現(xiàn)文本內(nèi)容幫助  回復(fù)  更多評論   

          2006-12-08 13:15 by 河風(fēng)
          Good THX

          # re: Eclipse plugin開發(fā)之TextEditor —— 如何實現(xiàn)文本內(nèi)容幫助  回復(fù)  更多評論   

          2007-03-30 19:54 by ffxiangyu
          最后一個方法是不是缺一段?

          # re: Eclipse plugin開發(fā)之TextEditor —— 如何實現(xiàn)文本內(nèi)容幫助  回復(fù)  更多評論   

          2007-07-25 02:28 by Acan
          對啊!關(guān)鍵的一段缺了,吊胃口!
          主站蜘蛛池模板: 兖州市| 富阳市| 柘城县| 甘肃省| 伊通| 巨野县| 平定县| 菏泽市| 乌什县| 长春市| 长子县| 将乐县| 六安市| 仙游县| 右玉县| 平泉县| 丹凤县| 洞口县| 南安市| 桐乡市| 伊宁市| 开远市| 金寨县| 唐山市| 古丈县| 皋兰县| 淮安市| 玛纳斯县| 博乐市| 邛崃市| 高平市| 扶绥县| 大名县| 鲁山县| 达州市| 都匀市| 中西区| 闻喜县| 任丘市| 定远县| 兴城市|