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

« back to all changes in this revision

Viewing changes to substance/src/main/java/org/pushingpixels/substance/internal/ui/SubstanceCheckBoxUI.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.ui;
 
31
 
 
32
import java.awt.AlphaComposite;
 
33
import java.awt.Graphics2D;
 
34
import java.awt.image.BufferedImage;
 
35
import java.util.Map;
 
36
 
 
37
import javax.swing.*;
 
38
import javax.swing.border.Border;
 
39
import javax.swing.plaf.ComponentUI;
 
40
import javax.swing.plaf.UIResource;
 
41
import javax.swing.plaf.basic.BasicButtonListener;
 
42
 
 
43
import org.pushingpixels.substance.api.*;
 
44
import org.pushingpixels.substance.api.painter.border.SubstanceBorderPainter;
 
45
import org.pushingpixels.substance.api.painter.fill.SubstanceFillPainter;
 
46
import org.pushingpixels.substance.internal.animation.StateTransitionTracker;
 
47
import org.pushingpixels.substance.internal.utils.*;
 
48
 
 
49
/**
 
50
 * UI for check boxes in <b>Substance</b> look and feel.
 
51
 * 
 
52
 * @author Kirill Grouchnikov
 
53
 */
 
54
public class SubstanceCheckBoxUI extends SubstanceRadioButtonUI {
 
55
        /**
 
56
         * Prefix for the checkbox-related properties in the {@link UIManager}.
 
57
         */
 
58
        private final static String propertyPrefix = "CheckBox" + ".";
 
59
 
 
60
        /*
 
61
         * (non-Javadoc)
 
62
         * 
 
63
         * @see javax.swing.plaf.ComponentUI#createUI(javax.swing.JComponent)
 
64
         */
 
65
        public static ComponentUI createUI(JComponent comp) {
 
66
                SubstanceCoreUtilities.testComponentCreationThreadingViolation(comp);
 
67
                return new SubstanceCheckBoxUI((JToggleButton) comp);
 
68
        }
 
69
 
 
70
        /**
 
71
         * Hash map for storing icons.
 
72
         */
 
73
        private static LazyResettableHashMap<Icon> icons = new LazyResettableHashMap<Icon>(
 
74
                        "SubstanceCheckBoxUI");
 
75
 
 
76
        /**
 
77
         * Simple constructor.
 
78
         * 
 
79
         * @param button
 
80
         *            The associated button.
 
81
         */
 
82
        public SubstanceCheckBoxUI(JToggleButton button) {
 
83
                super(button);
 
84
        }
 
85
 
 
86
        /*
 
87
         * (non-Javadoc)
 
88
         * 
 
89
         * @see javax.swing.plaf.basic.BasicRadioButtonUI#getPropertyPrefix()
 
90
         */
 
91
        @Override
 
92
        protected String getPropertyPrefix() {
 
93
                return propertyPrefix;
 
94
        }
 
95
 
 
96
        /*
 
97
         * (non-Javadoc)
 
98
         * 
 
99
         * @see
 
100
         * org.pushingpixels.substance.SubstanceRadioButtonUI#installDefaults(javax
 
101
         * .swing .AbstractButton)
 
102
         */
 
103
        @Override
 
104
        protected void installDefaults(AbstractButton b) {
 
105
                super.installDefaults(b);
 
106
 
 
107
                button.setRolloverEnabled(true);
 
108
 
 
109
                Border border = b.getBorder();
 
110
                if (border == null || border instanceof UIResource) {
 
111
                        b.setBorder(SubstanceSizeUtils.getCheckBoxBorder(SubstanceSizeUtils
 
112
                                        .getComponentFontSize(b), b.getComponentOrientation()
 
113
                                        .isLeftToRight()));
 
114
                }
 
115
        }
 
116
 
 
117
        /**
 
118
         * Returns the icon that matches the current and previous states of the
 
119
         * checkbox.
 
120
         * 
 
121
         * @param button
 
122
         *            Button (should be {@link JCheckBox}).
 
123
         * @param stateTransitionTracker
 
124
         *            state of the checkbox.
 
125
         * @return Matching icon.
 
126
         */
 
127
        private static Icon getIcon(JToggleButton button,
 
128
                        StateTransitionTracker stateTransitionTracker) {
 
129
                StateTransitionTracker.ModelStateInfo modelStateInfo = stateTransitionTracker
 
130
                                .getModelStateInfo();
 
131
                Map<ComponentState, StateTransitionTracker.StateContributionInfo> activeStates = modelStateInfo
 
132
                                .getStateContributionMap();
 
133
 
 
134
                SubstanceFillPainter fillPainter = SubstanceCoreUtilities
 
135
                                .getFillPainter(button);
 
136
                SubstanceBorderPainter borderPainter = SubstanceCoreUtilities
 
137
                                .getBorderPainter(button);
 
138
                ComponentState currState = modelStateInfo.getCurrModelState();
 
139
 
 
140
                SubstanceColorScheme baseFillColorScheme = SubstanceColorSchemeUtilities
 
141
                                .getColorScheme(button, ColorSchemeAssociationKind.FILL,
 
142
                                                currState);
 
143
                SubstanceColorScheme baseMarkColorScheme = SubstanceColorSchemeUtilities
 
144
                                .getColorScheme(button, ColorSchemeAssociationKind.MARK,
 
145
                                                currState);
 
146
                SubstanceColorScheme baseBorderColorScheme = SubstanceColorSchemeUtilities
 
147
                                .getColorScheme(button, ColorSchemeAssociationKind.BORDER,
 
148
                                                currState);
 
149
                float visibility = stateTransitionTracker
 
150
                                .getFacetStrength(ComponentStateFacet.SELECTION);
 
151
                boolean isCheckMarkFadingOut = !currState
 
152
                                .isFacetActive(ComponentStateFacet.SELECTION);
 
153
 
 
154
                int fontSize = SubstanceSizeUtils.getComponentFontSize(button);
 
155
                int checkMarkSize = SubstanceSizeUtils.getCheckBoxMarkSize(fontSize);
 
156
 
 
157
                HashMapKey keyBase = SubstanceCoreUtilities.getHashKey(fontSize,
 
158
                                checkMarkSize, fillPainter.getDisplayName(), borderPainter
 
159
                                                .getDisplayName(),
 
160
                                baseFillColorScheme.getDisplayName(), baseMarkColorScheme
 
161
                                                .getDisplayName(), baseBorderColorScheme
 
162
                                                .getDisplayName(), visibility, isCheckMarkFadingOut);
 
163
                Icon iconBase = icons.get(keyBase);
 
164
                if (iconBase == null) {
 
165
                        iconBase = new ImageIcon(SubstanceImageCreator.getCheckBox(button,
 
166
                                        fillPainter, borderPainter, checkMarkSize, currState,
 
167
                                        baseFillColorScheme, baseMarkColorScheme,
 
168
                                        baseBorderColorScheme, visibility, isCheckMarkFadingOut));
 
169
                        icons.put(keyBase, iconBase);
 
170
                }
 
171
                if (currState.isDisabled() || (activeStates.size() == 1)) {
 
172
                        return iconBase;
 
173
                }
 
174
 
 
175
                BufferedImage result = SubstanceCoreUtilities.getBlankImage(iconBase
 
176
                                .getIconWidth(), iconBase.getIconHeight());
 
177
                Graphics2D g2d = result.createGraphics();
 
178
                // draw the base layer
 
179
                iconBase.paintIcon(button, g2d, 0, 0);
 
180
 
 
181
                // draw other active layers
 
182
                for (Map.Entry<ComponentState, StateTransitionTracker.StateContributionInfo> activeEntry : activeStates
 
183
                                .entrySet()) {
 
184
                        ComponentState activeState = activeEntry.getKey();
 
185
                        // System.out.println("Painting state " + activeState + "[curr is "
 
186
                        // + currState + "] with " + activeEntry.getValue());
 
187
                        if (activeState == currState)
 
188
                                continue;
 
189
 
 
190
                        float stateContribution = activeEntry.getValue().getContribution();
 
191
                        if (stateContribution > 0.0f) {
 
192
                                g2d.setComposite(AlphaComposite.SrcOver
 
193
                                                .derive(stateContribution));
 
194
                                SubstanceColorScheme fillColorScheme = SubstanceColorSchemeUtilities
 
195
                                                .getColorScheme(button,
 
196
                                                                ColorSchemeAssociationKind.FILL, activeState);
 
197
                                SubstanceColorScheme markColorScheme = SubstanceColorSchemeUtilities
 
198
                                                .getColorScheme(button,
 
199
                                                                ColorSchemeAssociationKind.MARK, activeState);
 
200
                                SubstanceColorScheme borderColorScheme = SubstanceColorSchemeUtilities
 
201
                                                .getColorScheme(button,
 
202
                                                                ColorSchemeAssociationKind.BORDER, activeState);
 
203
 
 
204
                                HashMapKey keyLayer = SubstanceCoreUtilities.getHashKey(
 
205
                                                fontSize, checkMarkSize, fillPainter.getDisplayName(),
 
206
                                                borderPainter.getDisplayName(), fillColorScheme
 
207
                                                                .getDisplayName(), markColorScheme
 
208
                                                                .getDisplayName(), borderColorScheme
 
209
                                                                .getDisplayName(), visibility);
 
210
                                Icon iconLayer = icons.get(keyLayer);
 
211
                                if (iconLayer == null) {
 
212
                                        iconLayer = new ImageIcon(SubstanceImageCreator
 
213
                                                        .getCheckBox(button, fillPainter, borderPainter,
 
214
                                                                        checkMarkSize, currState, fillColorScheme,
 
215
                                                                        markColorScheme, borderColorScheme,
 
216
                                                                        visibility, isCheckMarkFadingOut));
 
217
                                        icons.put(keyLayer, iconLayer);
 
218
                                }
 
219
 
 
220
                                iconLayer.paintIcon(button, g2d, 0, 0);
 
221
                        }
 
222
                }
 
223
 
 
224
                g2d.dispose();
 
225
                return new ImageIcon(result);
 
226
        }
 
227
 
 
228
        /*
 
229
         * (non-Javadoc)
 
230
         * 
 
231
         * @see
 
232
         * javax.swing.plaf.basic.BasicButtonUI#createButtonListener(javax.swing
 
233
         * .AbstractButton)
 
234
         */
 
235
        @Override
 
236
        protected BasicButtonListener createButtonListener(AbstractButton b) {
 
237
                return new RolloverButtonListener(b, this.stateTransitionTracker);
 
238
        }
 
239
 
 
240
        /*
 
241
         * (non-Javadoc)
 
242
         * 
 
243
         * @see javax.swing.plaf.basic.BasicRadioButtonUI#getDefaultIcon()
 
244
         */
 
245
        @Override
 
246
        public Icon getDefaultIcon() {
 
247
                return SubstanceCheckBoxUI.getIcon(this.button,
 
248
                                this.stateTransitionTracker);
 
249
        }
 
250
 
 
251
        /**
 
252
         * Returns memory usage string.
 
253
         * 
 
254
         * @return Memory usage string.
 
255
         */
 
256
        public static String getMemoryUsage() {
 
257
                StringBuffer sb = new StringBuffer();
 
258
                sb.append("SubstanceCheckBox: \n");
 
259
                sb.append("\t" + SubstanceCheckBoxUI.icons.size() + " icons");
 
260
                return sb.toString();
 
261
        }
 
262
}