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.markup.html.form;
018
019import org.apache.wicket.markup.ComponentTag;
020import org.apache.wicket.model.IModel;
021
022/**
023 * A simple text field.
024 * 
025 * @author Jonathan Locke
026 * 
027 * @param <T>
028 *            The model object type
029 */
030public class TextField<T> extends AbstractTextComponent<T>
031{
032        private static final long serialVersionUID = 1L;
033
034        /**
035         * @see org.apache.wicket.Component#Component(String)
036         */
037        public TextField(final String id)
038        {
039                this(id, null, null);
040        }
041
042        /**
043         * @param id
044         *            See Component
045         * @param type
046         *            Type for field validation
047         */
048        public TextField(final String id, final Class<T> type)
049        {
050                this(id, null, type);
051        }
052
053        /**
054         * @param id
055         * @param model
056         * @see org.apache.wicket.Component#Component(String, IModel)
057         */
058        public TextField(final String id, final IModel<T> model)
059        {
060                this(id, model, null);
061        }
062
063        /**
064         * @param id
065         *            See Component
066         * @param model
067         *            See Component
068         * @param type
069         *            The type to use when updating the model for this text field
070         * @see org.apache.wicket.Component#Component(String, IModel)
071         */
072        public TextField(final String id, IModel<T> model, Class<T> type)
073        {
074                super(id, model);
075                setType(type);
076
077                // don't double encode the value. it is encoded by ComponentTag.writeOutput()
078                setEscapeModelStrings(false);
079        }
080
081        /**
082         * Processes the component tag.
083         * 
084         * @param tag
085         *            Tag to modify
086         * @see org.apache.wicket.Component#onComponentTag(ComponentTag)
087         */
088        @Override
089        protected void onComponentTag(final ComponentTag tag)
090        {
091                // Must be attached to an input tag
092                checkComponentTag(tag, "input");
093
094                // check for text type
095                String[] inputTypes = getInputTypes();
096                if (inputTypes != null)
097                {
098                        checkComponentTagAttribute(tag, "type", inputTypes);
099                }
100                else
101                {
102                        if (tag.getAttributes().containsKey("type"))
103                        {
104                                checkComponentTagAttribute(tag, "type", "text");
105                        }
106                }
107
108                tag.put("value", getValue());
109
110                // Default handling for component tag
111                super.onComponentTag(tag);
112        }
113
114        /**
115         * Subclass should override this method if this textfield is mapped on a different input type as
116         * text. Like PasswordTextField or HiddenField.
117         * 
118         * @return The input type of this textfield, default is null
119         */
120        protected String[] getInputTypes()
121        {
122                return null;
123        }
124}