001    /*
002     * Databinder: a simple bridge from Wicket to Hibernate
003     * Copyright (C) 2007  Nathan Hamblen nathan@technically.us
004     *
005     * This library is free software; you can redistribute it and/or
006     * modify it under the terms of the GNU Lesser General Public
007     * License as published by the Free Software Foundation; either
008     * version 2.1 of the License, or (at your option) any later version.
009     * 
010     * This library is distributed in the hope that it will be useful,
011     * but WITHOUT ANY WARRANTY; without even the implied warranty of
012     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013     * Lesser General Public License for more details.
014     * 
015     * You should have received a copy of the GNU Lesser General Public
016     * License along with this library; if not, write to the Free Software
017     * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
018     */
019    package net.databinder.components;
020    
021    import java.util.Locale;
022    
023    import net.databinder.converters.URIConverter;
024    import net.databinder.validators.URIValidator;
025    
026    import org.apache.wicket.markup.html.form.TextField;
027    import org.apache.wicket.util.convert.IConverter;
028    import org.apache.wicket.util.string.Strings;
029    
030    /**
031     * TextField for URIs with a default scheme. This field establishes a default URI scheme
032     * and equates it to a null URI. For the UriTextField.Http subclass, the field is initialized with 
033     * "http://" and if unchanged the model will be left null. 
034     * @author Nathan Hamblen
035     */
036    public class UriTextField extends TextField {
037            
038            private String scheme;
039            
040            /** Costructor called by nested subclasses. */
041            private UriTextField(String id, String scheme) {
042                    super(id, java.net.URI.class);
043                    this.scheme = scheme;
044            }
045            
046            private String defaultValue() { return  scheme + "://"; }
047            
048            /**
049             * Return default value when null. Note that converter is not called from base if null.
050             */
051            @Override
052            protected String getModelValue() {
053                    String value = super.getModelValue();
054                    if (Strings.isEmpty(value))
055                            return defaultValue();
056                    return value;
057            }
058            
059            /** @return specialized converter that equates a default string value to null */
060            @Override
061            public IConverter getConverter(Class type) {
062                    return new URIConverter() {
063                            @Override
064                            public Object convertToObject(String value, Locale locale) {
065                                    if (value == null || value.equals(defaultValue()))
066                                                    return null;
067                                    return super.convertToObject(value, locale);
068                            }
069                    };
070            }
071            
072            public static class Http extends UriTextField {
073                    public Http(String id) {
074                            super(id, "http");
075                            add(URIValidator.HttpScheme());
076                    }
077            }
078            
079            public static class Ftp extends UriTextField {
080                    public Ftp(String id) {
081                            super(id, "ftp");
082                            add(URIValidator.FtpScheme());
083                    }
084            }
085    }