I'd like to contribute a new tool to the Velocity project.
I wrote this tool because I needed a simple way to generate
HTML dropdowns given Lists of business objects (I use it
with Torque)

Along with the tool there's a simple interface that for a
"value/description getter".

Here's an example of how this tool can be used:

  <p>select an address: </p>
  #set ($addrs = $addrPeer.doSelectAll())
  #set ($selAddr = $form.Address)
  #if (!$selAddr) #set($selAddr = "") #end 
  ##I do this because I'm having trouble passing a "null"
object in velocity
  $listTool.getDropdown($addrs, $addrPeer.AddressVDGetter,
$selAddr, "address")

This assumes that the peer class has a static instance of a
ValueDescriptionGetter (which can be implemented as an
anonymous class).

I hope this is a useful contribution :)

Regards,
Otto
/*
 * The Apache Software License, Version 1.1
 *
 * Copyright (c) 2001 The Apache Software Foundation.  All rights
 * reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. The end-user documentation included with the redistribution, if
 *    any, must include the following acknowlegement:
 *       "This product includes software developed by the
 *        Apache Software Foundation (http://www.apache.org/)."
 *    Alternately, this acknowlegement may appear in the software itself,
 *    if and wherever such third-party acknowlegements normally appear.
 *
 * 4. The names "The Jakarta Project", "Velocity", and "Apache Software
 *    Foundation" must not be used to endorse or promote products derived
 *    from this software without prior written permission. For written
 *    permission, please contact [EMAIL PROTECTED]
 *
 * 5. Products derived from this software may not be called "Apache"
 *    nor may "Apache" appear in their names without prior written
 *    permission of the Apache Group.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 * ====================================================================
 *
 * This software consists of voluntary contributions made by many
 * individuals on behalf of the Apache Software Foundation.  For more
 * information on the Apache Software Foundation, please see
 * <http://www.apache.org/>.
 */
 
package org.apache.velocity.tools.tools;

import java.util.*;

/**
 * <p>A tool which can be used within a velocity template to retrieve a
 * collection of business object as an HTML list or dropdown
 * </p>
 * @author <a href="mailto:[EMAIL PROTECTED]";>Otto von Wachter</a>
 * @version $Revision: 1.0 $ $Date: 2/12/03 $
 */
public class ListTool {

        /**
         * Builds a HTML list (i.e. &lt;select&gt; element), using an iterator as
         * the input, and the {@link ValueDescriptionGetter} interface to retrive
         * value attribute and the description (body of the tag). You specify the
         * initially selected value (the item for whcih getValue() property of
         * {@link ValueDescriptionGetter} is equal to selectedValue will be
         * selected), name, and the size (height) of the list (null or "0" indicates
         * a dropdown)
         * @return HTML as String
         */ 
        public String getList(Iterator iter, ValueDescriptionGetter nvGetter, 
        String selectedValue, String name, String size) {
                StringBuffer output = new StringBuffer();
                output.append("<select name='");
                output.append(name);
                output.append("'");
                if (size != null) {
                        output.append(" size='");
                        output.append(size);
                        output.append("'");
                }
                output.append(">\n");
                Object element;
                String value;
                String desc;
                for (; iter.hasNext();) {
                        element = iter.next();
                        value = nvGetter.getValue(element);
                        desc = nvGetter.getDescription(element);
                        output.append("  <option value='");
                        output.append(value);
                        output.append("'");
                        if (value.equals(selectedValue)) {
                                output.append(" selected>");
                        } else {
                                output.append(">");
                        }
                        output.append(desc);
                        output.append("</option>\n");
                }
                output.append("</select>");
            return output.toString();
        }

        /**
         * Builds HTML dropdown
         * @return HTML as String
     * @see ListTool#getList(Iterator iter, ValueDescriptionGetter nvGetter, 
     * String selectedValue, String name, String size)
         */ 
        public String getDropdown(Iterator iter, ValueDescriptionGetter nvGetter, 
        String selectedValue, String name) {
                return getList(iter, nvGetter, selectedValue, name, null);             
 
        }
        
        /**
         * Builds HTML list
         * @return HTML as String
         * @see ListTool#getList(Iterator iter, ValueDescriptionGetter nvGetter, 
         * String selectedValue, String name, String size)
         */ 
        public String getList(List list, ValueDescriptionGetter nvGetter, 
        String selectedValue, String name, String size) {
                Iterator iter = list.iterator();
                return getList(iter, nvGetter, selectedValue, name, size);
        }

