KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > swing > plaf > util > GuaranteedValue


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 /*
20  * GuaranteedValue.java
21  *
22  * Created on March 13, 2004, 7:43 PM
23  */

24
25 package org.netbeans.swing.plaf.util;
26
27 import javax.swing.*;
28 import java.awt.*;
29
30 /** A simple mechanism for guaranteeing the presence of a value in UIDefaults.
31  * Some look and feels (GTK/Synth) don't necessarily provide these colors,
32  * resulting in null pointer exceptions. Since we want to make it easy to
33  * write maintainable components, it is preferable to centralize guaranteeing
34  * the value here, rather than have the codebase littered with null tests and
35  * fallbacks for null colors.
36  *
37  * It will either take on the existing value if present, or used the passed
38  * value if not present. If passed an array of UIManager keys, it will try
39  * them in order, and use the first value that returns non-null from
40  * <code>UIManager.get()</code>.
41  *
42  * Usage:
43  * <pre>
44  * UIManager.put (new GuaranteedValue("controlShadow", Color.LIGHT_GRAY));
45  * or
46  * UIManager.put (new GuaranteedValue(new String[] {"Tree.foreground",
47  * List.foreground", "textText"}, Color.BLACK));
48  * </pre>
49  *
50  * It can also be used to ensure a value matches another value, with a fallback
51  * if it is not present:
52  * <pre>
53  * UIManager.put("TextArea.font", new GuaranteedValue ("Label.font", new Font("Dialog", Font.PLAIN, 11)))
54  *
55  * @author Tim Boudreau
56  */

57 public class GuaranteedValue implements UIDefaults.LazyValue {
58     private Object JavaDoc value;
59     /** Creates a new instance of GuaranteedValue */
60     public GuaranteedValue(String JavaDoc key, Object JavaDoc fallback) {
61         //Be fail fast, so no random exceptions from random components later
62
if (key == null || fallback == null) {
63             throw new NullPointerException JavaDoc ("Null parameters: " + key + ',' + fallback);
64         }
65         
66         value = UIManager.get(key);
67         if (value == null) {
68             value = fallback;
69         }
70     }
71     
72     public GuaranteedValue(String JavaDoc[] keys, Object JavaDoc fallback) {
73         //Be fail fast, so no random exceptions from random components later
74
if (keys == null || fallback == null) {
75             throw new NullPointerException JavaDoc ("Null parameters: " + keys + ',' + fallback);
76         }
77         for (int i=0; i < keys.length; i++) {
78             value = UIManager.get(keys[i]);
79             if (value != null) {
80                 break;
81             }
82         }
83         if (value == null) {
84             value = fallback;
85         }
86     }
87     
88     public Object JavaDoc createValue(UIDefaults table) {
89         return value;
90     }
91     
92     /** Convenience getter of the value as a color - returns null if this
93      * instance was used for some other type */

94     public Color getColor() {
95         Object JavaDoc o = createValue(null);
96         if (o instanceof Color) {
97             return (Color) o;
98         } else {
99             return null;
100         }
101     }
102
103     public Font getFont() {
104         Object JavaDoc o = createValue(null);
105         if (o instanceof Font) {
106             return (Font) o;
107         } else {
108             return null;
109         }
110     }
111 }
112
Popular Tags