对于 Option 参数,CheckboxOptionControl 类会将 CARMA 缺省文本字段更改为一个复选框。因为此参数是一个“是/否”参数,所以使用复选框对于用户来说更方便。
- 在包资源管理器视图中,通过执行下列操作来打开您已创建的 CheckboxOptionControl 类:浏览 com.ibm.carma.plugin.howto > src > com.ibm.carma.plugin.howto.action,然后双击此类。
- 您将首先作为此类的实例数据来添加复选框按钮对象。这允许复选框按钮及其所有元数据可用于 CheckboxOptionControl 类。添加以下代码行:Button theButton;
- 接下来,您将覆盖 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;
}
- 现在,您将覆盖 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";
}
- 您需要覆盖的最后一个方法是 isUsingDefaultLabel 方法。如果设置为 false,那么此方法将不会显示您在将此参数添加至 RAM 时所提供的缺省标签。如果设置为 true,那么将正常显示标签。对于此样本,代码将使用缺省标签。 使用以下样本代码来覆盖此方法:
public boolean isUsingDefaultLabel()
{
return true;
}
- 最后,需要确保列示了所有必需的 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;
- 保存源代码,并调试存在的任何错误。
已为
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 check box 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;
}
}