Coverage Report - net.sf.jolene.dom.Option
 
Classes in this File Line Coverage Branch Coverage Complexity
Option
86%
32/37
100%
2/2
0
 
 1  
 package net.sf.jolene.dom;
 2  
 
 3  
 import net.sf.jolene.constants.Tags;
 4  
 
 5  
 /**
 6  
  * A select option object.
 7  
  *
 8  
  * @author Dan Howard
 9  
  * @since Nov 30, 2003
 10  
  */
 11  
 public final class Option {
 12  
 
 13  
     int index;
 14  
     boolean selected;
 15  
     String text;
 16  
     String value;
 17  
 
 18  
     //Select parent;
 19  
 
 20  1277
     Tags tag = Tags.option;
 21  
 
 22  
     /**
 23  
      * Creates a new Option object.
 24  
      */
 25  1254
     public Option() {
 26  1254
         index = -1;
 27  1254
         selected = false;
 28  1254
         text = "";
 29  1254
         value = "";
 30  1254
     }
 31  
 
 32  
     /**
 33  
      * Creates an Option object using the String value and text parameters.
 34  
      *
 35  
      * @param value The value of the option
 36  
      * @param text  The display text of the option
 37  
      */
 38  13
     public Option(String value, String text) {
 39  13
         index = -1;
 40  13
         selected = false;
 41  13
         this.text = text;
 42  13
         this.value = value;
 43  13
     }
 44  
 
 45  
     /**
 46  
      * Creates an Option object using an int value and String text parameters.
 47  
      *
 48  
      * @param value The value of the option
 49  
      * @param text  The display text of the option
 50  
      */
 51  10
     public Option(int value, String text) {
 52  10
         index = -1;
 53  10
         selected = false;
 54  10
         this.text = text;
 55  10
         this.value = String.valueOf(value);
 56  10
     }
 57  
 
 58  
 
 59  
     /**
 60  
      * Return the index of this option in the list of selections.
 61  
      *
 62  
      * @return the index of this option in the list of selections.
 63  
      */
 64  
     public int getIndex() {
 65  
         // Read only.
 66  0
         return index;
 67  
     }
 68  
 
 69  
     /**
 70  
      * Get the option text.
 71  
      *
 72  
      * @return text
 73  
      */
 74  
     public String getText() {
 75  7
         return text;
 76  
     }
 77  
 
 78  
     /**
 79  
      * Get the option value.
 80  
      *
 81  
      * @return value
 82  
      */
 83  
     public String getValue() {
 84  142
         return value;
 85  
     }
 86  
 
 87  
     /**
 88  
      * Determines if the option is currently selected. This boolean value is read/write.
 89  
      *
 90  
      * @return selected
 91  
      */
 92  
     public boolean isSelected() {
 93  126194
         return selected;
 94  
     }
 95  
 
 96  
     /**
 97  
      * Sets the option as currently selected.
 98  
      *
 99  
      * @param selected boolean
 100  
      */
 101  
     public void setSelected(boolean selected) {
 102  126238
         this.selected = selected;
 103  126238
     }
 104  
 
 105  
     /**
 106  
      * Sets the text to display in the option.
 107  
      *
 108  
      * @param string text to display.
 109  
      */
 110  
     public void setText(String string) {
 111  0
         text = string;
 112  0
     }
 113  
 
 114  
     /**
 115  
      * Sets the value which would be submitted when the form is submitted.
 116  
      *
 117  
      * @param string value of the option.
 118  
      */
 119  
     public void setValue(String string) {
 120  
         // Trim the value to make sure that there are no \t\n\r chars.
 121  0
         value = string.trim();
 122  0
     }
 123  
 
 124  
     /**
 125  
      * Renders the option.
 126  
      *
 127  
      * @return The option as a html string.
 128  
      */
 129  
     @Override
 130  
     public String toString() {
 131  1329
         StringBuffer sb = new StringBuffer("");
 132  
 
 133  1329
         sb.append(System.getProperty("line.separator"));
 134  1329
         sb.append("<").append(tag).append(" value=\"").append(this.value).append("\"");
 135  
 
 136  1329
         if (selected) {
 137  14
             sb.append(" selected='selected'>");
 138  
         } else {
 139  1315
             sb.append(">");
 140  
         }
 141  1329
         sb.append(this.text).append("</").append(tag).append(">");
 142  
 
 143  1329
         return new String(sb);
 144  
     }
 145  
 
 146  
 }