    001package org.apache.turbine.util.template;
    002
    003
    004/*
    005 * Licensed to the Apache Software Foundation (ASF) under one
    006 * or more contributor license agreements.  See the NOTICE file
    007 * distributed with this work for additional information
    008 * regarding copyright ownership.  The ASF licenses this file
    009 * to you under the Apache License, Version 2.0 (the
    010 * "License"); you may not use this file except in compliance
    011 * with the License.  You may obtain a copy of the License at
    012 *
    013 *   http://www.apache.org/licenses/LICENSE-2.0
    014 *
    015 * Unless required by applicable law or agreed to in writing,
    016 * software distributed under the License is distributed on an
    017 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    018 * KIND, either express or implied.  See the License for the
    019 * specific language governing permissions and limitations
    020 * under the License.
    021 */
    022
    023
    024import org.apache.ecs.html.Option;
    025import org.apache.ecs.html.Select;
    026
    027/**
    028 * This class is for generating a SelectorBox. It is good when used
    029 * with WM because you can stuff it into the context and then just
    030 * call it to generate the HTML.  It can be used in other cases as
    031 * well, but WM is the best case for it right now.
    032 *
    033 * <p>For example code showing the usage for this module, please see
    034 * the toString() method below to see how it would be refered to from
    035 * WM.
    036 *
    037 * <pre>
    038 * // get the roles for a user
    039 * RoleSet userRoles = new DefaultAccessControl().getRoles(loginid, null);
    040 * if ( userRoles != null )
    041 * {
    042 *     context.put("hasRoleSet", Boolean.TRUE);
    043 *
    044 *     // get an array of the users roles
    045 *     Role[] usersRoles = userRoles.getRolesArray();
    046 *     // get an array of all the roles in the system
    047 *     Role[] allRoles = ((RoleSet)RolePeer.retrieveSet()).getRolesArray();
    048 *
    049 *     Object[] names = new Object[allRoles.length];
    050 *     Object[] values = new Object[allRoles.length];
    051 *     for ( int i=0;i&lt;allRoles.length; i++ )
    052 *     {
    053 *         names[i] = Integer.valueOf(allRoles[i].getPrimaryKey()).toString();
    054 *         values[i] = allRoles[i].getName();
    055 *     }
    056 *
    057 *     SelectorBox sb = new SelectorBox("roleSetBox", names, values);
    058 *     sb.buildBooleans(usersRoles, allRoles);
    059 *     context.put("roleSetBox", sb);
    060 * }
    061 * else
    062 * {
    063 *     context.put("hasRoleSet", Boolean.FALSE);
    064 * }
    065 * </pre>
    066 *
    067 * @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a>
    068 * @version $Id: SelectorBox.java 1706239 2015-10-01 13:18:35Z tv $
    069 */
    070public class SelectorBox
    071{
    072    /** This is the Select ECS element. */
    073    private Select sel = null;
    074
    075    /** This is the size of the Select statement. */
    076    private int size = 1;
    077
    078    /** This is the name= value. */
    079    private String name = null;
    080
    081    /** This is the value= portion of the option element. */
    082    private Object[] names = null;
    083
    084    /** This is the data after the option element. */
    085    private Object[] values = null;
    086
    087    /** This is an array of which items are selected. */
    088    private boolean[] selected = null;
    089
    090    /**
    091     * Generic constructor, builds a select box with a default size of
    092     * 1 and no selected items.
    093     *
    094     * @param name A String with the name for the select box.
    095     * @param names An Object[] with the names.
    096     * @param values An Object[] with the values.
    097     */
    098    public SelectorBox(String name, Object[] names, Object[] values)
    099    {
    100        this(name, names, values, 1, null);
    101    }
    102
    103    /**
    104     * Generic constructor builds a select box.
    105     *
    106     * @param name A String with the name for the select box.
    107     * @param names An Object[] with the names.
    108     * @param values An Object[] with the values.
    109     * @param size An int specifying the size.
    110     */
    111    public SelectorBox(String name, Object[] names, Object[] values, int size)
    112    {
    113        this(name, names, values, size, null);
    114    }
    115
    116    /**
    117     * Generic constructor builds a select box.
    118     *
    119     * @param name A String with the name for the select box.
    120     * @param names An Object[] with the names.
    121     * @param values An Object[] with the values.
    122     * @param selected A boolean[] with the selected items.
    123     */
    124    public SelectorBox(String name, Object[] names, Object[] values,
    125                       boolean[] selected)
    126    {
    127        this(name, names, values, 1, selected);
    128    }
    129
    130    /**
    131     * Primary constructor for everything.
    132     *
    133     * @param name A String with the name for the select box.
    134     * @param names An Object[] with the names.
    135     * @param values An Object[] with the values.
    136     * @param size An int specifying the size.
    137     * @param selected A boolean[] with the selected items.
    138     */
    139    public SelectorBox(String name, Object[] names, Object[] values, int size,
    140                       boolean[] selected)
    141    {
    142        this.name = name;
    143        this.names = names;
    144        this.values = values;
    145        this.size = size;
    146        this.selected = selected;
    147
    148        sel = new Select(name, size);
    149        sel.setName(name);
    150        sel.setSize(size);
    151    }
    152
    153    /**
    154     * Pass in an array of selected items and the entire set of items
    155     * and it will determine which items in the selected set are also
    156     * in the entireset and then build a boolean[] up that is the same
    157     * size as the entireSet with markings to tell whether or not the
    158     * items are marked or not.  It uses toString().equalsIgnoreCase()
    159     * on the Object in the Object[] to determine if the items are
    160     * equal.
    161     *
    162     * @param selectedSet An Object[].
    163     * @param entireSet An Object[].
    164     */
    165    public void buildBooleans(Object[] selectedSet, Object[] entireSet)
    166    {
    167        selected = new boolean[entireSet.length];
    168        for (int j = 0; j < entireSet.length; j++)
    169        {
    170            Object r2 = entireSet[j];
    171            for (int i = 0; i < selectedSet.length; i++)
    172            {
    173                Object r1 = selectedSet[i];
    174                if (r1 != null && r2 != null &&
    175                        r1.toString().equalsIgnoreCase(r2.toString()))
    176                {
    177                    selected[j] = true;
    178                }
    179            }
    180        }
    181    }
    182
    183    /**
    184     * This builds out the select box at a certain size.  To use this
    185     * element in WM, you simply build this object in your java code,
    186     * put it into the context and then call $selectBox.toString(5).
    187     *
    188     * @param size An int with the size.
    189     * @return A String with the HTML code.
    190     */
    191    public String toString(int size)
    192    {
    193        sel.setSize(size);
    194        sel.setName(name);
    195        for (int f = 0; f < values.length; f++)
    196        {
    197            Option opt = new Option((String) values[f]);
    198            opt.addElement((String) names[f]);
    199            if (selected != null && selected[f] == true)
    200            {
    201                opt.setSelected(true);
    202            }
    203            sel.addElement(opt);
    204        }
    205        String output = sel.toString();
    206        reset();
    207        return output;
    208    }
    209
    210    /**
    211     * Resets the internal state of the SelectorBox.
    212     */
    213    public void reset()
    214    {
    215        sel = new Select(name, size);
    216    }
    217
    218    /**
    219     * This builds out the select box at a certain size.  To use this
    220     * element in WM, you simply build this object in your java code,
    221     * put it into the context and then call $selectBox and it will
    222     * build it with the default size of 1.
    223     *
    224     * @return A String with the HTML code.
    225     */
    226    @Override
    227    public String toString()
    228    {
    229        return this.toString(size);
    230    }
    231
    232    /**
    233     * This allows you to set the multiple attribute to the select
    234     * element.  Example usage from within WM is like this:
    235     *
    236     * <p>
    237     * $selectBox.setMultiple(true).toString(4)
    238     *
    239     * @param val True if multiple selection should be allowed.
    240     * @return A SelectorBox (self).
    241     */
    242    public SelectorBox setMultiple(boolean val)
    243    {
    244        sel.setMultiple(val);
    245        return this;
    246    }
    247
    248    /**
    249     * This allows one to set the name= attribute to the select
    250     * element.
    251     *
    252     * @param name A String with the name.
    253     * @return A SelectorBox (self).
    254     */
    255    public SelectorBox setName(String name)
    256    {
    257        this.name = name;
    258        sel.setName(name);
    259        return this;
    260    }
    261
    262    /**
    263     * This allows one to set the size of the select element.
    264     *
    265     * @param size An int with the size.
    266     * @return A SelectorBox (self).
    267     */
    268    public SelectorBox setSize(int size)
    269    {
    270        this.size = size;
    271        sel.setSize(size);
    272        return this;
    273    }
    274
    275    /**
    276     * This allows one to set an onChange attribute on the select tag
    277     *
    278     * @param script A string with the script to put in onChange
    279     * @return A SelectorBox (self).
    280     */
    281    public SelectorBox setOnChange(String script)
    282    {
    283        sel.setOnChange(script);
    284        return this;
    285    }
    286
    287    /**
    288     * This allows one to set the array of selected booleans.
    289     *
    290     * @param bools an array of booleans
    291     * @return A SelectorBox (self).
    292     */
    293    public SelectorBox setSelected(boolean[] bools)
    294    {
    295        this.selected = bools;
    296        return this;
    297    }
    298
    299    /**
    300     * This will set all elements as unselected, except for the
    301     * element(s) with the given name.
    302     *
    303     * @param name The name to appear as selected.
    304     * @return A SelectorBox (self).
    305     */
    306    public SelectorBox setSelected(Object name)
    307    {
    308        if (name != null)
    309        {
    310            selected = new boolean[names.length];
    311            for (int i = 0; i < names.length; i++)
    312            {
    313                Object o = names[i];
    314                if (o != null && o.toString().equalsIgnoreCase(name.toString()))
    315                {
    316                    selected[i] = true;
    317                }
    318            }
    319        }
    320        return this;
    321    }
    322}