课程 9:为 CheckboxOptionControl 类创建代码

CheckboxOptionControl 类将 CARMA 缺省文本字段更改为 "选项" 参数上的复选框。 由于此参数是 yes 或 no 参数,因此复选框对用户更方便。

过程

  1. 在 " 包资源管理器 " 视图中,打开通过浏览 com.ibm.carma.plugin.howto > src > com.ibm.carma.plugin.howto.action 并双击该类创建的 CheckboxOptionControl 类。
  2. 您将首先添加复选框按钮对象作为类的实例数据。 这允许复选框按钮及其所有元数据可用于 CheckboxOptionControl 类。 添加以下代码行: Button theButton;
  3. 接下来,您将覆盖 createControl 方法以创建复选框按钮并将其返回,而不是返回缺省文本字段。 以下伪码演示您想要此方法执行的操作:
    Create theButton with the checkbox style
    Give the theButton some text
    Return the theButton
    使用以下样本代码来覆盖此方法:
    /* Create a checkbox for the yes/no option */
    public Control createControl(Composite parent, 
                                 RepositoryManager repositoryManager,
                                 Parameter param, 
                                 Action action,
                                 CustomActionAccepter custActionAccepter,
                                 Object defaultValue) 
    {
       theButton = new Button(parent, SWT.CHECK);
       theButton.setText("Check me!");
       return theButton;
    }
  4. 现在,您将覆盖 getValue 方法。 此方法将返回参数的值。 由于您使用的是复选框而不是文本字段,因此您编写的代码必须将复选框的检查或未检查状态转换为期望的字符串格式。 以下伪码演示了此情况:
    if the checkbox is checked
    	return "Y" for yes
    else
    	return "N" for no
    使用以下样本代码来覆盖此方法:
    public Object getValue() 	{
    		if(theButton.getSelection())
    			return "Y";
    		else
    			return "N";
    	}
  5. 需要覆盖的最后一个方法是 isUsingDefaultLabel 方法。 如果设置为 false,那么此方法将不会显示您在将此参数添加至 RAM 时所提供的缺省标签。 如果设置为 true,那么将正常显示标签。 对于此样本,代码将使用缺省标签。
    使用以下样本代码来覆盖此方法:
    public boolean isUsingDefaultLabel()
    {
       return true;
    }
  6. 最后,需要确保列示了所有必需的 import 语句。 请添加您的类中未列示的下列任何 import 语句:
    import org.eclipse.swt.SWT;
    import org.eclipse.swt.widgets.*;
    
    import com.ibm.carma.model.*;
    
    import com.ibm.carma.ui.action.custom.AbstractCustomParameterControl;
  7. 保存源代码,并调试存在的任何错误。

结果

已为 CheckBoxOptionControl 创建的代码看起来应当类似于:
package com.ibm.carma.plugin.howto.action;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.*;

import com.ibm.carma.model.*;

import com.ibm.carma.ui.action.custom.AbstractCustomParameterControl;

public class CheckboxOptionAction extends AbstractCustomParameterControl {
   //Add the button to the instance data
   Button theButton;
   
   /* Create a checkbox for the yes/no option */
   public Control createControl(Composite parent, 
                                RepositoryManager repositoryManager,
                                Parameter param, 
                                Action action, 
                                CustomActionAccepter customActionAccepter, 
                                Object defaultValue) 	{
      theButton = new Button(parent, SWT.CHECK);
      theButton.setText("Check me!");
      return theButton;
   }

   @Override
   public Object getValue() 	{
      if(theButton.getSelection())
	      return "Y";
      else
         return "N";
   }

   @Override 
   public boolean isUsingDefaultLabel() 	{
      return true;
   }
}