| 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.event.MouseEvent; |
| 34 |
import java.awt.image.BufferedImage; |
| 35 |
import java.beans.PropertyChangeEvent; |
| 36 |
import java.beans.PropertyChangeListener; |
| 37 |
import java.util.Map; |
| 38 |
|
| 39 |
import javax.swing.*; |
| 40 |
import javax.swing.border.Border; |
| 41 |
import javax.swing.plaf.*; |
| 42 |
|
| 43 |
import org.pushingpixels.flamingo.api.bcb.JBreadcrumbBar; |
| 44 |
import org.pushingpixels.flamingo.api.common.*; |
| 45 |
import org.pushingpixels.flamingo.api.common.CommandButtonLayoutManager.CommandButtonSeparatorOrientation; |
| 46 |
import org.pushingpixels.flamingo.api.common.JCommandButton.CommandButtonKind; |
| 47 |
import org.pushingpixels.flamingo.api.common.JCommandButton.CommandButtonPopupOrientationKind; |
| 48 |
import org.pushingpixels.flamingo.api.common.icon.ResizableIcon; |
| 49 |
import org.pushingpixels.flamingo.api.common.model.PopupButtonModel; |
| 50 |
import org.pushingpixels.flamingo.api.common.popup.JCommandPopupMenu; |
| 51 |
import org.pushingpixels.flamingo.api.ribbon.AbstractRibbonBand; |
| 52 |
import org.pushingpixels.flamingo.internal.ui.common.BasicCommandButtonUI; |
| 53 |
import org.pushingpixels.flamingo.internal.ui.common.ResizableIconUIResource; |
| 54 |
import org.pushingpixels.flamingo.internal.utils.FlamingoUtilities; |
| 55 |
import org.pushingpixels.lafwidget.LafWidgetUtilities; |
| 56 |
import org.pushingpixels.lafwidget.animation.effects.GhostPaintingUtils; |
| 57 |
import org.pushingpixels.lafwidget.animation.effects.GhostingListener; |
| 58 |
import org.pushingpixels.substance.api.*; |
| 59 |
import org.pushingpixels.substance.api.shaper.ClassicButtonShaper; |
| 60 |
import org.pushingpixels.substance.api.shaper.SubstanceButtonShaper; |
| 61 |
import org.pushingpixels.substance.flamingo.common.TransitionAwareResizableIcon; |
| 62 |
import org.pushingpixels.substance.flamingo.utils.*; |
| 63 |
import org.pushingpixels.substance.internal.animation.StateTransitionTracker; |
| 64 |
import org.pushingpixels.substance.internal.animation.StateTransitionTracker.ModelStateInfo; |
| 65 |
import org.pushingpixels.substance.internal.painter.SeparatorPainterUtils; |
| 66 |
import org.pushingpixels.substance.internal.utils.*; |
| 67 |
import org.pushingpixels.substance.internal.utils.icon.TransitionAware; |
| 68 |
|
| 69 |
/** |
| 70 |
* UI for command buttons {@link JCommandButton} in <b>Substance </b> look and |
| 71 |
* feel. |
| 72 |
* |
| 73 |
* @author Kirill Grouchnikov |
| 74 |
*/ |
| 75 |
public class SubstanceCommandButtonUI extends BasicCommandButtonUI implements |
| 76 |
ActionPopupTransitionAwareUI { |
| 77 |
/** |
| 78 |
* Delegate for painting the background. |
| 79 |
*/ |
| 80 |
protected ButtonBackgroundDelegate backgroundDelegate; |
| 81 |
|
| 82 |
/** |
| 83 |
* Property change listener. Listens on changes to |
| 84 |
* {@link AbstractButton#MODEL_CHANGED_PROPERTY} property. |
| 85 |
*/ |
| 86 |
protected PropertyChangeListener substancePropertyListener; |
| 87 |
|
| 88 |
/** |
| 89 |
* Model change listener for ghost image effects. |
| 90 |
*/ |
| 91 |
private GhostingListener substanceModelChangeListener; |
| 92 |
|
| 93 |
/** |
| 94 |
* Tracker for visual state transitions. |
| 95 |
*/ |
| 96 |
protected CommandButtonVisualStateTracker substanceVisualStateTracker; |
| 97 |
|
| 98 |
private ButtonModel overallRolloverModel; |
| 99 |
|
| 100 |
protected RolloverControlListener substanceOverallRolloverListener; |
| 101 |
|
| 102 |
protected StateTransitionTracker overallStateTransitionTracker; |
| 103 |
|
| 104 |
/* |
| 105 |
* (non-Javadoc) |
| 106 |
* |
| 107 |
* @see javax.swing.plaf.ComponentUI#createUI(javax.swing.JComponent) |
| 108 |
*/ |
| 109 |
public static ComponentUI createUI(JComponent comp) { |
| 110 |
SubstanceCoreUtilities.testComponentCreationThreadingViolation(comp); |
| 111 |
return new SubstanceCommandButtonUI((JCommandButton) comp); |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Creates a new UI delegate for ribbon button. |
| 116 |
*/ |
| 117 |
public SubstanceCommandButtonUI(JCommandButton button) { |
| 118 |
super(); |
| 119 |
this.backgroundDelegate = new ButtonBackgroundDelegate(); |
| 120 |
|
| 121 |
this.overallRolloverModel = new DefaultButtonModel(); |
| 122 |
this.overallRolloverModel.setArmed(false); |
| 123 |
this.overallRolloverModel.setSelected(false); |
| 124 |
this.overallRolloverModel.setPressed(false); |
| 125 |
this.overallRolloverModel.setRollover(false); |
| 126 |
this.overallRolloverModel.setEnabled(button.isEnabled()); |
| 127 |
|
| 128 |
this.overallStateTransitionTracker = new StateTransitionTracker(button, |
| 129 |
this.overallRolloverModel); |
| 130 |
|
| 131 |
this.substanceVisualStateTracker = new CommandButtonVisualStateTracker(); |
| 132 |
} |
| 133 |
|
| 134 |
/* |
| 135 |
* (non-Javadoc) |
| 136 |
* |
| 137 |
* @see org.jvnet.flamingo.ribbon.ui.BasiccommandButtonUI#installDefaults() |
| 138 |
*/ |
| 139 |
@Override |
| 140 |
protected void installDefaults() { |
| 141 |
super.installDefaults(); |
| 142 |
this.commandButton.putClientProperty( |
| 143 |
SubstanceLookAndFeel.BUTTON_SHAPER_PROPERTY, |
| 144 |
ClassicButtonShaper.INSTANCE); |
| 145 |
|
| 146 |
this.commandButton.setOpaque(false); |
| 147 |
} |
| 148 |
|
| 149 |
/* |
| 150 |
* (non-Javadoc) |
| 151 |
* |
| 152 |
* @see org.jvnet.flamingo.common.ui.BasicCommandButtonUI#updateBorder() |
| 153 |
*/ |
| 154 |
@Override |
| 155 |
protected void updateBorder() { |
| 156 |
Border currBorder = this.commandButton.getBorder(); |
| 157 |
if ((currBorder == null) || (currBorder instanceof UIResource)) { |
| 158 |
Insets extra = SubstanceSizeUtils |
| 159 |
.getDefaultBorderInsets(SubstanceSizeUtils |
| 160 |
.getComponentFontSize(this.commandButton)); |
| 161 |
double hgapScaleFactor = this.commandButton.getHGapScaleFactor(); |
| 162 |
double vgapScaleFactor = this.commandButton.getVGapScaleFactor(); |
| 163 |
|
| 164 |
int top = 1 + (int) (vgapScaleFactor * extra.top); |
| 165 |
int left = 2 + (int) (hgapScaleFactor * (1 + extra.left)); |
| 166 |
int bottom = 0 + (int) (vgapScaleFactor * extra.bottom); |
| 167 |
int right = 2 + (int) (hgapScaleFactor * (1 + extra.right)); |
| 168 |
this.commandButton |
| 169 |
.setBorder(new BorderUIResource.EmptyBorderUIResource(top, |
| 170 |
left, bottom, right)); |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
/* |
| 175 |
* (non-Javadoc) |
| 176 |
* |
| 177 |
* @see org.jvnet.flamingo.ribbon.ui.BasiccommandButtonUI#installListeners() |
| 178 |
*/ |
| 179 |
@Override |
| 180 |
protected void installListeners() { |
| 181 |
super.installListeners(); |
| 182 |
|
| 183 |
this.substanceVisualStateTracker.installListeners(this.commandButton); |
| 184 |
|
| 185 |
this.substancePropertyListener = new PropertyChangeListener() { |
| 186 |
@Override |
| 187 |
public void propertyChange(PropertyChangeEvent evt) { |
| 188 |
if ("actionModel".equals(evt.getPropertyName())) { |
| 189 |
if (substanceModelChangeListener != null) |
| 190 |
substanceModelChangeListener.unregisterListeners(); |
| 191 |
substanceModelChangeListener = new GhostingListener( |
| 192 |
commandButton, commandButton.getActionModel()); |
| 193 |
substanceModelChangeListener.registerListeners(); |
| 194 |
} |
| 195 |
if ("enabled".equals(evt.getPropertyName())) { |
| 196 |
overallRolloverModel.setEnabled(commandButton.isEnabled()); |
| 197 |
} |
| 198 |
} |
| 199 |
}; |
| 200 |
this.commandButton |
| 201 |
.addPropertyChangeListener(this.substancePropertyListener); |
| 202 |
|
| 203 |
this.substanceModelChangeListener = new GhostingListener( |
| 204 |
this.commandButton, this.commandButton.getActionModel()); |
| 205 |
this.substanceModelChangeListener.registerListeners(); |
| 206 |
|
| 207 |
this.substanceOverallRolloverListener = new RolloverControlListener( |
| 208 |
this, this.overallRolloverModel); |
| 209 |
this.commandButton |
| 210 |
.addMouseListener(this.substanceOverallRolloverListener); |
| 211 |
this.commandButton |
| 212 |
.addMouseMotionListener(this.substanceOverallRolloverListener); |
| 213 |
|
| 214 |
this.overallStateTransitionTracker.registerModelListeners(); |
| 215 |
} |
| 216 |
|
| 217 |
/* |
| 218 |
* (non-Javadoc) |
| 219 |
* |
| 220 |
* @see |
| 221 |
* org.jvnet.flamingo.ribbon.ui.BasiccommandButtonUI#uninstallListeners() |
| 222 |
*/ |
| 223 |
@Override |
| 224 |
protected void uninstallListeners() { |
| 225 |
this.substanceVisualStateTracker.uninstallListeners(this.commandButton); |
| 226 |
this.substanceVisualStateTracker = null; |
| 227 |
|
| 228 |
this.commandButton |
| 229 |
.removePropertyChangeListener(this.substancePropertyListener); |
| 230 |
this.substancePropertyListener = null; |
| 231 |
|
| 232 |
this.substanceModelChangeListener.unregisterListeners(); |
| 233 |
this.substanceModelChangeListener = null; |
| 234 |
|
| 235 |
this.commandButton |
| 236 |
.removeMouseListener(this.substanceOverallRolloverListener); |
| 237 |
this.commandButton |
| 238 |
.removeMouseMotionListener(this.substanceOverallRolloverListener); |
| 239 |
this.substanceOverallRolloverListener = null; |
| 240 |
|
| 241 |
this.overallStateTransitionTracker.unregisterModelListeners(); |
| 242 |
|
| 243 |
super.uninstallListeners(); |
| 244 |
} |
| 245 |
|
| 246 |
/* |
| 247 |
* (non-Javadoc) |
| 248 |
* |
| 249 |
* @see |
| 250 |
* org.jvnet.flamingo.common.ui.BasicCommandButtonUI#paintButtonBackground |
| 251 |
* (java.awt.Graphics, java.awt.Rectangle) |
| 252 |
*/ |
| 253 |
@Override |
| 254 |
protected void paintButtonBackground(Graphics graphics, Rectangle toFill) { |
| 255 |
if (SubstanceCoreUtilities.isButtonNeverPainted(this.commandButton)) |
| 256 |
return; |
| 257 |
|
| 258 |
ButtonModel actionModel = this.commandButton.getActionModel(); |
| 259 |
PopupButtonModel popupModel = ((JCommandButton) this.commandButton) |
| 260 |
.getPopupModel(); |
| 261 |
Rectangle actionArea = this.getLayoutInfo().actionClickArea; |
| 262 |
Rectangle popupArea = this.getLayoutInfo().popupClickArea; |
| 263 |
|
| 264 |
BufferedImage fullAlphaBackground = CommandButtonBackgroundDelegate |
| 265 |
.getCombinedCommandButtonBackground(this.commandButton, |
| 266 |
actionModel, actionArea, popupModel, popupArea); |
| 267 |
|
| 268 |
// Two special cases here: |
| 269 |
// 1. Button has flat appearance and doesn't show the popup |
| 270 |
// 2. Button is disabled. |
| 271 |
// For both cases, we need to set custom translucency. |
| 272 |
boolean isFlat = this.commandButton.isFlat() |
| 273 |
&& !((JCommandButton) this.commandButton).getPopupModel() |
| 274 |
.isPopupShowing(); |
| 275 |
boolean isSpecial = isFlat || !this.commandButton.isEnabled(); |
| 276 |
float extraAlpha = 1.0f; |
| 277 |
if (isSpecial) { |
| 278 |
if (isFlat) { |
| 279 |
float extraActionAlpha = 0.0f; |
| 280 |
for (Map.Entry<ComponentState, StateTransitionTracker.StateContributionInfo> activeEntry : getActionTransitionTracker() |
| 281 |
.getModelStateInfo().getStateContributionMap() |
| 282 |
.entrySet()) { |
| 283 |
ComponentState activeState = activeEntry.getKey(); |
| 284 |
if (activeState.isDisabled()) |
| 285 |
continue; |
| 286 |
if (activeState == ComponentState.ENABLED) |
| 287 |
continue; |
| 288 |
extraActionAlpha += activeEntry.getValue() |
| 289 |
.getContribution(); |
| 290 |
} |
| 291 |
float extraPopupAlpha = 0.0f; |
| 292 |
for (Map.Entry<ComponentState, StateTransitionTracker.StateContributionInfo> activeEntry : getPopupTransitionTracker() |
| 293 |
.getModelStateInfo().getStateContributionMap() |
| 294 |
.entrySet()) { |
| 295 |
ComponentState activeState = activeEntry.getKey(); |
| 296 |
if (activeState.isDisabled()) |
| 297 |
continue; |
| 298 |
if (activeState == ComponentState.ENABLED) |
| 299 |
continue; |
| 300 |
extraPopupAlpha += activeEntry.getValue().getContribution(); |
| 301 |
} |
| 302 |
extraAlpha = Math.max(extraActionAlpha, extraPopupAlpha); |
| 303 |
} else { |
| 304 |
ComponentState actionAreaState = ComponentState.getState( |
| 305 |
actionModel, this.commandButton); |
| 306 |
if (actionAreaState.isDisabled()) { |
| 307 |
extraAlpha = SubstanceColorSchemeUtilities.getAlpha( |
| 308 |
this.commandButton, actionAreaState); |
| 309 |
} |
| 310 |
} |
| 311 |
} |
| 312 |
// System.out.println(extraAlpha); |
| 313 |
extraAlpha = Math.min(1.0f, extraAlpha); |
| 314 |
if (extraAlpha > 0.0f) { |
| 315 |
Graphics2D g2d = (Graphics2D) graphics.create(); |
| 316 |
g2d.setComposite(LafWidgetUtilities.getAlphaComposite( |
| 317 |
this.commandButton, extraAlpha, graphics)); |
| 318 |
g2d.drawImage(fullAlphaBackground, 0, 0, null); |
| 319 |
g2d.dispose(); |
| 320 |
} |
| 321 |
|
| 322 |
} |
| 323 |
|
| 324 |
/* |
| 325 |
* (non-Javadoc) |
| 326 |
* |
| 327 |
* @see |
| 328 |
* org.jvnet.flamingo.common.ui.BasicCommandButtonUI#paintButtonIcon(java |
| 329 |
* .awt.Graphics, java.awt.Rectangle) |
| 330 |
*/ |
| 331 |
@Override |
| 332 |
protected void paintButtonIcon(Graphics g, Rectangle iconRect) { |
| 333 |
JCommandButton jcb = (JCommandButton) this.commandButton; |
| 334 |
Icon regular = jcb.getIcon(); |
| 335 |
if (toUseDisabledIcon() |
| 336 |
&& (jcb.getDisabledIcon() != null) |
| 337 |
&& ((regular != null) && !regular.getClass() |
| 338 |
.isAnnotationPresent(TransitionAware.class))) |
| 339 |
regular = jcb.getDisabledIcon(); |
| 340 |
|
| 341 |
if ((iconRect == null) || (regular == null) || (iconRect.width == 0) |
| 342 |
|| (iconRect.height == 0)) { |
| 343 |
return; |
| 344 |
} |
| 345 |
|
| 346 |
boolean useThemed = SubstanceCoreUtilities |
| 347 |
.useThemedDefaultIcon(this.commandButton); |
| 348 |
if (regular != null) { |
| 349 |
Graphics2D g2d = (Graphics2D) g.create(); |
| 350 |
|
| 351 |
GhostPaintingUtils.paintGhostIcon(g2d, jcb, regular, iconRect); |
| 352 |
g2d.setComposite(LafWidgetUtilities.getAlphaComposite(jcb, g)); |
| 353 |
|
| 354 |
if (!useThemed) { |
| 355 |
regular.paintIcon(jcb, g2d, iconRect.x, iconRect.y); |
| 356 |
} else { |
| 357 |
StateTransitionTracker tracker = this.substanceVisualStateTracker |
| 358 |
.getActionStateTransitionTracker(); |
| 359 |
ButtonModel model = commandButton.getActionModel(); |
| 360 |
if (jcb.getCommandButtonKind() == CommandButtonKind.POPUP_ONLY) { |
| 361 |
tracker = this.substanceVisualStateTracker |
| 362 |
.getPopupStateTransitionTracker(); |
| 363 |
model = jcb.getPopupModel(); |
| 364 |
} |
| 365 |
CommandButtonBackgroundDelegate.paintThemedCommandButtonIcon( |
| 366 |
g2d, iconRect, jcb, regular, model, tracker); |
| 367 |
} |
| 368 |
g2d.dispose(); |
| 369 |
} |
| 370 |
} |
| 371 |
|
| 372 |
@Override |
| 373 |
protected void paintButtonHorizontalSeparator(Graphics graphics, |
| 374 |
Rectangle separatorArea) { |
| 375 |
Graphics2D g2d = (Graphics2D) graphics.create(); |
| 376 |
g2d.translate(0, separatorArea.y); |
| 377 |
|
| 378 |
SubstanceColorScheme colorScheme = SubstanceColorSchemeUtilities |
| 379 |
.getColorScheme(this.commandButton, |
| 380 |
ColorSchemeAssociationKind.SEPARATOR, ComponentState |
| 381 |
.getState(this.commandButton.getActionModel(), |
| 382 |
this.commandButton)); |
| 383 |
|
| 384 |
float fadeAlpha = this.getSeparatorAlpha(); |
| 385 |
g2d.setComposite(AlphaComposite.SrcOver.derive(fadeAlpha)); |
| 386 |
|
| 387 |
SeparatorPainterUtils.paintSeparator(this.commandButton, g2d, |
| 388 |
colorScheme, this.commandButton.getWidth(), 1, |
| 389 |
JSlider.HORIZONTAL, true, 4, 4, true); |
| 390 |
|
| 391 |
g2d.dispose(); |
| 392 |
} |
| 393 |
|
| 394 |
/* |
| 395 |
* (non-Javadoc) |
| 396 |
* |
| 397 |
* @seeorg.jvnet.flamingo.common.ui.BasicCommandButtonUI# |
| 398 |
* paintButtonVerticalSeparator(java.awt.Graphics, int) |
| 399 |
*/ |
| 400 |
@Override |
| 401 |
protected void paintButtonVerticalSeparator(Graphics graphics, |
| 402 |
Rectangle separatorArea) { |
| 403 |
Graphics2D g2d = (Graphics2D) graphics.create(); |
| 404 |
g2d.translate(separatorArea.x, 0); |
| 405 |
|
| 406 |
SubstanceColorScheme colorScheme = SubstanceColorSchemeUtilities |
| 407 |
.getColorScheme(this.commandButton, |
| 408 |
ColorSchemeAssociationKind.SEPARATOR, ComponentState |
| 409 |
.getState(this.commandButton.getActionModel(), |
| 410 |
this.commandButton)); |
| 411 |
|
| 412 |
float fadeAlpha = this.getSeparatorAlpha(); |
| 413 |
g2d.setComposite(AlphaComposite.SrcOver.derive(fadeAlpha)); |
| 414 |
|
| 415 |
SeparatorPainterUtils.paintSeparator(this.commandButton, g2d, |
| 416 |
colorScheme, 1, this.commandButton.getHeight(), |
| 417 |
JSlider.VERTICAL, true, 4, 4, true); |
| 418 |
|
| 419 |
g2d.dispose(); |
| 420 |
} |
| 421 |
|
| 422 |
/* |
| 423 |
* (non-Javadoc) |
| 424 |
* |
| 425 |
* @see |
| 426 |
* org.jvnet.flamingo.common.ui.BasicCommandButtonUI#isPaintingBackground() |
| 427 |
*/ |
| 428 |
@Override |
| 429 |
protected boolean isPaintingBackground() { |
| 430 |
if (super.isPaintingBackground()) |
| 431 |
return true; |
| 432 |
return (this.overallStateTransitionTracker |
| 433 |
.getFacetStrength(ComponentStateFacet.ROLLOVER) > 0.0f); |
| 434 |
} |
| 435 |
|
| 436 |
/* |
| 437 |
* (non-Javadoc) |
| 438 |
* |
| 439 |
* @see |
| 440 |
* org.jvnet.flamingo.common.ui.BasicCommandButtonUI#isPaintingSeparators() |
| 441 |
*/ |
| 442 |
@Override |
| 443 |
protected boolean isPaintingSeparators() { |
| 444 |
if (super.isPaintingSeparators()) |
| 445 |
return true; |
| 446 |
boolean hasIcon = (this.commandButton.getIcon() != null); |
| 447 |
return hasIcon |
| 448 |
&& (this.overallStateTransitionTracker |
| 449 |
.getFacetStrength(ComponentStateFacet.ROLLOVER) > 0.0f); |
| 450 |
} |
| 451 |
|
| 452 |
/* |
| 453 |
* (non-Javadoc) |
| 454 |
* |
| 455 |
* @see |
| 456 |
* org.jvnet.flamingo.common.ui.BasicCommandButtonUI#createPopupActionIcon() |
| 457 |
*/ |
| 458 |
@Override |
| 459 |
protected ResizableIcon createPopupActionIcon() { |
| 460 |
final int fontSize = SubstanceSizeUtils |
| 461 |
.getComponentFontSize(this.commandButton); |
| 462 |
int arrowIconHeight = (int) SubstanceSizeUtils |
| 463 |
.getArrowIconHeight(fontSize); |
| 464 |
int arrowIconWidth = (int) SubstanceSizeUtils |
| 465 |
.getArrowIconWidth(fontSize); |
| 466 |
ResizableIcon icon = new TransitionAwareResizableIcon( |
| 467 |
this.commandButton, |
| 468 |
new TransitionAwareResizableIcon.StateTransitionTrackerDelegate() { |
| 469 |
@Override |
| 470 |
public StateTransitionTracker getStateTransitionTracker() { |
| 471 |
return getPopupTransitionTracker(); |
| 472 |
} |
| 473 |
}, new TransitionAwareResizableIcon.Delegate() { |
| 474 |
@Override |
| 475 |
public Icon getColorSchemeIcon(SubstanceColorScheme scheme, |
| 476 |
int width, int height) { |
| 477 |
CommandButtonPopupOrientationKind orientation = ((JCommandButton) commandButton) |
| 478 |
.getPopupOrientationKind(); |
| 479 |
int direction = (orientation == CommandButtonPopupOrientationKind.DOWNWARD) ? SwingConstants.SOUTH |
| 480 |
: (commandButton.getComponentOrientation() |
| 481 |
.isLeftToRight() ? SwingConstants.EAST |
| 482 |
: SwingConstants.WEST); |
| 483 |
// System.out.println(direction + ":" + width + ":" |
| 484 |
// + height); |
| 485 |
Icon result = SubstanceImageCreator.getArrowIcon(width, |
| 486 |
height, SubstanceSizeUtils |
| 487 |
.getArrowStrokeWidth(fontSize) - 0.5f, |
| 488 |
direction, scheme); |
| 489 |
// System.out.println(" --> " + result.getIconWidth() |
| 490 |
// + "*" + result.getIconHeight()); |
| 491 |
return result; |
| 492 |
} |
| 493 |
}, new Dimension(arrowIconWidth, arrowIconHeight)); |
| 494 |
return icon; |
| 495 |
} |
| 496 |
|
| 497 |
/* |
| 498 |
* (non-Javadoc) |
| 499 |
* |
| 500 |
* @see |
| 501 |
* org.jvnet.flamingo.common.ui.BasicCommandButtonUI#paint(java.awt.Graphics |
| 502 |
* , javax.swing.JComponent) |
| 503 |
*/ |
| 504 |
@Override |
| 505 |
public void paint(Graphics g, JComponent c) { |
| 506 |
Graphics2D g2d = (Graphics2D) g.create(); |
| 507 |
g2d.setFont(FlamingoUtilities.getFont(this.commandButton, |
| 508 |
"Ribbon.font", "Button.font", "Panel.font")); |
| 509 |
|
| 510 |
this.layoutInfo = this.layoutManager.getLayoutInfo(this.commandButton, |
| 511 |
g); |
| 512 |
commandButton.putClientProperty("icon.bounds", layoutInfo.iconRect); |
| 513 |
commandButton.putClientProperty("icon", commandButton.getIcon()); |
| 514 |
|
| 515 |
if (this.isPaintingBackground()) { |
| 516 |
this.paintButtonBackground(g2d, new Rectangle(0, 0, c.getWidth(), c |
| 517 |
.getHeight())); |
| 518 |
} |
| 519 |
|
| 520 |
// decide which command button model should be used to |
| 521 |
// compute the foreground color of the command button's text |
| 522 |
boolean useActionAreaForFg = layoutInfo.isTextInActionArea; |
| 523 |
StateTransitionTracker transitionTrackerForFg = useActionAreaForFg ? this |
| 524 |
.getActionTransitionTracker() |
| 525 |
: this.getPopupTransitionTracker(); |
| 526 |
ModelStateInfo modelStateInfoForFg = transitionTrackerForFg |
| 527 |
.getModelStateInfo(); |
| 528 |
ComponentState currStateForFg = modelStateInfoForFg.getCurrModelState(); |
| 529 |
Color fgColor = this.commandButton.getForeground(); |
| 530 |
|
| 531 |
if (fgColor instanceof UIResource) { |
| 532 |
float buttonAlpha = SubstanceColorSchemeUtilities.getAlpha( |
| 533 |
this.commandButton, currStateForFg); |
| 534 |
fgColor = SubstanceTextUtilities.getForegroundColor( |
| 535 |
this.commandButton, this.commandButton.getText(), |
| 536 |
modelStateInfoForFg, buttonAlpha); |
| 537 |
} |
| 538 |
|
| 539 |
if (layoutInfo.textLayoutInfoList != null) { |
| 540 |
for (CommandButtonLayoutManager.TextLayoutInfo mainTextLayoutInfo : layoutInfo.textLayoutInfoList) { |
| 541 |
if (mainTextLayoutInfo.text != null) { |
| 542 |
SubstanceTextUtilities.paintText(g2d, c, |
| 543 |
mainTextLayoutInfo.textRect, |
| 544 |
mainTextLayoutInfo.text, -1, g2d.getFont(), |
| 545 |
fgColor, g2d.getClipBounds()); |
| 546 |
} |
| 547 |
} |
| 548 |
} |
| 549 |
|
| 550 |
if (layoutInfo.extraTextLayoutInfoList != null) { |
| 551 |
Color disabledFgColor = SubstanceColorSchemeUtilities |
| 552 |
.getColorScheme(this.commandButton, |
| 553 |
ComponentState.DISABLED_UNSELECTED) |
| 554 |
.getForegroundColor(); |
| 555 |
float buttonAlpha = SubstanceColorSchemeUtilities.getAlpha( |
| 556 |
this.commandButton, ComponentState.DISABLED_UNSELECTED); |
| 557 |
if (buttonAlpha < 1.0f) { |
| 558 |
Color bgFillColor = SubstanceColorUtilities |
| 559 |
.getBackgroundFillColor(this.commandButton); |
| 560 |
disabledFgColor = SubstanceColorUtilities.getInterpolatedColor( |
| 561 |
disabledFgColor, bgFillColor, buttonAlpha); |
| 562 |
} |
| 563 |
if (currStateForFg.isDisabled()) { |
| 564 |
disabledFgColor = SubstanceColorUtilities.getInterpolatedColor( |
| 565 |
disabledFgColor, SubstanceColorUtilities |
| 566 |
.getBackgroundFillColor(c), 0.5); |
| 567 |
} |
| 568 |
for (CommandButtonLayoutManager.TextLayoutInfo extraTextLayoutInfo : layoutInfo.extraTextLayoutInfoList) { |
| 569 |
if (extraTextLayoutInfo.text != null) { |
| 570 |
SubstanceTextUtilities.paintText(g2d, c, |
| 571 |
extraTextLayoutInfo.textRect, |
| 572 |
extraTextLayoutInfo.text, -1, g2d.getFont(), |
| 573 |
disabledFgColor, g2d.getClipBounds()); |
| 574 |
} |
| 575 |
} |
| 576 |
} |
| 577 |
|
| 578 |
if (layoutInfo.iconRect != null) { |
| 579 |
this.paintButtonIcon(g2d, layoutInfo.iconRect); |
| 580 |
} |
| 581 |
if (layoutInfo.popupActionRect.getWidth() > 0) { |
| 582 |
paintPopupActionIcon(g2d, layoutInfo.popupActionRect); |
| 583 |
} |
| 584 |
|
| 585 |
if (this.isPaintingSeparators() && (layoutInfo.separatorArea != null)) { |
| 586 |
if (layoutInfo.separatorOrientation == CommandButtonSeparatorOrientation.HORIZONTAL) { |
| 587 |
this.paintButtonHorizontalSeparator(g2d, |
| 588 |
layoutInfo.separatorArea); |
| 589 |
} else { |
| 590 |
this |
| 591 |
.paintButtonVerticalSeparator(g2d, |
| 592 |
layoutInfo.separatorArea); |
| 593 |
} |
| 594 |
} |
| 595 |
|
| 596 |
// g2d.setColor(Color.red); |
| 597 |
// g2d.draw(layoutInfo.iconRect); |
| 598 |
// g2d.setColor(Color.blue); |
| 599 |
// if (layoutInfo.textLayoutInfoList != null) { |
| 600 |
// for (CommandButtonLayoutManager.TextLayoutInfo mainTextLayoutInfo : |
| 601 |
// layoutInfo.textLayoutInfoList) { |
| 602 |
// if (mainTextLayoutInfo.text != null) { |
| 603 |
// g2d.draw(mainTextLayoutInfo.textRect); |
| 604 |
// } |
| 605 |
// } |
| 606 |
// } |
| 607 |
// g2d.setColor(Color.magenta); |
| 608 |
// if (layoutInfo.extraTextLayoutInfoList != null) { |
| 609 |
// for (CommandButtonLayoutManager.TextLayoutInfo extraTextLayoutInfo : |
| 610 |
// layoutInfo.extraTextLayoutInfoList) { |
| 611 |
// if (extraTextLayoutInfo.text != null) { |
| 612 |
// g2d.draw(extraTextLayoutInfo.textRect); |
| 613 |
// } |
| 614 |
// } |
| 615 |
// } |
| 616 |
// g2d.setColor(Color.green); |
| 617 |
// g2d.draw(layoutInfo.popupActionRect); |
| 618 |
|
| 619 |
g2d.dispose(); |
| 620 |
} |
| 621 |
|
| 622 |
/* |
| 623 |
* (non-Javadoc) |
| 624 |
* |
| 625 |
* @see |
| 626 |
* org.jvnet.flamingo.common.ui.BasicCommandButtonUI#paintPopupActionIcon |
| 627 |
* (java.awt.Graphics, java.awt.Rectangle) |
| 628 |
*/ |
| 629 |
@Override |
| 630 |
protected void paintPopupActionIcon(Graphics g, Rectangle popupActionRect) { |
| 631 |
int width = popupActionRect.width; |
| 632 |
int height = popupActionRect.height; |
| 633 |
if (((JCommandButton) this.commandButton).getPopupOrientationKind() == CommandButtonPopupOrientationKind.DOWNWARD) { |
| 634 |
width += 2; |
| 635 |
if (width % 2 == 0) |
| 636 |
width++; |
| 637 |
height = height / 2 - 1; |
| 638 |
} else { |
| 639 |
height /= 2; |
| 640 |
width++; |
| 641 |
if (width % 2 == 0) |
| 642 |
width++; |
| 643 |
} |
| 644 |
popupActionIcon.setDimension(new Dimension(width, height)); |
| 645 |
popupActionIcon.paintIcon(this.commandButton, g, popupActionRect.x |
| 646 |
+ (popupActionRect.width - width) / 2, popupActionRect.y |
| 647 |
+ (popupActionRect.height - height) / 2); |
| 648 |
} |
| 649 |
|
| 650 |
/* |
| 651 |
* (non-Javadoc) |
| 652 |
* |
| 653 |
* @see |
| 654 |
* org.jvnet.flamingo.common.ui.BasicCommandButtonUI#getPreferredSize(javax |
| 655 |
* .swing.JComponent) |
| 656 |
*/ |
| 657 |
@Override |
| 658 |
public Dimension getPreferredSize(JComponent c) { |
| 659 |
AbstractCommandButton button = (AbstractCommandButton) c; |
| 660 |
SubstanceButtonShaper shaper = SubstanceCoreUtilities |
| 661 |
.getButtonShaper(button); |
| 662 |
|
| 663 |
Dimension superPref = super.getPreferredSize(button); |
| 664 |
if (superPref == null) |
| 665 |
return null; |
| 666 |
|
| 667 |
if (shaper == null) |
| 668 |
return superPref; |
| 669 |
|
| 670 |
// fix for issue 35 on Flamingo - do not enforce |
| 671 |
// min size on buttons in the ribbon |
| 672 |
// Additional fix - buttons with popup action should |
| 673 |
// not have min size enforced as well |
| 674 |
// Additional fix - buttons in popup menus and breadcrumb bars should |
| 675 |
// not have min size enforced |
| 676 |
if ((button.getDisplayState() == CommandButtonDisplayState.MEDIUM) |
| 677 |
&& (SwingUtilities.getAncestorOfClass(AbstractRibbonBand.class, |
| 678 |
button) == null) |
| 679 |
&& (SwingUtilities.getAncestorOfClass(JBreadcrumbBar.class, |
| 680 |
button) == null) |
| 681 |
&& (SwingUtilities.getAncestorOfClass(JCommandPopupMenu.class, |
| 682 |
button) == null)) { |
| 683 |
JButton dummy = new JButton(button.getText(), button.getIcon()); |
| 684 |
Dimension result = shaper.getPreferredSize(dummy, superPref); |
| 685 |
if (FlamingoUtilities.hasPopupAction(button)) { |
| 686 |
result.width = superPref.width; |
| 687 |
} |
| 688 |
return result; |
| 689 |
} |
| 690 |
return superPref; |
| 691 |
} |
| 692 |
|
| 693 |
/** |
| 694 |
* Computes the alpha value for painting the separators. |
| 695 |
* |
| 696 |
* @return Alpha value for painting the separators. |
| 697 |
*/ |
| 698 |
private float getSeparatorAlpha() { |
| 699 |
ComponentState actionAreaState = this.getActionTransitionTracker() |
| 700 |
.getModelStateInfo().getCurrModelState(); |
| 701 |
|
| 702 |
if (!actionAreaState.isFacetActive(ComponentStateFacet.SELECTION) |
| 703 |
&& !actionAreaState.isDisabled()) { |
| 704 |
float actionRolloverCycle = this.getActionTransitionTracker() |
| 705 |
.getFacetStrength(ComponentStateFacet.ROLLOVER); |
| 706 |
float popupRolloverCycle = this.getPopupTransitionTracker() |
| 707 |
.getFacetStrength(ComponentStateFacet.ROLLOVER); |
| 708 |
return Math.min(1.0f, actionRolloverCycle + popupRolloverCycle); |
| 709 |
} |
| 710 |
return 1.0f; |
| 711 |
} |
| 712 |
|
| 713 |
/* |
| 714 |
* (non-Javadoc) |
| 715 |
* |
| 716 |
* @see org.jvnet.flamingo.common.ui.BasicCommandButtonUI#syncDisabledIcon() |
| 717 |
*/ |
| 718 |
@Override |
| 719 |
protected void syncDisabledIcon() { |
| 720 |
ResizableIcon currDisabledIcon = this.commandButton.getDisabledIcon(); |
| 721 |
ResizableIcon icon = this.commandButton.getIcon(); |
| 722 |
if ((currDisabledIcon == null) |
| 723 |
|| ((currDisabledIcon instanceof UIResource) && !currDisabledIcon |
| 724 |
.getClass().isAnnotationPresent(TransitionAware.class))) { |
| 725 |
if (icon != null) { |
| 726 |
this.commandButton.setDisabledIcon(new ResizableIconUIResource( |
| 727 |
new SubstanceDisabledResizableIcon(icon))); |
| 728 |
} else { |
| 729 |
this.commandButton.setDisabledIcon(null); |
| 730 |
} |
| 731 |
} else { |
| 732 |
// disabled icon coming from app code |
| 733 |
if (icon != null) { |
| 734 |
this.commandButton.getDisabledIcon() |
| 735 |
.setDimension( |
| 736 |
new Dimension(icon.getIconWidth(), icon |
| 737 |
.getIconHeight())); |
| 738 |
} |
| 739 |
} |
| 740 |
} |
| 741 |
|
| 742 |
@Override |
| 743 |
public StateTransitionTracker getTransitionTracker() { |
| 744 |
return this.overallStateTransitionTracker; |
| 745 |
} |
| 746 |
|
| 747 |
@Override |
| 748 |
public boolean isInside(MouseEvent me) { |
| 749 |
boolean inAction = (this.layoutInfo.actionClickArea != null) |
| 750 |
&& this.layoutInfo.actionClickArea.contains(me.getPoint()); |
| 751 |
boolean inPopup = (this.layoutInfo.popupClickArea != null) |
| 752 |
&& this.layoutInfo.popupClickArea.contains(me.getPoint()); |
| 753 |
return inAction || inPopup; |
| 754 |
} |
| 755 |
|
| 756 |
@Override |
| 757 |
public StateTransitionTracker getActionTransitionTracker() { |
| 758 |
return this.substanceVisualStateTracker |
| 759 |
.getActionStateTransitionTracker(); |
| 760 |
} |
| 761 |
|
| 762 |
@Override |
| 763 |
public StateTransitionTracker getPopupTransitionTracker() { |
| 764 |
return this.substanceVisualStateTracker |
| 765 |
.getPopupStateTransitionTracker(); |
| 766 |
} |
| 767 |
} |