// 下階層用テンプレート
#topicpath
----
//ここにコンテンツを記述します。

#contents

**Eclipse 3.3からのポップアップメニューへの機能追加 [#f991f74a]
[[メニューバーを構築する(Eclipse3.3版)>Eclipse/プラグイン開発のTIPS集/メニューバーを構築する(Eclipse3.3版)]]のページで、メインメニューやビュー内の△メニューへ機能を追加する方法が、Eclipse3.3から変更になったこと、その機能を使うための拡張ポイントがやインタフェースが、org.eclipse.ui.menus 拡張ポイントに統一化されたことを書きました。

さて、こんどはそのorg.eclipse.ui.menus 拡張ポイントを用いて、ポップアップメニューにコマンドを追加する方法をまとめたいと思います。

いままでは org.eclipse.ui.popupMenus 拡張ポイントと[[IViewActionDelegate>Eclipse/プラグイン開発のTIPS集/org.eclipse.ui.IViewActionDelegate(ポップアップ・メニューバー)]],[[IObjectActionDelegate>Eclipse/プラグイン開発のTIPS集/org.eclipse.ui.IObjectActionDelegate(ポップアップメニュー)]]これらのインタフェースで実装されていましたが、これらポップアップメニュー機能もorg.eclipse.ui.menus拡張ポイントに統合されています。


-plugin.xmlの記述。
 <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>

#ref(popup01.png)

このように、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"
などとビューのIDをURIに指定します((ちなみにこのIDは厳密には[[ビューのIDではないみたい>Eclipse/プラグイン開発のTIPS集/getSite().registerContextMenuを理解する]]なのでご注意。みたいっつうのは前自分で書いたけど、忘れてるからです(´д`;) ))。[[かつて>Eclipse/プラグイン開発のTIPS集/org.eclipse.ui.IViewActionDelegate(ポップアップ・メニューバー)]]は
 <viewerContribution
     id="nu.mine.kino.plugin.samples.rcp.viewerContribution1"
     targetID="org.eclipse.ui.views.ProblemView">
などとして、IViewActionDelegate を実装してなどとあまりキレイでなかったのですが、すっきり実装できるようになりました。

メニューの表示でやったときと同じく、locationURIは?でパラメタをつけてSeparator名を指定することで表示場所の制御を行うことも可能です。


ちなみに[[getSite().registerContextMenuを理解する>Eclipse/プラグイン開発のTIPS集/getSite().registerContextMenuを理解する]]にあるとおり、ポップアップにコマンドを追加するにはポップアップ側でメニューとSelectionProviderを公開しておく必要があります。SelectionProviderについては[[こちら>Eclipse/プラグイン開発のTIPS集/org.eclipse.ui.ISelectionListener]]。


**選択しているオブジェクトの種類で、表示・非表示を制御する [#g05720c8]
ポップアップへコマンドを追加するとき、特定のオブジェクトが選択されている状態のときのみ、コマンドを表示してあげるのがとても親切ですね。この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の場合のみって事ですね。

-IJavaElementじゃないモノを選択してるから、メニューに表示されない
#ref(popup02.png)

~~
-IJavaElementなモノを選択したらメニューに表示された
#ref(popup03.png)


うまくいきました。。


ちなみに
 <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$
 }
これらが全て、この箇所の変数として有効なのかはまだよく分かりませんが。。
 







**関連リンク [#r2c3af78]

-[[ポップアップやメニューバーにアクションを追加するための拡張ポイント、インターフェースのまとめ>Eclipse/プラグイン開発のTIPS集/メニューバーとか、ポップアップとかのまとめ]]
-[[Eclipse/プラグイン開発のTIPS集/メニューバーを構築する(Eclipse3.3版)]]
-[[ビュー内に配置されているウィジェットのポップアップメニューにアクションを追加したり、ビュー自体のメニューバーにアクションを追加したい(ビューを指定)>Eclipse/プラグイン開発のTIPS集/org.eclipse.ui.IViewActionDelegate(ポップアップ・メニューバー)]]
-[[ビュー内に配置されているウィジェットのポップアップメニューにアクションを追加したり、ビュー自体のメニューバーにアクションを追加したい(選択されているオブジェクトを指定)>Eclipse/プラグイン開発のTIPS集/org.eclipse.ui.IObjectActionDelegate(ポップアップメニュー)]]
-[[エディタを右クリックしたときに現れるポップアップメニューにアクションを追加したい>Eclipse/プラグイン開発のTIPS集/org.eclipse.ui.IEditorActionDelegate(ポップアップメニュー)]]
-[[よく出てくるgetSite().registerContextMenuでメニューを公開する仕組み>Eclipse/プラグイン開発のTIPS集/getSite().registerContextMenuを理解する]]

-[[Menu Contributions - Eclipsepedia>http://wiki.eclipse.org/index.php/Menu_Contributions]]

----
この記事は
#vote(おもしろかった,そうでもない)

#comment
#topicpath


SIZE(10){現在のアクセス:&counter;}


トップ   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS