ランタイム・サービス

HATS ランタイム、HATS リッチ・クライアント・アプリケーション、 および実行中アプリケーション・インスタンスとのインターフェースとして、 一連のサービスが用意されています (HATS ランタイム・サービスと総称します)。HATS ランタイムとの対話には、 提供されているこれらの API を使用することをお勧めします。それにより、 下位の詳細を考慮する必要がなくなります。

表 11 は、提供されているサービス・タイプに関する説明です。ランタイム・サービス およびクライアント・サービスのインスタンスは Eclipse 環境ごとに 1 つしか存在しませんが、 アプリケーション・サービスのインスタンスは複数存在することができ (単一の Eclipse 環境に 複数の HATS リッチ・クライアント・アプリケーションをインストールできるためです)、 セッション・サービスのインスタンスも複数存在することができます (ユーザーが 複数のセッション、つまりアプリケーションのインスタンスを開始できるためです)。

表 11. 提供済み サービス
サービス 説明 有効範囲
ランタイム ランタイム初期化などのランタイム関連サービスを提供します。これにより、新規クライアント・セッションが作成され、接続マネージャーおよびアプリケーション・マネージャー・クラスへのアクセスが提供されます。 Eclipse 環境ごとに 1 つ
アプリケーション HATS リッチ・クライアント・アプリケーションに関する情報を検索するためのインターフェースを提供します。 HATS リッチ・クライアント・プラグイン・アプリケーションごとに 1 つ
クライアント クライアントまたはユーザーに関する情報を検索するためのインターフェースを提供します。 Eclipse 環境ごとに 1 つ
セッション 特定のアプリケーション・インスタンス/ホスト接続に代わって、HATS ランタイムと対話するためのインターフェースを提供します。 実行アプリケーション・インスタンスごとに 1 つ
注:
サービス・クラスは拡張できません。
注:
以下のセクションのコード例では、 下記のインポートが必要です。
import java.util.Collection; import java.util.Iterator;  
import com.ibm.hats.rcp.runtime.RcpRuntimePlugin;
import com.ibm.hats.runtime.services.IApplicationService; 
import com.ibm.hats.runtime.services.IClientService;
import com.ibm.hats.runtime.services.IRuntimeService; 
import com.ibm.hats.runtime.services.IServiceManager;
import com.ibm.hats.runtime.services.ISessionService; 
import com.ibm.hats.runtime.services.ServiceType;

サービス・マネージャーへのアクセス

すべてのサービスは、単一のサービス・マネージャーによって作成され、保守されます (サービス・マネージャーは、com.ibm.hats.runtime.services.IServiceManager インターフェースをインプリメントします)。サービス・オブジェクトは、サービス・マネージャーの外側に構成しないでください。以下のコード・サンプルは、サービス・マネージャーのアクセス方法を示しています。

IServiceManager serviceManager = RcpRuntimePlugin.getDefault().getServiceManager();

表 12 に、IServiceManager オブジェクトから呼び出し可能なメソッドを示します。

表 12. IServiceManager メソッド
メソッド 説明
addServiceManagerListener(ServiceManagerListener) リスナーをこのサービス・マネージャーに追加します。
removeServiceManagerListener(ServiceManagerListener) リスナーをこのサービス・マネージャーから除去します。
getApplicationService(String) 指定したアプリケーション・プラグイン ID に応じて、IApplicationService を戻します。
getClientService(String) 指定したクライアント ID に対応する IClientService を戻します (リッチ・クライアントでは、クライアント ID は、必ず RcpRuntimeService.rcpClientId の値になっています)。
getRuntimeService() 環境に適合する IRuntimeService を戻します。
getSessionService(String, String, String) 指定したクライアント ID、アプリケーション・プラグイン ID、およびビュー ID に対応する ISessionService を戻します。
getServiceIDs(ServiceType) このサービス・マネージャーによって管理されているサービスに対応する、指定したサービス・タイプ getServiceEntryCount(ServiceType) を持つ一連の ID を戻します。
getServiceEntryCount(ServiceType) このサービス・マネージャーによって管理されている、指定したタイプを持つサービスの数を戻します。

詳しくは、com.ibm.hats.runtime.services.IServiceManager API を参照してください。

ランタイム・サービスの使用

ランタイム・サービス (com.ibm.hats.runtime.services.IRuntimeService インターフェースをインプリメントする) は、HATS ランタイムと対話するメソッドを提供します。このサービスに提供されているほとんどのメソッドは、直接呼び出すことはできません。ただし、HATS ランタイムの他のコンポーネントが通信できるように公開されています。アプリケーションのランタイム・サービス・オブジェクトは、サービス・マネージャーから検索する必要があります。ランタイム・サービスを取得するための コード例を以下に示します。

IRuntimeService runtimeService = 
            serviceManager.getRuntimeService();

以下のメソッドは、ランタイム・サービスによって呼び出すことができます。

表 13. IRuntimeService メソッド
メソッド 説明
getConnectionManager() デフォルトの ConnMgr インスタンスを戻します。
getServiceManager() このサービスをインスタンス化したサービス・マネージャーを戻します。

アプリケーション・サービスの使用

アプリケーション・サービス (com.ibm.hats.runtime.services.IApplicationService インターフェースをインプリメントする) は、HATS リッチ・クライアント・アプリケーションに関連する情報を検索するメソッドを提供します。Eclipse 環境にインストールされ、使用可能になっている、各 HATS リッチ・クライアント・アプリケーション・プラグインには、IApplicationService オブジェクトが関連付けられています。アプリケーションのアプリケーション・サービス・オブジェクトは、サービス・マネージャーから検索する必要があります。アプリケーション・サービスを取得するための コード例を以下に示します。

	IApplicationService applicationService = 
                 serviceManager.getApplicationService("myPluginId");
getApplicationService(String)

メソッドは、指定されているアプリケーション ID が無効な場合、またはプラグインが開始できなかった場合、NULL を戻します。

表 14 に、IApplicationService オブジェクトから呼び出し可能なメソッドを示します。

表 14. IApplicationService メソッド
メソッド 説明
getApplication() アプリケーションに関する情報が含まれている Application オブジェクトを戻します。このオブジェクトは application.hap ファイルとの 同期が保証されないため、このメソッドによって 戻されたオブジェクトは情報の取得のみに使用してください。このオブジェクトを使用して 設定を変更しないでください。
getApplicationId() アプリケーション ID を戻します。
getConfig() 構成を戻します。
getRuntimeService() ランタイム・サービスを戻します。
getServiceManager() このサービスをインスタンス化したサービス・マネージャーを戻します。

クライアント・サービスの使用

クライアント・サービス (com.ibm.hats.runtime.services.IClientService インターフェースを インプリメントする) は、クライアント・サービスに関連する情報を取得する メソッドを提供します。クライアント・サービスを取得するための コード例を以下に示します。

IServiceManager serviceManager = RcpRuntimePlugin.getDefault().getServiceManager();
IClientService clientService = serviceManager.getClientService("theClientId");

表 15 に、IClientService オブジェクトから呼び出し可能なメソッドを示します。

表 15. IClientService メソッド
メソッド 説明
getClientId() クライアント ID を戻します。リッチ・クライアントでは、 常に RcpRuntimeService.rcpClientId の値になります。
getRuntimeService() ランタイム・サービスを戻します。
getServiceManager() このサービスをインスタンス化したサービス・マネージャーを戻します。
getSession(String) 指定されたセッションを戻します。
注:
これは、型 ISession の ホスト・セッションです (セッション・サービス・インスタンスではありません)。

セッション・サービスの使用

セッション・サービス (com.ibm.hats.runtime.services.SessionService インターフェースをインプリメントする) は、HATS リッチ・クライアント・アプリケーションのインスタンスと対話するためのメソッドを提供します。実行中の 各アプリケーション・インスタンス (変換ビュー・インスタンスなど) には セッション・サービスが関連付けられています。変換ビュー・インスタンスおよびセッション・サービスとの間には、1 対 1 のマッピングが存在します。

セッション・サービスの完全なコレクションを 取得するためのコード例を以下に示します。

IServiceManager serviceManager = 
         RcpRuntimePlugin.getDefault().getServiceManager();
Collection sessionServices = 
         serviceManager.retrieveServiceEntries( ServiceType.SESSION );
Iterator itSessionServices = 
         sessionServices.iterator();
while ( itSessionServices.hasNext() )
{         
ISessionService aSessionService = 
      (ISessionService) itSessionServices.next();
// Your custom code goes here     
}

表 16 に、アプリケーションのインスタンスである ISessionService オブジェクトによって呼び出すことができるメソッドを示します。

表 16. ISessionService メソッド
メソッド 説明
addPresentationListener
         (IPresentationListener)

指定されたプレゼンテーション・リスナーをリストに 追加します。プレゼンテーションが更新されると、 リスナーに通知されます。
addSessionServiceListener
       (ISessionServiceListener)

指定されたセッション・サービス・リスナーをリストに 追加します。セッションの状態が変化したとき、 またはコマンドが送信されるときには、リスナーに通知されます。
canSendCommand(String) 現在のセッション・サービスの状態で 指定のコマンドを送信できる場合は true のブール値を戻します。 それ以外の場合は false のブール値を戻します。
disconnect() ホスト・セッションを切断します。このメソッドは、アプリケーション・キーパッド上の「切断」ボタンによって呼び出されます。関連付けられている変換ビューは、このメソッドではクローズされません。このメソッドは、セッション・サービスが既にコマンドを処理している場合には、何もアクションを実行しません。
getApplication() このセッション・サービス・インスタンスに 関連付けられたアプリケーションを戻します。このメソッドによって 戻されたオブジェクトは情報の取得のみに使用してください。このオブジェクトを使用して 設定を変更しないでください。
getApplicationId() このセッション・サービスに関連付けられた アプリケーションのアプリケーション ID (プラグイン ID など) を戻します。
getApplicationService() このセッション・サービス・インスタンスに 関連付けられたアプリケーション・サービスを戻します。
getClientId() このセッション・サービス・インスタンスに 関連付けられたクライアント ID を戻します。
getCurrentState() このセッション・サービス・インスタンスの現在の状態を戻します。
getHostScreen() このセッション・サービス・インスタンスに 関連付けられたホスト画面を戻します。これは、HATS ランタイムによって 処理された最後のホスト画面を戻します。戻り値は 必ずしも現在のホスト画面とは限りません。
getHostScreenDataAccessService() フォーム上のホスト・フィールド関連制御の値を含むモデルを戻します。このメソッドは、変換がビュー上で現在表示されている場合のみ有効です。
getLocale() メッセージの表示に使用するロケールを戻します。 プロジェクト設定エディターでアプリケーションのクライアント・ロケール設定を変更した場合、 このロケールは Eclipse 環境のロケールと異なる場合があります。
getMacroPromptDataAccessService() モデル・データへのアクセスに使用するサービスを戻します。
getParameterDataAccessService() フォーム上の非ホスト・フィールド関連制御の値を含むモデルを戻します。このサービスは、グローバル変数値など、その他のフォーム値の設定および検索を行うために使用されます。これらの値は、現行フォームが送信されると HATS ランタイムに引き渡されます。
getRuntimeService() ランタイム・サービスを戻します。
getServiceManager() このサービスをインスタンス化したサービス・マネージャーを戻します。
getSession() このセッション・サービス・インスタンスに 関連付けられたホスト・セッションを戻します。
getSessionServiceState() セッション・サービスの状態を状態オブジェクトとして戻します。
getViewId() 変換ビューのインスタンス ID を戻します。
isAsyncUpdateEnabled() 非同期更新が使用可能な場合は true のブール値を戻します。 それ以外の場合は false のブール値を戻します。
isCommandSupported(String) 指定されたコマンドが サポートされる場合は true のブール値を戻します。 それ以外の場合は false のブール値を戻します。
playMacro(String) 指定したマクロを実行します。
playMacro(String, Properties) 指定したマクロを実行し、マクロで定義されているすべてのプロンプトに値を引き渡します。
removePresentationListener
            (IPresentationListener)

指定されたプレゼンテーション・リスナーを リストから除去します。
removeSessionServiceListener
           (ISessionServiceListener)

指定されたセッション・サービス・リスナーを リストから除去します。
sendCommand(String) 指定したコマンドを送信します。一般的なコマンドは、[pf1]、[pf2]、[enter]、[fldext] です。
注:
コマンドの前後の大括弧は必須です。
sendCommand(String, Properties) String によって指定されたコマンドを 関連プロパティーとともに送信します。コマンドは、[enter] などの Host On-Demand ニーモニックか、default などの HATS コマンドの いずれかです。追加のパラメーターを指定できます。これらのパラメーターは、 実行依頼される要求に組み込まれます。これらのパラメーターにより、 ホスト入力フィールドなどの SDO モデルまたは グローバル変数から収集される同じ名前のパラメーターが オーバーライドされます。オーバーライド・パラメーターの値は 型 String[] でなければなりません。
sendContinue() continue コマンドを送信します。このコマンドを送信して、「コンポジットを表示」アクションまたはマクロ・プロンプト・アクションから続行する必要があります。
sendEnter() Enter キー・コマンドを送信します。
sendF1() - sendF24() 各 PF キー・コマンドを送信します。
sendRefresh() 更新コマンドを送信します。
sendShowDefault() デフォルト表示コマンドを送信します。
start(Properties) 指定された接続およびグローバル変数オーバーライドを使用して セッション・サービスを開始します。
stop() セッション・サービスを停止します。
updatePresentation(IPresentable) 指定されたプレゼンテーション可能オブジェクトで プレゼンテーション (一般には変換ビュー) を更新します。

