001 package net.sf.jolene.dom; 002 003 import net.sf.jolene.constants.Tags; 004 005 /** 006 * A select option object. 007 * 008 * @author Dan Howard 009 * @since Nov 30, 2003 010 */ 011 public final class Option { 012 013 int index; 014 boolean selected; 015 String text; 016 String value; 017 018 //Select parent; 019 020 Tags tag = Tags.option; 021 022 /** 023 * Creates a new Option object. 024 */ 025 public Option() { 026 index = -1; 027 selected = false; 028 text = ""; 029 value = ""; 030 } 031 032 /** 033 * Creates an Option object using the String value and text parameters. 034 * 035 * @param value The value of the option 036 * @param text The display text of the option 037 */ 038 public Option(String value, String text) { 039 index = -1; 040 selected = false; 041 this.text = text; 042 this.value = value; 043 } 044 045 /** 046 * Creates an Option object using an int value and String text parameters. 047 * 048 * @param value The value of the option 049 * @param text The display text of the option 050 */ 051 public Option(int value, String text) { 052 index = -1; 053 selected = false; 054 this.text = text; 055 this.value = String.valueOf(value); 056 } 057 058 059 /** 060 * Return the index of this option in the list of selections. 061 * 062 * @return the index of this option in the list of selections. 063 */ 064 public int getIndex() { 065 // Read only. 066 return index; 067 } 068 069 /** 070 * Get the option text. 071 * 072 * @return text 073 */ 074 public String getText() { 075 return text; 076 } 077 078 /** 079 * Get the option value. 080 * 081 * @return value 082 */ 083 public String getValue() { 084 return value; 085 } 086 087 /** 088 * Determines if the option is currently selected. This boolean value is read/write. 089 * 090 * @return selected 091 */ 092 public boolean isSelected() { 093 return selected; 094 } 095 096 /** 097 * Sets the option as currently selected. 098 * 099 * @param selected boolean 100 */ 101 public void setSelected(boolean selected) { 102 this.selected = selected; 103 } 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 text = string; 112 } 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 value = string.trim(); 122 } 123 124 /** 125 * Renders the option. 126 * 127 * @return The option as a html string. 128 */ 129 @Override 130 public String toString() { 131 StringBuffer sb = new StringBuffer(""); 132 133 sb.append(System.getProperty("line.separator")); 134 sb.append("<").append(tag).append(" value=\"").append(this.value).append("\""); 135 136 if (selected) { 137 sb.append(" selected='selected'>"); 138 } else { 139 sb.append(">"); 140 } 141 sb.append(this.text).append("</").append(tag).append(">"); 142 143 return new String(sb); 144 } 145 146 }