首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 網頁程式設計 >> JSP 程式設計 >> 關於JSP

Eclipse 插件開發實戰:基於 WTP 打造自定義 JSP 編輯器(二)— 構建編輯器雛形與 JFace Text 基石

     編輯:關於JSP

承接上篇架構認知,本文進入實戰:在準備好開發環境後,基於 `StructuredTextEditor` 快速構建可運行的自定義 JSP 編輯器雛形,並深入剖析 JFace Text Framework 核心三大角色(`IEditorPart`、`IDocumentProvider`、`SourceViewerConfiguration`)的協作機制,為後續功能定制奠定堅實基礎。

開發環境確認清單

  • Eclipse 3.2 + WTP 1.5 (教學基線版本,現代版本請對應升級)
  • GEF 3.2.1, EMF 2.2.1 — 版本強綁定,不可混用
  • 已導入核心插件(二進制或源碼):
    • org.eclipse.wst.sse.core / .ui
    • org.eclipse.wst.xml.core / .ui
    • org.eclipse.wst.html.core / .ui
    • org.eclipse.jst.jsp.core / .ui
    • org.eclipse.wst.sse.ui

JFace Text Framework 三大核心角色深度解析

1. IEditorPart — 編輯器部件契約

  • 生命周期:`init(IEditorSite, IEditorInput)` → `createPartControl(Composite)` → `setInput(IEditorInput)` → `isDirty()` / `doSave()` / `doSaveAs()` → `dispose()`。
  • 關鍵:`getAdapter(Class)` 支援屬性視圖、大綱視圖、上下文菜單等擴展。

2. IDocumentProvider — 文檔橋樑

  • 職責:`IEditorInput` (如 `FileEditorInput`) ↔ `IDocument` (文檔模型) 映射、緩存、脏狀態、編碼轉換、只讀狀態。
  • 核心方法:`getDocument(input)`、`createDocument(input)`、`saveDocument(input)`、`isDeleted(input)`、`isReadOnly(input)`。
  • WTP 提供 `StructuredTextDocumentProvider`,支援結構化文檔的增量解析、模型同步。

3. SourceViewerConfiguration — 行為配置總線

編輯器構造時注入:`setSourceViewerConfiguration(new JSPConfiguration())`。所有編輯器行為通過此類配置:

配置方法返回類型功能定制切入點
getDocumentPartitioning()String分區類型 ID (如 "JSP")自定義分區規則
getPresentationReconciler()IPresentationReconciler語法著色、增量掃描自定義著色規則
getContentAssistant()IContentAssistant代碼完成、參數提示核心:智能提示定制
getAnnotationHover()IAnnotationHover懸停顯示錯誤/文檔自定義懸停內容
getHyperlinkDetector()IHyperlinkDetectorCtrl+Click 跳轉自定義跳轉目標
getAutoEditStrategies()Map<String, IAutoEditStrategy>智能編輯(括號、引號、縮進)自定義編輯行為
getContentFormatter()IContentFormatter代碼格式化 (Ctrl+Shift+F)自定義格式化規則
getOverviewRulerAnnotationHover()IAnnotationHover概覽標尺懸停錯誤導航增強

實戰:創建第一個可運行的 JSP 編輯器

1. 插件清單 (plugin.xml 關鍵擴展)

<extension point="org.eclipse.ui.editors">
  <editor
      class="com.example.jsp.editor.CustomJSPEditor"
      contributorClass="org.eclipse.ui.texteditor.BasicTextEditorActionContributor"
      default="true"
      extensions="jsp,jspf"
      icon="icons/jsp.gif"
      id="com.example.jsp.editor.CustomJSPEditor"
      name="Custom JSP Editor">
  </editor>
</extension>

<extension point="org.eclipse.core.contenttype.contentTypes">
  <content-type
      base-type="org.eclipse.core.runtime.xml"
      file-extensions="jsp,jspf"
      id="com.example.jsp.contenttype"
      name="JSP File"
      priority="normal">
  </content-type>
</extension>

2. 編輯器類

package com.example.jsp.editor;

import org.eclipse.jst.jsp.ui.editor.JSPEditor;
import org.eclipse.wst.sse.ui.StructuredTextEditor;

public class CustomJSPEditor extends JSPEditor { // 直接繼承 WTP 提供的 JSPEditor
    public CustomJSPEditor() {
        super();
        // 可選:覆寫配置、文檔提供者
        // setSourceViewerConfiguration(new CustomJSPConfiguration());
        // setDocumentProvider(new CustomDocumentProvider());
    }
}

💡 **技巧**:直接繼承 `org.eclipse.jst.jsp.ui.editor.JSPEditor` 即可獲得完整 JSP 編輯能力(語法著色、提示、驗證),僅需覆寫需定制的部分,避免從零構建。

3. 啟動測試

  1. Run → Run Configurations → Eclipse Application → New。
  2. 啟動新 Eclipse 實例,創建 Dynamic Web Project,新建 test.jsp。
  3. 右鍵 Open With → Custom JSP Editor,驗證語法著色、Content Assist (Ctrl+Space) 是否生效。

常見啟動失敗排查

現象原因解決
ClassNotFoundException: JSPEditor依賴缺失 org.eclipse.jst.jsp.uiMANIFEST.MF 添加 Require-Bundle
編輯器打開報錯 "No editor descriptor"plugin.xml editor 擴展點 id/class 錯誤檢查 class 全限定名、id 唯一性
內容助手不工作SourceViewerConfiguration 未正確注入確認 setSourceViewerConfiguration 調用
語法著色異常分區器配置錯誤覆寫 getDocumentPartitioning 返回 "JSP"

下一步預告

擁有可運行編輯器後,下兩篇將深入 WTP 兩大核心數據模型:

  1. IStructuredDocument (語法層):文本區域劃分、節點樹結構、Composite 模式實現、分析視圖開發。
  2. IStructuredModel (語義層):XML DOM 模型構建、模型管理器、句柄機制、模型分析視圖開發。

掌握這兩個模型,才能真正理解「內容助手如何知道當前標籤屬性」、「驗證器如何檢測標籤嵌套錯誤」、「超鏈接如何跳轉到標籤定義」。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved