メニューバーを構築する(Eclipse3.3版)のページで、メインメニューやビュー内の△メニューへ機能を追加する方法が、Eclipse3.3から変更になったこと、その機能を使うための拡張ポイントがやインタフェースが、org.eclipse.ui.menus 拡張ポイントに統一化されたことを書きました。
さて、こんどはそのorg.eclipse.ui.menus 拡張ポイントを用いて、ポップアップメニューにコマンドを追加する方法をまとめたいと思います。
いままでは org.eclipse.ui.popupMenus 拡張ポイントとIViewActionDelegate,IObjectActionDelegateこれらのインタフェースで実装されていましたが、これらポップアップメニュー機能もorg.eclipse.ui.menus拡張ポイントに統合されています。
<extension point="org.eclipse.ui.menus"> <menuContribution locationURI="popup:org.eclipse.ui.popup.any"> <menu id="nu.mine.kino.plugin.newaction.menus.sampleMenu" label="トップのメニュー"> <command commandId="nu.mine.kino.plugin.samples.rcp.command" style="push"> </command> <command commandId="nu.mine.kino.plugin.newaction.commands.sampleCommand" style="push"> </command> </menu> </menuContribution> </extension>
このように、org.eclipse.ui.menus を使用して、URLの指定を
locationURI="popup:org.eclipse.ui.popup.any"
とするということで、ポップアップにコマンドを追加することができました。org.eclipse.ui.popup.any というIDは、全部のポップアップメニューという特殊なIDです。
特定のビューのみに表示したい場合は
locationURI="popup:org.eclipse.ui.views.ProblemView"
<viewerContribution id="nu.mine.kino.plugin.samples.rcp.viewerContribution1" targetID="org.eclipse.ui.views.ProblemView">
などとして、IViewActionDelegate? を実装してなどとあまりキレイでなかったのですが、すっきり実装できるようになりました。
メニューの表示でやったときと同じく、locationURIは?でパラメタをつけてSeparator名を指定することで表示場所の制御を行うことも可能です。
ちなみにgetSite().registerContextMenuを理解するにあるとおり、ポップアップにコマンドを追加するにはポップアップ側でメニューとSelectionProvider?を公開しておく必要があります。SelectionProvider?についてはこちら。
ポップアップへコマンドを追加するとき、特定のオブジェクトが選択されている状態のときのみ、コマンドを表示してあげるのがとても親切ですね。このorg.eclipse.ui.menus 拡張ポイントはある条件が成立する状況下でのみ、コマンドを表示するという機能があります。
<extension point="org.eclipse.ui.menus"> <menuContribution locationURI="popup:org.eclipse.ui.popup.any"> <menu id="nu.mine.kino.plugin.newaction.menus.sampleMenu" label="トップのメニュー"> <command commandId="nu.mine.kino.plugin.samples.rcp.command" style="push"> </command> <command commandId="nu.mine.kino.plugin.newaction.commands.sampleCommand" style="push"> </command> <visibleWhen> <with variable="activeMenuSelection"> <iterate ifEmpty="false"> <-ifEmptyは、Selectionがなかった時の判定。 <adapt type="org.eclipse.jdt.core.IJavaElement"> </adapt> </iterate> </with> </visibleWhen> </menu> </menuContribution> </extension>
このようにvisibleWhen要素で条件を指定することで、ある状況のみコマンドを表示するなんて事ができます。上記の指定は、選択されているオブジェクトがIJavaElement?の場合のみって事ですね。
うまくいきました。。
ちなみに
<with variable="activeMenuSelection">
この変数に設定できる項目は org.eclipse.ui.ISources に定義されているようです。
package org.eclipse.ui; import org.eclipse.core.expressions.IEvaluationContext; import org.eclipse.ui.part.IShowInSource; public interface ISources { public static final String ACTIVE_CONTEXT_NAME = "activeContexts"; public static final String ACTIVE_ACTION_SETS_NAME = "activeActionSets"; public static final String ACTIVE_SHELL_NAME = "activeShell"; public static final String ACTIVE_WORKBENCH_WINDOW_SHELL_NAME = "activeWorkbenchWindowShell"; public static final String ACTIVE_WORKBENCH_WINDOW_NAME = "activeWorkbenchWindow"; public static final String ACTIVE_WORKBENCH_WINDOW_IS_COOLBAR_VISIBLE_NAME = ACTIVE_WORKBENCH_WINDOW_NAME + ".isCoolbarVisible"; public static final String ACTIVE_WORKBENCH_WINDOW_IS_PERSPECTIVEBAR_VISIBLE_NAME = ACTIVE_WORKBENCH_WINDOW_NAME + ".isPerspectiveBarVisible"; public static final String ACTIVE_WORKBENCH_WINDOW_ACTIVE_PERSPECTIVE_NAME = ACTIVE_WORKBENCH_WINDOW_NAME + ".activePerspective"; public static final String ACTIVE_EDITOR_NAME = "activeEditor"; public static final String ACTIVE_EDITOR_ID_NAME = "activeEditorId"; public static final String ACTIVE_MENU_EDITOR_INPUT_NAME = "activeMenuEditorInput"; public static final String ACTIVE_FOCUS_CONTROL_NAME = "activeFocusControl"; public static final String ACTIVE_FOCUS_CONTROL_ID_NAME = "activeFocusControlId"; }
ちなみに全体はこんな感じ。関係ないところは省いてます。
package org.eclipse.ui; import org.eclipse.core.expressions.IEvaluationContext; import org.eclipse.ui.part.IShowInSource; public interface ISources { /** * The variable name for the active contexts. This is for use with the * <code>ISourceProvider</code> and <code>IEvaluationContext</code>. * @since 3.2 */ public static final String ACTIVE_CONTEXT_NAME = "activeContexts"; //$NON-NLS-1$ /** * The variable name for the active action sets. This is for use with the * {@link ISourceProvider} and {@link IEvaluationContext}. * @since 3.2 */ public static final String ACTIVE_ACTION_SETS_NAME = "activeActionSets"; //$NON-NLS-1$ /** * The variable name for the active shell. This is for use with the * <code>ISourceProvider</code> and <code>IEvaluationContext</code>. */ public static final String ACTIVE_SHELL_NAME = "activeShell"; //$NON-NLS-1$ /** * The variable name for the active workbench window shell. This is for use * with the <code>ISourceProvider</code> and * <code>IEvaluationContext</code>. * @since 3.2 */ public static final String ACTIVE_WORKBENCH_WINDOW_SHELL_NAME = "activeWorkbenchWindowShell"; //$NON-NLS-1$ /** * The variable name for the active workbench window. This is for use with * the <code>ISourceProvider</code> and <code>IEvaluationContext</code>. */ public static final String ACTIVE_WORKBENCH_WINDOW_NAME = "activeWorkbenchWindow"; //$NON-NLS-1$ /** * The variable name for the coolbar visibility state of the active * workbench window. This is for use with the <code>ISourceProvider</code> * and <code>IEvaluationContext</code>. * * @since 3.3 */ public static final String ACTIVE_WORKBENCH_WINDOW_IS_COOLBAR_VISIBLE_NAME = ACTIVE_WORKBENCH_WINDOW_NAME + ".isCoolbarVisible"; //$NON-NLS-1$ /** * The variable name for the perspective bar visibility state of the active * workbench window. This is for use with the <code>ISourceProvider</code> * and <code>IEvaluationContext</code>. * * @since 3.3 */ public static final String ACTIVE_WORKBENCH_WINDOW_IS_PERSPECTIVEBAR_VISIBLE_NAME = ACTIVE_WORKBENCH_WINDOW_NAME + ".isPerspectiveBarVisible"; //$NON-NLS-1$ /** * The variable name for the current perspective of the active workbench * window. This is for use with the <code>ISourceProvider</code> and * <code>IEvaluationContext</code>. * * @since 3.4 */ public static final String ACTIVE_WORKBENCH_WINDOW_ACTIVE_PERSPECTIVE_NAME = ACTIVE_WORKBENCH_WINDOW_NAME + ".activePerspective"; //$NON-NLS-1$ /** * The variable name for the active editor part. This is for use with the * <code>ISourceProvider</code> and <code>IEvaluationContext</code>. * @since 3.2 */ public static final String ACTIVE_EDITOR_NAME = "activeEditor"; //$NON-NLS-1$ /** * The variable name for the active editor identifier. This is for use with * the <code>ISourceProvider</code> and <code>IEvaluationContext</code>. * * @since 3.2 */ public static final String ACTIVE_EDITOR_ID_NAME = "activeEditorId"; //$NON-NLS-1$ /** * The variable name for the <b>local</b> editor input which is sometimes * available while a context menu is visible. * * @since 3.3 */ public static final String ACTIVE_MENU_EDITOR_INPUT_NAME = "activeMenuEditorInput"; //$NON-NLS-1$ /** * The variable name for the active focus Control, when provided by the * IFocusService. * * @since 3.3 */ public static final String ACTIVE_FOCUS_CONTROL_NAME = "activeFocusControl"; //$NON-NLS-1$ /** * The variable name for the active focus Control id, when provided by the * IFocusService. * * @since 3.3 */ public static final String ACTIVE_FOCUS_CONTROL_ID_NAME = "activeFocusControlId"; //$NON-NLS-1$ }
これらが全て、この箇所の変数として有効なのかはまだよく分かりませんが。。
この記事は
現在のアクセス:15722