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

Eclipse 外掛開發入門:新增工具列按鈕、選單項目與對話框

     編輯:Java 相關知識

擴展點

Eclipse UI 不允許任意位置新增元件,只能在文件化的「擴展點」掛載。開啟 plugin.xml → Extensions 頁籤,點擊 Add... 可見數百個擴展點,常用者附帶模板(如 Action Set 的 Hello World 模板)。

擴展頁 新增擴展向導,擴展列表

新增工具列按鈕

三步驟:宣告擴展 → 具體標記 → 撰寫 Action Delegate

1. 宣告擴展

plugin.xml → Add... → 選 org.eclipse.ui.actionSets(無模板直接 Finish)。設定:

  • Id: NewInvokatronAction
  • Name: New Invokatron Document Action
  • Point: org.eclipse.ui.actionSets

2. 以 XML 擴充宣告

在 <extension> 內加入 <actionSet> → <action>:

<extension id="NewInvokatronAction" name="New Invokatron Document Action" point="org.eclipse.ui.actionSets">
  <actionSet id="invokatron.actionSet" label="Invokatron Actions" visible="true">
    <action id="invokatron.wizard.RunWizardAction"
            toolbarPath="org.eclipse.ui.workbench.file/new.ext"
            icon="icons/InvokatronIcon16.gif"
            tooltip="Starts the New Invokatron Document Wizard."
            class="invokatron.wizard.RunWizardAction">
    </action>
  </actionSet>
</extension>

關鍵屬性:

  • toolbarPath:按鈕位置,org.eclipse.ui.workbench.file/new.ext 為「檔案」工具列的「新建」群組。
  • icon:16x16 GIF,放入 icons/ 目錄(已含於二進位建置路徑)。
  • class:Action Delegate 完整類名。

常用工具列路徑

工具列基礎路徑常用群組標記
檔案org.eclipse.ui.workbench.filenew.ext, save.ext, print.ext, build.ext
導航org.eclipse.ui.workbench.navigate—
執行org.eclipse.debug.ui.launchActionSet—
搜尋org.eclipse.search.searchActionSet—

若不帶群組標記,按鈕會建立新工具列;若自訂工具列 ID,會置於「檔案」工具列之後。

3. 撰寫 Action Delegate

package invokatron.wizard;
import org.eclipse.jface.action.Action;
import org.eclipse.ui.IWorkbenchWindowActionDelegate;
import org.eclipse.ui.PlatformUI;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.swt.widgets.Shell;

public class RunWizardAction extends Action implements IWorkbenchWindowActionDelegate {
    public void init(IWorkbenchWindow window) {}
    public void dispose() {}
    public void run(IAction action) {
        InvokatronWizard wizard = new InvokatronWizard();
        Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
        WizardDialog dialog = new WizardDialog(shell, wizard);
        dialog.create();
        dialog.open();
    }
    public void selectionChanged(IAction action, ISelection selection) {}
}

新增選單項目

流程幾乎相同,差異在 <action> 使用 menubarPath 而非 toolbarPath,並需 label 屬性(顯示文字,快捷鍵用 &amp; 表示)。

<action id="invokatron.wizard.RunWizardAction"
        label="New Invokatron"
        menubarPath="file/new.ext"
        icon="icons/InvokatronIcon16.gif"
        tooltip="Starts the New Invokatron Document Wizard."
        class="invokatron.wizard.RunWizardAction">
</action>

合併工具列與選單:同時指定 toolbarPath 與 menubarPath。

選單路徑與群組標記

選單列基礎路徑常用群組
檔案filefileStart, new/additions, new.ext, close.ext, save.ext, additions, fileEnd
編輯editeditStart, undo.ext, cut.ext, find.ext, additions, fileEnd
視窗windowadditions, additionsend
說明helphelpStart, group.main.ext, group.about.ext, helpEnd

完整路徑範例:file/new.ext(檔案→新增群組)。建議工具列與選單路徑保持一致性。

自訂對話框輔助類

Eclipse 標準對話框由 org.eclipse.jface.dialogs.MessageDialog 提供:

類型建立方法對應 Swing
確認openConfirm(parent, title, msg)JOptionPane.showConfirmDialog(OK_CANCEL)
錯誤openError(parent, title, msg)JOptionPane.showMessageDialog(ERROR)
資訊openInformation(parent, title, msg)JOptionPane.showMessageDialog(INFORMATION)
是/否openQuestion(parent, title, q)JOptionPane.showConfirmDialog(YES_NO)
警告openWarning(parent, title, msg)JOptionPane.showMessageDialog(WARNING)

覆蓋確認對話框

public static boolean openOverwriteDialog(Shell parent) {
    return MessageDialog.openConfirm(parent, "Confirmation",
        "You are about to overwrite your class. Are you sure you want to continue?");
}

輸入對話框

MessageDialog 無輸入欄,改用 org.eclipse.jface.dialogs.InputDialog:

public static String openInputDialog(Shell parent, String title, String question, String initialValue) {
    InputDialog dlg = new InputDialog(parent, title, question, initialValue, null);
    dlg.open();
    if (dlg.getReturnCode() != Window.OK) return null;
    return dlg.getValue();
}
對話框

成果展示

新的選單項目和工具列按鈕

掌握擴展點與對話框,如同學會樂器,即可譜寫屬於自己的 Eclipse 交響樂。

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