View Javadoc

1   /*
2       Jiffie Plugin for Jameleon - An Internet Explorer plug-in for Jameleon
3       Copyright (C) 2004-2006 Christian W. Hargraves (engrean@hotmail.com) 
4                          and Matthias Marschall (matthias@marschalls.de)
5   
6       This program is free software; you can redistribute it and/or modify
7       it under the terms of the GNU General Public License as published by
8       the Free Software Foundation; either version 2 of the License, or
9       (at your option) any later version.
10  
11      This program is distributed in the hope that it will be useful,
12      but WITHOUT ANY WARRANTY; without even the implied warranty of
13      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14      GNU General Public License for more details.
15  
16      You should have received a copy of the GNU General Public License
17      along with this program; if not, write to the Free Software
18      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20  package net.sf.jameleon.plugin.jiffie.tags;
21  
22  import java.util.List;
23  
24  /***
25   * Sets the value(s) of any select field. 
26   * Only one of optionText, optionValue, or optionIndex can set. In other words, they
27   * can't be used in conjunction with one another.
28   * 
29   * Some example uses might be:
30   * 
31   * Selecting 
32   * <pre><source>
33   *          &lt;select name="selectField"&gt;
34   *                           &lt;option value="1"&gt;one&lt;/option&gt;
35   *                           &lt;option value="2"&gt;two&lt;/option&gt;
36   *                           &lt;option value="3"&gt;three&lt;/option&gt;
37   *                           &lt;option value="4"&gt;four&lt;/option&gt;
38   *          &lt;/select&gt;
39   * </source></pre>
40   * 
41   * <pre><source>
42   *  &lt;ie-set-select-field functionId="Set the select field not in a form to the value displayed as 'two'" 
43   *       name="selectField" 
44   *       optionText="two"/&gt;
45   * </source></pre>
46   * 
47   * Selecting multiple values from:
48   * <pre><source>
49   *           &lt;select name="selectField" mulitple&gt;
50   *                           &lt;option value="1"&gt;one&lt;/option&gt;
51   *                           &lt;option value="2"&gt;two&lt;/option&gt;
52   *                           &lt;option value="3"&gt;three&lt;/option&gt;
53   *                           &lt;option value="4"&gt;four&lt;/option&gt;
54   *           &lt;/select&gt;
55   * </source></pre>
56   * 
57   * <pre><source>
58   *  &lt;map-variable toVariable="fieldValues" variableType="List"&gt;
59   *     &lt;variable-value&gt;1&lt;/variable-value&gt;
60   *     &lt;variable-value&gt;4&lt;/variable-value&gt;
61   *  &lt;/map-variable&gt;
62   *  &lt;ie-set-select-field functionId="Set mulitple values in the the select field via the value '1' and '4'" 
63   *       name="selectField" 
64   *       optionValue="${fieldValues}"/&gt;
65   * </source></pre>
66   * 
67   * <pre><source>
68   *  &lt;ie-set-select-field functionId="Set the select field to the value displayed as 'two'" 
69   *       name="selectField" 
70   *       optionText="two"
71   *       form="testForm"/&gt;
72   * </source></pre>
73   * 
74   * <pre><source>
75   *  &lt;ie-set-select-field functionId="Set the select field to the value '3' as defined by its value attribute" 
76   *       name="selectField" 
77   *       optionValue="3"
78   *       form="testForm"/&gt;
79   * </source></pre>
80   * <pre><source>
81   *  &lt;ie-set-select-field functionId="Set the select field to the fourth selection" 
82   *       name="selectField" 
83   *       optionIndex="3"
84   *       form="testForm"/&gt;
85   * </source></pre>
86   * NOTE: <b>This tag does not currently support the xpath attribute nor the nested ie-attribute tags.</b>
87   * @jameleon.function name="ie-set-select-field" type="action"
88   * @jameleon.step Find the given form and set it as the working form
89   * @jameleon.step Find the given select field and set it's value
90   * @author Christian Hargraves
91   * @author Pravin Bhujbal
92   */
93  public class IESetSelectFieldTag extends IEFireEventTag {
94  
95      /***
96       * The name of the text field
97       * @jameleon.attribute required="true" contextName="ieSetSelectFieldName"
98       */
99      protected String name;
100     /***
101      * The displayed text of the option to select.
102      * This attribute can have multiple values. In which case each one will be validated.
103      * @jameleon.attribute contextName="ieSetSelectFieldOptionText"
104      */
105     protected List optionText;
106     /***
107      * The value attribute of the option tag to select.
108      * This attribute can have multiple values. In which case each one will be validated.
109      * NOTE: This is not the text that appears in the drop-down list.
110      * Instead it is the value attribute of the option tag which should be
111      * nested inside the select tag.
112      * @jameleon.attribute contextName="ieSetSelectFieldOptionValue"
113      */
114     protected List optionValue;
115     /***
116      * The index of the option to select. For example, to select the 2nd option, pass in 1
117      * This attribute can have multiple values. In which case each one will be validated.
118      * @jameleon.attribute contextName="ieSetSelectFieldOptionIndex"
119      */
120     protected List optionIndex;
121     /***
122      * The name, id or index of the field
123      * @jameleon.attribute contextName="ieSetSelectFieldForm"
124      */
125     protected String form;
126 
127     public void testBlock() {
128         if (htmlElement != null ||
129             xpath != null ||
130             getParamLength() > 0) {
131             throw new RuntimeException("htmlElement, xpath and ie-attribute are not supported by this tag");
132         }
133         if (optionText != null &&
134             optionValue != null &&
135             optionIndex != null) {
136             throw new RuntimeException("Onle of optionIndex, optionValue and optionText can be used at a time.");
137         }
138         setWorkingForm(form);
139         if (optionText != null && optionText.size() > 0) {
140             fireEvent(setSelectFieldOptionTextValues(name, optionText));
141         }else if (optionIndex != null && optionIndex.size() > 0) {
142             int[] indexes = new int[optionIndex.size()];
143             for (int i = 0; i < indexes.length; i++) {
144                 indexes[i] = (new Integer((String)optionIndex.get(i)).intValue());
145             }
146             fireEvent(setSelectFieldOptionIndexes(name, indexes));
147         }else if (optionValue != null && optionValue.size() > 0) {
148             fireEvent(setSelectFieldOptionValues(name, optionValue));
149         }else{
150             fail("You must define one of the following 'displayedText', 'optionIndex', or 'optionValue'");
151         }
152     }
153 }