Rational Developer for System z


课程 7:为 ValueParamValidator 类创建代码

ValueParamValidator 类将处理对 HowTo 操作中的 Value 参数进行验证。本课程将详细描述如何创建代码来演示 com.ibm.carma.ui.parameterValidators 扩展点的参数验证和确认功能。
对于此样本,以下是用户可能输入的值以及所显示的参考消息组成的列表。
  1. 包资源管理器视图中,通过执行下列操作来打开 ValueParamValidator 类:浏览 com.ibm.carma.plugin.howto > src > com.ibm.carma.plugin.howto.action,并双击此类以在编辑器中将它打开。
  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)