| 1 |
/* |
| 2 |
* Copyright (c) 2005-2010 Flamingo / 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 Flamingo 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.flamingo.common.ui; |
| 31 |
|
| 32 |
import java.awt.*; |
| 33 |
import java.awt.geom.GeneralPath; |
| 34 |
import java.util.Map; |
| 35 |
|
| 36 |
import javax.swing.Icon; |
| 37 |
import javax.swing.JComponent; |
| 38 |
import javax.swing.plaf.ComponentUI; |
| 39 |
import javax.swing.plaf.UIResource; |
| 40 |
|
| 41 |
import org.pushingpixels.flamingo.api.common.AbstractCommandButton; |
| 42 |
import org.pushingpixels.flamingo.api.common.JCommandToggleMenuButton; |
| 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.animation.StateTransitionTracker.ModelStateInfo; |
| 48 |
import org.pushingpixels.substance.internal.utils.*; |
| 49 |
|
| 50 |
/** |
| 51 |
* UI for {@link JCommandToggleMenuButton} components in <b>Substance</b> look |
| 52 |
* and feel. |
| 53 |
* |
| 54 |
* @author Kirill Grouchnikov |
| 55 |
*/ |
| 56 |
public class SubstanceCommandToggleMenuButtonUI extends |
| 57 |
SubstanceCommandToggleButtonUI { |
| 58 |
public static ComponentUI createUI(JComponent c) { |
| 59 |
return new SubstanceCommandToggleMenuButtonUI(); |
| 60 |
} |
| 61 |
|
| 62 |
@Override |
| 63 |
protected void paintButtonIcon(Graphics g, Rectangle iconRect) { |
| 64 |
boolean isSelected = this.commandButton.getActionModel().isSelected(); |
| 65 |
if (isSelected) { |
| 66 |
Graphics2D g2d = (Graphics2D) g.create(); |
| 67 |
Rectangle extended = new Rectangle(iconRect.x - 1, iconRect.y - 1, |
| 68 |
iconRect.width + 1, iconRect.height + 1); |
| 69 |
|
| 70 |
ComponentState currState = this.commandButton.getActionModel() |
| 71 |
.isEnabled() ? ComponentState.SELECTED |
| 72 |
: ComponentState.DISABLED_SELECTED; |
| 73 |
|
| 74 |
SubstanceColorScheme fillScheme = SubstanceColorSchemeUtilities |
| 75 |
.getColorScheme(this.commandButton, |
| 76 |
ColorSchemeAssociationKind.HIGHLIGHT, currState); |
| 77 |
SubstanceFillPainter fillPainter = SubstanceCoreUtilities |
| 78 |
.getFillPainter(this.commandButton); |
| 79 |
fillPainter.paintContourBackground(g2d, this.commandButton, |
| 80 |
extended.width, extended.height, extended, false, |
| 81 |
fillScheme, false); |
| 82 |
|
| 83 |
SubstanceColorScheme borderScheme = SubstanceColorSchemeUtilities |
| 84 |
.getColorScheme(this.commandButton, |
| 85 |
ColorSchemeAssociationKind.HIGHLIGHT_BORDER, |
| 86 |
currState); |
| 87 |
SubstanceBorderPainter borderPainter = SubstanceCoreUtilities |
| 88 |
.getBorderPainter(this.commandButton); |
| 89 |
borderPainter.paintBorder(g2d, this.commandButton, extended.width, |
| 90 |
extended.height, extended, null, borderScheme); |
| 91 |
|
| 92 |
g2d.dispose(); |
| 93 |
} |
| 94 |
super.paintButtonIcon(g, iconRect); |
| 95 |
// does it actually have an icon? |
| 96 |
Icon iconToPaint = this.getIconToPaint(); |
| 97 |
if (isSelected && (iconToPaint == null)) { |
| 98 |
// draw a checkmark |
| 99 |
Graphics2D g2d = (Graphics2D) g.create(); |
| 100 |
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, |
| 101 |
RenderingHints.VALUE_ANTIALIAS_ON); |
| 102 |
ComponentState currState = this.commandButton.getActionModel() |
| 103 |
.isEnabled() ? ComponentState.SELECTED |
| 104 |
: ComponentState.DISABLED_SELECTED; |
| 105 |
SubstanceColorScheme fillScheme = SubstanceColorSchemeUtilities |
| 106 |
.getColorScheme(this.commandButton, |
| 107 |
ColorSchemeAssociationKind.HIGHLIGHT, currState); |
| 108 |
g2d.setColor(fillScheme.getForegroundColor()); |
| 109 |
|
| 110 |
int iw = iconRect.width; |
| 111 |
int ih = iconRect.height; |
| 112 |
GeneralPath path = new GeneralPath(); |
| 113 |
|
| 114 |
path.moveTo(0.2f * iw, 0.5f * ih); |
| 115 |
path.lineTo(0.42f * iw, 0.8f * ih); |
| 116 |
path.lineTo(0.8f * iw, 0.2f * ih); |
| 117 |
g2d.translate(iconRect.x, iconRect.y); |
| 118 |
Stroke stroke = new BasicStroke((float) 0.12 * iw, |
| 119 |
BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND); |
| 120 |
g2d.setStroke(stroke); |
| 121 |
g2d.draw(path); |
| 122 |
|
| 123 |
g2d.dispose(); |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
@Override |
| 128 |
protected boolean isPaintingBackground() { |
| 129 |
boolean isActionRollover = this.commandButton.getActionModel() |
| 130 |
.isRollover(); |
| 131 |
|
| 132 |
if (isActionRollover || !this.commandButton.isFlat()) { |
| 133 |
return true; |
| 134 |
} |
| 135 |
|
| 136 |
return (this.getActionTransitionTracker().getFacetStrength( |
| 137 |
ComponentStateFacet.ROLLOVER) > 0.0f); |
| 138 |
} |
| 139 |
|
| 140 |
@Override |
| 141 |
protected Color getForegroundColor(ModelStateInfo modelStateInfo) { |
| 142 |
Color fgColor = this.commandButton.getForeground(); |
| 143 |
if (fgColor instanceof UIResource) { |
| 144 |
float buttonAlpha = SubstanceColorSchemeUtilities.getAlpha( |
| 145 |
this.commandButton, modelStateInfo.getCurrModelState()); |
| 146 |
|
| 147 |
fgColor = getMenuButtonForegroundColor(this.commandButton, |
| 148 |
modelStateInfo); |
| 149 |
|
| 150 |
if (buttonAlpha < 1.0f) { |
| 151 |
Color bgFillColor = SubstanceColorUtilities |
| 152 |
.getBackgroundFillColor(this.commandButton); |
| 153 |
fgColor = SubstanceColorUtilities.getInterpolatedColor(fgColor, |
| 154 |
bgFillColor, buttonAlpha); |
| 155 |
} |
| 156 |
} |
| 157 |
return fgColor; |
| 158 |
} |
| 159 |
|
| 160 |
private static Color getMenuButtonForegroundColor( |
| 161 |
AbstractCommandButton menuButton, |
| 162 |
StateTransitionTracker.ModelStateInfo modelStateInfo) { |
| 163 |
ComponentState currState = modelStateInfo |
| 164 |
.getCurrModelStateNoSelection(); |
| 165 |
Map<ComponentState, StateTransitionTracker.StateContributionInfo> activeStates = modelStateInfo |
| 166 |
.getStateNoSelectionContributionMap(); |
| 167 |
|
| 168 |
ColorSchemeAssociationKind currAssocKind = ColorSchemeAssociationKind.FILL; |
| 169 |
// use HIGHLIGHT on active and non-rollover menu items |
| 170 |
if (!currState.isDisabled() && (currState != ComponentState.ENABLED) |
| 171 |
&& !currState.isFacetActive(ComponentStateFacet.ROLLOVER)) |
| 172 |
currAssocKind = ColorSchemeAssociationKind.HIGHLIGHT; |
| 173 |
SubstanceColorScheme colorScheme = SubstanceColorSchemeUtilities |
| 174 |
.getColorScheme(menuButton, currAssocKind, currState); |
| 175 |
if (currState.isDisabled() || (activeStates == null) |
| 176 |
|| (activeStates.size() == 1)) { |
| 177 |
return colorScheme.getForegroundColor(); |
| 178 |
} |
| 179 |
|
| 180 |
float aggrRed = 0; |
| 181 |
float aggrGreen = 0; |
| 182 |
float aggrBlue = 0; |
| 183 |
for (Map.Entry<ComponentState, StateTransitionTracker.StateContributionInfo> activeEntry : activeStates |
| 184 |
.entrySet()) { |
| 185 |
ComponentState activeState = activeEntry.getKey(); |
| 186 |
float alpha = activeEntry.getValue().getContribution(); |
| 187 |
ColorSchemeAssociationKind assocKind = ColorSchemeAssociationKind.FILL; |
| 188 |
// use HIGHLIGHT on active and non-rollover menu items |
| 189 |
if (!activeState.isDisabled() |
| 190 |
&& (activeState != ComponentState.ENABLED) |
| 191 |
&& !activeState.isFacetActive(ComponentStateFacet.ROLLOVER)) |
| 192 |
assocKind = ColorSchemeAssociationKind.HIGHLIGHT; |
| 193 |
SubstanceColorScheme activeColorScheme = SubstanceColorSchemeUtilities |
| 194 |
.getColorScheme(menuButton, assocKind, activeState); |
| 195 |
Color activeForeground = activeColorScheme.getForegroundColor(); |
| 196 |
aggrRed += alpha * activeForeground.getRed(); |
| 197 |
aggrGreen += alpha * activeForeground.getGreen(); |
| 198 |
aggrBlue += alpha * activeForeground.getBlue(); |
| 199 |
} |
| 200 |
return new Color((int) aggrRed, (int) aggrGreen, (int) aggrBlue); |
| 201 |
} |
| 202 |
|
| 203 |
} |