変換ビューに関連する ISessionService オブジェクトは、ビュー上で getSessionService() メソッドを呼び出すことによって、検索できます。このメソッドは、ITransformationView インターフェースによって提供されます。このインターフェースは、基本 TransformationView クラスによってインプリメントされます。

他の Eclipse UI ビューとの統合

ランタイム・サービス API を 使用することによって、HATS 変換ビュー (HATS リッチ・クライアント・アプリケーション) は 他の Eclipse UI ビュー (非 HATS リッチ・クライアント・アプリケーション) と 通信できます。この通信は、HATS アプリケーションに対して 着信または発信のいずれかにすることができます。着信通信は、HATS リッチ・クライアント・セッション受信データ または HATS 外部のエンティティーからのコマンドとして 定義されます。発信通信は、HATS リッチ・クライアント・セッション送信データ または外部エンティティーに対するコマンドとして定義されます。この機能により、 適切なデータを複数のソースから取得して単一のビューで 表示できます。したがって、HATS リッチ・クライアント・アプリケーションは、 他の Eclipse リッチ・クライアント・アプリケーションから データを受信して表示するビューか、1 つ以上の Eclipse リッチ・クライアント・アプリケーションに データを提供して表示させるアプリケーションのいずれかにすることができます。

着信通信のシナリオ

以下のシナリオでは、フライト番号を入力として受け入れ、 そのフライトに関する情報を表示するホスト・アプリケーションを 想定します。このアプリケーションは HATS RCP アプリケーションに 変換されています。別の Eclipse RCP ベースのアプリケーションは、 フライト番号を含む予約情報を受け入れます。フライト番号は、 このアプリケーションに入力されると HATS RCP アプリケーションに送信され、 そのフライト番号に対する情報で HATS ビューが更新されます。このシナリオの コード例を以下に示します。

IServiceManager serviceManager = RcpRuntimePlugin.getDefault().getServiceManager();
IClientService clientService = serviceManager.getClientService("theClientId");
/*--------------------------------------------------------------------------------
// 
// Provides an example of a HATS application receiving information from another 
// application.  This example cannot be executed directly but is provided to 
// highlight the approach required to achieve this functionality.
// 
// @param  Flight number from GUI as a Java String.  This value cannot be 
// <code>null</code>.
// 
*///------------------------------------------------------------------------------ 
    public void inboundToHATSApplication( String flightNumber )
    {      
        // Save flight number from GUI
        Properties params = new Properties();
        
        params.setProperty( "fltNum",  flightNumber );

        // Get the service manager from singleton plug-in instance.
        IServiceManager serviceManager = 
                                RcpRuntimePlugin.getDefault().getServiceManager();

        // This will be the plug-in session we use to play our macro
        ISessionService aSessionService = null;

        // Find the correct HATS plug-in session
        Collection sessionServices = 
                     serviceManager.retrieveServiceEntries( ServiceType.SESSION );
        Iterator itSessionServices = sessionServices.iterator();
        
        while ( itSessionServices.hasNext() )
        {
            aSessionService = (ISessionService) itSessionServices.next();

            // Assumes the first instance of the HATS plug-in called "myApp" 
            // is the one we want.
            if ( "myApp".getApplicationId().equals( aSessionService ) )
            {
                break;
            }
        }

        // Refer to documentation to see how to launch an RCP plug-in instance if 
        // one is not already launched
        if ( aSessionService != null )
        {
            // Really should check the state in a limited-time loop, 
            // but let's assume we can go ahead
            if ( aSessionService.getSessionServiceState().isOperational() )
            {
                // Ask HATS session service to play the macro with the fltNum 
                // from our GUI
                // Assumes that the macro can start from current screen or from 
                // the results screen.
                aSessionService.playMacro( "displayFlightInfo", params );
            }
        }
    }

サンプル

