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-2006 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: /*
042: * GuaranteedValue.java
043: *
044: * Created on March 13, 2004, 7:43 PM
045: */
046:
047: package org.netbeans.swing.plaf.util;
048:
049: import javax.swing.*;
050: import java.awt.*;
051:
052: /** A simple mechanism for guaranteeing the presence of a value in UIDefaults.
053: * Some look and feels (GTK/Synth) don't necessarily provide these colors,
054: * resulting in null pointer exceptions. Since we want to make it easy to
055: * write maintainable components, it is preferable to centralize guaranteeing
056: * the value here, rather than have the codebase littered with null tests and
057: * fallbacks for null colors.
058: *
059: * It will either take on the existing value if present, or used the passed
060: * value if not present. If passed an array of UIManager keys, it will try
061: * them in order, and use the first value that returns non-null from
062: * <code>UIManager.get()</code>.
063: *
064: * Usage:
065: * <pre>
066: * UIManager.put (new GuaranteedValue("controlShadow", Color.LIGHT_GRAY));
067: * or
068: * UIManager.put (new GuaranteedValue(new String[] {"Tree.foreground",
069: * List.foreground", "textText"}, Color.BLACK));
070: * </pre>
071: *
072: * It can also be used to ensure a value matches another value, with a fallback
073: * if it is not present:
074: * <pre>
075: * UIManager.put("TextArea.font", new GuaranteedValue ("Label.font", new Font("Dialog", Font.PLAIN, 11)))
076: *
077: * @author Tim Boudreau
078: */
079: public class GuaranteedValue implements UIDefaults.LazyValue {
080: private Object value;
081:
082: /** Creates a new instance of GuaranteedValue */
083: public GuaranteedValue(String key, Object fallback) {
084: //Be fail fast, so no random exceptions from random components later
085: if (key == null || fallback == null) {
086: throw new NullPointerException("Null parameters: " + key
087: + ',' + fallback);
088: }
089:
090: value = UIManager.get(key);
091: if (value == null) {
092: value = fallback;
093: }
094: }
095:
096: public GuaranteedValue(String[] keys, Object fallback) {
097: //Be fail fast, so no random exceptions from random components later
098: if (keys == null || fallback == null) {
099: throw new NullPointerException("Null parameters: " + keys
100: + ',' + fallback);
101: }
102: for (int i = 0; i < keys.length; i++) {
103: value = UIManager.get(keys[i]);
104: if (value != null) {
105: break;
106: }
107: }
108: if (value == null) {
109: value = fallback;
110: }
111: }
112:
113: public Object createValue(UIDefaults table) {
114: return value;
115: }
116:
117: /** Convenience getter of the value as a color - returns null if this
118: * instance was used for some other type */
119: public Color getColor() {
120: Object o = createValue(null);
121: if (o instanceof Color) {
122: return (Color) o;
123: } else {
124: return null;
125: }
126: }
127:
128: public Font getFont() {
129: Object o = createValue(null);
130: if (o instanceof Font) {
131: return (Font) o;
132: } else {
133: return null;
134: }
135: }
136: }
|