<source id="4vppl"><ins id="4vppl"></ins></source>
<u id="4vppl"><sub id="4vppl"><label id="4vppl"></label></sub></u>
<object id="4vppl"></object>
  • <u id="4vppl"><li id="4vppl"><label id="4vppl"></label></li></u>

    <object id="4vppl"></object>
    <b id="4vppl"><sub id="4vppl"><tr id="4vppl"></tr></sub></b>

      <i id="4vppl"><thead id="4vppl"></thead></i>

      <thead id="4vppl"><li id="4vppl"><label id="4vppl"></label></li></thead>

      當(dāng)前位置:首頁(yè) > 網(wǎng)站舊欄目 > 學(xué)習(xí)園地 > 設(shè)計(jì)軟件教程 > JFace Text Editor完全掌握之終極指南(4)

      JFace Text Editor完全掌握之終極指南(4)
      2010-01-14 23:09:22  作者:  來(lái)源:

      錯(cuò)誤標(biāo)識(shí)(Error Marking)
      Error Marking用來(lái)對(duì)編輯的文檔根據(jù)一定的規(guī)則進(jìn)行驗(yàn)證,比如對(duì)于XML文檔來(lái)說(shuō),可能是XML DTD或者XML Schema.其實(shí)現(xiàn)跟內(nèi)容大綱比較類似,首先在解析文檔的時(shí)候?qū)rror加以標(biāo)識(shí).這里我們使用了SAX ErrorHandler來(lái)收集和定位所有的error, 接著在生成內(nèi)容大綱的同時(shí)進(jìn)行驗(yàn)證和error marking,這個(gè)工作在文檔被加載和文檔保存的時(shí)候都會(huì)進(jìn)行.

      Support for error marking in the enhanced XML editor 
      在XMLEditor的validateAndMark()方法中完成對(duì)error marking的初始化:

       

      java 代碼
       
      1. protected void validateAndMark()   
      2. {   
      3.   
      4.     IDocument document = getInputDocument();   
      5.     MarkingErrorHandler markingErrorHandler =    
      6.      new MarkingErrorHandler(getInputFile(), document);   
      7.     markingErrorHandler.removeExistingMarkers();   
      8.   
      9.     XMLParser parser = new XMLParser();   
      10.     parser.setErrorHandler(markingErrorHandler);   
      11.   
      12.     String text = document.get();   
      13.     parser.doParse(text);   
      14.   
      15. }  

      MarkingErrorHandler的實(shí)例化需要兩個(gè)參數(shù):一個(gè)是IFile實(shí)例,用來(lái)執(zhí)行marking(Eclipse Marker API將通過(guò)IFile來(lái)引用底層的Resource對(duì)象),另一個(gè)是編輯的IDocument實(shí)例(用來(lái)確定插入到文檔中的marker的位置)

      在文檔被解析之前,已有的error marker都必須先清掉, 在解析文檔的時(shí)候如果發(fā)現(xiàn)錯(cuò)誤,將調(diào)用MarkingErrorHandler的handleError()方法:

      java 代碼
       
      1. protected void handleError(SAXParseException e, boolean isFatal)   
      2. {   
      3.   
      4.     int lineNumber = e.getLineNumber();   
      5.     int columnNumber = e.getColumnNumber();   
      6.   
      7.     Map map = new HashMap();   
      8.     MarkerUtilities.setLineNumber(map, lineNumber);   
      9.     MarkerUtilities.setMessage(map, e.getMessage());   
      10.     map.put(IMarker.MESSAGE, e.getMessage());   
      11.     map.put(IMarker.LOCATION, file.getFullPath().toString());   
      12.   
      13.     Integer charStart = getCharStart(lineNumber, columnNumber);   
      14.     if (charStart != null)   
      15.         map.put(IMarker.CHAR_START, charStart);   
      16.   
      17.     Integer charEnd = getCharEnd(lineNumber, columnNumber);   
      18.     if (charEnd != null)   
      19.         map.put(IMarker.CHAR_END, charEnd);   
      20.   
      21.     map.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_ERROR));   
      22.   
      23.     try  
      24.     {   
      25.         MarkerUtilities.createMarker(file, map, ERROR_MARKER_ID);   
      26.     }   
      27.     catch (CoreException ee)   
      28.     {   
      29.         ee.printStackTrace();   
      30.     }   
      31. }   
      32.   

      這里我們的編輯器通過(guò)XML解析器(Xerces)不僅取得了error信息,而且還得到了發(fā)生錯(cuò)誤的位置信息,因此上面的代碼看起來(lái)非常的清晰:首先取得錯(cuò)誤信息的行號(hào)和列號(hào),然后使用Eclipse Marker API創(chuàng)建一個(gè)Error Marker

      內(nèi)容輔助
      最后我們將要介紹的一個(gè)功能是內(nèi)容輔助, 下圖是我們的實(shí)現(xiàn)效果, 這里我們只是一個(gè)簡(jiǎn)單的實(shí)現(xiàn),對(duì)于一個(gè)商業(yè)的XML編輯器來(lái)說(shuō),更強(qiáng)悍的就是能夠根據(jù)當(dāng)前光標(biāo)的位置以及定義的DTD做更精確的內(nèi)容輔助

      Support for content assist in the enhanced XML editor 

      為了讓我們的內(nèi)容輔助功能做的更智能,我們需要知道當(dāng)前文檔的結(jié)構(gòu)以及當(dāng)前光標(biāo)在文檔結(jié)構(gòu)中的位置

      跟其他功能類似,內(nèi)容輔助功能也是通過(guò)SourceViewerConfiguration來(lái)提供的,下面是我們的實(shí)現(xiàn)代碼:

      java 代碼
       
      1. public IContentAssistant getContentAssistant(ISourceViewer sourceViewer)   
      2. {   
      3.   
      4.     ContentAssistant assistant = new ContentAssistant();   
      5.   
      6.     IContentAssistProcessor tagContentAssistProcessor    
      7.         = new TagContentAssistProcessor(getXMLTagScanner());   
      8.     assistant.setContentAssistProcessor(tagContentAssistProcessor,   
      9.             XMLPartitionScanner.XML_START_TAG);   
      10.     assistant.enableAutoActivation(true);   
      11.     assistant.setAutoActivationDelay(500);   
      12.     assistant.setProposalPopupOrientation(IContentAssistant.CONTEXT_INFO_BELOW);   
      13.     assistant.setContextInformationPopupOrientation(IContentAssistant.CONTEXT_INFO_BELOW);   
      14.     return assistant;   
      15.   
      16. }   
      17.   

      上面的代碼比較簡(jiǎn)單,首先創(chuàng)建一個(gè)ContentAssistant實(shí)例,然后設(shè)置一些UI屬性,這里主要注意IContentAssistProcessor的實(shí)現(xiàn),我們實(shí)現(xiàn)的內(nèi)容輔助只是針對(duì)節(jié)點(diǎn),而且內(nèi)容輔助也是建立在對(duì)編輯文檔的分割處理的基礎(chǔ)上.分割處理我們前面已經(jīng)講的夠多了,這里我們就不再做說(shuō)明

      內(nèi)容輔助的UI處理都在ContentAssistant中實(shí)現(xiàn),一般情況下我們不需要子類化,除非當(dāng)前的功能無(wú)法滿足我們的要求

      內(nèi)容輔助的智能之處主要體現(xiàn)IContentAssistProcessor的實(shí)現(xiàn)上,而一般我們最感興趣的就是ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int offset);方法,內(nèi)容輔助的提示內(nèi)容列表就是在該方法中提供,這里是我們的代碼實(shí)現(xiàn):

      java 代碼
       
      1. public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int offset)   
      2. {   
      3.   
      4.     IDocument document = viewer.getDocument();   
      5.     boolean isAttribute = isAttribute(offset, document);   
      6.   
      7.     TextInfo currentText = currentText(document, offset);   
      8.   
      9.     if (!isAttribute)   
      10.     {   
      11.   
      12.         List allElements = dtdTree.getAllElements();   
      13.   
      14.         ICompletionProposal[] result = new ICompletionProposal[allElements.size()];   
      15.         int i = 0;   
      16.         for (Iterator iter = allElements.iterator(); iter.hasNext();)   
      17.         {   
      18.             XMLElement element = (XMLElement) iter.next();   
      19.             String name = element.getName();   
      20.   
      21.             String text = "" + name + ">" + "</" + name + ">";   
      22.             }   
      23.   
      24.             result[i++] = new CompletionProposal(text,    
      25.              currentText.documentOffset,    
      26.              currentText.text.length(),    
      27.              text.length());   
      28.   
      29.         }   
      30.         return result;   
      31.   
      32.     }   
      33.     else  
      34.     {   
      35.   
      36.         List allAttributes = dtdTree.getAllAttributes();   
      37.   
      38.         ICompletionProposal[] result = new ICompletionProposal[allAttributes.size()];   
      39.         int i = 0;   
      40.         for (Iterator iter = allAttributes.iterator(); iter.hasNext();)   
      41.         {   
      42.             String name = (String) iter.next();   
      43.   
      44.             String text = name + "= \"\" ";   
      45.                
      46.             result[i++] = new CompletionProposal(text,    
      47.              currentText.documentOffset,    
      48.              currentText.text.length(),    
      49.              text.length());   
      50.         }   
      51.         return result;   
      52.     }   
      53.   
      54. }   
      55.   

      上面的代碼非常的簡(jiǎn)單,首先根據(jù)當(dāng)前位置是否為屬性,是則列出已知的所有屬性名,否則列出所有的節(jié)點(diǎn)名.
      當(dāng)然這里我們的做法非常簡(jiǎn)單,更高級(jí)的實(shí)現(xiàn)是對(duì)整個(gè)文檔進(jìn)行掃描來(lái)確定當(dāng)前光標(biāo)在整個(gè)文檔結(jié)構(gòu)中所處的位置, 然后使用DTD驗(yàn)證計(jì)算當(dāng)前需要提示的更精確的內(nèi)容列表, 這就需要根據(jù)DTD來(lái)理解我們的文檔

      總結(jié)
      構(gòu)建一個(gè)強(qiáng)大的文本編輯器在Eclipse插件開(kāi)發(fā)中常常會(huì)碰到, 而JFace Text Editor是我們展開(kāi)工作的基礎(chǔ), 它是Eclipse非常強(qiáng)大,非常重要的一套API, 同時(shí)也是非常復(fù)雜的一套API.
      這里我們從Eclipse PDE提供的XML Editor向?qū)Ю尤胧?通過(guò)對(duì)其進(jìn)行擴(kuò)展, 演示了高亮顯示, 內(nèi)容格式化, 內(nèi)容大綱, 錯(cuò)誤標(biāo)記, 內(nèi)容輔助幾個(gè)功能的實(shí)現(xiàn), 希望這篇文章對(duì)你來(lái)實(shí)現(xiàn)自己強(qiáng)大的文本編輯器能有所幫助


      安徽新華電腦學(xué)校專業(yè)職業(yè)規(guī)劃師為你提供更多幫助【在線咨詢
      国产午夜福三级在线播放_亚洲精品成a人片在线观看_亚洲自慰一区二区三区_久久棈精品久久久久久噜噜
      <source id="4vppl"><ins id="4vppl"></ins></source>
      <u id="4vppl"><sub id="4vppl"><label id="4vppl"></label></sub></u>
      <object id="4vppl"></object>
    1. <u id="4vppl"><li id="4vppl"><label id="4vppl"></label></li></u>

      <object id="4vppl"></object>
      <b id="4vppl"><sub id="4vppl"><tr id="4vppl"></tr></sub></b>

        <i id="4vppl"><thead id="4vppl"></thead></i>

        <thead id="4vppl"><li id="4vppl"><label id="4vppl"></label></li></thead>
        亚洲国产91精品在线 | 亚洲午夜天堂视频在线观看 | 色久悠悠五月婷婷 | 欧美国产日韩a欧美在线视频 | 亚洲不卡在线视频 | 亚拍精品一区二区三区 |