| 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.awt.image.BufferedImage; |
| 35 |
|
| 36 |
import javax.swing.Icon; |
| 37 |
import javax.swing.JComponent; |
| 38 |
import javax.swing.plaf.ComponentUI; |
| 39 |
|
| 40 |
import org.pushingpixels.flamingo.api.ribbon.JRibbon; |
| 41 |
import org.pushingpixels.flamingo.internal.ui.ribbon.appmenu.BasicRibbonApplicationMenuButtonUI; |
| 42 |
import org.pushingpixels.flamingo.internal.ui.ribbon.appmenu.JRibbonApplicationMenuButton; |
| 43 |
import org.pushingpixels.lafwidget.LafWidgetUtilities; |
| 44 |
import org.pushingpixels.lafwidget.animation.effects.GhostPaintingUtils; |
| 45 |
import org.pushingpixels.lafwidget.animation.effects.GhostingListener; |
| 46 |
import org.pushingpixels.substance.api.painter.border.SubstanceBorderPainter; |
| 47 |
import org.pushingpixels.substance.api.painter.fill.SubstanceFillPainter; |
| 48 |
import org.pushingpixels.substance.flamingo.common.ui.ActionPopupTransitionAwareUI; |
| 49 |
import org.pushingpixels.substance.flamingo.utils.*; |
| 50 |
import org.pushingpixels.substance.internal.animation.StateTransitionTracker; |
| 51 |
import org.pushingpixels.substance.internal.utils.SubstanceCoreUtilities; |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
public class SubstanceRibbonApplicationMenuButtonUI extends |
| 60 |
BasicRibbonApplicationMenuButtonUI implements |
| 61 |
ActionPopupTransitionAwareUI { |
| 62 |
|
| 63 |
public SubstanceRibbonApplicationMenuButtonUI(JRibbon ribbon) { |
| 64 |
super(ribbon); |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
private GhostingListener substanceModelChangeListener; |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
protected CommandButtonVisualStateTracker substanceVisualStateTracker; |
| 76 |
|
| 77 |
public static ComponentUI createUI(JComponent component) { |
| 78 |
if (component instanceof JRibbon) { |
| 79 |
return new SubstanceRibbonApplicationMenuButtonUI((JRibbon) component); |
| 80 |
} else if (component instanceof JRibbonApplicationMenuButton) { |
| 81 |
return new SubstanceRibbonApplicationMenuButtonUI( |
| 82 |
((JRibbonApplicationMenuButton) component).getRibbon()); |
| 83 |
} |
| 84 |
throw new IllegalArgumentException( |
| 85 |
"creating a BasicRibbonApplicationMenuButtonUI requires a JRibbon"); |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
@Override |
| 94 |
protected void installListeners() { |
| 95 |
super.installListeners(); |
| 96 |
|
| 97 |
this.substanceVisualStateTracker = new CommandButtonVisualStateTracker(); |
| 98 |
this.substanceVisualStateTracker.installListeners(this.commandButton); |
| 99 |
|
| 100 |
this.substanceModelChangeListener = new GhostingListener( |
| 101 |
this.commandButton, this.commandButton.getActionModel()); |
| 102 |
this.substanceModelChangeListener.registerListeners(); |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
@Override |
| 112 |
protected void uninstallListeners() { |
| 113 |
this.substanceVisualStateTracker.uninstallListeners(this.commandButton); |
| 114 |
this.substanceVisualStateTracker = null; |
| 115 |
|
| 116 |
this.substanceModelChangeListener.unregisterListeners(); |
| 117 |
this.substanceModelChangeListener = null; |
| 118 |
|
| 119 |
super.uninstallListeners(); |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
@Override |
| 129 |
public void paint(Graphics g, JComponent c) { |
| 130 |
JRibbonApplicationMenuButton b = (JRibbonApplicationMenuButton) c; |
| 131 |
|
| 132 |
this.layoutInfo = this.layoutManager.getLayoutInfo(this.commandButton, |
| 133 |
g); |
| 134 |
commandButton.putClientProperty("icon.bounds", layoutInfo.iconRect); |
| 135 |
|
| 136 |
Graphics2D g2d = (Graphics2D) g.create(); |
| 137 |
SubstanceFillPainter fillPainter = SubstanceCoreUtilities |
| 138 |
.getFillPainter(commandButton); |
| 139 |
SubstanceBorderPainter borderPainter = SubstanceCoreUtilities |
| 140 |
.getBorderPainter(commandButton); |
| 141 |
BufferedImage fullAlphaBackground = RibbonApplicationMenuButtonBackgroundDelegate |
| 142 |
.getFullAlphaBackground(b, fillPainter, borderPainter, |
| 143 |
commandButton.getWidth(), commandButton.getHeight()); |
| 144 |
g2d.drawImage(fullAlphaBackground, 0, 0, null); |
| 145 |
|
| 146 |
|
| 147 |
Icon icon = b.getIcon(); |
| 148 |
if (icon != null) { |
| 149 |
int iconWidth = icon.getIconWidth(); |
| 150 |
int iconHeight = icon.getIconHeight(); |
| 151 |
Rectangle iconRect = new Rectangle((c.getWidth() - iconWidth) / 2, |
| 152 |
(c.getHeight() - iconHeight) / 2, iconWidth, iconHeight); |
| 153 |
paintButtonIcon(g2d, iconRect); |
| 154 |
} |
| 155 |
|
| 156 |
g2d.dispose(); |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
@Override |
| 167 |
protected void paintButtonIcon(Graphics g, Rectangle iconRect) { |
| 168 |
Icon regular = this.applicationMenuButton.isEnabled() ? this.applicationMenuButton |
| 169 |
.getIcon() |
| 170 |
: this.applicationMenuButton.getDisabledIcon(); |
| 171 |
if (regular == null) |
| 172 |
return; |
| 173 |
boolean useThemed = SubstanceCoreUtilities |
| 174 |
.useThemedDefaultIcon(this.applicationMenuButton); |
| 175 |
|
| 176 |
if (regular != null) { |
| 177 |
Graphics2D g2d = (Graphics2D) g.create(); |
| 178 |
|
| 179 |
GhostPaintingUtils.paintGhostIcon(g2d, this.applicationMenuButton, |
| 180 |
regular, iconRect); |
| 181 |
g2d.setComposite(LafWidgetUtilities.getAlphaComposite( |
| 182 |
this.applicationMenuButton, g)); |
| 183 |
|
| 184 |
if (!useThemed) { |
| 185 |
regular.paintIcon(this.applicationMenuButton, g2d, iconRect.x, |
| 186 |
iconRect.y); |
| 187 |
} else { |
| 188 |
CommandButtonBackgroundDelegate.paintThemedCommandButtonIcon( |
| 189 |
g2d, iconRect, this.applicationMenuButton, regular, |
| 190 |
this.applicationMenuButton.getPopupModel(), |
| 191 |
this.substanceVisualStateTracker |
| 192 |
.getPopupStateTransitionTracker()); |
| 193 |
} |
| 194 |
g2d.dispose(); |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
@Override |
| 206 |
public void update(Graphics g, JComponent c) { |
| 207 |
this.paint(g, c); |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
@Override |
| 218 |
public boolean contains(JComponent c, int x, int y) { |
| 219 |
|
| 220 |
|
| 221 |
return (x >= 0) && (x < c.getWidth()) && (y >= 0) |
| 222 |
&& (y < c.getHeight()); |
| 223 |
} |
| 224 |
|
| 225 |
@Override |
| 226 |
public StateTransitionTracker getActionTransitionTracker() { |
| 227 |
return this.substanceVisualStateTracker |
| 228 |
.getActionStateTransitionTracker(); |
| 229 |
} |
| 230 |
|
| 231 |
@Override |
| 232 |
public StateTransitionTracker getPopupTransitionTracker() { |
| 233 |
return this.substanceVisualStateTracker |
| 234 |
.getPopupStateTransitionTracker(); |
| 235 |
} |
| 236 |
|
| 237 |
@Override |
| 238 |
public StateTransitionTracker getTransitionTracker() { |
| 239 |
return this.substanceVisualStateTracker |
| 240 |
.getPopupStateTransitionTracker(); |
| 241 |
} |
| 242 |
|
| 243 |
@Override |
| 244 |
public boolean isInside(MouseEvent me) { |
| 245 |
return true; |
| 246 |
} |
| 247 |
} |