View Javadoc
1   /*
2    * Copyright (c) 1998, 2013, Oracle and/or its affiliates. All rights reserved.
3    * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4    *
5    * This code is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU General Public License version 2 only, as
7    * published by the Free Software Foundation.  Oracle designates this
8    * particular file as subject to the "Classpath" exception as provided
9    * by Oracle in the LICENSE file that accompanied this code.
10   *
11   * This code is distributed in the hope that it will be useful, but WITHOUT
12   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13   * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14   * version 2 for more details (a copy is included in the LICENSE file that
15   * accompanied this code).
16   *
17   * You should have received a copy of the GNU General Public License version
18   * 2 along with this work; if not, write to the Free Software Foundation,
19   * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20   *
21   * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22   * or visit www.oracle.com if you need additional information or have any
23   * questions.
24   */
25  package javax.swing.text.html;
26  
27  import java.io.Serializable;
28  import javax.swing.text.*;
29  
30  /**
31   * Value for the ListModel used to represent
32   * <option> elements.  This is the object
33   * installed as items of the DefaultComboBoxModel
34   * used to represent the <select> element.
35   * <p>
36   * <strong>Warning:</strong>
37   * Serialized objects of this class will not be compatible with
38   * future Swing releases. The current serialization support is
39   * appropriate for short term storage or RMI between applications running
40   * the same version of Swing.  As of 1.4, support for long term storage
41   * of all JavaBeans&trade;
42   * has been added to the <code>java.beans</code> package.
43   * Please see {@link java.beans.XMLEncoder}.
44   *
45   * @author  Timothy Prinzing
46   */
47  public class Option implements Serializable {
48  
49      /**
50       * Creates a new Option object.
51       *
52       * @param attr the attributes associated with the
53       *  option element.  The attributes are copied to
54       *  ensure they won't change.
55       */
56      public Option(AttributeSet attr) {
57          this.attr = attr.copyAttributes();
58          selected = (attr.getAttribute(HTML.Attribute.SELECTED) != null);
59      }
60  
61      /**
62       * Sets the label to be used for the option.
63       */
64      public void setLabel(String label) {
65          this.label = label;
66      }
67  
68      /**
69       * Fetch the label associated with the option.
70       */
71      public String getLabel() {
72          return label;
73      }
74  
75      /**
76       * Fetch the attributes associated with this option.
77       */
78      public AttributeSet getAttributes() {
79          return attr;
80      }
81  
82      /**
83       * String representation is the label.
84       */
85      public String toString() {
86          return label;
87      }
88  
89      /**
90       * Sets the selected state.
91       */
92      protected void setSelection(boolean state) {
93          selected = state;
94      }
95  
96      /**
97       * Fetches the selection state associated with this option.
98       */
99      public boolean isSelected() {
100         return selected;
101     }
102 
103     /**
104      * Convenience method to return the string associated
105      * with the <code>value</code> attribute.  If the
106      * value has not been specified, the label will be
107      * returned.
108      */
109     public String getValue() {
110         String value = (String) attr.getAttribute(HTML.Attribute.VALUE);
111         if (value == null) {
112             value = label;
113         }
114         return value;
115     }
116 
117     private boolean selected;
118     private String label;
119     private AttributeSet attr;
120 }