001    /*
002     * Databinder: a simple bridge from Wicket to Hibernate
003     * Copyright (C) 2006  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.Arrays;
022    import java.util.Collection;
023    import java.util.HashSet;
024    
025    import org.apache.wicket.markup.html.form.TextArea;
026    import org.apache.wicket.markup.html.form.TextField;
027    import org.apache.wicket.markup.html.panel.Panel;
028    import org.apache.wicket.model.IModel;
029    import org.apache.wicket.util.string.Strings;
030    
031    /**
032     * Panel for a comma-separated list of tag strings displayed in a text field or text area. The 
033     * backing model for this component must be a Set of String tags. Tags are converted to 
034     * lower case before insertion to the set.
035     */
036    public class TagField extends Panel {
037            
038            /** Generates text field */
039            public TagField(String id) {
040                    this(id, null);
041            }
042            /** Generates text field */
043            public TagField(String id, IModel model) {
044                    this(id, model, false);
045            }
046            /** @param textarea true to generate a text area */
047            public TagField(String id, boolean textarea) {
048                    this(id, null, textarea);
049            }
050            /** @param textarea true to generate a text area */
051            public TagField(String id, IModel model, boolean textarea) {
052                    super(id, model);
053                    IModel tagModel = new IModel() {
054                            public void detach() {}
055                            @SuppressWarnings("unchecked")
056                            public Object getObject() {
057                                    Collection<String> tags = (Collection<String>) TagField.this.getModelObject();
058                                    if (tags == null) return null;
059                                    return Strings.join(", ",  tags.toArray(new String[tags.size()]));
060                            }
061                            public void setObject(Object object) {
062                                    if (object == null)
063                                            TagField.this.setModelObject(new HashSet<String>());
064                                    else {
065                                            String value = ((String) object).toLowerCase();
066                                            String[] tagstrs = value.split(" *, *,* *"); // also consumes empty ' ,  ,' tags
067                                            TagField.this.setModelObject(new HashSet<String>(Arrays.asList(tagstrs)));
068                                    }
069                            }
070                    };
071                    add(new TextField("field", tagModel).setVisible(!textarea));
072                    add(new TextArea("area", tagModel).setVisible(textarea));
073            }
074    }