1 /*
   2  * Copyright (c) 1998, 2014, 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 @SuppressWarnings("serial") // Same-version serialization only
  48 public class Option implements Serializable {
  49 
  50     /**
  51      * Creates a new Option object.
  52      *
  53      * @param attr the attributes associated with the
  54      *  option element.  The attributes are copied to
  55      *  ensure they won't change.
  56      */
  57     public Option(AttributeSet attr) {
  58         this.attr = attr.copyAttributes();
  59         selected = (attr.getAttribute(HTML.Attribute.SELECTED) != null);
  60     }
  61 
  62     /**
  63      * Sets the label to be used for the option.
  64      *
  65      * @param label a label.
  66      */
  67     public void setLabel(String label) {
  68         this.label = label;
  69     }
  70 
  71     /**
  72      * Fetch the label associated with the option.
  73      *
  74      * @return the label associated with the option.
  75      */
  76     public String getLabel() {
  77         return label;
  78     }
  79 
  80     /**
  81      * Fetch the attributes associated with this option.
  82      *
  83      * @return the attributes associated with this option.
  84      */
  85     public AttributeSet getAttributes() {
  86         return attr;
  87     }
  88 
  89     /**
  90      * String representation is the label.
  91      */
  92     public String toString() {
  93         return label;
  94     }
  95 
  96     /**
  97      * Sets the selected state.
  98      *
  99      * @param state a selection state
 100      */
 101     protected void setSelection(boolean state) {
 102         selected = state;
 103     }
 104 
 105     /**
 106      * Fetches the selection state associated with this option.
 107      *
 108      * @return the selection state.
 109      */
 110     public boolean isSelected() {
 111         return selected;
 112     }
 113 
 114     /**
 115      * Convenient method to return the string associated
 116      * with the {@code value} attribute. If the
 117      * {@code value} has not been specified, the {@code label} will be
 118      * returned.
 119      *
 120      * @return the string associated with the {@code value} attribute,
 121      * or {@code label} if the value has been not specified.
 122      */
 123     public String getValue() {
 124         String value = (String) attr.getAttribute(HTML.Attribute.VALUE);
 125         if (value == null) {
 126             value = label;
 127         }
 128         return value;
 129     }
 130 
 131     private boolean selected;
 132     private String label;
 133     private AttributeSet attr;
 134 }