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     public void setLabel(String label) {
  66         this.label = label;
  67     }
  68 
  69     /**
  70      * Fetch the label associated with the option.
  71      */
  72     public String getLabel() {
  73         return label;
  74     }
  75 
  76     /**
  77      * Fetch the attributes associated with this option.
  78      */
  79     public AttributeSet getAttributes() {
  80         return attr;
  81     }
  82 
  83     /**
  84      * String representation is the label.
  85      */
  86     public String toString() {
  87         return label;
  88     }
  89 
  90     /**
  91      * Sets the selected state.
  92      */
  93     protected void setSelection(boolean state) {
  94         selected = state;
  95     }
  96 
  97     /**
  98      * Fetches the selection state associated with this option.
  99      */
 100     public boolean isSelected() {
 101         return selected;
 102     }
 103 
 104     /**
 105      * Convenience method to return the string associated
 106      * with the <code>value</code> attribute.  If the
 107      * value has not been specified, the label will be
 108      * returned.
 109      */
 110     public String getValue() {
 111         String value = (String) attr.getAttribute(HTML.Attribute.VALUE);
 112         if (value == null) {
 113             value = label;
 114         }
 115         return value;
 116     }
 117 
 118     private boolean selected;
 119     private String label;
 120     private AttributeSet attr;
 121 }