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

« back to all changes in this revision

Viewing changes to substance/src/main/java/org/pushingpixels/substance/api/shaper/StandardButtonShaper.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.shaper;
 
31
 
 
32
import java.awt.Component;
 
33
import java.awt.Dimension;
 
34
import java.awt.Insets;
 
35
import java.awt.geom.GeneralPath;
 
36
import java.util.Set;
 
37
 
 
38
import javax.swing.AbstractButton;
 
39
import javax.swing.Icon;
 
40
import javax.swing.JComponent;
 
41
import javax.swing.border.Border;
 
42
 
 
43
import org.pushingpixels.substance.api.SubstanceConstants;
 
44
import org.pushingpixels.substance.api.SubstanceLookAndFeel;
 
45
import org.pushingpixels.substance.internal.utils.HashMapKey;
 
46
import org.pushingpixels.substance.internal.utils.LazyResettableHashMap;
 
47
import org.pushingpixels.substance.internal.utils.SubstanceCoreUtilities;
 
48
import org.pushingpixels.substance.internal.utils.SubstanceInternalArrowButton;
 
49
import org.pushingpixels.substance.internal.utils.SubstanceOutlineUtilities;
 
50
import org.pushingpixels.substance.internal.utils.SubstanceSizeUtils;
 
51
import org.pushingpixels.substance.internal.utils.border.SubstanceBorder;
 
52
import org.pushingpixels.substance.internal.utils.border.SubstanceButtonBorder;
 
53
 
 
54
/**
 
55
 * Button shaper that returns buttons with completely rounded corners (ala Mac
 
56
 * 10.4). This class is part of officially supported API.
 
57
 * 
 
58
 * @author Kirill Grouchnikov
 
59
 */
 
60
public class StandardButtonShaper implements SubstanceButtonShaper,
 
