001: /*
002: * Copyright (c) 2005-2008 Substance Kirill Grouchnikov. All Rights Reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, 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 Substance Kirill Grouchnikov nor the names of
015: * its contributors may be used to endorse or promote products derived
016: * from this software without 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,
020: * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
021: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
022: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
023: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
025: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
026: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
027: * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
028: * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029: */
030: package org.jvnet.substance.border;
031:
032: import java.awt.*;
033: import java.util.Map;
034:
035: import org.jvnet.substance.color.*;
036: import org.jvnet.substance.utils.SoftHashMap;
037: import org.jvnet.substance.utils.SubstanceCoreUtilities;
038: import org.jvnet.substance.utils.SubstanceConstants.ColorShiftKind;
039:
040: /**
041: * Abstract base class for painters that provide inner painting. The
042: * implementation is based on three main parts:
043: * <ul>
044: * <li>The delegate border painter that paints the outer and inner parts of the
045: * border.</li>
046: * <li>Shift kind that specifies how to compute the color scheme of the inner
047: * part based on the color scheme of the border.</li>
048: * <li>Shift coefficient that specifies how different is the color scheme of
049: * the inner part from the color scheme of the border.</li>
050: * </ul>
051: *
052: * @author Kirill Grouchnikov
053: */
054: public abstract class InnerDelegateBorderPainter implements
055: SubstanceBorderPainter {
056: /**
057: * Display name of <code>this</code> painter.
058: */
059: protected String painterName;
060:
061: /**
062: * Mandatory delegate painter.
063: */
064: protected SubstanceBorderPainter delegate;
065:
066: /**
067: * Shift coefficient. Must be in 0.0-1.0 range.
068: */
069: protected float shiftCoef;
070:
071: /**
072: * Color shift kind.
073: */
074: protected ColorShiftKind shiftKind;
075:
076: /**
077: * Map of shifted color schemes (to speed up the subsequent lookups).
078: */
079: protected static Map<String, ColorScheme> shiftMap = new SoftHashMap<String, ColorScheme>();
080:
081: /**
082: * Creates an inner painter.
083: *
084: * @param painterName
085: * Painter display name.
086: * @param delegate
087: * Delegate painter.
088: */
089: public InnerDelegateBorderPainter(String painterName,
090: SubstanceBorderPainter delegate) {
091: this (painterName, delegate, 0.7f, ColorShiftKind.TINT);
092: }
093:
094: /**
095: * Creates an inner painter.
096: *
097: * @param painterName
098: * Painter display name.
099: * @param delegate
100: * Delegate painter.
101: * @param shiftCoef
102: * Shift coefficient. Must be in 0.0-1.0 range.
103: * @param shiftKind
104: * Color shift kind.
105: */
106: public InnerDelegateBorderPainter(String painterName,
107: SubstanceBorderPainter delegate, float shiftCoef,
108: ColorShiftKind shiftKind) {
109: this .painterName = painterName;
110: this .delegate = delegate;
111: this .shiftCoef = shiftCoef;
112: this .shiftKind = shiftKind;
113: }
114:
115: /*
116: * (non-Javadoc)
117: *
118: * @see org.jvnet.substance.border.SubstanceBorderPainter#getDisplayName()
119: */
120: public String getDisplayName() {
121: return this .painterName;
122: }
123:
124: /*
125: * (non-Javadoc)
126: *
127: * @see org.jvnet.substance.border.SubstanceBorderPainter#paintBorder(java.awt.Graphics,
128: * java.awt.Component, int, int, java.awt.Shape, java.awt.Shape,
129: * org.jvnet.substance.color.ColorScheme,
130: * org.jvnet.substance.color.ColorScheme, float, boolean)
131: */
132: public void paintBorder(Graphics g, Component comp, int width,
133: int height, Shape contour, Shape innerContour,
134: ColorScheme colorScheme1, ColorScheme colorScheme2,
135: float cyclePos, boolean useCyclePosAsInterpolation) {
136: if (innerContour != null) {
137: this .delegate.paintBorder(g, comp, width, height,
138: innerContour, null, getShiftScheme(colorScheme1,
139: this .shiftCoef, this .shiftKind),
140: getShiftScheme(colorScheme2, this .shiftCoef,
141: this .shiftKind), cyclePos,
142: useCyclePosAsInterpolation);
143: }
144: this .delegate.paintBorder(g, comp, width, height, contour,
145: null, colorScheme1, colorScheme2, cyclePos,
146: useCyclePosAsInterpolation);
147: }
148:
149: /**
150: * Retrieves a shifted color scheme.
151: *
152: * @param orig
153: * Original color scheme.
154: * @param shiftCoef
155: * Shift coefficient. Must be in 0.0-1.0 range.
156: * @param shiftKind
157: * Shift kind.
158: * @return Shifted color scheme.
159: */
160: private static synchronized ColorScheme getShiftScheme(
161: ColorScheme orig, float shiftCoef, ColorShiftKind shiftKind) {
162: String key = SubstanceCoreUtilities.getSchemeId(orig) + ":"
163: + shiftCoef + ":" + shiftKind.name();
164: if (!shiftMap.containsKey(key)) {
165: switch (shiftKind) {
166: case TINT:
167: shiftMap.put(key, new TintColorScheme(orig, shiftCoef));
168: break;
169: case TONE:
170: shiftMap.put(key, new ToneColorScheme(orig, shiftCoef));
171: break;
172: case SHADE:
173: shiftMap
174: .put(key, new ShadeColorScheme(orig, shiftCoef));
175: break;
176: case THEME_LIGHT:
177: shiftMap.put(key, new ShiftColorScheme(orig, orig
178: .getUltraLightColor().brighter(), shiftCoef));
179: break;
180: }
181: }
182: return shiftMap.get(key);
183: }
184:
185: /**
186: * Returns the painting delegate.
187: *
188: * @return Painting delegate.
189: */
190: public SubstanceBorderPainter getDelegate() {
191: return this.delegate;
192: }
193: }
|