CheckboxOptionControl クラスは、CARMA のデフォルトのテキスト・フィールドを Option パラメーターのチェック・ボックスに変更します。
このパラメーターは、はいまたはいいえのパラメーターであるため、ユーザーにとってチェック・ボックスの方が便利です。
- 「パッケージ・エクスプローラー」ビューで、com.ibm.carma.plugin.howto > src > com.ibm.carma.plugin.howto.action にナビゲートして、作成した CheckboxOptionControl クラスをダブルクリックして開きます。
- 最初にクラスのインスタンス・データとして、チェック・ボックス・ボタン・オブジェクトを追加します。これにより、チェック・ボックス・ボタンとすべてのメタデータが、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 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;
}
}