複数のランタイム・サービスにアクセスする方法を示すクラスおよびメソッドの例

/*---------------------------------------------------------------------------------
//
// Host Access Transformation Services technology
//
// Module Name: RuntimeServicesExamples.java
//
// Description: Provides java code samples for accessing HATS Rich Client Program 
// Runtime Services.
//
*///-------------------------------------------------------------------------------

package com.ibm.hats;

import java.util.Collection;
import java.util.Iterator;

import com.ibm.hats.rcp.runtime.RcpRuntimePlugin;

import com.ibm.hats.runtime.services.IApplicationService;
import com.ibm.hats.runtime.services.IClientService;
import com.ibm.hats.runtime.services.IRuntimeService;
import com.ibm.hats.runtime.services.IServiceManager;
import com.ibm.hats.runtime.services.ISessionService;
import com.ibm.hats.runtime.services.ServiceType;

//---------------------------------------------------------------------------------


/** <code><B>
*  Class Name:  </B> RuntimeServicesExamples <B><BR>
*  Class Type:  </B> Normal <B><BR>
*  Base Class:  </B> None   <B><BR>
*  Intf Class:  </B> None   <B><BR>
*  Description: </code></B> Rich Client Program Runtime Services java code samples.
*///-------------------------------------------------------------------------------
public class RuntimeServicesExamples
{
    //-----------------------------| Constants |-----------------------------------
    
    //-----------------------------| Variables |-----------------------------------

    //-----------------------------------------------------------------------------
    /**
    * The default constructor.
    *///---------------------------------------------------------------------------
    public RuntimeServicesExamples()
    {
        super();
    }

    //-----------------------------------------------------------------------------
    /**
    * Provides an example of accessing each application service.
    *///---------------------------------------------------------------------------
    public void accessApplicationService()
    {
        Collection applicationServices = this.retrieveApplicationServices();
        
        Iterator itApplicationServices = applicationServices.iterator();
        
        while ( itApplicationServices.hasNext() )
        {
            IApplicationService aApplicationService = (IApplicationService) 
                                              itApplicationServices.next();
            
            // Custom code goes here
        }
    }

    //-----------------------------------------------------------------------------
    /**
    * Provides an example of accessing each client service.  Currently, there is 
    // only one.
    *///---------------------------------------------------------------------------
    public void accessClientService()
    {
        Collection clientServices = this.retrieveClientServices();
        
        Iterator itClientServices = clientServices.iterator();
        
        while ( itClientServices.hasNext() )
        {
            IClientService aClientService = (IClientService) 
                                                          itClientServices.next();
            
            // Custom code goes here
        }
    }

    //-----------------------------------------------------------------------------
    /**
    * Provides an example of accessing each runtime service.  Currently, there is 
    // only one.
    *///---------------------------------------------------------------------------
    public void accessRuntimeService()
    {
        Collection runtimeServices = this.retrieveRuntimeServices();
        
        Iterator itRuntimeServices = runtimeServices.iterator();
        
        while ( itRuntimeServices.hasNext() )
        {
            IRuntimeService aRuntimeService = (IRuntimeService) 
                                                          itRuntimeServices.next();
            
            // Custom code goes here
        }
    }
    
    //-----------------------------------------------------------------------------
    /**
    * Provides an example of accessing each session service.
    *///---------------------------------------------------------------------------
    public void accessSessionService()
    {
        Collection sessionServices = this.retrieveSessionServices();
        
        Iterator itSessionServices = sessionServices.iterator();
        
        while ( itSessionServices.hasNext() )
        {
            ISessionService aSessionService = (ISessionService) 
                                                          itSessionServices.next();
            
            // Custom code goes here
        }
    }
    
    //-----------------------------------------------------------------------------
    /**
    * Retrieve the client service.
    * 
    * @return  The client service.
    *///---------------------------------------------------------------------------
    public IClientService getClientService( final String clientId )
    {
        return this.getServiceManager().getClientService( clientId );
    }
   
    //-----------------------------------------------------------------------------
    /**
    * Retrieve the runtime service.
    * 
    * @return  The runtime service.
    *///---------------------------------------------------------------------------
    public IRuntimeService getRuntimeService()
    {
        return this.getServiceManager().getRuntimeService();
    }
    
    //-----------------------------------------------------------------------------
    /**
    * Retrieve the service manager.
    * 
    * @return  The service manager.
    *///---------------------------------------------------------------------------
    public IServiceManager getServiceManager()
    {
        return RcpRuntimePlugin.getDefault().getServiceManager();
    }

