jQuery mobile CheckBox の、Wicket での表現、
先日の複数チェックボックスのコードを、もっとコンパクトに汎用性を持つようにする。
<div data-role="fieldcontain">
<fieldset data-role="controlgroup" data-type="horizontal">
<span wicket:id="choice">
<input type="checkbox" wicket:id="chkbox"/><label wicket:id="label"></label>
</span>
</fieldset>
</div>
wicket:id="choice"に ListView 継承クラスとして用意する JqmCheckListView を生成する
ListView<PChoice> view = new JqmCheckListView<PChoice>(
"view","chkbox","label",defineList,targetList,selectedList));
// PChoice は、label タグで表示するラベル名と任意の総称型のインスタンスで表現するクラスで
// equals(java.lang.Object) と、hashCode() を実装する
// defineList = 全ての選択要素の宣言リスト→ List<PChoice>
// targetList = submit後に選択結果が格納される サイズ=0のCollection インスタンス、→Collection<PChoice>
// selectedList = 画面表示初期状態で、選択済みの選択要素リストで省略可能、→ List<PChoice>
-----------------------------------------
import java.util.Collection;
import java.util.List;
import org.apache.wicket.AttributeModifier;
import org.apache.wicket.markup.ComponentTag;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.CheckBox;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
public class JqmCheckListView extends ListView<PChoice>{
private Collection<PChoice> targets;
private List<PChoice> selectedlist;
private String chkboxId;
private String labelId;
private int ix=0;
/**
* コンストラクタ.
* @param id ListView ID
* @param chkboxId CheckBox ID
* @param labelId labelタグ ID
* @param defineList 全ての選択要素の宣言リスト
* @param targets submit後に選択結果が格納される サイズ=0のCollection インスタンス
* @param selectedlist 画面表示初期状態で、選択済みの選択要素リスト
*/
public JqmCheckListView(String id,String chkboxId,String labelId
,List<PChoice> defineList
,Collection<PChoice> targets
,List<PChoice> selectedlist){
super(id,defineList);
this.chkboxId = chkboxId;
this.labelId = labelId;
this.targets = targets;
this.selectedlist = selectedlist;
}
public JqmCheckListView(String id,String chkboxId,String labelId
,List<PChoice> defineList
,Collection<PChoice> targets){
super(id,defineList);
this.chkboxId = chkboxId;
this.labelId = labelId;
this.targets = targets;
}
@Override
protected void populateItem(ListItem<PChoice> item){
PChoice choice = item.getModelObject();
final String idAttr = this.getIdAttr();
CheckBox chk = new CheckBox(this.chkboxId
,new SelectItemUsingCheckboxModel<PChoice>(choice,this.targets));
chk.add(new AttributeModifier("id",idAttr));
chk.add(new AttributeModifier("data-theme",getTheme()));
if (this.isSelected(choice)){
chk.add(new AttributeModifier("checked","checked"));
}
item.add(chk);
item.add(new Label(this.labelId,choice.getLabel()){
@Override
protected void onComponentTag(ComponentTag tag){
tag.put("for",idAttr);
}
});
this.ix++;
item.setRenderBodyOnly(true);
}
private boolean isSelected(PChoice c){
if (this.selectedlist==null) return false;
for(PChoice e : this.selectedlist){
if (e.equals(c)){
return true;
}
}
return false;
}
private String getIdAttr(){
return getId()+"_"+Integer.toString(this.ix);
}
public String getTheme(){
return "c";
}
}
----------------------------------
import java.io.Serializable;
/**
* PChoice<T>
* T は、equals(java.lang.Object) と、hashCode() を実装すること
*/
public class PChoice<T> implements Serializable{
private T t;
private String label;
public PChoice(T t,String label){
this.t = t;
this.label = label;
}
public String getLabel(){
return this.label;
}
public T get(){
return this.t;
}
@Override
public boolean equals(Object obj){
if (obj==this) return true;
if (!(obj instanceof PChoice)) return false;
PChoice c = (PChoice)obj;
if (this.t.equals(c.get()) && this.label.equals(c.getLabel())){
return true;
}
return false;
}
@Override
public int hashCode(){
int hash = 1;
hash = hash * 17 + this.t.hashCode();
hash = hash * 17 + (this.t == null ? 0 : this.t.hashCode());
hash = hash * 17 + this.label.hashCode();
hash = hash * 17 + (this.label == null ? 0 : this.label.hashCode());
return hash;
}
}
----------------------------
import java.util.Collection;
import org.apache.wicket.extensions.model.AbstractCheckBoxModel;
/**
* SelectItemUsingCheckboxModel
*/
public class SelectItemUsingCheckboxModel<T> extends AbstractCheckBoxModel{
private T t;
private Collection<T> selection;
public SelectItemUsingCheckboxModel(T t,Collection<T> selection){
this.t = t;
this.selection = selection;
}
@Override
public boolean isSelected(){
return this.selection.contains(this.t);
}
@Override
public void select(){
this.selection.add(this.t);
}
@Override
public void unselect(){
this.selection.remove(this.t);
}
}