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.internal.ui;
32
import java.awt.AlphaComposite;
33
import java.awt.Graphics2D;
34
import java.awt.image.BufferedImage;
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;
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.*;
50
* UI for check boxes in <b>Substance</b> look and feel.
52
* @author Kirill Grouchnikov
54
public class SubstanceCheckBoxUI extends SubstanceRadioButtonUI {
56
* Prefix for the checkbox-related properties in the {@link UIManager}.
58
private final static String propertyPrefix = "CheckBox" + ".";
63
* @see javax.swing.plaf.ComponentUI#createUI(javax.swing.JComponent)
65
public static ComponentUI createUI(JComponent comp) {
66
SubstanceCoreUtilities.testComponentCreationThreadingViolation(comp);
67
return new SubstanceCheckBoxUI((JToggleButton) comp);
71
* Hash map for storing icons.
73
private static LazyResettableHashMap<Icon> icons = new LazyResettableHashMap<Icon>(
74
"SubstanceCheckBoxUI");
80
* The associated button.
82
public SubstanceCheckBoxUI(JToggleButton button) {
89
* @see javax.swing.plaf.basic.BasicRadioButtonUI#getPropertyPrefix()
92
protected String getPropertyPrefix() {
93
return propertyPrefix;
100
* org.pushingpixels.substance.SubstanceRadioButtonUI#installDefaults(javax
101
* .swing .AbstractButton)
104
protected void installDefaults(AbstractButton b) {
105
super.installDefaults(b);
107
button.setRolloverEnabled(true);
109
Border border = b.getBorder();
110
if (border == null || border instanceof UIResource) {
111
b.setBorder(SubstanceSizeUtils.getCheckBoxBorder(SubstanceSizeUtils
112
.getComponentFontSize(b), b.getComponentOrientation()
118
* Returns the icon that matches the current and previous states of the
122
* Button (should be {@link JCheckBox}).
123
* @param stateTransitionTracker
124
* state of the checkbox.
125
* @return Matching icon.
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();
134
SubstanceFillPainter fillPainter = SubstanceCoreUtilities
135
.getFillPainter(button);
136
SubstanceBorderPainter borderPainter = SubstanceCoreUtilities
137
.getBorderPainter(button);
138
ComponentState currState = modelStateInfo.getCurrModelState();
140
SubstanceColorScheme baseFillColorScheme = SubstanceColorSchemeUtilities
141
.getColorScheme(button, ColorSchemeAssociationKind.FILL,
143
SubstanceColorScheme baseMarkColorScheme = SubstanceColorSchemeUtilities
144
.getColorScheme(button, ColorSchemeAssociationKind.MARK,
146
SubstanceColorScheme baseBorderColorScheme = SubstanceColorSchemeUtilities
147
.getColorScheme(button, ColorSchemeAssociationKind.BORDER,
149
float visibility = stateTransitionTracker
150
.getFacetStrength(ComponentStateFacet.SELECTION);
151
boolean isCheckMarkFadingOut = !currState
152
.isFacetActive(ComponentStateFacet.SELECTION);
154
int fontSize = SubstanceSizeUtils.getComponentFontSize(button);
155
int checkMarkSize = SubstanceSizeUtils.getCheckBoxMarkSize(fontSize);
157
HashMapKey keyBase = SubstanceCoreUtilities.getHashKey(fontSize,
158
checkMarkSize, fillPainter.getDisplayName(), borderPainter
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);
171
if (currState.isDisabled() || (activeStates.size() == 1)) {
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);
181
// draw other active layers
182
for (Map.Entry<ComponentState, StateTransitionTracker.StateContributionInfo> activeEntry : activeStates
184
ComponentState activeState = activeEntry.getKey();
185
// System.out.println("Painting state " + activeState + "[curr is "
186
// + currState + "] with " + activeEntry.getValue());
187
if (activeState == currState)
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);
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);
220
iconLayer.paintIcon(button, g2d, 0, 0);
225
return new ImageIcon(result);
232
* javax.swing.plaf.basic.BasicButtonUI#createButtonListener(javax.swing
236
protected BasicButtonListener createButtonListener(AbstractButton b) {
237
return new RolloverButtonListener(b, this.stateTransitionTracker);
243
* @see javax.swing.plaf.basic.BasicRadioButtonUI#getDefaultIcon()
246
public Icon getDefaultIcon() {
247
return SubstanceCheckBoxUI.getIcon(this.button,
248
this.stateTransitionTracker);
252
* Returns memory usage string.
254
* @return Memory usage string.
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();