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