Rational Developer for System z


レッスン 7: ValueParamValidator クラス用のコードの作成

ValueParamValidator クラスは、HowTo アクションの Value パラメーターの検査を処理します。このレッスンでは、com.ibm.carma.ui.parameterValidators 拡張ポイントのパラメーターの検査と妥当性検査の機能を示すコードの作成方法を詳細に説明します。
このサンプルについて、ユーザーからの可能な入力と表示される通知メッセージのリストを次に示します。
  1. 「パッケージ・エクスプローラー」ビューで、com.ibm.carma.plugin.howto > src > com.ibm.carma.plugin.howto.action にナビゲートして、ValueParamValidator クラスをダブルクリックしてエディターで開きます。
  2. 最初に verifyInput メソッドを実装します。 このメソッドは、入力テキストで無効な文字がないかどうかを確認します。このサンプルでは、このメソッドは数字のみを受け入れます。次の疑似コードはこの例です。
    if the length of input is greater than 0
       then allow input if the input characters are 0-9
    else
       do not allow input
    次のサンプル・コードを使用して、verifyInput メソッドを実装します。
    /*Accept only numeric characters as valid. */
    public void verifyInput(ParameterValidationEvent event)
    {
       if(event.text.length() > 0)
          event.allowInput = (event.text.matches("[0-9]"));
    } 
  3. 次に、前述のとおりに、入力された値に応じて適切なメソッドを表示するように validateParameter メソッドをオーバーライドします。 次の疑似コードはこの例です。
    if input = 0
       return an informational message
    else if input = 1
       return a warning message
    else
       return an error message 
    次のサンプル・コードを使用して、このメソッドをオーバーライドします。
    /* Returns an informational message if 0 is entered,
     * Returns a warning message if 1 is entered,
     * Returns an error message if a numeric value greater than 1 is entered
     */
    public ValidationResult validateParameter(ParameterValidationevent event)
    {
       ValidationResult result = new ValidationResult();
       if(event.text.contains("0"))
       {
          //Display informational message.
          result.severity = ValidationResult.INFO;
          result.message = "You entered a 0!";
       }
       else if(event.text.contains("1"))
       {
          //Display a warning message
          result.severity = ValidationResult.WARNING;
          result.message = "Values greater than 1 will result in an error!";
       }
       else
       {
          //Display an error message.
          result.severity = ValidationResult.ERROR;
          result.message = "Value is too great, enter a lower value.";
       }
    
       return result;
    }
  4. ソースを保存し、すべてのエラーをデバッグします。

ご利用条件 | フィードバック



このインフォメーション・センターでは Eclipse テクノロジーが採用されています。(http://www.eclipse.org)