001// Copyright (c) 2001 Hursh Jain (http://www.mollypages.org) 
002// The Molly framework is freely distributable under the terms of an
003// MIT-style license. For details, see the molly pages web site at:
004// http://www.mollypages.org/. Use, modify, have fun !
005
006package fc.web.simpleforms;
007
008import javax.servlet.*;
009import javax.servlet.http.*;
010import java.io.*;
011import java.util.*;
012
013import fc.io.*;
014import fc.util.*;
015
016/**
017Represents an option for a select input type. This class is the
018fc.web.simpleforms equivalent to the similar class {@link
019fc.web.forms.Select.Option} found in package fc.web.forms
020
021@author hursh jain
022*/
023public final class SelectOption 
024  {
025  private String  value;
026  private String  text;
027  private boolean orig_selected;
028  
029  /** 
030  Constructs a new option with the specified text and 
031  value of the option tag.
032  
033  @param  text    the html text of the option tag 
034  @param  value   the value of the option tag
035  @param  selected  <tt>true</tt> if this option is selected
036            by default. If more than one selected option
037            is added to a select field and that select
038            field does <b>not</b> have it's multiple
039            attribute set, then the option displayed as
040            selected is browser dependent (Moz1, IE6
041            show the last selected, N4 the first). More
042            than one selected option should not be shown
043            for non multiple select fields anyway.
044  **/
045  public SelectOption(String text, String value, boolean selected) 
046    {
047    this.text = text;
048    this.value = value;
049    this.orig_selected = selected;
050    }
051
052  /** 
053  Constructs a new unselected option with the specified 
054  text and value of the option tag.
055  
056  @param  text  the html text of the option tag 
057  @param  value the value of the option tag
058  **/
059  public SelectOption(String text, String value) {
060    this(text, value, false);
061    }
062  
063  /** 
064  Constructs a new option with the specified text (and no
065  separate value tag).
066  
067  @param  text    the html text of the option tag 
068  @param  selected  <tt>true</tt> to select this option
069            <tt>false</tt> otherwise
070  **/
071  public SelectOption(String text, boolean selected) {
072    this(text, null, selected);
073    }
074
075  /** 
076  Constructs a new unselected option with the specified
077  html text and no separate value.
078
079  @param  text  the html text of the option tag 
080  **/
081  public SelectOption(String text) {
082    this(text, null, false);
083    }
084    
085  boolean selected() {
086    return orig_selected;
087    }
088        
089  /** 
090  Returns the value of this option tag. If no value is set, 
091  returns the html text value for this option tag 
092  **/
093  public String getValue() 
094    {
095    if (value != null)
096      return value;
097    else
098      return text;
099    } 
100    
101  /**
102  Returns the html text for this option.
103  */
104  public String getHTML()
105    {
106    return text;
107    }
108        
109  } //~class SelectOption