Debian Codegraph @

substance-flamingo/src/main/java/org/pushingpixels/substance/flamingo/ribbon/ui/SubstanceRibbonApplicationMenuButtonUI.java

raw symbols SCIP
1 /*
2 * Copyright (c) 2005-2010 Substance 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 Substance 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.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 * UI for {@link JRibbonApplicationMenuButton} components in <b>Substance</b>
55 * look and feel.
56 *
57 * @author Kirill Grouchnikov
58 */
59 public class SubstanceRibbonApplicationMenuButtonUI extends
60 BasicRibbonApplicationMenuButtonUI implements
61 ActionPopupTransitionAwareUI {
62
63 public SubstanceRibbonApplicationMenuButtonUI(JRibbon ribbon) {
64 super(ribbon);
65 }
66
67 /**
68 * Model change listener for ghost image effects.
69 */
70 private GhostingListener substanceModelChangeListener;
71
72 /**
73 * Tracker for visual state transitions.
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 * (non-Javadoc)
90 *
91 * @see org.jvnet.flamingo.common.ui.BasicCommandButtonUI#installListeners()
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 * (non-Javadoc)
107 *
108 * @see
109 * org.jvnet.flamingo.common.ui.BasicCommandButtonUI#uninstallListeners()
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 * (non-Javadoc)
124 *
125 * @see javax.swing.plaf.basic.BasicButtonUI#paint(java.awt.Graphics,
126 * javax.swing.JComponent)
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 // Paint the icon
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 * (non-Javadoc)
161 *
162 * @see
163 * org.jvnet.flamingo.common.ui.BasicCommandButtonUI#paintButtonIcon(java
164 * .awt.Graphics, java.awt.Rectangle)
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 * (non-Javadoc)
200 *
201 * @see
202 * org.jvnet.flamingo.ribbon.ui.appmenu.BasicRibbonApplicationMenuButtonUI
203 * #update(java.awt.Graphics, javax.swing.JComponent)
204 */
205 @Override
206 public void update(Graphics g, JComponent c) {
207 this.paint(g, c);
208 }
209
210 /*
211 * (non-Javadoc)
212 *
213 * @see
214 * org.jvnet.substance.SubstanceButtonUI#contains(javax.swing.JComponent,
215 * int, int)
216 */
217 @Override
218 public boolean contains(JComponent c, int x, int y) {
219 // allow clicking anywhere in the area (around the button
220 // round outline as well) to activate the button.
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 }