    //-----------------------------------------------------------------------------
    /**
    * Provides an example of retrieving a Collection of current application 
    * service objects.
    * 
    * @return  A Collection of current application service objects.
    *///---------------------------------------------------------------------------
    public Collection retrieveApplicationServices()
    {
        IServiceManager manager = this.getServiceManager();

        Collection applicationServices = manager.retrieveServiceEntries( 
                                                         ServiceType.APPLICATION );
        
        return applicationServices;
    }

    //------------------------------------------------------------------------------
    /**
    * Provides an example of retrieving a Collection of current client service 
    * objects.  Currently, there is only one.
    * 
    * @return  A Collection of current client service objects.
    *///----------------------------------------------------------------------------
    public Collection retrieveClientServices()
    {
        IServiceManager manager = this.getServiceManager();

        Collection clientServices = manager.retrieveServiceEntries( 
                                                             ServiceType.CLIENT );
        
        return clientServices;
    }

    //-----------------------------------------------------------------------------
    /**
    * Provides an example of retrieving a Collection of current runtime service 
    * objects.  Currently, there is only one.
    * 
    * @return  A Collection of current runtime service objects.
    *///---------------------------------------------------------------------------
    public Collection retrieveRuntimeServices()
    {
        IServiceManager manager = this.getServiceManager();

        Collection runtimeServices = manager.retrieveServiceEntries( 
                                                             ServiceType.RUNTIME );
        
        return runtimeServices;
    }
    
    //-----------------------------------------------------------------------------
    /**
    * Provides an example of retrieving a Collection of current session service 
    * objects.
    * 
    * @return  A Collection of current session service objects.
    *///---------------------------------------------------------------------------
    public Collection retrieveSessionServices()
    {
        IServiceManager manager = this.getServiceManager();

        Collection sessionServices = manager.retrieveServiceEntries( 
                                                             ServiceType.SESSION );
        
        return sessionServices;
    }
    //-----------------------------------------------------------------------------
    /**
    * Provides an example of turning off border rendering for all input fields. 
    * 
    * 
    *///---------------------------------------------------------------------------
    public int getControlClassStyle(class controlClass) 
    {
      if (controlClass.equals(Text.class)) 
      {
        return 0;// super.getControlClassStyle(controlClass);
       }
      else
      {
        return super.getControlClassStyle(controlClass);
    }
}

3270 印刷ジョブの listen

サービス・マネージャーを使用して プラグインが 3270 印刷ジョブ・イベントを listen できるようにする方法を 以下の例に示します。以下の変更は、RCP アプリケーション・プラグイン・クラスに対して行います。

