001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.wicket.extensions.markup.html.form.select;
018
019import org.apache.wicket.Component;
020import org.apache.wicket.IGenericComponent;
021import org.apache.wicket.WicketRuntimeException;
022import org.apache.wicket.markup.ComponentTag;
023import org.apache.wicket.markup.html.WebMarkupContainer;
024import org.apache.wicket.model.IModel;
025
026/**
027 * Component representing a single <code>&lt;option&gt;</code> html element
028 *
029 * @see Select
030 * @param <T>
031 * 
032 * @author Igor Vaynberg
033 */
034public class SelectOption<T> extends WebMarkupContainer implements IGenericComponent<T, SelectOption<T>>
035{
036        private static final long serialVersionUID = 1L;
037
038        /**
039         * page-scoped uuid of this option. this property must not be accessed directly, instead
040         * {@link #getValue()} must be used
041         */
042        private int uuid = -1;
043
044        /**
045         * @see WebMarkupContainer#WebMarkupContainer(String)
046         */
047        public SelectOption(final String id)
048        {
049                super(id);
050        }
051
052        /**
053         * @param id
054         * @param model
055         * @see WebMarkupContainer#WebMarkupContainer(String, IModel)
056         */
057        public SelectOption(final String id, final IModel<T> model)
058        {
059                super(id, model);
060        }
061
062        /**
063         * Form submission value used for this select option. This string will appear as the value of
064         * the <code>value</code> html attribute for the <code>option</code> tag.
065         * 
066         * @return form submission value
067         */
068        public String getValue()
069        {
070                if (uuid < 0)
071                {
072                        uuid = getPage().getAutoIndex();
073                }
074                return "option" + uuid;
075        }
076
077        /**
078         * @see Component#onComponentTag(ComponentTag)
079         * @param tag
080         *            the abstraction representing html tag of this component
081         */
082        @Override
083        protected void onComponentTag(final ComponentTag tag)
084        {
085                // must be attached to <option .../> tag
086                checkComponentTag(tag, "option");
087
088                Select<?> select = findParent(Select.class);
089                if (select == null)
090                {
091                        throw new WicketRuntimeException(
092                                "SelectOption component [" +
093                                        getPath() +
094                                        "] cannot find its parent Select. All SelectOption components must be a child of or below in the hierarchy of a Select component.");
095                }
096
097                final String uuid = getValue();
098
099                // assign value
100                tag.put("value", uuid);
101
102                if (select.isSelected(this))
103                {
104                        tag.put("selected", "selected");
105                }
106
107                // Default handling for component tag
108                super.onComponentTag(tag);
109        }
110}