Commands Part 3:パラメータを使ったコマンド

Eclipse Tipsの翻訳Commandシリーズ第3段です。Part3では動的にパラメータを変更するコマンドについて書かれています。

原文

著者:Prakash G.R.
2009/01/07に記す

エディタの中からファイルを開くコマンドを考えてみましょう。「ファイルを開く - somefile.txt」とか、「ファイルを開く - someother.txt」とか...そういったメニューを動的に変更するコマンドはなかなか作成できませんよね。「ファイルを開く」と、実際に開いているファイルを組み合わせるというようなコマンドも作ってみたいですよね。このTipsでは、そういったコマンドの作り方について解説していきます。

こういったコマンドとパラメータは、下記のように拡張ポイントを定義します。

<command name="Open File"
      defaultHandler="com.eclipse_tips.commads.OpenFileHandler"
      id="com.eclipse-tips.commands.openFileCommand">
   <commandParameter
         id="com.eclipse-tips.commands.filePathParameter"
         name="File Path">
   </commandParameter>
</command>

そして対応するhandlerのコードはこんな感じになります。

public Object execute(ExecutionEvent event) throws ExecutionException {   
    String filePathParam = event.getParameter("com.eclipse-tips.commands.filePathParameter");
    IPath filePath = new Path(filePathParam);
    IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(filePath);
    IWorkbenchPage activePage = ...// get the activePage
    try {
        IDE.openEditor(activePage, file);
    } catch (PartInitException e) {
        ;// display error message
    }
    return null;
}

こんな感じでExecutionEventからあらかじめ定義しておいたidを通してパラメータを取得することができます。パラメータを取得すれば、あなたの使いたいように使うことができます。さてほんとにちゃんと違ったパラメータがわたってきているんでしょうか?ツールバー/メニューに配置するときに、合わせてパラメータを定義することができます。

<menuContribution
      locationURI="toolbar:org.eclipse.ui.main.toolbar">
   <toolbar
         id="com.eclipse-tips.commands.toolbar1">
      <command
            commandId="com.eclipse-tips.commands.someCommand"
            id="com.eclipse-tips.commands.someCommandInToolBar">
      </command>
      <command style="push"
            commandId="com.eclipse-tips.commands.openFileCommand"
            label="Opens somefile">            
         <parameter
               name="com.eclipse-tips.commands.filePathParameter"
               value="MyProject/somefile.txt">
         </parameter>
      </command>
      <command style="push"
            commandId="com.eclipse-tips.commands.openFileCommand"
            label="Opens some other file">
         <parameter
               name="com.eclipse-tips.commands.filePathParameter"
               value="MyProject/someotherfile.txt">
         </parameter>
      </command>
   </toolbar>
</menuContribution>

上記の例は最も簡単な固定のパラメータの例です。それではより現実的な例として"Show View"コマンドを見ていきましょう。このコマンドは動的で、すべてのビューを見られるコマンドです。

ビューのIDはこんな感じでコマンドにパラメータとして与えます。、

<command
        name="%command.showView.name"
        description="%command.showView.description"
        categoryId="org.eclipse.ui.category.views"
        id="org.eclipse.ui.views.showView"
        defaultHandler="org.eclipse.ui.handlers.ShowViewHandler">
   <commandParameter
            id="org.eclipse.ui.views.showView.viewId"
            name="%command.showView.viewIdParameter"
            values="org.eclipse.ui.internal.registry.ViewParameterValues" />
  <commandParameter
        id="org.eclipse.ui.views.showView.makeFast"
        name="%command.showView.makeFastParameter"
        optional="true">
  </commandParameter>

パラメータとしてとりうる全ての値のリストをViewParameterValuesクラスから与えます。このクラスはビューレジストリを走査し、ビューのIDを返します。この情報はキーバインディングの定義に使われます。

原文をみてください

パラメータの値を定義すると、キーバインディングのページには値が定義されたコマンドを表示できるのです。

原文をみてください

名前とIDのマップが返す全ての値がパラメータとして定義されます。

public class FileParameters implements IParameterValues {
    public Map getParameterValues() {
        Map params = new HashMap();
        params.put("Some File", "MyProject/somefile.txt");
        params.put("Some Other File", "MyProject/someotherfile.txt");
        return params;
    }
}

キーバインディングページでは定義した名前が表示され、定義したキーシーケンスが入力されると、そのコマンドが実行されるでしょう。