The CheckboxOptionControl
class changes
the CARMA default text field to a checkbox on the Option parameter.
Since this parameter is a yes or no parameter, a checkbox is more
convenient for the user.
Procedure
- In the Package Explorer view, open
the
CheckboxOptionControl
class you created by navigating com.ibm.carma.plugin.howto
> src
> com.ibm.carma.plugin.howto.action
and
double-clicking on the class.
- You will first add the checkbox button object as instance
data for the class. This allows the checkbox button and all its metadata
to be available to the
CheckboxOptionControl
class.
Add the following line of code: Button theButton;
- Next, you override the
createControl
method
to create the checkbox button and return it instead of the default
text field. The following pseudocode demonstrates what you want this
method to do: Create theButton with the checkbox style
Give the theButton some text
Return the theButton
Use the following sample code to override
this method:
/* 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;
}
- Now, you override the
getValue
method.
This method returns the value of the parameters. Since you are using
a checkbox instead of a text field, the code you write will have
to translate the check or unchecked status of the checkbox into the
expected string format. The following pseudocode demonstrates this:
if the checkbox is checked
return "Y" for yes
else
return "N" for no
Use the following sample code to override
this method:
public Object getValue() {
if(theButton.getSelection())
return "Y";
else
return "N";
}
- The last method that you need to override is the
isUsingDefaultLabel
method.
If set to false, this method will not display the default label that
you provided when you added the parameter to the RAM. If set to true,
then the label is displayed like normal. For this sample, the code
will use the default label. Use the following sample code
to override the method:
public boolean isUsingDefaultLabel()
{
return true;
}
- Finally, you need to ensure that all required imports are
listed. Add any of the following import statements that are not listed
in your class:
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.*;
import com.ibm.carma.model.*;
import com.ibm.carma.ui.action.custom.AbstractCustomParameterControl;
- Save the source and debug any errors.
Results
Your completed code for the CheckBoxOptionControl
should
look like: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;
}
}