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

« back to all changes in this revision

Viewing changes to substance/src/main/java/org/pushingpixels/substance/internal/utils/border/SubstanceTextComponentBorder.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.internal.utils.border;
 
31
 
 
32
import java.awt.*;
 
33
import java.awt.image.BufferedImage;
 
34
import java.util.Map;
 
35
 
 
36
import javax.swing.JComponent;
 
37
import javax.swing.border.Border;
 
38
import javax.swing.plaf.ComponentUI;
 
39
import javax.swing.plaf.UIResource;
 
40
import javax.swing.text.JTextComponent;
 
41
 
 
42
import org.pushingpixels.substance.api.*;
 
43
import org.pushingpixels.substance.internal.animation.StateTransitionTracker;
 
44
import org.pushingpixels.substance.internal.animation.TransitionAwareUI;
 
45
import org.pushingpixels.substance.internal.utils.*;
 
46
 
 
47
/**
 
48
 * Text component border for the <b>Substance</b> look and feel.
 
49
 * 
 
50
 * @author Kirill Grouchnikov
 
51
 */
 
52
public class SubstanceTextComponentBorder implements Border, UIResource {
 
53
        /**
 
54
         * Insets of <code>this</code> border.
 
55
         */
 
56
        protected Insets myInsets;
 
57
 
 
58
        /**
 
59
         * Cache of small border images.
 
60
         */
 
61
        private static LazyResettableHashMap<BufferedImage> smallImageCache = new LazyResettableHashMap<BufferedImage>(
 
62
                        "SubstanceTextComponentBorder");
 
63
 
 
64
        /**
 
65
         * Creates a new border with the specified insets.
 
66
         * 
 
67
         * @param insets
 
68
         *            Insets.
 
69
         */
 
70
        public SubstanceTextComponentBorder(Insets insets) {
 
71
                this.myInsets = new Insets(insets.top, insets.left, insets.bottom,
 
72
                                insets.right);
 
73
        }
 
74
 
 
75
        /**
 
76
         * Paints border instance for the specified component.
 
77
         * 
 
78
         * @param c
 
79
         *            The component.
 
80
         * @param g
 
81
         *            Graphics context.
 
82
         * @param x
 
83
         *            Component left X (in graphics context).
 
84
         * @param y
 
85
         *            Component top Y (in graphics context).
 
86
         * @param width
 
87
         *            Component width.
 
88
         * @param height
 
89
         *            Component height.
 
90
         * @param isEnabled
 
91
         *            Component enabled status.
 
92
         * @param hasFocus
 
93
         *            Component focus ownership status.
 
94
         */
 
95
        private void paintBorder(JComponent c, Graphics g, int x, int y, int width,
 
96
                        int height, boolean isEnabled, boolean hasFocus) {
 
97
                // failsafe for LAF change
 
98
                if (!SubstanceLookAndFeel.isCurrentLookAndFeel()) {
 
99
                        return;
 
100
                }
 
101
 
 
102
                if ((width <= 0) || (height <= 0))
 
103
                        return;
 
104
 
 
105
                Graphics2D graphics = (Graphics2D) g.create();
 
106
 
 
107
                // float cyclePos = 1.0f;
 
108
 
 
109
                float radius = 2.0f * SubstanceSizeUtils
 
110
                                .getClassicButtonCornerRadius(SubstanceSizeUtils
 
111
                                                .getComponentFontSize(c));
 
112
 
 
113
                JTextComponent componentForTransitions = SubstanceCoreUtilities
 
114
                                .getTextComponentForTransitions(c);
 
115
 
 
116
                if (componentForTransitions != null) {
 
117
                        ComponentUI ui = componentForTransitions.getUI();
 
118
                        if (ui instanceof TransitionAwareUI) {
 
119
                                TransitionAwareUI trackable = (TransitionAwareUI) ui;
 
120
                                StateTransitionTracker stateTransitionTracker = trackable
 
121
                                                .getTransitionTracker();
 
122
                                StateTransitionTracker.ModelStateInfo modelStateInfo = stateTransitionTracker
 
123
                                                .getModelStateInfo();
 
124
                                Map<ComponentState, StateTransitionTracker.StateContributionInfo> activeStates = modelStateInfo
 
125
                                                .getStateContributionMap();
 
126
                                ComponentState currState = modelStateInfo.getCurrModelState();
 
127
                                if (currState.isDisabled())
 
128
                                        currState = ComponentState.DISABLED_SELECTED;
 
129
                                if (width * height < 100000) {
 
130
                                        SubstanceColorScheme baseBorderScheme = SubstanceColorSchemeUtilities
 
131
                                                        .getColorScheme(componentForTransitions,
 
132
                                                                        ColorSchemeAssociationKind.BORDER,
 
133
                                                                        currState);
 
134
 
 
135
                                        HashMapKey baseKey = SubstanceCoreUtilities.getHashKey(
 
136
                                                        SubstanceSizeUtils.getComponentFontSize(c), width,
 
137
                                                        height, radius, baseBorderScheme.getDisplayName());
 
138
                                        BufferedImage baseLayer = smallImageCache.get(baseKey);
 
139
                                        float baseAlpha = SubstanceColorSchemeUtilities.getAlpha(c,
 
140
                                                        currState);
 
141
 
 
142
                                        if (baseLayer == null) {
 
143
                                                baseLayer = SubstanceCoreUtilities.getBlankImage(width,
 
144
                                                                height);
 
145
                                                Graphics2D g2d = baseLayer.createGraphics();
 
146
                                                SubstanceImageCreator.paintTextComponentBorder(c, g2d,
 
147
                                                                0, 0, width, height, radius, baseBorderScheme);
 
148
                                                g2d.dispose();
 
149
                                                smallImageCache.put(baseKey, baseLayer);
 
150
                                        }
 
151
 
 
152
                                        graphics.setComposite(AlphaComposite.SrcOver
 
153
                                                        .derive(baseAlpha));
 
154
                                        graphics.drawImage(baseLayer, x, y, null);
 
155
 
 
156
                                        if (!currState.isDisabled() && (activeStates.size() > 1)) {
 
157
                                                for (Map.Entry<ComponentState, StateTransitionTracker.StateContributionInfo> activeEntry : activeStates
 
158
                                                                .entrySet()) {
 
159
                                                        ComponentState activeState = activeEntry.getKey();
 
160
                                                        if (activeState == currState)
 
161
                                                                continue;
 
162
 
 
163
                                                        float contribution = activeEntry.getValue()
 
164
                                                                        .getContribution();
 
165
                                                        if (contribution == 0.0f)
 
166
                                                                continue;
 
167
 
 
168
                                                        SubstanceColorScheme borderScheme = SubstanceColorSchemeUtilities
 
169
                                                                        .getColorScheme(componentForTransitions,
 
170
                                                                                        ColorSchemeAssociationKind.BORDER,
 
171
                                                                                        activeState);
 
172
 
 
173
                                                        HashMapKey key = SubstanceCoreUtilities.getHashKey(
 
174
                                                                        SubstanceSizeUtils.getComponentFontSize(c),
 
175
                                                                        width, height, radius, borderScheme
 
176
                                                                                        .getDisplayName());
 
177
                                                        BufferedImage layer = smallImageCache.get(key);
 
178
                                                        float alpha = SubstanceColorSchemeUtilities
 
179
                                                                        .getAlpha(c, activeState);
 
180
 
 
181
                                                        if (layer == null) {
 
182
                                                                layer = SubstanceCoreUtilities.getBlankImage(
 
183
                                                                                width, height);
 
184
                                                                Graphics2D g2d = layer.createGraphics();
 
185
                                                                SubstanceImageCreator.paintTextComponentBorder(
 
186
                                                                                c, g2d, 0, 0, width, height, radius,
 
187
                                                                                borderScheme);
 
188
                                                                g2d.dispose();
 
189
                                                                smallImageCache.put(key, layer);
 
190
                                                        }
 
191
 
 
192
                                                        graphics.setComposite(AlphaComposite.SrcOver
 
193
                                                                        .derive(alpha * contribution));
 
194
                                                        graphics.drawImage(layer, x, y, null);
 
195
                                                }
 
196
                                        }
 
197
 
 
198
                                } else {
 
199
                                        // for borders larger than 100000 pixels, use simple
 
200
                                        // painting
 
201
                                        graphics.translate(x, y);
 
202
 
 
203
                                        SubstanceColorScheme baseBorderScheme = SubstanceColorSchemeUtilities
 
204
                                                        .getColorScheme(componentForTransitions,
 
205
                                                                        ColorSchemeAssociationKind.BORDER,
 
206
                                                                        currState);
 
207
                                        float baseAlpha = SubstanceColorSchemeUtilities.getAlpha(c,
 
208
                                                        currState);
 
209
                                        graphics.setComposite(AlphaComposite.SrcOver
 
210
                                                        .derive(baseAlpha));
 
211
                                        SubstanceImageCreator.paintSimpleBorder(c, graphics, width,
 
212
                                                        height, baseBorderScheme);
 
213
 
 
214
                                        if (!currState.isDisabled() && (activeStates.size() > 1)) {
 
215
                                                for (Map.Entry<ComponentState, StateTransitionTracker.StateContributionInfo> activeEntry : activeStates
 
216
                                                                .entrySet()) {
 
217
                                                        ComponentState activeState = activeEntry.getKey();
 
218
                                                        if (activeState == currState)
 
219
                                                                continue;
 
220
 
 
221
                                                        float contribution = activeEntry.getValue()
 
222
                                                                        .getContribution();
 
223
                                                        if (contribution == 0.0f)
 
224
                                                                continue;
 
225
 
 
226
                                                        SubstanceColorScheme borderScheme = SubstanceColorSchemeUtilities
 
227
                                                                        .getColorScheme(componentForTransitions,
 
228
                                                                                        ColorSchemeAssociationKind.BORDER,
 
229
                                                                                        activeState);
 
230
                                                        float alpha = SubstanceColorSchemeUtilities
 
231
                                                                        .getAlpha(c, activeState);
 
232
                                                        graphics.setComposite(AlphaComposite.SrcOver
 
233
                                                                        .derive(alpha * contribution));
 
234
                                                        SubstanceImageCreator.paintSimpleBorder(c,
 
235
                                                                        graphics, width, height, borderScheme);
 
236
                                                }
 
237
                                        }
 
238
                                }
 
239
                        }
 
240
                } else {
 
241
                        ComponentState currState = isEnabled ? ComponentState.ENABLED
 
242
                                        : ComponentState.DISABLED_UNSELECTED;
 
243
                        SubstanceColorScheme borderColorScheme = SubstanceColorSchemeUtilities
 
244
                                        .getColorScheme(c, ColorSchemeAssociationKind.BORDER,
 
245
                                                        currState);
 
246
                        if (width * height < 100000) {
 
247
                                HashMapKey hashKey = SubstanceCoreUtilities.getHashKey(
 
248
                                                SubstanceSizeUtils.getComponentFontSize(c), width,
 
249
                                                height, radius, borderColorScheme.getDisplayName());
 
250
                                BufferedImage result = smallImageCache.get(hashKey);
 
251
                                if (result == null) {
 
252
                                        result = SubstanceCoreUtilities
 
253
                                                        .getBlankImage(width, height);
 
254
                                        Graphics2D g2d = result.createGraphics();
 
255
                                        SubstanceImageCreator.paintTextComponentBorder(c, g2d, 0,
 
256
                                                        0, width, height, radius, borderColorScheme);
 
257
                                        g2d.dispose();
 
258
                                        smallImageCache.put(hashKey, result);
 
259
                                }
 
260
                                graphics.drawImage(result, x, y, null);
 
261
                        } else {
 
262
                                // for borders larger than 100000 pixels, use simple
 
263
                                // painting
 
264
                                graphics.translate(x, y);
 
265
                                SubstanceImageCreator.paintSimpleBorder(c, graphics, width,
 
266
                                                height, borderColorScheme);
 
267
                        }
 
268
                }
 
269
 
 
270
                graphics.dispose();
 
271
        }
 
272
 
 
273
        /*
 
274
         * (non-Javadoc)
 
275
         * 
 
276
         * @see javax.swing.border.Border#paintBorder(java.awt.Component,
 
277
         * java.awt.Graphics, int, int, int, int)
 
278
         */
 
279
        @Override
 
280
    public void paintBorder(Component c, Graphics g, int x, int y, int width,
 
281
                        int height) {
 
282
                paintBorder((JComponent) c, g, x, y, width, height, c.isEnabled(), c
 
283
                                .hasFocus());
 
284
        }
 
285
 
 
286
        /*
 
287
         * (non-Javadoc)
 
288
         * 
 
289
         * @see javax.swing.border.Border#getBorderInsets(java.awt.Component)
 
290
         */
 
291
        @Override
 
292
    public Insets getBorderInsets(Component c) {
 
293
                return this.myInsets;
 
294
        }
 
295
 
 
296
        /*
 
297
         * (non-Javadoc)
 
298
         * 
 
299
         * @see javax.swing.border.Border#isBorderOpaque()
 
300
         */
 
301
        @Override
 
302
    public boolean isBorderOpaque() {
 
303
                return false;
 
304
        }
 
305
}