コントローラの実装

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

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

ここに示したコード例は、ContextInfoサンプル・プロジェクトのものです。この例と他の拡張機能の例は、Oracle Technology Network(OTN)のWebサイト(http://otn.oracle.com/products/jdev/htdocs/partners/addins/exchange/docsamples/content.html)で入手できます。

コンストラクタの定義

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

supervisorメソッドの定義

拡張機能がデフォルトとして選択したイベントを処理するコントローラを返すよう、supervisorを実装します。大部分の拡張機能にとって適切なBaseController定義は、Ideオブジェクトを返します。



public Controller supervisor() { return Ide.getInstance(); }

handleEventメソッドの定義

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



public boolean handleEvent(IdeAction action, Context context) { int cmdId = action.getCommandId(); // Handle actions containing this command: if (cmdId == CONTEXT_INFO_CMD_ID) { CommandProcessor cmdProc = Ide.getCommandProcessor(); 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; } } // Delegate other actions to the supervisor. return supervisor().handleEvent(action, context); }

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

checkCommandsメソッドの定義

checkCommandsメソッドは、選択したアクションが更新されるようにします。メソッドを定義し、この拡張機能に定義されたアクションに対する更新をコールします。スーパーバイザに定義されたアクションは更新しないでください。これはスーパーバイザのcheckCommandsメソッドの役割であり、このメソッドの最初のアクションとして起動する必要があります。渡されたactiveControllerに定義されたupdateメソッドをコールし、将来の拡張機能で現在の機能を拡張し、そのアクションをオーバーライドできるようにします。BaseController定義はそのスーパーバイザを起動しますが、それ以外の効果はありません。



public void checkCommands( Context context, Controller activeController ) { // Ask the supervisor to update its commands. supervisor().checkCommands( context, activeController ); // Ask the active controller to update this controller's command(s). if ( context != null ) { activeController.update( ideCutAction, context ); activeController.update( ideCopyAction, context ); activeController.update( idePasteAction, context ); activeController.update( ideDeleteAction, context ); activeController.update( ideSelectAllAction, context ); activeController.update( ideSaveAction, context ); } }

updateメソッドの定義

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



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; } // Delegate other actions to the supervisor. return supervisor().update(action, context); }

コマンド定数の定義

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



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

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

oracle.ide.Ide
oracle.ide.addin.Controller
oracle.ide.addin.BaseController
oracle.ide.model.Document