        /**
         * Builds HTML dropdown
         * @return HTML as String
         * @see ListTool#getList(Iterator iter, ValueDescriptionGetter nvGetter, 
         * String selectedValue, String name, String size)
         */ 
        public String getDropdown(List list, ValueDescriptionGetter nvGetter, 
        String selectedValue, String name) {
                Iterator iter = list.iterator();
                return getList(iter, nvGetter, selectedValue, name, null);
        }
}
/*
 * The Apache Software License, Version 1.1
 *
 * Copyright (c) 2001 The Apache Software Foundation.  All rights
 * reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. The end-user documentation included with the redistribution, if
 *    any, must include the following acknowlegement:
 *       "This product includes software developed by the
 *        Apache Software Foundation (http://www.apache.org/)."
 *    Alternately, this acknowlegement may appear in the software itself,
 *    if and wherever such third-party acknowlegements normally appear.
 *
 * 4. The names "The Jakarta Project", "Velocity", and "Apache Software
 *    Foundation" must not be used to endorse or promote products derived
 *    from this software without prior written permission. For written
 *    permission, please contact [EMAIL PROTECTED]
 *
 * 5. Products derived from this software may not be called "Apache"
 *    nor may "Apache" appear in their names without prior written
 *    permission of the Apache Group.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 * ====================================================================
 *
 * This software consists of voluntary contributions made by many
 * individuals on behalf of the Apache Software Foundation.  For more
 * information on the Apache Software Foundation, please see
 * <http://www.apache.org/>.
 */

package org.apache.velocity.tools.tools;

import java.awt.Color;
import java.util.*;

import junit.framework.TestCase;

public class ListToolTest extends TestCase {

        List list;
        ListTool tool = new ListTool();
        public static final ValueDescriptionGetter COLOR_VD_GETTER = new 
ColorValueDescriptionGetter();
        
        public ListToolTest(String name) {
                super(name);
        }

        public void setUp() {
                list = new Vector();
                list.add(Color.yellow);
                list.add(Color.blue);
                list.add(Color.red);
        }
        
        public void testGetList() {
                //test List, with selected color blue
                StringBuffer expected = new StringBuffer();
                expected.append("<select name='colors' size='10'>\n");
                expected.append("  <option 
value='"+Color.yellow.getRGB()+"'>"+Color.yellow.toString()+"</option>\n");
                expected.append("  <option value='"+Color.blue.getRGB()+"' 
selected>"+Color.blue.toString()+"</option>\n");
                expected.append("  <option 
value='"+Color.red.getRGB()+"'>"+Color.red.toString()+"</option>\n");
                expected.append("</select>");
                String result = 
tool.getList(list,COLOR_VD_GETTER,Color.blue.getRGB()+"","colors","10");
                System.out.println("exp: "+expected);
                System.out.println("res: "+result);
                assertEquals(expected.toString(),result);       
        }

        public void testGetDropdown() {
                //test dropdown, with no selected color
                StringBuffer expected = new StringBuffer();
                expected.append("<select name='colors'>\n");
                expected.append("  <option 
value='"+Color.yellow.getRGB()+"'>"+Color.yellow.toString()+"</option>\n");
                expected.append("  <option 
value='"+Color.blue.getRGB()+"'>"+Color.blue.toString()+"</option>\n");
                expected.append("  <option 
value='"+Color.red.getRGB()+"'>"+Color.red.toString()+"</option>\n");
                expected.append("</select>");
                String result = tool.getDropdown(list,COLOR_VD_GETTER,"","colors");
                System.out.println("exp: "+expected);
                System.out.println("res: "+result);
                assertEquals(expected.toString(),result);       
        }
                
}

class ColorValueDescriptionGetter implements ValueDescriptionGetter {

        public String getValue(Object obj) {
                Color item = (Color) obj;
                return item.getRGB()+"";
        }

        public String getDescription(Object obj) {
                Color item = (Color) obj;
                return item.toString();
        }               
}
/*
 * The Apache Software License, Version 1.1
 *
 * Copyright (c) 2001 The Apache Software Foundation.  All rights
 * reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. The end-user documentation included with the redistribution, if
 *    any, must include the following acknowlegement:
 *       "This product includes software developed by the
 *        Apache Software Foundation (http://www.apache.org/)."
 *    Alternately, this acknowlegement may appear in the software itself,
 *    if and wherever such third-party acknowlegements normally appear.
 *
 * 4. The names "The Jakarta Project", "Velocity", and "Apache Software
 *    Foundation" must not be used to endorse or promote products derived
 *    from this software without prior written permission. For written
 *    permission, please contact [EMAIL PROTECTED]
 *
 * 5. Products derived from this software may not be called "Apache"
 *    nor may "Apache" appear in their names without prior written
 *    permission of the Apache Group.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 * ====================================================================
 *
 * This software consists of voluntary contributions made by many
 * individuals on behalf of the Apache Software Foundation.  For more
 * information on the Apache Software Foundation, please see
 * <http://www.apache.org/>.
 */

package org.apache.velocity.tools.tools;

/**
 * <p>Simple interface that knows how to return String representations for
 * value attribute and body of HTML &lt;option&gt; tag, given a domain object
 * </p>
 * @author <a href="mailto:[EMAIL PROTECTED]";>Otto von Wachter</a>
 * @version $Revision: 1.0 $ $Date: 2/12/03 $
 */
public interface ValueDescriptionGetter {
        
        /**
         * @return String representation of value attribute
         */ 
        public String getValue(Object obj) throws ClassCastException;
        
        /**
         * @return String representation of body of HTML &lt;option&gt; tag
         */ 
        public String getDescription(Object obj) throws ClassCastException;

}

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to