Debian Codegraph @

substance-flamingo/src/main/java/org/pushingpixels/substance/flamingo/common/ui/SubstanceCommandToggleButtonUI.java

raw
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.common.*;
44 import org.pushingpixels.flamingo.api.common.icon.ResizableIcon;
45 import org.pushingpixels.flamingo.api.ribbon.AbstractRibbonBand;
46 import org.pushingpixels.flamingo.internal.ui.common.BasicCommandToggleButtonUI;
47 import org.pushingpixels.flamingo.internal.ui.common.ResizableIconUIResource;
48 import org.pushingpixels.flamingo.internal.utils.FlamingoUtilities;
49 import org.pushingpixels.lafwidget.LafWidgetUtilities;
50 import org.pushingpixels.lafwidget.animation.effects.GhostPaintingUtils;
51 import org.pushingpixels.lafwidget.animation.effects.GhostingListener;
52 import org.pushingpixels.substance.api.*;
53 import org.pushingpixels.substance.api.painter.border.SubstanceBorderPainter;
54 import org.pushingpixels.substance.api.painter.fill.SubstanceFillPainter;
55 import org.pushingpixels.substance.api.shaper.ClassicButtonShaper;
56 import org.pushingpixels.substance.api.shaper.SubstanceButtonShaper;
57 import org.pushingpixels.substance.flamingo.utils.*;
58 import org.pushingpixels.substance.internal.animation.StateTransitionTracker;
59 import org.pushingpixels.substance.internal.animation.StateTransitionTracker.ModelStateInfo;
60 import org.pushingpixels.substance.internal.utils.*;
61 import org.pushingpixels.substance.internal.utils.icon.TransitionAware;
62
63 /**
64 * UI for command buttons {@link JCommandToggleButton} in <b>Substance </b> look
65 * and feel.
66 *
67 * @author Kirill Grouchnikov
68 */
69 public class SubstanceCommandToggleButtonUI extends BasicCommandToggleButtonUI
70 implements ActionPopupTransitionAwareUI {
71 /**
72 * Delegate for painting the background.
73 */
74 protected ButtonBackgroundDelegate backgroundDelegate;
75
76 /**
77 * Property change listener. Listens on changes to
78 * {@link AbstractButton#MODEL_CHANGED_PROPERTY} property.
79 */
80 protected PropertyChangeListener substancePropertyListener;
81
82 /**
83 * Model change listener for ghost image effects.
84 */
85 private GhostingListener substanceModelChangeListener;
86
87 /**
88 * Tracker for visual state transitions.
89 */
90 protected CommandButtonVisualStateTracker substanceVisualStateTracker;
91
92 /*
93 * (non-Javadoc)
94 *
95 * @see javax.swing.plaf.ComponentUI#createUI(javax.swing.JComponent)
96 */
97 public static ComponentUI createUI(JComponent comp) {
98 SubstanceCoreUtilities.testComponentCreationThreadingViolation(comp);
99 return new SubstanceCommandToggleButtonUI();
100 }
101
102 /**
103 * Creates a new UI delegate for ribbon button.
104 */
105 public SubstanceCommandToggleButtonUI() {
106 super();
107 this.backgroundDelegate = new ButtonBackgroundDelegate();
108
109 this.substanceVisualStateTracker = new CommandButtonVisualStateTracker();
110 }
111
112 /*
113 * (non-Javadoc)
114 *
115 * @see org.jvnet.flamingo.ribbon.ui.BasiccommandButtonUI#installDefaults()
116 */
117 @Override
118 protected void installDefaults() {
119 super.installDefaults();
120 this.commandButton.putClientProperty(
121 SubstanceLookAndFeel.BUTTON_SHAPER_PROPERTY,
122 ClassicButtonShaper.INSTANCE);
123
124 this.commandButton.setOpaque(false);
125 }
126
127 /*
128 * (non-Javadoc)
129 *
130 * @see org.jvnet.flamingo.common.ui.BasicCommandButtonUI#updateBorder()
131 */
132 @Override
133 protected void updateBorder() {
134 Border currBorder = this.commandButton.getBorder();
135 if ((currBorder == null) || (currBorder instanceof UIResource)) {
136 Insets extra = SubstanceSizeUtils
137 .getDefaultBorderInsets(SubstanceSizeUtils
138 .getComponentFontSize(this.commandButton));
139 double hgapScaleFactor = this.commandButton.getHGapScaleFactor();
140 double vgapScaleFactor = this.commandButton.getVGapScaleFactor();
141
142 int top = 1 + (int) (vgapScaleFactor * extra.top);
143 int left = 2 + (int) (hgapScaleFactor * (1 + extra.left));
144 int bottom = 0 + (int) (vgapScaleFactor * extra.bottom);
145 int right = 2 + (int) (hgapScaleFactor * (1 + extra.right));
146 this.commandButton
147 .setBorder(new BorderUIResource.EmptyBorderUIResource(top,
148 left, bottom, right));
149 }
150 }
151
152 /*
153 * (non-Javadoc)
154 *
155 * @see org.jvnet.flamingo.ribbon.ui.BasiccommandButtonUI#installListeners()
156 */
157 @Override
158 protected void installListeners() {
159 super.installListeners();
160
161 this.substanceVisualStateTracker.installListeners(this.commandButton);
162
163 this.substancePropertyListener = new PropertyChangeListener() {
164 @Override
165 public void propertyChange(PropertyChangeEvent evt) {
166 if (AbstractButton.MODEL_CHANGED_PROPERTY.equals(evt
167 .getPropertyName())) {
168 if (substanceModelChangeListener != null)
169 substanceModelChangeListener.unregisterListeners();
170 substanceModelChangeListener = new GhostingListener(
171 commandButton, commandButton.getActionModel());
172 substanceModelChangeListener.registerListeners();
173 }
174 }
175 };
176 this.commandButton
177 .addPropertyChangeListener(this.substancePropertyListener);
178
179 this.substanceModelChangeListener = new GhostingListener(
180 this.commandButton, this.commandButton.getActionModel());
181 this.substanceModelChangeListener.registerListeners();
182 }
183
184 /*
185 * (non-Javadoc)
186 *
187 * @see
188 * org.jvnet.flamingo.ribbon.ui.BasiccommandButtonUI#uninstallListeners()
189 */
190 @Override
191 protected void uninstallListeners() {
192 this.substanceVisualStateTracker.uninstallListeners(this.commandButton);
193 this.substanceVisualStateTracker = null;
194
195 this.commandButton
196 .removePropertyChangeListener(this.substancePropertyListener);
197 this.substancePropertyListener = null;
198
199 this.substanceModelChangeListener.unregisterListeners();
200 this.substanceModelChangeListener = null;
201
202 super.uninstallListeners();
203 }
204
205 /*
206 * (non-Javadoc)
207 *
208 * @see
209 * org.jvnet.flamingo.common.ui.BasicCommandButtonUI#paintButtonIcon(java
210 * .awt.Graphics, java.awt.Rectangle)
211 */
212 @Override
213 protected void paintButtonIcon(Graphics g, Rectangle iconRect) {
214 JCommandToggleButton jctb = (JCommandToggleButton) this.commandButton;
215 Icon regular = jctb.getIcon();
216 if (toUseDisabledIcon()
217 && (jctb.getDisabledIcon() != null)
218 && ((regular != null) && !regular.getClass()
219 .isAnnotationPresent(TransitionAware.class))) {
220 regular = jctb.getDisabledIcon();
221 }
222
223 boolean useThemed = SubstanceCoreUtilities
224 .useThemedDefaultIcon(this.commandButton);
225
226 if ((iconRect == null) || (regular == null) || (iconRect.width == 0)
227 || (iconRect.height == 0)) {
228 return;
229 }
230
231 if (regular != null) {
232 Graphics2D g2d = (Graphics2D) g.create();
233
234 GhostPaintingUtils.paintGhostIcon(g2d, jctb, regular, iconRect);
235 g2d.setComposite(LafWidgetUtilities.getAlphaComposite(jctb, g));
236
237 if (!useThemed) {
238 regular.paintIcon(jctb, g2d, iconRect.x, iconRect.y);
239 } else {
240 CommandButtonBackgroundDelegate.paintThemedCommandButtonIcon(
241 g2d, iconRect, jctb, regular, jctb.getActionModel(),
242 this.substanceVisualStateTracker
243 .getPopupStateTransitionTracker());
244 }
245 g2d.dispose();
246 }
247 }
248
249 /*
250 * (non-Javadoc)
251 *
252 * @see
253 * org.jvnet.flamingo.common.ui.BasicCommandButtonUI#isPaintingBackground()
254 */
255 @Override
256 protected boolean isPaintingBackground() {
257 if (super.isPaintingBackground())
258 return true;
259 return (this.getActionTransitionTracker().getFacetStrength(
260 ComponentStateFacet.ROLLOVER) > 0.0f);
261 }
262
263 /*
264 * (non-Javadoc)
265 *
266 * @see
267 * org.jvnet.flamingo.common.ui.BasicCommandButtonUI#isPaintingSeparators()
268 */
269 @Override
270 protected boolean isPaintingSeparators() {
271 return false;
272 }
273
274 /*
275 * (non-Javadoc)
276 *
277 * @see
278 * org.jvnet.flamingo.common.ui.BasicCommandButtonUI#paint(java.awt.Graphics
279 * , javax.swing.JComponent)
280 */
281 @Override
282 public void paint(Graphics g, JComponent c) {
283 Graphics2D g2d = (Graphics2D) g.create();
284 g2d.setFont(FlamingoUtilities.getFont(this.commandButton,
285 "Ribbon.font", "Button.font", "Panel.font"));
286
287 this.layoutInfo = this.layoutManager.getLayoutInfo(this.commandButton,
288 g);
289 commandButton.putClientProperty("icon.bounds", layoutInfo.iconRect);
290 commandButton.putClientProperty("icon", commandButton.getIcon());
291
292 if (this.isPaintingBackground()) {
293 this.paintButtonBackground(g2d, new Rectangle(0, 0, c.getWidth(), c
294 .getHeight()));
295 }
296
297 // action model should be used to
298 // compute the foreground color of the command button's text
299 ModelStateInfo modelStateInfo = this.substanceVisualStateTracker
300 .getActionStateTransitionTracker().getModelStateInfo();
301 Color fgColor = this.getForegroundColor(modelStateInfo);
302
303 if (layoutInfo.textLayoutInfoList != null) {
304 for (CommandButtonLayoutManager.TextLayoutInfo mainTextLayoutInfo : layoutInfo.textLayoutInfoList) {
305 if (mainTextLayoutInfo.text != null) {
306 SubstanceTextUtilities.paintText(g2d, c,
307 mainTextLayoutInfo.textRect,
308 mainTextLayoutInfo.text, -1, g2d.getFont(),
309 fgColor, g2d.getClipBounds());
310 }
311 }
312 }
313
314 if (layoutInfo.extraTextLayoutInfoList != null) {
315 Color disabledFgColor = SubstanceColorSchemeUtilities
316 .getColorScheme(this.commandButton,
317 ComponentState.DISABLED_UNSELECTED)
318 .getForegroundColor();
319 float buttonAlpha = SubstanceColorSchemeUtilities.getAlpha(
320 this.commandButton, ComponentState.DISABLED_UNSELECTED);
321 if (buttonAlpha < 1.0f) {
322 Color bgFillColor = SubstanceColorUtilities
323 .getBackgroundFillColor(this.commandButton);
324 disabledFgColor = SubstanceColorUtilities.getInterpolatedColor(
325 disabledFgColor, bgFillColor, buttonAlpha);
326 }
327 if (modelStateInfo.getCurrModelState().isDisabled()) {
328 disabledFgColor = SubstanceColorUtilities.getInterpolatedColor(
329 disabledFgColor, SubstanceColorUtilities
330 .getBackgroundFillColor(c), 0.5);
331 }
332 for (CommandButtonLayoutManager.TextLayoutInfo extraTextLayoutInfo : layoutInfo.extraTextLayoutInfoList) {
333 if (extraTextLayoutInfo.text != null) {
334 SubstanceTextUtilities.paintText(g2d, c,
335 extraTextLayoutInfo.textRect,
336 extraTextLayoutInfo.text, -1, g2d.getFont(),
337 disabledFgColor, g2d.getClipBounds());
338 }
339 }
340 }
341
342 if (layoutInfo.iconRect != null) {
343 this.paintButtonIcon(g2d, layoutInfo.iconRect);
344 }
345
346 g2d.dispose();
347 }
348
349 protected Color getForegroundColor(ModelStateInfo modelStateInfo) {
350 Color fgColor = this.commandButton.getForeground();
351 if (fgColor instanceof UIResource) {
352 float buttonAlpha = SubstanceColorSchemeUtilities.getAlpha(
353 this.commandButton, modelStateInfo.getCurrModelState());
354 fgColor = SubstanceTextUtilities.getForegroundColor(
355 this.commandButton, this.commandButton.getText(),
356 modelStateInfo, buttonAlpha);
357 }
358 return fgColor;
359 }
360
361 /*
362 * (non-Javadoc)
363 *
364 * @see
365 * org.jvnet.flamingo.common.ui.BasicCommandButtonUI#getPreferredSize(javax
366 * .swing.JComponent)
367 */
368 @Override
369 public Dimension getPreferredSize(JComponent c) {
370 AbstractCommandButton button = (AbstractCommandButton) c;
371 SubstanceButtonShaper shaper = SubstanceCoreUtilities
372 .getButtonShaper(button);
373
374 Dimension superPref = super.getPreferredSize(button);
375 if (superPref == null)
376 return null;
377
378 if (shaper == null)
379 return superPref;
380
381 // fix for issue 35 on Flamingo - do not enforce
382 // min size on buttons in the ribbon
383 if ((button.getDisplayState() == CommandButtonDisplayState.MEDIUM)
384 && (SwingUtilities.getAncestorOfClass(AbstractRibbonBand.class,
385 button) == null)) {
386 JButton dummy = new JButton(button.getText(), button.getIcon());
387 return shaper.getPreferredSize(dummy, superPref);
388 }
389 return superPref;
390 }
391
392 /*
393 * (non-Javadoc)
394 *
395 * @see
396 * org.jvnet.flamingo.common.ui.BasicCommandButtonUI#paintButtonBackground
397 * (java.awt.Graphics, java.awt.Rectangle)
398 */
399 @Override
400 protected void paintButtonBackground(Graphics graphics, Rectangle toFill) {
401 if (SubstanceCoreUtilities.isButtonNeverPainted(this.commandButton))
402 return;
403
404 ButtonModel actionModel = this.commandButton.getActionModel();
405
406 SubstanceFillPainter fillPainter = SubstanceCoreUtilities
407 .getFillPainter(this.commandButton);
408 SubstanceBorderPainter borderPainter = SubstanceCoreUtilities
409 .getBorderPainter(this.commandButton);
410
411 boolean ignoreSelections = this.commandButton instanceof JCommandToggleMenuButton;
412 BufferedImage fullAlphaBackground = CommandButtonBackgroundDelegate
413 .getFullAlphaBackground(this.commandButton, actionModel,
414 fillPainter, borderPainter, this.commandButton
415 .getWidth(), this.commandButton.getHeight(),
416 this.getActionTransitionTracker(), ignoreSelections);
417
418 StateTransitionTracker.ModelStateInfo modelStateInfo = getActionTransitionTracker()
419 .getModelStateInfo();
420 Map<ComponentState, StateTransitionTracker.StateContributionInfo> activeStates = ignoreSelections ? modelStateInfo
421 .getStateNoSelectionContributionMap()
422 : modelStateInfo.getStateContributionMap();
423
424 // Two special cases here:
425 // 1. Button has flat appearance.
426 // 2. Button is disabled.
427 // For both cases, we need to set custom translucency.
428 boolean isFlat = this.commandButton.isFlat();
429 boolean isSpecial = isFlat || !this.commandButton.isEnabled();
430 float extraAlpha = 1.0f;
431 if (isSpecial) {
432 if (isFlat) {
433 extraAlpha = 0.0f;
434 for (Map.Entry<ComponentState, StateTransitionTracker.StateContributionInfo> activeEntry : activeStates
435 .entrySet()) {
436 ComponentState activeState = activeEntry.getKey();
437 if (activeState.isDisabled())
438 continue;
439 if (activeState == ComponentState.ENABLED)
440 continue;
441 extraAlpha += activeEntry.getValue().getContribution();
442 }
443 } else {
444 ComponentState actionAreaState = ComponentState.getState(
445 actionModel, this.commandButton);
446 if (actionAreaState.isDisabled()) {
447 extraAlpha = SubstanceColorSchemeUtilities.getAlpha(
448 this.commandButton, actionAreaState);
449 }
450 }
451 }
452 // System.out.println(extraAlpha);
453 extraAlpha = Math.min(1.0f, extraAlpha);
454 if (extraAlpha > 0.0f) {
455 Graphics2D g2d = (Graphics2D) graphics.create();
456 g2d.setComposite(LafWidgetUtilities.getAlphaComposite(
457 this.commandButton, extraAlpha, graphics));
458 g2d.drawImage(fullAlphaBackground, 0, 0, null);
459 g2d.dispose();
460 }
461 }
462
463 /*
464 * (non-Javadoc)
465 *
466 * @see org.jvnet.flamingo.common.ui.BasicCommandButtonUI#syncDisabledIcon()
467 */
468 @Override
469 protected void syncDisabledIcon() {
470 ResizableIcon currDisabledIcon = this.commandButton.getDisabledIcon();
471 ResizableIcon icon = this.commandButton.getIcon();
472 if ((currDisabledIcon == null)
473 || ((currDisabledIcon instanceof UIResource) && !currDisabledIcon
474 .getClass().isAnnotationPresent(TransitionAware.class))) {
475 if (icon != null) {
476 this.commandButton.setDisabledIcon(new ResizableIconUIResource(
477 new SubstanceDisabledResizableIcon(icon)));
478 } else {
479 this.commandButton.setDisabledIcon(null);
480 }
481 } else {
482 // disabled icon coming from app code
483 if (icon != null) {
484 this.commandButton.getDisabledIcon()
485 .setDimension(
486 new Dimension(icon.getIconWidth(), icon
487 .getIconHeight()));
488 }
489 }
490 }
491
492 @Override
493 public StateTransitionTracker getActionTransitionTracker() {
494 return this.substanceVisualStateTracker
495 .getActionStateTransitionTracker();
496 }
497
498 @Override
499 public StateTransitionTracker getPopupTransitionTracker() {
500 return this.substanceVisualStateTracker
501 .getPopupStateTransitionTracker();
502 }
503
504 @Override
505 public StateTransitionTracker getTransitionTracker() {
506 return this.substanceVisualStateTracker
507 .getActionStateTransitionTracker();
508 }
509
510 @Override
511 public boolean isInside(MouseEvent me) {
512 return this.getLayoutInfo().actionClickArea.contains(me.getPoint());
513 }
514 }