This is an automated email from the ASF dual-hosted git repository. dashorst pushed a commit to branch archive/wicket-1.1.x in repository https://gitbox.apache.org/repos/asf/wicket.git
commit d5ad2ef5ee30d744c607f36ea66d7fc49e363a78 Author: Igor Vaynberg <[email protected]> AuthorDate: Wed Nov 23 08:13:05 2005 +0000 first pass on additional <select> components git-svn-id: https://svn.apache.org/repos/asf/incubator/wicket/branches/WICKET_1_1@458077 13f79535-47bb-0310-9956-ffa450edef68 --- .../markup/html/form/select/IOptionRenderer.java | 30 ++++ .../extensions/markup/html/form/select/Select.java | 159 +++++++++++++++++++++ .../markup/html/form/select/SelectOption.java | 124 ++++++++++++++++ .../markup/html/form/select/SelectOptions.java | 114 +++++++++++++++ 4 files changed, 427 insertions(+) diff --git a/wicket-extensions/src/java/wicket/extensions/markup/html/form/select/IOptionRenderer.java b/wicket-extensions/src/java/wicket/extensions/markup/html/form/select/IOptionRenderer.java new file mode 100644 index 0000000000..aaba735248 --- /dev/null +++ b/wicket-extensions/src/java/wicket/extensions/markup/html/form/select/IOptionRenderer.java @@ -0,0 +1,30 @@ +package wicket.extensions.markup.html.form.select; + +import wicket.model.IModel; + +/** + * @author Igor Vaynberg (ivaynberg) + * + */ +public interface IOptionRenderer +{ + /** + * Get the value for displaying to the user. + * + * @param object + * SelectOption model object + * @return the value for displaying to the user. + */ + public String getDisplayValue(Object object); + + /** + * Gets the model that will be used to represent the value object. + * + * This is a good place to wrap the value object with a detachable model one + * is desired + * + * @param value + * @return model that will contain the value object + */ + public IModel getModel(Object value); +} diff --git a/wicket-extensions/src/java/wicket/extensions/markup/html/form/select/Select.java b/wicket-extensions/src/java/wicket/extensions/markup/html/form/select/Select.java new file mode 100644 index 0000000000..ae754b7d4b --- /dev/null +++ b/wicket-extensions/src/java/wicket/extensions/markup/html/form/select/Select.java @@ -0,0 +1,159 @@ +/* + * $Id$ $Revision$ + * $Date$ + * + * ============================================================================== + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package wicket.extensions.markup.html.form.select; + +import java.util.Collection; + +import wicket.WicketRuntimeException; +import wicket.markup.html.WebMarkupContainer; +import wicket.markup.html.form.FormComponent; +import wicket.model.IModel; + +/** + * Component that represents a <code><select></code> box. Elements are + * provided by one or more <code>SelectChoice</code> or + * <code>SelectOptions</code> components in the hierarchy below the + * <code>Select</code> component. + * + * Advantages to the standard choice components is that the user has a lot more + * control over the markup between the <select> tag and its children + * <option> tags: allowing for such things as <optgroup> tags. + * + * TODO example + * + * @see SelectOption + * @see SelectOptions + * + * @author Igor Vaynberg ([email protected]) + * + */ +public class Select extends FormComponent +{ + /** + * + */ + private static final long serialVersionUID = 1L; + + /** + * Constructor that will create a default model collection + * + * @param id + * component id + */ + public Select(String id) + { + super(id); + } + + /** + * @see WebMarkupContainer#WebMarkupContainer(String, IModel) + */ + public Select(String id, IModel model) + { + super(id, model); + } + + /** + * @see FormComponent#updateModel() + */ + public void updateModel() + { + modelChanging(); + + Object object = getModelObject(); + boolean isModelCollection = object instanceof Collection; + + /* + * clear the model + */ + if (isModelCollection) + { + ((Collection)object).clear(); + } + else + { + getModel().setObject(this, null); + } + + /* + * the input contains an array of full path of the selected option + * components unless nothing was selected in which case the input + * contains null + */ + String[] paths = inputAsStringArray(); + + /* + * if the input is null we do not need to do anything since the model + * collection has already been cleared + */ + + if (paths != null && paths.length > 0) + { + if (!isModelCollection && paths.length > 1) + { + throw new WicketRuntimeException( + "The model of Select component [" + + getPath() + + "] is not of type java.util.Collection, but more then one SelectOption component has been selected. Either remove the multiple attribute from the select tag or make the model of the Select component a collection"); + } + + for (int i = 0; i < paths.length; i++) + { + String path = paths[i]; + + if (path != null) + { + /* + * option component path sans select component path = + * relative path from group to option since we know the + * option is child of select + */ + + path = path.substring(getPath().length() + 1); + + // retrieve the selected checkbox component + SelectOption option = (SelectOption)get(path); + + if (option == null) + { + throw new WicketRuntimeException( + "submitted http post value [" + + paths.toString() + + "] for SelectOption component [" + + getPath() + + "] contains an illegal relative path element [" + + path + + "] which does not point to an SelectOption component. Due to this the Select component cannot resolve the selected SelectOption component pointed to by the illegal value. A possible reason is that component hierarchy changed between rendering and form submission."); + } + + // assign the value + if (isModelCollection) + { + ((Collection)object).add(option.getModelObject()); + } + else + { + setModelObject(option.getModelObject()); + } + } + } + } + + modelChanged(); + } +} diff --git a/wicket-extensions/src/java/wicket/extensions/markup/html/form/select/SelectOption.java b/wicket-extensions/src/java/wicket/extensions/markup/html/form/select/SelectOption.java new file mode 100644 index 0000000000..a0f2acef30 --- /dev/null +++ b/wicket-extensions/src/java/wicket/extensions/markup/html/form/select/SelectOption.java @@ -0,0 +1,124 @@ +/* + * $Id$ $Revision$ + * $Date$ + * + * ============================================================================== + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package wicket.extensions.markup.html.form.select; + +import java.util.Collection; + +import wicket.Component; +import wicket.WicketRuntimeException; +import wicket.markup.ComponentTag; +import wicket.markup.html.WebMarkupContainer; +import wicket.model.IModel; + +/** + * Component representing a single <code><option></code> html element + * + * TODO example + * + * @see Select + * + * @author Igor Vaynberg ([email protected]) + * + */ +public class SelectOption extends WebMarkupContainer +{ + /** + * + */ + private static final long serialVersionUID = 1L; + + + /** + * @see WebMarkupContainer#WebMarkupContainer(String) + */ + public SelectOption(String id) + { + super(id); + } + + /** + * @see WebMarkupContainer#WebMarkupContainer(String, IModel) + */ + public SelectOption(String id, IModel model) + { + super(id, model); + } + + + /** + * @see Component#onComponentTag(ComponentTag) + * @param tag + * the abstraction representing html tag of this component + */ + protected void onComponentTag(final ComponentTag tag) + { + + // must be attached to <option .../> tag + checkComponentTag(tag, "option"); + + Select select = (Select)findParent(Select.class); + if (select == null) + { + throw new WicketRuntimeException( + "SelectOption component [" + + getPath() + + "] cannot find its parent Select. All SelectOption components must be a child of or below in the hierarchy of a Select component."); + } + + // assign name and value + tag.put("value", getPath()); + + // check if the model collection of the select contains the model object. + // if it does mark the option as selected + Object selected=select.getModelObject(); + + // check for npe in select's model object + if (selected == null) + { + throw new WicketRuntimeException( + "Select [" + + select.getPath() + + "] contains a null model object, must be an object of type java.lang.Object or java.util.Collection (in case of multi-valued select element"); + } + + boolean isSelected=false; + + Object value=getModelObject(); + + if (selected instanceof Collection) { + + if (value instanceof Collection) { + isSelected=selected.equals(value); + } else { + isSelected=((Collection)selected).contains(value); + } + } else { + isSelected=selected.equals(value); + } + + if (isSelected) + { + tag.put("selected", "true"); + } + + // Default handling for component tag + super.onComponentTag(tag); + } + + +} diff --git a/wicket-extensions/src/java/wicket/extensions/markup/html/form/select/SelectOptions.java b/wicket-extensions/src/java/wicket/extensions/markup/html/form/select/SelectOptions.java new file mode 100644 index 0000000000..675e7f5334 --- /dev/null +++ b/wicket-extensions/src/java/wicket/extensions/markup/html/form/select/SelectOptions.java @@ -0,0 +1,114 @@ +package wicket.extensions.markup.html.form.select; + +import java.util.Collection; +import java.util.Iterator; + +import wicket.WicketRuntimeException; +import wicket.extensions.markup.html.repeater.OrderedRepeatingView; +import wicket.markup.ComponentTag; +import wicket.markup.MarkupStream; +import wicket.markup.html.WebMarkupContainer; +import wicket.model.IModel; + +/** + * Component that makes it easy to produce a list of SelectOption components + * + * @author Igor Vaynberg (ivaynberg) + * + */ +public class SelectOptions extends OrderedRepeatingView +{ + private static final long serialVersionUID = 1L; + private boolean recreateChoices = false; + private IOptionRenderer renderer; + + /** + * Constructor + * + * @param id + * @param model + * @param renderer + */ + public SelectOptions(String id, IModel model, IOptionRenderer renderer) + { + super(id, model); + this.renderer = renderer; + setRenderBodyOnly(true); + } + + /** + * Controls whether or not SelectChoice objects are recreated every request + * + * @param refresh + * @return this for chaining + */ + public SelectOptions setRecreateChoices(boolean refresh) + { + this.recreateChoices = refresh; + return this; + } + + protected void onBeginRequest() + { + if (size() == 0 || recreateChoices) + { + // populate this repeating view with SelectOption components + removeAll(); + + Object modelObject = getModelObject(); + + if (modelObject != null) + { + if (!(modelObject instanceof Collection)) + { + // TODO + throw new WicketRuntimeException("model not a collection"); + } + + // iterator over model objects for SelectOption components + Iterator it = ((Collection)modelObject).iterator(); + + while (it.hasNext()) + { + // we need a container to represent a row in repeater + WebMarkupContainer row = new WebMarkupContainer(newChildId()); + row.setRenderBodyOnly(true); + add(row); + + // we add our actual SelectOption component to the row + Object value = it.next(); + String text = renderer.getDisplayValue(value); + IModel model = renderer.getModel(value); + row.add(new SimpleSelectOption("option", model, text)); + } + } + } + } + + private static class SimpleSelectOption extends SelectOption + { + + private String text; + + /** + * @param id + * @param model + * @param text + */ + public SimpleSelectOption(String id, IModel model, String text) + { + super(id, model); + this.text = text; + } + + protected void onComponentTagBody(MarkupStream markupStream, ComponentTag openTag) + { + replaceComponentTagBody(markupStream, openTag, text); + } + + + private static final long serialVersionUID = 1L; + + + } +}