61
                RectangularButtonShaper {
 
62
        /**
 
63
         * Cache of already computed contours.
 
64
         */
 
65
        private final static LazyResettableHashMap<GeneralPath> contours = new LazyResettableHashMap<GeneralPath>(
 
66
                        "StandardButtonShaper");
 
67
 
 
68
        /*
 
69
         * (non-Javadoc)
 
70
         * 
 
71
         * @see
 
72
         * org.pushingpixels.substance.button.SubstanceButtonShaper#getDisplayName()
 
73
         */
 
74
        @Override
 
75
    public String getDisplayName() {
 
76
                return "Standard";
 
77
        }
 
78
 
 
79
        /*
 
80
         * (non-Javadoc)
 
81
         * 
 
82
         * @see
 
83
         * org.pushingpixels.substance.shaper.SubstanceButtonShaper#getButtonOutline
 
84
         * (javax .swing.AbstractButton, java.awt.Insets, int, int, boolean)
 
85
         */
 
86
        @Override
 
87
        public GeneralPath getButtonOutline(AbstractButton button, Insets insets,
 
88
                        int width, int height, boolean isInner) {
 
89
                Set<SubstanceConstants.Side> straightSides = SubstanceCoreUtilities
 
90
                                .getSides(button, SubstanceLookAndFeel.BUTTON_SIDE_PROPERTY);
 
91
 
 
92
                float radius = this.getCornerRadius(button, insets);
 
93
                if (isInner) {
 
94
                        radius -= (int) SubstanceSizeUtils
 
95
                                        .getBorderStrokeWidth(SubstanceSizeUtils
 
96
                                                        .getComponentFontSize(button));
 
97
                        if (radius < 0.0f)
 
98
                                radius = 0.0f;
 
99
                }
 
100
 
 
101
                HashMapKey key = SubstanceCoreUtilities.getHashKey(width, height,
 
102
                                straightSides, radius, insets);
 
103
 
 
104
                GeneralPath result = contours.get(key);
 
105
                if (result != null) {
 
106
                        return result;
 
107
                }
 
108
 
 
109
                result = SubstanceOutlineUtilities.getBaseOutline(width, height,
 
110
                                radius, straightSides, insets);
 
111
                contours.put(key, result);
 
112
                return result;
 
113
        }
 
114
 
 
115
        /*
 
116
         * (non-Javadoc)
 
117
         * 
 
118
         * @see
 
119
         * org.pushingpixels.substance.button.SubstanceButtonShaper#getButtonBorder
 
120
         * (javax .swing.AbstractButton)
 
121
         */
 
122
        @Override
 
123
    public Border getButtonBorder(final AbstractButton button) {
 
124
                return new SubstanceButtonBorder(StandardButtonShaper.class) {
 
125
                        @Override
 
126
            public Insets getBorderInsets(Component c) {
 
127
                                int fontSize = SubstanceSizeUtils.getComponentFontSize(button);
 
128
                                Insets buttonInsets = SubstanceSizeUtils
 
129
                                                .getButtonInsets(fontSize);
 
130
                                int focusPadding = SubstanceSizeUtils
 
131
                                                .getFocusRingPadding(fontSize);
 
132
                                int lrPadding = SubstanceCoreUtilities.hasText(button) ? SubstanceSizeUtils
 
133
                                                .getTextButtonLRPadding(fontSize)
 
134
                                                : 0;
 
135
                                Set<SubstanceConstants.Side> openSides = SubstanceCoreUtilities
 
136
                                                .getSides(button,
 
137
                                                                SubstanceLookAndFeel.BUTTON_OPEN_SIDE_PROPERTY);
 
138
                                int left = lrPadding
 
139
                                                + buttonInsets.left
 
140
                                                + focusPadding
 
141
                                                + ((openSides != null)
 
142
                                                                && openSides
 
143
                                                                                .contains(SubstanceConstants.Side.LEFT) ? -1
 
144
                                                                : 0);
 
145
                                int right = lrPadding
 
146
                                                + buttonInsets.right
 
147
                                                + focusPadding
 
148
                                                + ((openSides != null)
 
149
                                                                && openSides
 
150
                                                                                .contains(SubstanceConstants.Side.RIGHT) ? -1
 
151
                                                                : 0);
 
152
                                int top = buttonInsets.top
 
153
                                                + ((openSides != null)
 
154
                                                                && openSides
 
155
                                                                                .contains(SubstanceConstants.Side.TOP) ? -1
 
156
                                                                : 0);
 
157
                                int bottom = buttonInsets.bottom
 
158
                                                + ((openSides != null)
 
159
                                                                && openSides
 
160
                                                                                .contains(SubstanceConstants.Side.BOTTOM) ? -1
 
161
                                                                : 0);
 
162
                                return new Insets(top, left, bottom, right);
 
163
                        }
 
164
                };
 
165
        }
 
166
 
 
167
        /*
 
168
         * (non-Javadoc)
 
169
         * 
 
170
         * @see
 
171
         * org.pushingpixels.substance.button.SubstanceButtonShaper#getPreferredSize
 
172
         * (javax .swing.AbstractButton, java.awt.Dimension)
 
173
         */
 
174
        @Override
 
175
    public Dimension getPreferredSize(AbstractButton button,
 
176
                        Dimension uiPreferredSize) {
 
177
                Dimension result;
 
178
                boolean toTweakWidth = false;
 
179
                boolean toTweakHeight = false;
 
180
 
 
181
                Icon icon = button.getIcon();
 
182
                boolean hasIcon = SubstanceCoreUtilities.hasIcon(button);
 
183
                boolean hasText = SubstanceCoreUtilities.hasText(button);
 
184
                Insets margin = button.getMargin();
 
185
 
 
186
                result = uiPreferredSize;
 
187
 
 
188
                boolean hasNoMinSizeProperty = SubstanceCoreUtilities
 
189
                                .hasNoMinSizeProperty(button);
 
190
                if ((!hasNoMinSizeProperty) && hasText) {
 
191
                        int baseWidth = uiPreferredSize.width;
 
192
                        baseWidth = Math.max(baseWidth + uiPreferredSize.height,
 
193
                                        SubstanceSizeUtils.getMinButtonWidth(SubstanceSizeUtils
 
194
                                                        .getComponentFontSize(button)));
 
195
                        // if (baseWidth < DEFAULT_WIDTH) {
 
196
                        // baseWidth = DEFAULT_WIDTH;
 
197
                        // }
 
198
                        result = new Dimension(baseWidth, uiPreferredSize.height);
 
199
                        int baseHeight = result.height;
 
200
                        // baseHeight = Math.max(baseHeight, SubstanceSizeUtils
 
201
                        // .getMinButtonHeight(SubstanceSizeUtils
 
202
                        // .getComponentFontSize(button)));
 
203
                        result = new Dimension(result.width, baseHeight);
 
204
                } else {
 
205
                        if (hasNoMinSizeProperty) {
 
206
                                if (margin != null) {
 
207
                                        result = new Dimension(result.width + margin.left
 
208
                                                        + margin.right, result.height + margin.top
 
209
                                                        + margin.bottom);
 
210
                                }
 
211
                        }
 
212
                }
 
213
 
 
214
                int extraPadding = SubstanceSizeUtils
 
215
                                .getExtraPadding(SubstanceSizeUtils
 
216
                                                .getComponentFontSize(button));
 
217
                int iconPaddingWidth = 6 + 2 * extraPadding;
 
218
                int iconPaddingHeight = 6 + 2 * extraPadding;
 
219
                if (margin != null) {
 
220
                        iconPaddingWidth = Math.max(iconPaddingWidth, margin.left
 
221
                                        + margin.right);
 
222
                        iconPaddingHeight = Math.max(iconPaddingHeight, margin.top
 
223
                                        + margin.bottom);
 
224
                }
 
225
                if (hasIcon) {
 
226
                        // check the icon height
 
227
                        int iconHeight = icon.getIconHeight();
 
228
                        if (iconHeight > (result.getHeight() - iconPaddingHeight)) {
 
229
                                result = new Dimension(result.width, iconHeight);
 
230
                                toTweakHeight = true;
 
231
                        }
 
232
                        int iconWidth = icon.getIconWidth();
 
233
                        if (iconWidth > (result.getWidth() - iconPaddingWidth)) {
 
234
                                result = new Dimension(iconWidth, result.height);
 
235
                                toTweakWidth = true;
 
236
                        }
 
237
                }
 
238
 
 
239
                if (SubstanceCoreUtilities.isScrollBarButton(button)) {
 
240
                        toTweakWidth = false;
 
241
                        toTweakHeight = false;
 
242
                }
 
243
 
 
244
                if (toTweakWidth) {
 
245
                        result = new Dimension(result.width + iconPaddingWidth,
 
246
                                        result.height);
 
247
                }
 
248
                if (toTweakHeight) {
 
249
                        result = new Dimension(result.width, result.height
 
250
                                        + iconPaddingHeight);
 
251
                }
 
252
 
 
253
                if (result.height % 2 != 0)
 
254
                        result.height++;
 
255
 
 
256
                return result;
 
257
        }
 
258
 
 
259
        /**
 
260
         * Returns indication whether the specified button should be drawn with
 
261
         * completely round corners.
 
262
         * 
 
263
         * @param button
 
264
         *            A button.
 
265
         * @return <code>true</code> if the specified button should be drawn with
 
266
         *         completely round corners, <code>false</code> otherwise.
 
267
         */
 
268
        public static boolean isRoundButton(AbstractButton button) {
 
269
                return (!SubstanceCoreUtilities.isComboBoxButton(button))
 
270
                                && (!SubstanceCoreUtilities.isScrollButton(button))
 
271
                                && SubstanceCoreUtilities.hasText(button);
 
272
        }
 
273
 
 
274
        /*
 
275
         * (non-Javadoc)
 
276
         * 
 
277
         * @see
 
278
         * org.pushingpixels.substance.button.SubstanceButtonShaper#isProportionate
 
279
         * ()
 
280
         */
 
281
        @Override
 
282
    public boolean isProportionate() {
 
283
                return true;
 
284
        }
 
285
 
 
286
        /*
 
287
         * (non-Javadoc)
 
288
         * 
 
289
         * @see
 
290
         * org.pushingpixels.substance.shaper.RectangularButtonShaper#getCornerRadius
 
291
         * (javax .swing.JComponent, java.awt.Insets)
 
292
         */
 
293
        @Override
 
294
        public float getCornerRadius(AbstractButton button, Insets insets) {
 
295
                int width = button.getWidth();
 
296
                int height = button.getHeight();
 
297
 
 
298
                boolean isRoundCorners = isRoundButton(button);
 
299
                float radius = SubstanceSizeUtils
 
300
                                .getClassicButtonCornerRadius(SubstanceSizeUtils
 
301
                                                .getComponentFontSize(button));
 
302
                if (button instanceof SubstanceInternalArrowButton) {
 
303
                        Border parentBorder = ((JComponent) button.getParent()).getBorder();
 
304
                        if (parentBorder instanceof SubstanceBorder) {
 
305
                                radius *= ((SubstanceBorder) parentBorder)
 
306
                                                .getRadiusScaleFactor();
 
307
                        }
 
308
                }
 
309
 
 
310
                if (insets != null) {
 
311
                        width -= (insets.left + insets.right);
 
312
                        height -= (insets.top + insets.bottom);
 
313
                }
 
314
                if (isRoundCorners) {
 
315
                        if (width > height) {
 
316
                                radius = (height) / 2.0f;
 
317
                        } else {
 
318
                                radius = (width) / 2.0f;
 
319
                        }
 
320
                }
 
321
 
 
322
                if (SubstanceCoreUtilities.isToolBarButton(button)) {
 
323
                        radius = SubstanceCoreUtilities.getToolbarButtonCornerRadius(
 
324
                                        button, insets);
 
325
                }
 
326
                return radius;
 
327
        }
 
328
}