001: /*
002: * Copyright (c) 2004 JETA Software, Inc. All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without modification,
005: * are permitted provided that the following conditions are met:
006: *
007: * o Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * o Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * o Neither the name of JETA Software nor the names of its contributors may
015: * be used to endorse or promote products derived from this software without
016: * specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
020: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
021: * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
022: * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
023: * INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
024: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
025: * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
026: * INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
027: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
028: */
029:
030: package com.jeta.forms.colormgr;
031:
032: import java.awt.Color;
033: import java.util.Collection;
034: import java.util.Iterator;
035: import java.util.LinkedList;
036: import java.util.TreeSet;
037:
038: import javax.swing.UIDefaults;
039: import javax.swing.UIManager;
040:
041: /**
042: * The default implementation of ColorManager. All calls for getColor are
043: * forwarded to UIManager.getColor.
044: *
045: * @author Jeff Tassin
046: */
047: public class DefaultColorManager implements ColorManager {
048: /**
049: * A list of color keys (String objects)
050: */
051: private LinkedList m_color_keys;
052:
053: /**
054: * ctor
055: */
056: public DefaultColorManager() {
057: }
058:
059: /**
060: * @return a Collection of color names (String objects) maintained by this
061: * color manager.
062: */
063: public Collection getColorKeys() {
064: if (m_color_keys == null) {
065: m_color_keys = new LinkedList();
066: m_color_keys.add("constant");
067: m_color_keys.add("control");
068:
069: TreeSet default_colors = new TreeSet(
070: String.CASE_INSENSITIVE_ORDER);
071:
072: /** add default UIManager keys for components and borders */
073: try {
074:
075: UIDefaults defs = UIManager.getLookAndFeelDefaults();
076: LinkedList keys = new LinkedList(defs.keySet());
077: Iterator iter = keys.iterator();
078: while (iter.hasNext()) {
079: Object key = iter.next();
080: Object value = defs.get(key);
081: if (value instanceof Color
082: && !("control".equals(key))) {
083: default_colors.add(key.toString());
084: }
085: }
086: } catch (Exception e) {
087: e.printStackTrace();
088: }
089:
090: Iterator iter = default_colors.iterator();
091: while (iter.hasNext()) {
092: m_color_keys.add(iter.next());
093: }
094: // m_color_keys.add( "Table.background" );
095: // m_color_keys.add( "Table.foreground" );
096:
097: }
098: return m_color_keys;
099: }
100:
101: /**
102: * @param colorKey
103: * the name associated with the color to retrieve.
104: * @param defaultColor
105: * the color to return if not color is found associated with
106: * colorKey
107: * @return the color associated with the given color name. It is up to the
108: * implementation to determine this association. Most
109: * implementations will probabaly use the UIManager.getColor( Object
110: * colorName ) call to get the color. However, it is possible for an
111: * implementation to provide custom logic for a subset of
112: * colorNames.
113: */
114: public Color getColor(String colorKey, Color defaultColor) {
115: Color c = UIManager.getColor(colorKey);
116: if (c != null) {
117: /**
118: * we need to do this because some look and feels (e.g. Alloy L&F)
119: * will create their own Color objects that are invalid if the look
120: * and feel is not available on the deployed system
121: */
122: c = new Color(c.getRed(), c.getGreen(), c.getBlue());
123: }
124:
125: return (c == null ? defaultColor : c);
126: }
127:
128: }
|