jQuery mobile の Radio button を Wicket で、表示する場合、
Wicket の RadioGroup を使うのが判り易くよいのだが、
org.apache.wicket.markup.html.form.Radio を継承するものを用意するともっと綺麗になる。
import org.apache.wicket.AttributeModifier;
import org.apache.wicket.markup.html.form.Radio;
import org.apache.wicket.model.IModel;
/**
* JqmRadio for jQuery mobile Radio.
*/
public class JqmRadio<T> extends Radio<T>{
public JqmRadio(String id,IModel<T> model){
super(id,model);
add(new AttributeModifier("data-theme",getTheme()));
}
public String getTheme(){
return "c";
}
@Override
public String getMarkupId(){
return getId();
}
}
-------------------------
import java.io.Serializable;
/**
* PChoice
*/
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;
}
}
------------------------------
<div data-role="fieldcontain">
<fieldset data-role="controlgroup">
<span wicket:id="colors1">
<legend>Choose Color1:</legend>
<input type="radio" wicket:id="red1"/> <label for="red1">Red</label>
<input type="radio" wicket:id="green1"/><label for="green1">Green</label>
<input type="radio" wicket:id="blue1"/> <label for="blue1">Blue</label>
</span>
</fieldset>
</div>
<div data-role="fieldcontain">
<fieldset data-role="controlgroup" data-type="horizontal">
<span wicket:id="colors2">
<legend>Choose Color2:</legend>
<input type="radio" wicket:id="red2"/> <label for="red2">R</label>
<input type="radio" wicket:id="green2"/><label for="green2">G</label>
<input type="radio" wicket:id="blue2"/> <label for="blue2">B</label>
</span>
</fieldset>
</div>
------------------------------------
RadioGroup に渡す Model
PChoice<Integer> selectColor1 = new PChoice<Integer>(4,"Red");
PChoice<Integer> selectColor2;
RadioGroup<PChoice<Integer>> colors1
= new RadioGroup<PChoice<Integer>>("colors1"
,new PropertyModel<PChoice<Integer>>(this,"selectColor1"));
colors1.add(new JqmRadio<PChoice<Integer>>("red1"
,new Model<PChoice<Integer>>(new PChoice<Integer>(4,"Red"))));
colors1.add(new JqmRadio<PChoice<Integer>>("green1"
,new Model<PChoice<Integer>>(new PChoice<Integer>(2,"Green"))));
colors1.add(new JqmRadio<PChoice<Integer>>("blue1"
,new Model<PChoice<Integer>>(new PChoice<Integer>(1,"Blue"))));
RadioGroup<PChoice<Integer>> colors2
= new RadioGroup<PChoice<Integer>>("colors2"
,new PropertyModel<PChoice<Integer>>(this,"selectColor2"));
colors2.add(new JqmRadio<PChoice<Integer>>("red2"
,new Model<PChoice<Integer>>(new PChoice<Integer>(4,"Red"))));
colors2.add(new JqmRadio<PChoice<Integer>>("green2"
,new Model<PChoice<Integer>>(new PChoice<Integer>(2,"Green"))));
colors2.add(new JqmRadio<PChoice<Integer>>("blue2"
,new Model<PChoice<Integer>>(new PChoice<Integer>(1,"Blue"))));