  1. ServiceManagerListener、ISessionServiceListener、 および IPrintJobManagerChangeListener をインプリメントします。
  2. ServiceManagerListener メソッド public void serviceChanged(ServiceManagerEvent event) を インプリメントします。このメソッドは、サービス・マネージャーが 変化したときに呼び出されます。この例では、いつ新規セッションが作成または 破棄されたかを判別するために使用します。新規セッションが作成されるときには、 自分自身をリスナーとして追加します。セッションが破棄されるときには、 自分自身をリスナーから除去します。
  3. ISessionServiceListener メソッドをインプリメントします。
  4. IPrintJobManagerChangeListeners をインプリメントします。
  5. アプリケーション・プラグインの start() メソッドで、 自分自身をサービス・マネージャー・リスナーとして追加します。
  6. アプリケーション・プラグインの stop() メソッドで、 自分自身をサービス・マネージャー・リスナーから除去します。

package printExample;

import java.util.HashSet;

import org.eclipse.jface.resource.ImageDescriptor;
import org.osgi.framework.BundleContext;

import com.ibm.hats.rcp.runtime.RcpRuntimePlugin;
import com.ibm.hats.rcp.ui.AbstractRcpApplicationPlugin;
import com.ibm.hats.runtime.ApplicationSpecificInfo;
import com.ibm.hats.runtime.ClientSpecificInfo;
import com.ibm.hats.runtime.IPrintJobManagerChangeListener;
import com.ibm.hats.runtime.PrintJob;
import com.ibm.hats.runtime.PrintJobManager;
import com.ibm.hats.runtime.PrintResourceHandler;
import com.ibm.hats.runtime.PrintSpecificInfo;
import com.ibm.hats.runtime.events.CommandEvent;
import com.ibm.hats.runtime.events.ISessionServiceListener;
import com.ibm.hats.runtime.events.ServiceManagerEvent;
import com.ibm.hats.runtime.events.ServiceManagerListener;
import com.ibm.hats.runtime.events.StateChangeEvent;
import com.ibm.hats.runtime.services.IService;
import com.ibm.hats.runtime.services.IServiceManager;
import com.ibm.hats.runtime.services.ISessionService;
import com.ibm.hats.util.StateType;

/**
 * The activator class controls the plug-in life cycle
 */
public class PrintExamplePlugin extends AbstractRcpApplicationPlugin implements 
                               ServiceManagerListener, ISessionServiceListener, 
                               IPrintJobManagerChangeListener
{
    // The plug-in ID
    public static final String PLUGIN_ID = "PrintExample";

    // The shared instance
    private static PrintExamplePlugin plugin;
    
    // The service manager
    private IServiceManager serviceManager = null;
    
    // The print managers we are listeners for
    private HashSet printJobManagers = new HashSet();

    /**
     * The constructor
     */
    public PrintExamplePlugin()
    {
        plugin = this;
        
        System.out.println("PrintExamplePlugin: ctor");
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.ui.plugin.AbstractRcpApplicationPlugin#
     *                  start(org.osgi.framework.BundleContext)
     */
    public void start(BundleContext context) throws Exception
    {
        super.start(context);
        
        // Add the service manager listener
        serviceManager = RcpRuntimePlugin.getDefault().getServiceManager();
        serviceManager.addServiceManagerListener(this);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.ui.plugin.AbstractRcpApplicationPlugin#stop(org.osgi.
     *                                               framework.BundleContext)
     */
    public void stop(BundleContext context) throws Exception
    {
        plugin = null;
        
        // Remove the service manager listener
        if (serviceManager != null)
            serviceManager.removeServiceManagerListener(this);
        
        super.stop(context);
        
    }

    /**
     * Returns the shared instance
     * 
     * @return the shared instance
     */
    public static PrintExamplePlugin getDefault()
    {
        return plugin;
    }

    /**
     * Returns an image descriptor for the image file at the given plug-in
     * relative path
     * 
     * @param path
     *            the path
     * @return the image descriptor
     */
    public static ImageDescriptor getImageDescriptor(String path)
    {
        return imageDescriptorFromPlugin(PLUGIN_ID, path);
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.ibm.hats.runtime.IPrintJobManagerChangeListener#
     *         addPrintJob(com.ibm.hats.runtime.PrintJobManager,
     *         com.ibm.hats.runtime.PrintJob)
     */
    public void addPrintJob(PrintJobManager printJobManager, 
                                           PrintJob printJob)
    {
        System.out.println("PrintExamplePlugin: addPrintJob: 
                                     " + printJob.toString());
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.ibm.hats.runtime.IPrintJobManagerChangeListener#
     *      removePrintJob(com.ibm.hats.runtime.PrintJobManager,
     *      com.ibm.hats.runtime.PrintJob)
     */
    public void removePrintJob(PrintJobManager printJobManager, 
                                              PrintJob printJob)
    {
        System.out.println("PrintExamplePlugin: removePrintJob: " 
+ printJob.toString());
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.ibm.hats.runtime.IPrintJobManagerChangeListener#
     *      updatePrintJob(com.ibm.hats.runtime.PrintJobManager,
     *      com.ibm.hats.runtime.PrintJob)
     */
    public void updatePrintJob(PrintJobManager printJobManager, 
                                             PrintJob printJob)
    {
        System.out.println("PrintExamplePlugin: updatePrintJob: " 
                                           + printJob.toString());
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.ibm.hats.runtime.events.ServiceManagerListener#
     *      serviceChanged(com.ibm.hats.runtime.events.ServiceManagerEvent)
     */
    public void serviceChanged(ServiceManagerEvent event)
    {
        IService theService = event.getService();

        if (theService instanceof ISessionService)
        {
            ISessionService sessionService = (ISessionService) theService;

            int type = event.getType();
            if (type == ServiceManagerEvent.TYPE_SERVICE_CREATED)
            {
                sessionService.addSessionServiceListener(this);
            }
            else if (type == ServiceManagerEvent.TYPE_SERVICE_DESTROYED)
            {
                sessionService.removeSessionServiceListener(this);
            }
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.ibm.hats.runtime.events.SessionStateListener#
     *      sessionStateChanged(com.ibm.hats.runtime.events.SessionStateEvent)
     */
    public void sessionStateChanged(StateChangeEvent event)
    {
        // If the session has change to operational then add a listener to
        // the PrintJobManager (if there is one and we haven't already done so).
        StateType state = event.getNewState();
        if (state == StateType.OPERATIONAL)
        {
            ISessionService sessionService = event.getSessionService();
            ClientSpecificInfo csi = sessionService.getRuntimeService()
                 .getClientContainer().accessClient(sessionService.getClientId());

            if (csi != null)
            {
                String asiId = ApplicationSpecificInfo.
                                            createCompositeAsiId(sessionService.
                                            getApplicationId(), 
                                                      sessionService.getViewId());
                ApplicationSpecificInfo asi = csi.peekAll(asiId);
                if (asi != null)
                {
                    PrintSpecificInfo psi = asi.getPrint();
                    if (psi != null)
                    {
                        PrintResourceHandler prh = psi.getResourceHandler();
                        if (prh != null)
                        {
                            PrintJobManager pjm = prh.getPrintJobManager();
                            
                            // Add this PrintJobManager to our list of 
                            // PrintJobManagers.  
                            // This will return true if we haven't encountered 
                            // this PrintJobManager before.                            
                            // In this case, add ourselves as a 
                            // listener to this PrintJobManager.
                            if (printJobManagers.add(pjm))
                            {
                                pjm.addChangeListener(this);
                            }
                        }
                    }
                }
            }
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.ibm.hats.runtime.events.ISessionServiceListener#
     *      aboutToProcessCommand(com.ibm.hats.runtime.events.CommandEvent)
     */
    public void aboutToProcessCommand(CommandEvent event)
    {
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.ibm.hats.runtime.events.ISessionServiceListener#
     *      afterProcessCommand(com.ibm.hats.runtime.events.CommandEvent)
     */
    public void afterProcessCommand(CommandEvent event)
    {
    }
}

アクションの表示で使用するカスタム・コンポジットの作成

「コンポジットを表示」アクションで参照される SWT コンポジットは、com.ibm.hats.rcp.transform.IRenderable インターフェースをインプリメントする必要があります。SWT コンポジットが com.ibm.hats.rcp.transform.IRenderable をインプリメントしている場合、このコンポジットは特に、コンポジットが表示されるセッションの IsessionService を含む RcpContextAttributes にアクセスできます。ISessionService の sendContinue() メソッドは、HATS アプリケーションの実行を再開するために呼び出すことができます。

Visual Editor を使用してコンポジットを作成するには、以下を実行します (変換の編集を参照)。

  1. 「ファイル」>「新規」>「その他」>「Java™」>「ビジュアル・クラス」を選択して、「新規 Java ビジュアル・クラス」ウィザードを開始します。
  2. プラグイン・プロジェクトの src フォルダーを選択し、新規コンポジット・クラスの名前 (例えば、MyComposite) を入力します。
  3. 「スタイル」ツリーで、「SWT」>「コンポジット」を選択します。
  4. 「追加」をクリックして、com.ibm.hats.rcp.transform.IRenderable インターフェースを選択します。
  5. クラスを作成するには、「終了」をクリックします。

Visual Editor を使用して続行ボタンをコンポジットに追加するには、以下を実行します (変換の編集を参照)。

  1. 「パレット」ビューから、「SWT コントロール」ドロワーを開き、「ボタン」を選択します。
  2. ボタンをコンポジットにドラッグ・アンド・ドロップします。プロンプトが出たら、変数のデフォルト名を受諾します。
  3. 選択されたボタンを右クリックして「テキストの設定」を選択します。 ボタンの表題を入力します。例えば、「OK」または「サブミット」と入力します。「OK」をクリックします。
  4. ボタンを再度右クリックして「イベント」 > 「イベントの追加」を 選択します。「選択」>「widgetSelected」を選択し、「終了」をクリックします。
  5. widgetSelected(SelectionEvent) メソッドの一時コンテンツを、以下と置き換えます。
    	RcpContextAttributes contextAttributes = 
                              (RcpContextAttributes)getContextAttributes();
    	ISessionService sessionService = contextAttributes.getSessionService();
    	                                 sessionService.sendContinue();

注:
クラスのインポート・セクションを 更新する必要がある場合があります。これを行うには、CTRL+SHIFT+O、またはクラスのソースを右クリックし、「ソース」>「インポートの編成」を選択します。