| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
package org.pushingpixels.substance.flamingo.ribbon.ui; |
| 31 |
|
| 32 |
import java.awt.*; |
| 33 |
import java.awt.event.MouseEvent; |
| 34 |
import java.beans.PropertyChangeEvent; |
| 35 |
import java.beans.PropertyChangeListener; |
| 36 |
import java.util.Map; |
| 37 |
|
| 38 |
import javax.swing.JButton; |
| 39 |
import javax.swing.JComponent; |
| 40 |
import javax.swing.border.Border; |
| 41 |
import javax.swing.plaf.ComponentUI; |
| 42 |
|
| 43 |
import org.pushingpixels.flamingo.api.common.AbstractCommandButton; |
| 44 |
import org.pushingpixels.flamingo.api.ribbon.RibbonContextualTaskGroup; |
| 45 |
import org.pushingpixels.flamingo.internal.ui.ribbon.BasicRibbonTaskToggleButtonUI; |
| 46 |
import org.pushingpixels.flamingo.internal.ui.ribbon.JRibbonTaskToggleButton; |
| 47 |
import org.pushingpixels.substance.api.*; |
| 48 |
import org.pushingpixels.substance.api.shaper.SubstanceButtonShaper; |
| 49 |
import org.pushingpixels.substance.flamingo.ribbon.RibbonBackgroundDelegate; |
| 50 |
import org.pushingpixels.substance.flamingo.utils.CommandButtonVisualStateTracker; |
| 51 |
import org.pushingpixels.substance.internal.animation.StateTransitionTracker; |
| 52 |
import org.pushingpixels.substance.internal.animation.TransitionAwareUI; |
| 53 |
import org.pushingpixels.substance.internal.painter.DecorationPainterUtils; |
| 54 |
import org.pushingpixels.substance.internal.utils.*; |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
public class SubstanceRibbonTaskToggleButtonUI extends |
| 63 |
BasicRibbonTaskToggleButtonUI implements TransitionAwareUI { |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
protected CommandButtonVisualStateTracker substanceVisualStateTracker; |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
private PropertyChangeListener substancePropertyChangeListener; |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
public static ComponentUI createUI(JComponent comp) { |
| 80 |
return new SubstanceRibbonTaskToggleButtonUI(); |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
private RibbonBackgroundDelegate delegate; |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
public SubstanceRibbonTaskToggleButtonUI() { |
| 92 |
this.delegate = new RibbonBackgroundDelegate(); |
| 93 |
} |
| 94 |
|
| 95 |
@Override |
| 96 |
protected void installDefaults() { |
| 97 |
super.installDefaults(); |
| 98 |
this.commandButton.setOpaque(false); |
| 99 |
this.commandButton.setBorder(new Border() { |
| 100 |
@Override |
| 101 |
public Insets getBorderInsets(Component c) { |
| 102 |
return new Insets(0, 12, 0, 12); |
| 103 |
} |
| 104 |
|
| 105 |
@Override |
| 106 |
public boolean isBorderOpaque() { |
| 107 |
return false; |
| 108 |
} |
| 109 |
|
| 110 |
@Override |
| 111 |
public void paintBorder(Component c, Graphics g, int x, int y, |
| 112 |
int width, int height) { |
| 113 |
} |
| 114 |
}); |
| 115 |
SubstanceLookAndFeel.setDecorationType(this.commandButton, |
| 116 |
DecorationAreaType.GENERAL); |
| 117 |
|
| 118 |
this.commandButton.putClientProperty( |
| 119 |
SubstanceLookAndFeel.COLORIZATION_FACTOR, |
| 120 |
RibbonContextualTaskGroup.HUE_ALPHA); |
| 121 |
} |
| 122 |
|
| 123 |
@Override |
| 124 |
protected void uninstallComponents() { |
| 125 |
DecorationPainterUtils.clearDecorationType(this.commandButton); |
| 126 |
super.uninstallDefaults(); |
| 127 |
} |
| 128 |
|
| 129 |
@Override |
| 130 |
protected void installListeners() { |
| 131 |
super.installListeners(); |
| 132 |
|
| 133 |
this.substanceVisualStateTracker = new CommandButtonVisualStateTracker(); |
| 134 |
this.substanceVisualStateTracker.installListeners(this.commandButton); |
| 135 |
|
| 136 |
this.substancePropertyChangeListener = new PropertyChangeListener() { |
| 137 |
@Override |
| 138 |
public void propertyChange(PropertyChangeEvent evt) { |
| 139 |
if ("contextualGroupHueColor".equals(evt.getPropertyName())) { |
| 140 |
Color newValue = (Color) evt.getNewValue(); |
| 141 |
commandButton.setBackground(newValue); |
| 142 |
} |
| 143 |
} |
| 144 |
}; |
| 145 |
this.commandButton |
| 146 |
.addPropertyChangeListener(this.substancePropertyChangeListener); |
| 147 |
} |
| 148 |
|
| 149 |
@Override |
| 150 |
protected void uninstallListeners() { |
| 151 |
this.substanceVisualStateTracker.uninstallListeners(this.commandButton); |
| 152 |
this.substanceVisualStateTracker = null; |
| 153 |
|
| 154 |
this.commandButton |
| 155 |
.removePropertyChangeListener(this.substancePropertyChangeListener); |
| 156 |
this.substancePropertyChangeListener = null; |
| 157 |
|
| 158 |
super.uninstallListeners(); |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
@Override |
| 168 |
public void paint(Graphics g, JComponent c) { |
| 169 |
this.layoutInfo = this.layoutManager.getLayoutInfo(this.commandButton, |
| 170 |
g); |
| 171 |
|
| 172 |
this.delegate.updateTaskToggleButtonBackground(g, |
| 173 |
(JRibbonTaskToggleButton) this.commandButton); |
| 174 |
this.paintText(g); |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
@Override |
| 185 |
public void update(Graphics g, JComponent c) { |
| 186 |
this.paint(g, c); |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
@Override |
| 197 |
protected void paintText(Graphics g) { |
| 198 |
FontMetrics fm = g.getFontMetrics(); |
| 199 |
String toPaint = this.commandButton.getText(); |
| 200 |
|
| 201 |
|
| 202 |
int fullInsets = this.commandButton.getInsets().left; |
| 203 |
int pw = this.getPreferredSize(this.commandButton).width; |
| 204 |
int mw = this.getMinimumSize(this.commandButton).width; |
| 205 |
int w = this.commandButton.getWidth(); |
| 206 |
int h = this.commandButton.getHeight(); |
| 207 |
int insets = fullInsets - (pw - w) * (fullInsets - 2) / (pw - mw); |
| 208 |
|
| 209 |
|
| 210 |
Rectangle textRect = new Rectangle(insets, |
| 211 |
1 + (h - fm.getHeight()) / 2, w - 2 * insets, fm.getHeight()); |
| 212 |
|
| 213 |
|
| 214 |
while (true) { |
| 215 |
if (toPaint.length() == 0) |
| 216 |
break; |
| 217 |
int strWidth = fm.stringWidth(toPaint); |
| 218 |
if (strWidth <= textRect.width) |
| 219 |
break; |
| 220 |
toPaint = toPaint.substring(0, toPaint.length() - 1); |
| 221 |
} |
| 222 |
|
| 223 |
int finalStrWidth = fm.stringWidth(toPaint); |
| 224 |
if (finalStrWidth < textRect.width) { |
| 225 |
int delta = textRect.width - finalStrWidth; |
| 226 |
textRect.x += delta / 2; |
| 227 |
textRect.width -= delta; |
| 228 |
} |
| 229 |
|
| 230 |
StateTransitionTracker.ModelStateInfo modelStateInfo = this.substanceVisualStateTracker |
| 231 |
.getActionStateTransitionTracker().getModelStateInfo(); |
| 232 |
ComponentState currState = modelStateInfo.getCurrModelState(); |
| 233 |
float buttonAlpha = SubstanceColorSchemeUtilities.getAlpha( |
| 234 |
this.commandButton, currState); |
| 235 |
|
| 236 |
Color fgColor = getForegroundColor(this.commandButton, modelStateInfo); |
| 237 |
|
| 238 |
if (buttonAlpha < 1.0f) { |
| 239 |
Color bgFillColor = SubstanceColorUtilities |
| 240 |
.getBackgroundFillColor(this.commandButton); |
| 241 |
fgColor = SubstanceColorUtilities.getInterpolatedColor(fgColor, |
| 242 |
bgFillColor, buttonAlpha); |
| 243 |
} |
| 244 |
|
| 245 |
SubstanceTextUtilities.paintText(g, this.commandButton, textRect, |
| 246 |
toPaint, -1, this.commandButton.getFont(), fgColor, null); |
| 247 |
} |
| 248 |
|
| 249 |
private static Color getForegroundColor(AbstractCommandButton button, |
| 250 |
StateTransitionTracker.ModelStateInfo modelStateInfo) { |
| 251 |
ComponentState currState = modelStateInfo.getCurrModelState(); |
| 252 |
Map<ComponentState, StateTransitionTracker.StateContributionInfo> activeStates = modelStateInfo |
| 253 |
.getStateContributionMap(); |
| 254 |
|
| 255 |
SubstanceColorScheme colorScheme = SubstanceColorSchemeUtilities |
| 256 |
.getColorScheme( |
| 257 |
button.isFlat() && currState == ComponentState.ENABLED ? button |
| 258 |
.getParent() : button, |
| 259 |
ColorSchemeAssociationKind.TAB, currState); |
| 260 |
if (currState.isDisabled() || (activeStates == null) |
| 261 |
|| (activeStates.size() == 1)) { |
| 262 |
return colorScheme.getForegroundColor(); |
| 263 |
} |
| 264 |
|
| 265 |
float aggrRed = 0; |
| 266 |
float aggrGreen = 0; |
| 267 |
float aggrBlue = 0; |
| 268 |
for (Map.Entry<ComponentState, StateTransitionTracker.StateContributionInfo> activeEntry : activeStates |
| 269 |
.entrySet()) { |
| 270 |
ComponentState activeState = activeEntry.getKey(); |
| 271 |
float alpha = activeEntry.getValue().getContribution(); |
| 272 |
SubstanceColorScheme activeColorScheme = SubstanceColorSchemeUtilities |
| 273 |
.getColorScheme( |
| 274 |
button.isFlat() |
| 275 |
&& activeState == ComponentState.ENABLED ? button |
| 276 |
.getParent() : button, |
| 277 |
ColorSchemeAssociationKind.TAB, activeState); |
| 278 |
Color activeForeground = activeColorScheme.getForegroundColor(); |
| 279 |
aggrRed += alpha * activeForeground.getRed(); |
| 280 |
aggrGreen += alpha * activeForeground.getGreen(); |
| 281 |
aggrBlue += alpha * activeForeground.getBlue(); |
| 282 |
} |
| 283 |
return new Color((int) aggrRed, (int) aggrGreen, (int) aggrBlue); |
| 284 |
} |
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
@Override |
| 293 |
public Dimension getPreferredSize(JComponent c) { |
| 294 |
AbstractCommandButton button = (AbstractCommandButton) c; |
| 295 |
SubstanceButtonShaper shaper = SubstanceCoreUtilities |
| 296 |
.getButtonShaper(button); |
| 297 |
|
| 298 |
Dimension superPref = super.getPreferredSize(button); |
| 299 |
if (superPref == null) |
| 300 |
return null; |
| 301 |
|
| 302 |
if (shaper == null) |
| 303 |
return superPref; |
| 304 |
|
| 305 |
JButton dummy = new JButton(button.getText(), button.getIcon()); |
| 306 |
return shaper.getPreferredSize(dummy, superPref); |
| 307 |
} |
| 308 |
|
| 309 |
@Override |
| 310 |
public StateTransitionTracker getTransitionTracker() { |
| 311 |
return this.substanceVisualStateTracker |
| 312 |
.getActionStateTransitionTracker(); |
| 313 |
} |
| 314 |
|
| 315 |
@Override |
| 316 |
public boolean isInside(MouseEvent me) { |
| 317 |
return true; |
| 318 |
} |
| 319 |
} |