001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package com.sun.rave.propertyeditors.domains;
042:
043: import com.sun.rave.propertyeditors.util.JavaInitializer;
044: import java.lang.Comparable;
045:
046: /**
047: * An individual item in the set of items returned by a {@link Domain}. An
048: * element consists of a value, an optional label (for display purposes), and
049: * an optional description. Any component of an element may be null.
050: */
051:
052: public class Element implements Comparable {
053:
054: /**
055: * An empty array of elements.
056: */
057: public static final Element[] EMPTY_ARRAY = new Element[0];
058:
059: String description = null;
060: String label = null;
061: Object value = null;
062:
063: /**
064: * Construct a new instance that uses value.toString() as the label, and
065: * no description.
066: *
067: * @param value Property value that should be set if this element
068: * is selected
069: */
070: public Element(Object value) {
071: this (value, (value == null ? "" : value.toString()), null);
072: }
073:
074: /**
075: * Construct a new instance that has an element value and localized
076: * label, but no description.
077: *
078: * @param value Property value that should be set if this element
079: * is selected
080: * @param label Localized label for this element value
081: */
082: public Element(Object value, String label) {
083: this (value, label, null);
084: }
085:
086: /**
087: * Construct a new, fully configured, instance.
088: *
089: * @param value Property value that should be set if this element
090: * is selected
091: * @param label Localized label for this element value
092: * @param description Optional localized description of this element
093: */
094: public Element(Object value, String label, String description) {
095: this .value = value;
096: this .label = label;
097: this .description = description;
098: }
099:
100: /**
101: * Return the localized description for this element value. Returns null if
102: * there is no description.
103: */
104: public String getDescription() {
105: return this .description;
106: }
107:
108: /**
109: * Return the localized label for this element value. If not specified,
110: * the Stringified version of the object value is used instead. If no label
111: * or value is present, returns null.
112: */
113: public String getLabel() {
114: return this .label;
115: }
116:
117: /**
118: * Return the element value for this element. Returns null if there is no
119: * value.
120: */
121: public Object getValue() {
122: return this .value;
123: }
124:
125: /**
126: * An element is equal to another if their values are equal.
127: */
128: public boolean equals(Object obj) {
129: if (this == obj)
130: return true;
131: if (!(obj instanceof Element))
132: return false;
133: if (this .value == null)
134: return ((Element) obj).value == null ? true : false;
135: return this .value.equals(((Element) obj).value);
136: }
137:
138: /**
139: * Returns the element's label's hash code.
140: */
141: public int hashCode() {
142: return this .label == null ? 0 : this .label.hashCode();
143: }
144:
145: /**
146: * Compare this element to another element. If the elements' values implement
147: * {@link java.lang.Comparable}, then their values are compared. Otherwise, their
148: * labels are compared.
149: */
150: public int compareTo(Object obj) {
151: if (this == obj)
152: return 0;
153: if (!(obj instanceof Element))
154: return 1;
155: Element that = (Element) obj;
156: if (this .value == null)
157: return that.value == null ? 0 : -1;
158: if (that.value == null)
159: return 1;
160: if (this .value instanceof Comparable)
161: return ((Comparable) this .value).compareTo(that.value);
162: return this .label.compareTo(that.label);
163: }
164:
165: /**
166: * By default, this method passes the element's value to
167: * {@link com.sun.rave.propertyeditors.util.JavaInitializer#toJavaInitializationString(Object)}.
168: * Classes that need special behavior should override this method.
169: */
170: public String getJavaInitializationString() {
171: return JavaInitializer.toJavaInitializationString(this
172: .getValue());
173: }
174:
175: }
|