Coverage Report - net.sf.jolene.dom.Select
 
Classes in this File Line Coverage Branch Coverage Complexity
Select
92%
71/77
84%
27/32
0
Select$1
0%
0/4
N/A
0
 
 1  
 /*
 2  
  * Created on Dec 23, 2003
 3  
 */
 4  
 package net.sf.jolene.dom;
 5  
 
 6  
 import net.sf.jolene.constants.Prefs;
 7  
 import net.sf.jolene.constants.Tags;
 8  
 
 9  
 import java.util.*;
 10  
 
 11  
 /**
 12  
  * A Select in an html document.
 13  
  *
 14  
  * @author Dan Howard
 15  
  */
 16  
 
 17  3
 public final class Select extends HTMLElement {
 18  
 
 19  
     private boolean multiple;
 20  
 
 21  
     // Not public because we don't want the user to access the arraylist directly.
 22  
     // The only way to add a new option is to use the addOption method.
 23  36
     List<Option> options = new ArrayList<Option>(3);
 24  
 
 25  
     /**
 26  
      * Default constructor.
 27  
      */
 28  36
     public Select() {
 29  36
         tag = Tags.select;
 30  36
         multiple = false;
 31  36
     }
 32  
 
 33  
 
 34  
     /**
 35  
      * Construct a select with the specified name.
 36  
      *
 37  
      * @param name name of the select.
 38  
      */
 39  
     public Select(String name) {
 40  2
         this();
 41  2
         setName(name);
 42  2
     }
 43  
 
 44  
     /**
 45  
      * This method adds a new option to the available options for the select object.
 46  
      *
 47  
      * @param option An option object to add to the select list
 48  
      * @return int - the new size of the option list.
 49  
      */
 50  
     public int addOption(Option option) {
 51  1277
         int index = -1;
 52  
         //option.parent = this;
 53  
 
 54  1277
         StringTokenizer st = new StringTokenizer(getValue(), Prefs.SelectValueSeperator.getValue());
 55  
         String itemValue;
 56  1303
         while (st.hasMoreTokens()) {
 57  26
             itemValue = st.nextToken();
 58  26
             if (itemValue.equals(option.getValue())) {
 59  4
                 option.setSelected(true);
 60  
             }
 61  
         }
 62  
 
 63  1277
         options.add(option);
 64  1277
         index = options.size();
 65  1277
         return index;
 66  
     }
 67  
 
 68  
     /**
 69  
      * Clears the list of options on the select.
 70  
      */
 71  
     public void clearOptions() {
 72  2
         options.clear();
 73  2
     }
 74  
 
 75  
 
 76  
     /**
 77  
      * Returns a clone of the select object.
 78  
      *
 79  
      * @return Select object.
 80  
      */
 81  
     @Override
 82  
     public Select clone() {
 83  3
         Select select = (Select) super.clone();
 84  
 
 85  10
         for (int j = 0; j < options.size(); j++) {
 86  7
             select.addOption(new Option(options.get(j).getValue(), options.get(j).getText()));
 87  
         }
 88  3
         return select;
 89  
     }
 90  
 
 91  
     /**
 92  
      * Returns the specified option object by the numeric index.
 93  
      *
 94  
      * @param index the index of the option to retrive
 95  
      * @return Option object.
 96  
      */
 97  
     public Option options(int index) {
 98  7
         return options.get(index);
 99  
     }
 100  
 
 101  
     /**
 102  
      * Gets the value of the select object.
 103  
      *
 104  
      * @return value of the select object.
 105  
      */
 106  
     @Override
 107  
     public String getValue() {
 108  1329
         String selectValue = super.getValue();
 109  
 
 110  
         // Ensure that exisiting options are flagged properly.
 111  1329
         setValue(selectValue);
 112  
 
 113  1329
         String value = "";
 114  
 
 115  
         // Loop through the options and add the values of the 'selected' options
 116  
         // to the value string + commas...
 117  1329
         String sep = Prefs.SelectValueSeperator.getValue();
 118  127521
         for (int j = 0; j < options.size(); j++) {
 119  126192
             Option option = options.get(j);
 120  
 
 121  126192
             if (option.isSelected()) {
 122  27
                 value += option.getValue() + sep;
 123  
             }
 124  
         }
 125  
 
 126  
         // Strip last comma
 127  1329
         int n = value.lastIndexOf(",");
 128  1329
         if (n > -1) {
 129  16
             value = value.substring(0, n);
 130  
         }
 131  
 
 132  1329
         if (value.trim().length() == 0 && selectValue.trim().length() > 0) {
 133  
             // If there was no value found in the options list but the user
 134  
             // had set a value in the select object itself then return that
 135  
             // value but log a warning.
 136  13
             value = selectValue;
 137  13
             log.warn("Returning Select object " + getName() + " assigned value: " + value + " which does not exist in the options list.");
 138  
         }
 139  1329
         return value;
 140  
     }
 141  
 
 142  
     /**
 143  
      * Indicates if the select is a multiple select.
 144  
      *
 145  
      * @return mulitple
 146  
      */
 147  
     public boolean isMultiple() {
 148  0
         return multiple;
 149  
     }
 150  
 
 151  
     /**
 152  
      * Set the name of the select object.
 153  
      *
 154  
      * @param name name of the select object.
 155  
      */
 156  
     @Override
 157  
     public void setName(String name) {
 158  33
         super.setName(name);
 159  33
         setAttribute("name", name);
 160  33
     }
 161  
 
 162  
     /**
 163  
      * Sets if this select is a multiple select.
 164  
      *
 165  
      * @param multiple boolean.
 166  
      */
 167  
     public void setMultiple(boolean multiple) {
 168  
 
 169  5
         this.multiple = multiple;
 170  5
         if (multiple) {
 171  5
             setAttribute("multiple", "multiple");
 172  
         } else {
 173  0
             removeAttribute("multiple");
 174  
         }
 175  5
     }
 176  
 
 177  
     /**
 178  
      * Sets the value for the select object. The value to  assign
 179  
      * may be a single value or multiple values seperate by a comma.
 180  
      *
 181  
      * @param value String value to set.
 182  
      */
 183  
     @Override
 184  
     public void setValue(String value) {
 185  1341
         super.setValue(value);
 186  
 
 187  
         // locate the 1st option whose itemValue = value
 188  
         // If multi-select value could be comma seperated?
 189  127546
         for (int j = 0; j < options.size(); j++) {
 190  
 
 191  126205
             Option option = options.get(j);
 192  
 
 193  126205
             option.setSelected(false);
 194  
 
 195  126205
             StringTokenizer st = new StringTokenizer(value, Prefs.SelectValueSeperator.getValue());
 196  126258
             while (st.hasMoreTokens()) {
 197  82
                 String itemValue = st.nextToken();
 198  82
                 String optionValue = option.getValue();
 199  
 
 200  82
                 if (optionValue.equals(itemValue)) {
 201  29
                     option.setSelected(true);
 202  29
                     break;
 203  
                 }
 204  53
             }
 205  
         }
 206  
 
 207  1341
     }
 208  
 
 209  
     /**
 210  
      * Sort the options on the select.
 211  
      */
 212  
     public void sortOptions() {
 213  0
         Collections.sort(options, new Comparator<Option>() {
 214  
 
 215  0
             public int compare(Option o1, Option o2) {
 216  0
                 String s1 = o1.getText();
 217  0
                 String s2 = o2.getText();
 218  0
                 return s1.compareTo(s2);
 219  
             }
 220  
         });
 221  0
     }
 222  
 
 223  
 
 224  
     /**
 225  
      * Renders the select.
 226  
      *
 227  
      * @return The select as a html string.
 228  
      */
 229  
     @Override
 230  
     public String toString() {
 231  
 
 232  49
         if (swapWith != null) {
 233  0
             return swapWith.toString();
 234  
         }
 235  
 
 236  49
         if (!isRenderable() && !log.isDebugEnabled()) {
 237  0
             return "";
 238  
         }
 239  
 
 240  
         // Used to ensure options are initialized properly.
 241  49
         getValue();
 242  
 
 243  49
         StringBuilder sb = new StringBuilder(super.toString());
 244  1373
         for (int j = 0; j < options.size(); j++) {
 245  1324
             sb.append(options.get(j).toString());
 246  
         }
 247  49
         String ls = System.getProperty("line.separator");
 248  49
         sb.append(ls);
 249  49
         sb.append("</").append(tag).append(">");
 250  49
         sb.append(ls);
 251  49
         return sb.toString();
 252  
     }
 253  
 }