2008年10月6日月曜日

Click FrameworkのCheckBoxコントロールをグループ化するコントロールを作成する

デフォルトではRadioGroupコントロールをグループ化するコントロールとしてRadioGroupコントロールが存在しますがCheckBoxについてはありません。ですのでRadioGroupコントロールを参考に作成しましょう。
面倒なのでJavaScriptによるバリデーションはなにも実装しません。
実装時に利用者が個々に実装すればよいでしょう(「2つチェックボックス選択が必須」等)。

追加機能としてチェックボックスのラベル表示位置を設定するメソッドとそのGetter,Setterを追加した。
もともとのRadioGroupではラベルの表示位置設定がないのでこまったので。
/*
*
*/

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import net.sf.click.control.Checkbox;
import net.sf.click.control.Field;
import net.sf.click.control.Form;
import net.sf.click.util.HtmlStringBuffer;
import net.sf.click.util.PropertyUtils;

public class CheckBoxGroup extends Field {

private static final long serialVersionUID = 1L;

protected List checkBoxList;

protected boolean isVerticalLayout = true;

protected int labelPosition = LABEL_POSITION_RIGHT;

protected static final int LABEL_POSITION_NONE = 0;
protected static final int LABEL_POSITION_LEFT = 1;
protected static final int LABEL_POSITION_RIGHT = 2;

public CheckBoxGroup(String name) {
super(name);
}

public CheckBoxGroup(String name, boolean required) {
super(name);
setRequired(required);
}

public CheckBoxGroup(String name, String label) {
super(name, label);
}

public CheckBoxGroup(String name, String label, boolean required) {
super(name, label);
setRequired(required);
}

public CheckBoxGroup() {
super();
}

public void add(Checkbox checkBox) {
if (checkBox == null) {
throw new IllegalArgumentException("Null checkbox parameter");
}

checkBox.setParent(this);
getCheckBoxList().add(checkBox);
if (getForm() != null) {
checkBox.setForm(getForm());
}
}

public void addAll(Collection options) {
if (options == null) {
String msg = "options parameter cannot be null";
throw new IllegalArgumentException(msg);
}
for (Iterator i = options.iterator(); i.hasNext();) {
Checkbox checkBox = (Checkbox) i.next();
add(checkBox);
}
}

public void addAll(Map options) {
if (options == null) {
String msg = "options parameter cannot be null";
throw new IllegalArgumentException(msg);
}
for (Iterator i = options.entrySet().iterator(); i.hasNext();) {
Map.Entry entry = (Map.Entry) i.next();
Checkbox checkBox = new Checkbox(entry.getKey().toString(),
entry.getValue().toString());
add(checkBox);
}
}

public void addAll(Collection objects, String value, String label) {
if (objects == null) {
String msg = "objects parameter cannot be null";
throw new IllegalArgumentException(msg);
}
if (value == null) {
String msg = "value parameter cannot be null";
throw new IllegalArgumentException(msg);
}
if (label == null) {
String msg = "label parameter cannot be null";
throw new IllegalArgumentException(msg);
}

if (objects.isEmpty()) {
return;
}

Map cache = new HashMap();

for (Iterator i = objects.iterator(); i.hasNext();) {
Object object = i.next();

try {
Object valueResult = PropertyUtils.getValue(object, value, cache);
Object labelResult = PropertyUtils.getValue(object, label, cache);

Checkbox checkBox = null;

if (labelResult != null) {
checkBox = new Checkbox(valueResult.toString(), labelResult.toString());

} else {
checkBox = new Checkbox(valueResult.toString());
}

add(checkBox);

} catch (Exception e) {
throw new RuntimeException(e);
}
}
}

public void setForm(Form form) {
super.setForm(form);
if (hasCheckBoxes()) {
for (int i = 0, size = getCheckBoxList().size(); i < checkbox =" (Checkbox)" isverticallayout =" vertical;" checkboxlist ="="" checkboxlist =" new" i =" 0," size =" getCheckBoxList().size();" checkbox =" (Checkbox)" continueprocessing =" true;" i =" 0," size =" getCheckBoxList().size();" checkbox =" (Checkbox)" continueprocessing =" checkBox.onProcess();" i =" 0," size =" getCheckBoxList().size();" checkbox =" (Checkbox)" size =" getCheckBoxList().size();" buffer =" new" value =" getValue();" i =" 0;" checkbox =" (Checkbox)"> 0) {
if (checkBox.getValue().equals(value)) {
checkBox.setChecked(true);
} else {
checkBox.setChecked(false);
}
}

if (labelPosition != LABEL_POSITION_NONE && labelPosition == LABEL_POSITION_LEFT) {

buffer.elementStart("label");
buffer.appendAttribute("for", checkBox.getId());
buffer.closeTag();
buffer.appendEscaped(checkBox.getLabel());
buffer.elementEnd("label");

}

buffer.append(checkBox.toString());

if (labelPosition != LABEL_POSITION_NONE && labelPosition == LABEL_POSITION_RIGHT) {

buffer.elementStart("label");
buffer.appendAttribute("for", checkBox.getId());
buffer.closeTag();
buffer.appendEscaped(checkBox.getLabel());
buffer.elementEnd("label");

}

if (isVerticalLayout() && (i < size - 1)) {                 buffer.append("
");
}

}

return buffer.toString();
}

public void validate() {
setError(null);

if (isRequired() && getValue().length() == 0) {
setErrorMessage("select-error");
}
}

public int getLabelPosition() {
return labelPosition;
}

public void setLabelPosition(int labelPosition) {
this.labelPosition = labelPosition;
}



}
以上

0 件のコメント:

コメントを投稿