コントローラの実装

拡張機能がビュー・クラスを実装する場合、そのビューのイベントを処理するコントローラ・クラスも実装する必要があります。コントローラを実装するには、Controllerインタフェースを使用します。

コントローラ・クラスを実装するには、次の定義が必要です。

ここに示したコード例は、ContextInfoサンプル・プロジェクトのものです。 この例と他の拡張機能の例は、OTNのExtensions SDKのページで入手できます。

コンストラクタの定義

単項または零項コンストラクタを定義します。

handleEventメソッドの定義

handleEventメソッドは、基本的に、選択したコマンドに対応するアクションを処理し、残りをスーパーバイザに委任する分岐文です。このメソッドを定義し、拡張機能に定義されたコマンド、および拡張機能がオーバーライドする必要のあるその他のコマンドを処理します。




public boolean handleEvent(IdeAction action, Context context) { int cmdId = action.getCommandId(); // Handle actions containing this command: if (cmdId == CONTEXT_INFO_CMD_ID) { CommandProcessor cmdProc = CommandProcessor.getInstance(); String commandName = action.getCommand(); Command command = cmdProc.createCommand(commandName, context); // Use command processor to execute command. try { cmdProc.invoke(command); } catch (Exception e) { System.err.println(e.toString()); } finally { return true; } } // Let the IDE try to find another Controller to handle this action. return false; }

イベントは、そのデータを操作するメソッドをコールすることにより、直接処理できます。あるいは、コマンド・オブジェクトを作成し、コマンド・プロセッサを介して起動することにより、イベントを間接的に処理することもできます。次のような場合には、後者が適しています。

updateメソッドの定義

updateメソッドは、基本的に、選択したアクションを使用可能および使用不可にし、残りをスーパーバイザに委任する分岐文です。拡張機能に定義されたアクション、および拡張機能でオーバーライドする必要のあるその他のアクションに対し、このメソッドを定義します。




public boolean update(IdeAction action, Context context) { int cmdId = action.getCommandId(); // Set the enabled status for relevant actions. if ( cmdId == CONTEXT_INFO_CMD_ID ) { action.setEnabled(enableContextInfo(context)); return true; } // Let the IDE try to find another Controller to update this action. return false; }

コマンド定数の定義

拡張機能に必要なコマンドを、コマンドIDを指定することにより定義します。staticメソッドIde.findOrCreateCmdIDは、一意のコマンドIDを割り当てます。




public static final int CONTEXT_INFO_CMD_ID = Ide.findOrCreateCmdID("ContextInfoController.CONTEXT_INFO_CMD_ID");

エディタの開発
コマンドの開発
ユーザー・インタフェース・イベントについて
コマンドの実装
アクションの定義

oracle.ide.Ide
oracle.ide.Addin.Controller
oracle.ide.model.Document

 

Copyright © 1997, 2004, Oracle.All rights reserved.