Debian Codegraph @

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

raw symbols SCIP
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.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 * UI for {@link JRibbonTaskToggleButton} components in <b>Substance</b> look
58 * and feel.
59 *
60 * @author Kirill Grouchnikov
61 */
62 public class SubstanceRibbonTaskToggleButtonUI extends
63 BasicRibbonTaskToggleButtonUI implements TransitionAwareUI {
64 /**
65 * Tracker for visual state transitions.
66 */
67 protected CommandButtonVisualStateTracker substanceVisualStateTracker;
68
69 /**
70 * Property change listener on the button.
71 */
72 private PropertyChangeListener substancePropertyChangeListener;
73
74 /*
75 * (non-Javadoc)
76 *
77 * @see javax.swing.plaf.ComponentUI#createUI(javax.swing.JComponent)
78 */
79 public static ComponentUI createUI(JComponent comp) {
80 return new SubstanceRibbonTaskToggleButtonUI();
81 }
82
83 /**
84 * Painting delegate.
85 */
86 private RibbonBackgroundDelegate delegate;
87
88 /**
89 * Simple constructor.
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 * (non-Javadoc)
163 *
164 * @see javax.swing.plaf.basic.BasicToggleButtonUI#paint(java.awt.Graphics,
165 * javax.swing.JComponent)
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 * (non-Javadoc)
179 *
180 * @see
181 * org.jvnet.flamingo.ribbon.ui.BasicToggleTabButtonUI#update(java.awt.Graphics
182 * , javax.swing.JComponent)
183 */
184 @Override
185 public void update(Graphics g, JComponent c) {
186 this.paint(g, c);
187 }
188
189 /*
190 * (non-Javadoc)
191 *
192 * @see
193 * org.jvnet.flamingo.ribbon.ui.BasicRibbonTaskToggleButtonUI#paintText(
194 * java.awt.Graphics)
195 */
196 @Override
197 protected void paintText(Graphics g) {
198 FontMetrics fm = g.getFontMetrics();
199 String toPaint = this.commandButton.getText();
200
201 // compute the insets
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 // and the text rectangle
210 Rectangle textRect = new Rectangle(insets,
211 1 + (h - fm.getHeight()) / 2, w - 2 * insets, fm.getHeight());
212
213 // show the first characters that fit into the available text rectangle
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 * (non-Javadoc)
288 *
289 * @see
290 * javax.swing.plaf.ComponentUI#getPreferredSize(javax.swing.JComponent)
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 }