File Source: selecttag.java

         /* 
    P/P   *  Method: net.sourceforge.pebble.web.tagext.SelectTag__static_init
          */
     1  /*
     2   * Copyright (c) 2003-2006, Simon Brown
     3   * All rights reserved.
     4   *
     5   * Redistribution and use in source and binary forms, with or without
     6   * modification, are permitted provided that the following conditions are met:
     7   *
     8   *   - Redistributions of source code must retain the above copyright
     9   *     notice, this list of conditions and the following disclaimer.
    10   *
    11   *   - Redistributions in binary form must reproduce the above copyright
    12   *     notice, this list of conditions and the following disclaimer in
    13   *     the documentation and/or other materials provided with the
    14   *     distribution.
    15   *
    16   *   - Neither the name of Pebble nor the names of its contributors may
    17   *     be used to endorse or promote products derived from this software
    18   *     without specific prior written permission.
    19   *
    20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
    24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    30   * POSSIBILITY OF SUCH DAMAGE.
    31   */
    32  package net.sourceforge.pebble.web.tagext;
    33  
         /* 
    P/P   *  Method: void net.sourceforge.pebble.web.tagext.SelectTag()
          * 
          *  Postconditions:
          *    this.multiple == 0
          *    this.size == -1
          */
    34  import javax.servlet.jsp.JspException;
    35  import javax.servlet.jsp.JspTagException;
    36  import javax.servlet.jsp.JspWriter;
    37  import javax.servlet.jsp.tagext.TagSupport;
    38  import java.lang.reflect.Method;
    39  import java.util.Arrays;
    40  import java.util.Collection;
    41  import java.util.Iterator;
    42  
    43  /**
    44   * Given a Collection or array, this tag produces a HTML select (dropdown) list
    45   * based upon the items contained within.
    46   *
    47   * @author    Simon Brown
    48   */
    49  public class SelectTag extends TagSupport {
    50  
    51    /** the items over which this tag should iterate */
    52    private Collection items;
    53  
    54    /** the name of the select control */
    55    private String name;
    56  
    57    /** the size of the select control */
    58    private int size = -1;
    59  
    60    /** the multiple attribute */
    61    private boolean multiple = false;
    62  
    63    /** the name of the property to be used as the displayed label */
    64    private String label;
    65  
    66    /** the name of the property to be used as the hidden value */
    67    private String value;
    68  
    69    /** the selected value */
    70    private Object selected;
    71  
    72    /**
    73     * Called when the starting tag is encountered.
    74     */
           /* 
    P/P     *  Method: int doStartTag()
            * 
            *  Preconditions:
            *    this.items != null
            *    init'ed(this.multiple)
            *    init'ed(this.name)
            *    this.pageContext != null
            *    init'ed(this.size)
            *    (soft) init'ed(this.label)
            *    (soft) init'ed(this.selected)
            *    (soft) init'ed(this.value)
            * 
            *  Presumptions:
            *    java.lang.Class:getMethod(...)@106 != null
            *    java.lang.Class:getMethod(...)@137 != null
            *    java.lang.Object:getClass(...)@106 != null
            *    java.lang.Object:getClass(...)@137 != null
            *    java.lang.reflect.Method:invoke(...)@107 != null
            *    ...
            * 
            *  Postconditions:
            *    return_value == 0
            * 
            *  Test Vectors:
            *    this.label: Addr_Set{null}, Inverse{null}
            *    this.multiple: {0}, {1}
            *    this.selected: Addr_Set{null}, Inverse{null}
            *    this.size: {-231..0}, {1..232-1}
            *    this.value: Addr_Set{null}, Inverse{null}
            *    java.lang.String:equals(...)@127: {0}, {1}
            *    java.util.Collection:contains(...)@124: {0}, {1}
            *    java.util.Collection:instanceof(...)@122: {0}, {1}
            *    java.util.Iterator:hasNext(...)@99: {1}, {0}
            */
    75    public int doStartTag() throws JspException {
    76      // setup the iterator to be used
    77      Iterator iterator = items.iterator();
    78  
    79      try {
    80        JspWriter out = pageContext.getOut();
    81  
    82        // write the starting tag of the select control
    83        out.print("<select name=\"");
    84        out.print(name);
    85        out.print("\"");
    86  
    87        if (size > 0) {
    88          out.print(" size=\"");
    89          out.print(size);
    90          out.print("\"");
    91        }
    92  
    93        if (multiple) {
    94          out.print(" multiple=\"true\"");
    95        }
    96  
    97        out.print(">");
    98  
    99        while (iterator.hasNext()) {
   100          // get the next JavaBean from the items
   101          Object o = iterator.next();
   102  
   103          // and the property used to represent the hidden value
   104          String hiddenValue;
   105          if (value != null) {
   106            Method m = o.getClass().getMethod("get" + value.substring(0, 1).toUpperCase() + value.substring(1), new Class[] {});
   107            hiddenValue = m.invoke(o, new Object[]{}).toString();
   108          } else {
   109            hiddenValue = o.toString();
   110          }
   111  
   112          // and now generate the HTML
   113          out.print("<option value=\"");
   114  
   115          // call the accessor method for the value property
   116          // (this is the same as calling get<PropertyName>() on
   117          // the JavaBean instance)
   118          out.print(hiddenValue);
   119          out.print("\"");
   120  
   121          if (selected != null) {
   122            if (selected instanceof Collection) {
   123              Collection coll = (Collection)selected;
   124              if (coll.contains(hiddenValue)) {
   125                out.print(" selected=\"true\"");
   126              }
   127            } else if (selected.toString().equals(hiddenValue)) {
   128              out.print(" selected=\"true\"");
   129            }
   130          }
   131          out.print(">");
   132  
   133          if (label != null) {
   134            // and do the same for the label property
   135            // and use it to create a description of the property used
   136            // to represent the displayable label
   137            Method m = o.getClass().getMethod("get" + label.substring(0, 1).toUpperCase() + label.substring(1), new Class[] {});
   138            out.print(
   139                m.invoke(o, new Object[]{}));
   140          } else {
   141            out.print(o.toString());
   142          }
   143          out.print("</option>");
   144        }
   145  
   146        // write the ending tag of the select control
   147        out.print("</select>");
   148      } catch (Exception e) {
   149        e.printStackTrace();
   150        throw new JspTagException(e.getMessage());
   151      }
   152  
   153      // and skip the body
   154      return SKIP_BODY;
   155    }
   156  
   157    /**
   158     * Sets the items over which this tag should iterate.
   159     *
   160     * @param items   a Collection or array
   161     */
           /* 
    P/P     *  Method: void setItems(Object)
            * 
            *  Postconditions:
            *    possibly_updated(this.items)
            */
   162    public void setItems(Object items) {
   163      if (items instanceof Collection) {
   164        this.items = (Collection)items;
   165      } else if (items instanceof Object[]) {
   166        this.items = Arrays.asList((Object[])items);
   167      }
   168    }
   169  
   170    /**
   171     * Sets the name for the generated select control.
   172     *
   173     * @param name    the name as a String
   174     */
           /* 
    P/P     *  Method: void setName(String)
            * 
            *  Postconditions:
            *    this.name == name
            *    init'ed(this.name)
            */
   175    public void setName(String name) {
   176      this.name = name;
   177    }
   178  
   179    /**
   180     * Sets the size of the generated select control.
   181     *
   182     * @param size    the size
   183     */
           /* 
    P/P     *  Method: void setSize(int)
            * 
            *  Postconditions:
            *    this.size == size
            *    init'ed(this.size)
            */
   184    public void setSize(int size) {
   185      this.size = size;
   186    }
   187  
   188    /**
   189     * Sets the multiple attribute on the underlying select control.
   190     *
   191     * @param   multiple    a boolean
   192     */
           /* 
    P/P     *  Method: void setMultiple(bool)
            * 
            *  Postconditions:
            *    this.multiple == multiple
            *    init'ed(this.multiple)
            */
   193    public void setMultiple(boolean multiple) {
   194      this.multiple = multiple;
   195    }
   196  
   197    /**
   198     * Sets the name of the property to display.
   199     *
   200     * @param label   the name of the label property
   201     */
           /* 
    P/P     *  Method: void setLabel(String)
            * 
            *  Postconditions:
            *    this.label == label
            *    init'ed(this.label)
            */
   202    public void setLabel(String label) {
   203      this.label = label;
   204    }
   205  
   206    /**
   207     * Sets the name of the property to use as the hidden value.
   208     *
   209     * @param value   the name of the value property
   210     */
           /* 
    P/P     *  Method: void setValue(String)
            * 
            *  Postconditions:
            *    this.value == value
            *    init'ed(this.value)
            */
   211    public void setValue(String value) {
   212      this.value = value;
   213    }
   214  
   215    /**
   216     * Sets the selected value.
   217     *
   218     * @param selected    the selected value
   219     */
           /* 
    P/P     *  Method: void setSelected(Object)
            * 
            *  Postconditions:
            *    this.selected == selected
            *    init'ed(this.selected)
            */
   220    public void setSelected(Object selected) {
   221      this.selected = selected;
   222    }
   223  
   224  }








SofCheck Inspector Build Version : 2.22510
selecttag.java 2010-Jun-25 19:40:34
selecttag.class 2010-Jul-19 20:23:38