1. /*
  2. * @(#)Option.java 1.6 01/11/29
  3. *
  4. * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package javax.swing.text.html;
  8. import java.awt.*;
  9. import javax.swing.*;
  10. import javax.swing.text.*;
  11. /**
  12. * Value for the ListModel used to represent
  13. * <option> elements. This is the object
  14. * installed as items of the DefaultComboBoxModel
  15. * used to represent the <select> element.
  16. *
  17. * @author Timothy Prinzing
  18. * @version 1.6 11/29/01
  19. */
  20. public class Option {
  21. /**
  22. * Creates a new Option object.
  23. *
  24. * @param attr the attributes associated with the
  25. * option element. The attributes are copied to
  26. * ensure they won't change.
  27. */
  28. public Option(AttributeSet attr) {
  29. this.attr = attr.copyAttributes();
  30. selected = (attr.getAttribute(HTML.Attribute.SELECTED) != null);
  31. }
  32. /**
  33. * Sets the label to be used for the option.
  34. */
  35. public void setLabel(String label) {
  36. this.label = label;
  37. }
  38. /**
  39. * Fetch the label associated with the option.
  40. */
  41. public String getLabel() {
  42. return label;
  43. }
  44. /**
  45. * Fetch the attributes associated with this option.
  46. */
  47. public AttributeSet getAttributes() {
  48. return attr;
  49. }
  50. /**
  51. * String representation is the label.
  52. */
  53. public String toString() {
  54. return label;
  55. }
  56. /**
  57. * Sets the selected state.
  58. */
  59. protected void setSelection(boolean state) {
  60. selected = state;
  61. }
  62. /**
  63. * Fetches the selection state associated with this option.
  64. */
  65. public boolean isSelected() {
  66. return selected;
  67. }
  68. /**
  69. * Convenience method to return the string associated
  70. * with the <code>value</code> attribute. If the
  71. * value has not been specified, the label will be
  72. * returned.
  73. */
  74. public String getValue() {
  75. String value = (String) attr.getAttribute(HTML.Attribute.VALUE);
  76. if (value == null) {
  77. value = label;
  78. }
  79. return value;
  80. }
  81. private boolean selected;
  82. private String label;
  83. private AttributeSet attr;
  84. }