SWT スライダー・ウィジェットのホスト入力フィールドへの結合
スライダーは、ユーザーが値の範囲から数値を選択できるようにするコントロールです。例えば、OS/400® のメインメニューでは、スライダーを使用して選択を行うことができます。
パレットを使用して、SWT スライダー・ウィジェットを変換に追加します。RcpTransformation クラスの postRender() メソッドをオーバーライドします。postRender() メソッドに、以下のコードを追加します。
// The following code creates 2 adapters : a FieldAdapter that adapts to a
// Host Screen field located at a specific position, an IControlAdapter that
// adapts to a Slider widget, and bind these 2 adapters so that when slider
// selection is changed, its selection value is updated in the model, and
// when the data in the model is changed, the slider selection is updated.
// In this example, we bind to the a host screen field located at
// position (20,007).
final RcpContextAttributes attrs = (RcpContextAttributes)
getContextAttributes();
HostScreenDataModelManager dataModelManager = attrs.
getHostScreenDataModelManager();
HostScreenModelAdapterFactory factory = (HostScreenModelAdapterFactory)
dataModelManager.getModelAdapterFactory();
final IModelAdapter modelAdapter = factory.getFieldAdapter(1527);
IControlAdapter sliderAdapter = new SliderWidgetAdapter(slider,dataModelManager);
dataModelManager.bindControlToModel(sliderAdapter, modelAdapter);
SliderWidgetAdapter クラスの例
//This class allows the slider widget to update a host field with its values and
//also update its value from a host field.
public class SliderWidgetAdapter implements IControlAdapter {
private Slider slider; //the target slider widget.
private IDataModelManager dataModelManager; //handles updating values in the model
public SliderWidgetAdapter(Slider slider,IDataModelManager dataModelManager) {
this.slider = slider;
this.dataModelManager = dataModelManager;
//create a SelectionListener such that when the value of the slider
//is changed, it updates the host field.
this.slider.addSelectionListener(new SelectionListener()
{
public void widgetDefaultSelected(SelectionEvent event) {
}
public void widgetSelected(SelectionEvent event) {
updateModel();
}
});
}
//this method calls the updateModel from the IDataModelManager to
//update the host field.
private void updateModel() {
if ( dataModelManager != null )
dataModelManager.updateModel(this);
}
//return the value of slider selection.
public String getValue() {
return slider.getSelection() + "";
}
//this method updates the slider selection from a value. This method
// is called when the value of a host field is changed.
public void setValue(String value) {
try {
slider.setSelection(Integer.parseInt(value));
}
catch ( NumberFormatException e ) {
slider.setSelection(0);
}
}
}