~ubuntu-branches/debian/stretch/insubstantial/stretch

« back to all changes in this revision

Viewing changes to substance/src/main/java/org/pushingpixels/substance/api/painter/border/DelegateFractionBasedBorderPainter.java

  • Committer: Package Import Robot
  • Author(s): Felix Natter
  • Date: 2016-01-18 20:58:45 UTC
  • Revision ID: package-import@ubuntu.com-20160118205845-crbmrkda61qsi5qa
Tags: upstream-7.3+dfsg2
ImportĀ upstreamĀ versionĀ 7.3+dfsg2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (c) 2005-2010 Substance Kirill Grouchnikov. All Rights Reserved.
 
3
 *
 
4
 * Redistribution and use in source and binary forms, with or without 
 
5
 * modification, are permitted provided that the following conditions are met:
 
6
 * 
 
7
 *  o Redistributions of source code must retain the above copyright notice, 
 
8
 *    this list of conditions and the following disclaimer. 
 
9
 *     
 
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. 
 
13
 *     
 
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. 
 
17
 *     
 
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. 
 
29
 */
 
30
package org.pushingpixels.substance.api.painter.border;
 
31
 
 
32
import java.awt.*;
 
33
import java.awt.MultipleGradientPaint.CycleMethod;
 
34
 
 
35
import org.pushingpixels.substance.api.*;
 
36
import org.pushingpixels.substance.internal.utils.*;
 
37
 
 
38
/**
 
39
 * Delegate border painter that allows tweaking the visual appearance of
 
40
 * borders.
 
41
 * 
 
42
 * @author Kirill Grouchnikov
 
43
 */
 
44
public class DelegateFractionBasedBorderPainter implements
 
45
                SubstanceBorderPainter {
 
46
        /**
 
47
         * Display name of this border painter.
 
48
         */
 
49
        protected String displayName;
 
50
 
 
51
        /**
 
52
         * The delegate border painter.
 
53
         */
 
54
        protected FractionBasedBorderPainter delegate;
 
55
 
 
56
        /**
 
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
 
60
         * color.
 
61
         */
 
62
        protected int[] masks;
 
63
 
 
64
        /**
 
65
         * Transformation to be applied on the color schemes prior to compute the
 
66
         * colors to be used for border painting.
 
67
         */
 
68
        protected ColorSchemeTransform transform;
 
69
 
 
70
        /**
 
71
         * Creates a new delegate border painter
 
72
         * 
 
73
         * @param displayName
 
74
         *            Display name of this border painter.
 
75
         * @param delegate
 
76
         *            The delegate border painter.
 
77
         * @param masks
 
78
         *            Array of 8-digit hexadecimal masks applied on the relevant
 
79
         *            colors painted by the <code>delegate</code>.
 
80
         * @param transform
 
81
         *            Transformation to be applied on the color schemes prior to
 
82
         *            compute the colors to be used for border painting.
 
83
         */
 
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;
 
92
        }
 
93
 
 
94
        /**
 
95
         * Map of transformed color schemes (to speed up the subsequent lookups).
 
96
         */
 
97
        protected final static LazyResettableHashMap<SubstanceColorScheme> transformMap = new LazyResettableHashMap<SubstanceColorScheme>(
 
98
                        "DelegateBorderPainter");
 
99
 
 
100
        @Override
 
101
        public boolean isPaintingInnerContour() {
 
102
                return false;
 
103
        }
 
104
 
 
105
        @Override
 
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);
 
113
 
 
114
                // shift schemes
 
115
                SubstanceColorScheme scheme = getShiftScheme(borderScheme);
 
116
 
 
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);
 
123
                        // apply masks
 
124
                        color = new Color(this.masks[i] & color.getRGB(), true);
 
125
                        fillColors[i] = color;
 
126
                }
 
127
 
 
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
 
133
                // JTree context.
 
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));
 
140
 
 
141
                MultipleGradientPaint gradient = new LinearGradientPaint(0, 0, 0,
 
142
                                height, fractions, fillColors, CycleMethod.REPEAT);
 
143
                graphics.setPaint(gradient);
 
144
                graphics.draw(contour);
 
145
                graphics.dispose();
 
146
        }
 
147
 
 
148
        @Override
 
149
        public String getDisplayName() {
 
150
                return this.displayName;
 
151
        }
 
152
 
 
153
        /**
 
154
         * Retrieves a transformed color scheme.
 
155
         * 
 
156
         * @param orig
 
157
         *            Original color scheme.
 
158
         * @return Transformed color scheme.
 
159
         */
 
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);
 
167
                }
 
168
                return result;
 
169
        }
 
170
 
 
171
}