001: /*
002: * $Id: ChoicePage.java 460265 2006-04-16 13:36:52Z jdonnerstag $ $Revision: 460265 $
003: * $Date: 2006-04-16 15:36:52 +0200 (Sun, 16 Apr 2006) $
004: *
005: * ==============================================================================
006: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
007: * use this file except in compliance with the License. You may obtain a copy of
008: * the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
014: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
015: * License for the specific language governing permissions and limitations under
016: * the License.
017: */
018: package wicket.examples.ajax.builtin;
019:
020: import java.util.ArrayList;
021: import java.util.Arrays;
022: import java.util.Collections;
023: import java.util.HashMap;
024: import java.util.List;
025: import java.util.Map;
026: import java.util.Set;
027:
028: import wicket.Component;
029: import wicket.ajax.AjaxRequestTarget;
030: import wicket.ajax.form.AjaxFormComponentUpdatingBehavior;
031: import wicket.markup.html.form.DropDownChoice;
032: import wicket.markup.html.form.Form;
033: import wicket.model.AbstractReadOnlyModel;
034: import wicket.model.IModel;
035: import wicket.model.Model;
036: import wicket.model.PropertyModel;
037:
038: /**
039: * Linked select boxes example
040: *
041: * @author Igor Vaynberg (ivaynberg)
042: */
043: public class ChoicePage extends BasePage {
044: private String selectedMake;
045:
046: private Map modelsMap = new HashMap(); // map:company->model
047:
048: /**
049: * @return Currently selected make
050: */
051: public String getSelectedMake() {
052: return selectedMake;
053: }
054:
055: /**
056: * @param selectedMake
057: * The make that is currently selected
058: */
059: public void setSelectedMake(String selectedMake) {
060: this .selectedMake = selectedMake;
061: }
062:
063: /**
064: * Constructor.
065: */
066: public ChoicePage() {
067: modelsMap.put("AUDI", Arrays.asList(new String[] { "A4", "A6",
068: "TT" }));
069: modelsMap.put("CADILLAC", Arrays.asList(new String[] { "CTS",
070: "DTS", "ESCALADE", "SRX", "DEVILLE" }));
071: modelsMap.put("FORD", Arrays.asList(new String[] { "CROWN",
072: "ESCAPE", "EXPEDITION", "EXPLORER", "F-150" }));
073:
074: IModel makeChoices = new AbstractReadOnlyModel() {
075: public Object getObject(Component component) {
076: Set keys = modelsMap.keySet();
077: List list = new ArrayList(keys);
078: return list;
079: }
080:
081: };
082:
083: IModel modelChoices = new AbstractReadOnlyModel() {
084: public Object getObject(Component component) {
085: List models = (List) modelsMap.get(selectedMake);
086: if (models == null) {
087: models = Collections.EMPTY_LIST;
088: }
089: return models;
090: }
091:
092: };
093:
094: Form form = new Form("form");
095: add(form);
096:
097: final DropDownChoice makes = new DropDownChoice("makes",
098: new PropertyModel(this , "selectedMake"), makeChoices);
099:
100: final DropDownChoice models = new DropDownChoice("models",
101: new Model(), modelChoices);
102: models.setOutputMarkupId(true);
103:
104: form.add(makes);
105: form.add(models);
106:
107: makes.add(new AjaxFormComponentUpdatingBehavior("onchange") {
108: protected void onUpdate(AjaxRequestTarget target) {
109: target.addComponent(models);
110: }
111: });
112: }
113: }
|