java - AbstractReadonlyModel in wicket Framework -
package org.apache.wicket.examples.ajax.builtin; import java.util.arraylist; import java.util.arrays; import java.util.collections; import java.util.hashmap; import java.util.list; import java.util.map; import java.util.set; import org.apache.wicket.ajax.ajaxrequesttarget; import org.apache.wicket.ajax.form.ajaxformcomponentupdatingbehavior; import org.apache.wicket.markup.html.form.dropdownchoice; import org.apache.wicket.markup.html.form.form; import org.apache.wicket.model.abstractreadonlymodel; import org.apache.wicket.model.imodel; import org.apache.wicket.model.model; import org.apache.wicket.model.propertymodel; /** * linked select boxes example * * @author igor vaynberg (ivaynberg) */ public class choicepage extends basepage { private string selectedmake; private final map<string, list<string>> modelsmap = new hashmap<string, list<string>>(); // map:company->model /** * @return selected make */ public string getselectedmake() { return selectedmake; } /** * @param selectedmake * make selected */ public void setselectedmake(string selectedmake) { this.selectedmake = selectedmake; } /** * constructor. */ public choicepage() { modelsmap.put("audi", arrays.aslist(new string[] { "a4", "a6", "tt" })); modelsmap.put("cadillac", arrays.aslist(new string[] { "cts", "dts", "escalade", "srx", "deville" })); modelsmap.put("ford", arrays.aslist(new string[] { "crown", "escape", "expedition", "explorer", "f-150" })); imodel<list<? extends string>> makechoices = new abstractreadonlymodel<list<? extends string>>() { @override public list<string> getobject() { set<string> keys = modelsmap.keyset(); list<string> list = new arraylist<string>(keys); return list; } }; imodel<list<? extends string>> modelchoices = new abstractreadonlymodel<list<? extends string>>() { @override public list<string> getobject() { list<string> models = modelsmap.get(selectedmake); if (models == null) { models = collections.emptylist(); } return models; } }; form<?> form = new form("form"); add(form); final dropdownchoice<string> makes = new dropdownchoice<string>("makes", new propertymodel<string>(this, "selectedmake"), makechoices); final dropdownchoice<string> models = new dropdownchoice<string>("models", new model<string>(), modelchoices); models.setoutputmarkupid(true); form.add(makes); form.add(models); makes.add(new ajaxformcomponentupdatingbehavior("onchange") { @override protected void onupdate(ajaxrequesttarget target) { target.addcomponent(models); } }); }
}
what use 2 abstractreadonlymodel here?i couldnt understand logic of program.can plz understand one
these abstractreadonlymodels
used, because content must not changed.
the first model provides list of car brands, may selected via dropdownlist. second model returns dependent on selected brand list of car types, selectable via dropdownlist.
here wouldn't make sense alter list of brands or car types.
Comments
Post a Comment