2
* Copyright (c) 2005-2010 Substance Kirill Grouchnikov. All Rights Reserved.
4
* Redistribution and use in source and binary forms, with or without
5
* modification, are permitted provided that the following conditions are met:
7
* o Redistributions of source code must retain the above copyright notice,
8
* this list of conditions and the following disclaimer.
10
* o Redistributions in binary form must reproduce the above copyright notice,
11
* this list of conditions and the following disclaimer in the documentation
12
* and/or other materials provided with the distribution.
14
* o Neither the name of Substance Kirill Grouchnikov nor the names of
15
* its contributors may be used to endorse or promote products derived
16
* from this software without specific prior written permission.
18
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
22
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
27
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
package org.pushingpixels.substance.api.painter.border;
33
import java.awt.MultipleGradientPaint.CycleMethod;
35
import org.pushingpixels.substance.api.*;
36
import org.pushingpixels.substance.internal.utils.*;
39
* Delegate border painter that allows tweaking the visual appearance of
42
* @author Kirill Grouchnikov
44
public class DelegateFractionBasedBorderPainter implements
45
SubstanceBorderPainter {
47
* Display name of this border painter.
49
protected String displayName;
52
* The delegate border painter.
54
protected FractionBasedBorderPainter delegate;
57
* 8-digit hexadecimal masks applied on the colors painted by
58
* {@link #delegate}. Can be used to apply custom translucency. For example,
59
* value 0x80FFFFFF will result in 50% translucency of the original border
62
protected int[] masks;
65
* Transformation to be applied on the color schemes prior to compute the
66
* colors to be used for border painting.
68
protected ColorSchemeTransform transform;
71
* Creates a new delegate border painter
74
* Display name of this border painter.
76
* The delegate border painter.
78
* Array of 8-digit hexadecimal masks applied on the relevant
79
* colors painted by the <code>delegate</code>.
81
* Transformation to be applied on the color schemes prior to
82
* compute the colors to be used for border painting.
84
public DelegateFractionBasedBorderPainter(String displayName,
85
FractionBasedBorderPainter delegate, int[] masks,
86
ColorSchemeTransform transform) {
87
this.displayName = displayName;
88
this.delegate = delegate;
89
this.masks = new int[masks.length];
90
System.arraycopy(masks, 0, this.masks, 0, masks.length);
91
this.transform = transform;
95
* Map of transformed color schemes (to speed up the subsequent lookups).
97
protected final static LazyResettableHashMap<SubstanceColorScheme> transformMap = new LazyResettableHashMap<SubstanceColorScheme>(
98
"DelegateBorderPainter");
101
public boolean isPaintingInnerContour() {
106
public void paintBorder(Graphics g, Component c, int width, int height,
107
Shape contour, Shape innerContour, SubstanceColorScheme borderScheme) {
108
Graphics2D graphics = (Graphics2D) g.create();
109
graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
110
RenderingHints.VALUE_ANTIALIAS_ON);
111
graphics.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL,
112
RenderingHints.VALUE_STROKE_NORMALIZE);
115
SubstanceColorScheme scheme = getShiftScheme(borderScheme);
117
float[] fractions = delegate.getFractions();
118
ColorSchemeSingleColorQuery[] colorQueries = delegate.getColorQueries();
119
Color[] fillColors = new Color[fractions.length];
120
for (int i = 0; i < fractions.length; i++) {
121
ColorSchemeSingleColorQuery colorQuery = colorQueries[i];
122
Color color = colorQuery.query(scheme);
124
color = new Color(this.masks[i] & color.getRGB(), true);
125
fillColors[i] = color;
128
float strokeWidth = SubstanceSizeUtils
129
.getBorderStrokeWidth(SubstanceSizeUtils
130
.getComponentFontSize(c));
131
// issue 433 - the "c" can be null when painting
132
// the border of a tree icon used outside the
134
boolean isSpecialButton = c instanceof SubstanceInternalArrowButton;
135
int joinKind = isSpecialButton ? BasicStroke.JOIN_MITER
136
: BasicStroke.JOIN_ROUND;
137
int capKind = isSpecialButton ? BasicStroke.CAP_SQUARE
138
: BasicStroke.CAP_BUTT;
139
graphics.setStroke(new BasicStroke(strokeWidth, capKind, joinKind));
141
MultipleGradientPaint gradient = new LinearGradientPaint(0, 0, 0,
142
height, fractions, fillColors, CycleMethod.REPEAT);
143
graphics.setPaint(gradient);
144
graphics.draw(contour);
149
public String getDisplayName() {
150
return this.displayName;
154
* Retrieves a transformed color scheme.
157
* Original color scheme.
158
* @return Transformed color scheme.
160
private SubstanceColorScheme getShiftScheme(SubstanceColorScheme orig) {
161
HashMapKey key = SubstanceCoreUtilities.getHashKey(orig
162
.getDisplayName(), this.getDisplayName(), this.transform);
163
SubstanceColorScheme result = transformMap.get(key);
164
if (result == null) {
165
result = this.transform.transform(orig);
166
transformMap.put(key, result);