Debian Codegraph @

substance-flamingo/src/main/java/org/pushingpixels/substance/flamingo/ribbon/RibbonBackgroundDelegate.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;
31
32 import java.awt.*;
33 import java.awt.geom.GeneralPath;
34 import java.awt.image.BufferedImage;
35 import java.util.*;
36
37 import javax.swing.AbstractButton;
38 import javax.swing.SwingUtilities;
39
40 import org.pushingpixels.flamingo.api.ribbon.*;
41 import org.pushingpixels.flamingo.internal.ui.ribbon.JRibbonTaskToggleButton;
42 import org.pushingpixels.lafwidget.LafWidgetUtilities;
43 import org.pushingpixels.substance.api.*;
44 import org.pushingpixels.substance.api.SubstanceConstants.Side;
45 import org.pushingpixels.substance.api.painter.border.SubstanceBorderPainter;
46 import org.pushingpixels.substance.api.painter.fill.SubstanceFillPainter;
47 import org.pushingpixels.substance.flamingo.ribbon.ui.RibbonBorderShaper;
48 import org.pushingpixels.substance.internal.animation.StateTransitionTracker;
49 import org.pushingpixels.substance.internal.animation.TransitionAwareUI;
50 import org.pushingpixels.substance.internal.colorscheme.ShiftColorScheme;
51 import org.pushingpixels.substance.internal.utils.*;
52
53 /**
54 * Delegate class for painting backgrounds of buttons in Ribbon plugin.
55 *
56 * @author Kirill Grouchnikov
57 */
58 public class RibbonBackgroundDelegate {
59 /**
60 * Cache for background images of ribbon buttons. Each time
61 * {@link #getTaskToggleButtonBackground(org.pushingpixels.flamingo.internal.ui.ribbon.JRibbonTaskToggleButton, int, int)} is
62 * called with <code>isRoundCorners</code> equal to <code>true</code>, it
63 * checks <code>this</code> map to see if it already contains such
64 * background. If so, the background from the map is returned.
65 */
66 private static LazyResettableHashMap<BufferedImage> imageCache = new LazyResettableHashMap<BufferedImage>(
67 "Substance.Flamingo.RibbonBackgroundDelegate");
68
69 /**
70 * Retrieves background image for the specified ribbon button.
71 *
72 * @param button
73 * Button.
74 * @param width
75 * Button width.
76 * @param height
77 * Button height.
78 * @return Button background image.
79 */
80 private static synchronized BufferedImage getTaskToggleButtonBackground(
81 JRibbonTaskToggleButton button, int width, int height) {
82 JRibbon ribbon = (JRibbon) SwingUtilities.getAncestorOfClass(
83 JRibbon.class, button);
84 TransitionAwareUI transitionAwareUI = (TransitionAwareUI) button
85 .getUI();
86 StateTransitionTracker stateTransitionTracker = transitionAwareUI
87 .getTransitionTracker();
88 ComponentState currState = ComponentState.getState(button
89 .getActionModel(), button);
90 StateTransitionTracker.ModelStateInfo modelStateInfo = stateTransitionTracker
91 .getModelStateInfo();
92 Map<ComponentState, StateTransitionTracker.StateContributionInfo> activeStates = modelStateInfo
93 .getStateContributionMap();
94
95 SubstanceSkin skin = SubstanceCoreUtilities.getSkin(button);
96
97 SubstanceColorScheme baseFillScheme = skin.getColorScheme(button,
98 ColorSchemeAssociationKind.TAB, currState);
99 SubstanceColorScheme baseBorderScheme = skin.getColorScheme(ribbon,
100 ColorSchemeAssociationKind.TAB_BORDER, currState);
101
102 SubstanceFillPainter fillPainter = skin.getFillPainter();
103 SubstanceBorderPainter borderPainter = skin.getBorderPainter();
104
105 JRibbon parent = (JRibbon) SwingUtilities.getAncestorOfClass(
106 JRibbon.class, button);
107 RibbonTask selectedTask = parent.getSelectedTask();
108 AbstractRibbonBand band = (selectedTask.getBandCount() == 0) ? null
109 : selectedTask.getBand(0);
110 Color bgColor = (band != null) ? band.getBackground() : parent
111 .getBackground();
112
113 HashMapKey baseKey = SubstanceCoreUtilities.getHashKey(width, height,
114 baseFillScheme.getDisplayName(), baseBorderScheme
115 .getDisplayName(), fillPainter.getDisplayName(),
116 borderPainter.getDisplayName(), button.getParent()
117 .getBackground().getRGB(), button.getActionModel()
118 .isSelected(), button.getContextualGroupHueColor(),
119 button.getActionModel().isSelected(), ribbon.isMinimized(),
120 skin.getSelectedTabFadeStart(), skin.getSelectedTabFadeEnd(),
121 bgColor);
122 BufferedImage baseLayer = imageCache.get(baseKey);
123 if (baseLayer == null) {
124 baseLayer = getSingleLayer(button, width, height, ribbon,
125 baseFillScheme, baseBorderScheme, fillPainter,
126 borderPainter);
127
128 imageCache.put(baseKey, baseLayer);
129 }
130
131 if (currState.isDisabled() || (activeStates.size() == 1)) {
132 return baseLayer;
133 }
134
135 BufferedImage result = SubstanceCoreUtilities.getBlankImage(width,
136 height);
137 Graphics2D g2d = result.createGraphics();
138
139 g2d.drawImage(baseLayer, 0, 0, null);
140
141 for (Map.Entry<ComponentState, StateTransitionTracker.StateContributionInfo> activeEntry : activeStates
142 .entrySet()) {
143 ComponentState activeState = activeEntry.getKey();
144 if (activeState == currState)
145 continue;
146
147 float contribution = activeEntry.getValue().getContribution();
148 if (contribution == 0.0f)
149 continue;
150
151 SubstanceColorScheme fillScheme = SubstanceColorSchemeUtilities
152 .getColorScheme(button, ColorSchemeAssociationKind.TAB,
153 activeState);
154 SubstanceColorScheme borderScheme = SubstanceColorSchemeUtilities
155 .getColorScheme(ribbon,
156 ColorSchemeAssociationKind.TAB_BORDER, activeState);
157
158 HashMapKey key = SubstanceCoreUtilities.getHashKey(width, height,
159 fillScheme.getDisplayName(), borderScheme.getDisplayName(),
160 fillPainter.getDisplayName(), borderPainter
161 .getDisplayName(), button.getParent()
162 .getBackground().getRGB(), button.getActionModel()
163 .isSelected(), button.getContextualGroupHueColor(),
164 button.getActionModel().isSelected(), ribbon.isMinimized(),
165 skin.getSelectedTabFadeStart(), skin
166 .getSelectedTabFadeEnd(), bgColor);
167
168 BufferedImage layer = imageCache.get(key);
169 if (layer == null) {
170 layer = getSingleLayer(button, width, height, ribbon,
171 fillScheme, borderScheme, fillPainter, borderPainter);
172
173 imageCache.put(key, layer);
174 }
175
176 g2d.setComposite(AlphaComposite.SrcOver.derive(contribution));
177 g2d.drawImage(layer, 0, 0, null);
178 }
179
180 g2d.dispose();
181 return result;
182 }
183
184 private static BufferedImage getSingleLayer(JRibbonTaskToggleButton button,
185 int width, int height, JRibbon ribbon,
186 SubstanceColorScheme fillScheme, SubstanceColorScheme borderScheme,
187 SubstanceFillPainter fillPainter,
188 SubstanceBorderPainter borderPainter) {
189 Set<Side> bottom = new HashSet<Side>();
190 bottom.add(Side.BOTTOM);
191
192 Color contextualGroupHueColor = button.getContextualGroupHueColor();
193 if (contextualGroupHueColor != null) {
194 fillScheme = ShiftColorScheme.getShiftedScheme(fillScheme,
195 contextualGroupHueColor,
196 RibbonContextualTaskGroup.HUE_ALPHA, null, 0.0f);
197 }
198
199 float radius = RibbonBorderShaper.getRibbonToggleButtonRadius(button);
200 int borderDelta = (int) Math.ceil(2.0 * SubstanceSizeUtils
201 .getBorderStrokeWidth(SubstanceSizeUtils
202 .getComponentFontSize(button)));
203 int borderInsets = (int) Math.floor(SubstanceSizeUtils
204 .getBorderStrokeWidth(SubstanceSizeUtils
205 .getComponentFontSize(button)) / 2.0);
206 GeneralPath contour = SubstanceOutlineUtilities.getBaseOutline(width,
207 height + 2 + borderDelta, radius, bottom, borderInsets);
208
209 BufferedImage result = SubstanceCoreUtilities.getBlankImage(width,
210 height + 2);
211 Graphics2D graphics = result.createGraphics();
212 fillPainter.paintContourBackground(graphics, button, width, height + 2
213 + borderDelta, contour, false, fillScheme, true);
214
215 int borderThickness = (int) SubstanceSizeUtils
216 .getBorderStrokeWidth(SubstanceSizeUtils
217 .getComponentFontSize(button));
218 GeneralPath contourInner = SubstanceOutlineUtilities.getBaseOutline(
219 width, height + 2 + borderDelta, radius, bottom,
220 borderThickness + borderInsets);
221
222 borderPainter.paintBorder(graphics, button, width, height + 2, contour,
223 contourInner, borderScheme);
224 graphics.dispose();
225
226 if (button.getActionModel().isSelected()
227 && (button.getContextualGroupHueColor() == null)) {
228 int fw = result.getWidth();
229 int fh = result.getHeight();
230 BufferedImage fade = SubstanceCoreUtilities.getBlankImage(fw, fh);
231 Graphics2D fadeGraphics = fade.createGraphics();
232 JRibbon parent = (JRibbon) SwingUtilities.getAncestorOfClass(
233 JRibbon.class, button);
234 RibbonTask selectedTask = parent.getSelectedTask();
235 AbstractRibbonBand band = (selectedTask.getBandCount() == 0) ? null
236 : selectedTask.getBand(0);
237 if (band != null) {
238 fadeGraphics.setColor(band.getBackground());
239 } else {
240 fadeGraphics.setColor(parent.getBackground());
241 }
242 fadeGraphics.fillRect(0, 0, fw, fh);
243 SubstanceSkin skin = SubstanceCoreUtilities.getSkin(button);
244 if (skin.getWatermark() != null)
245 skin.getWatermark().drawWatermarkImage(fadeGraphics, button, 0,
246 0, fw, fh);
247
248 borderPainter.paintBorder(fadeGraphics, button, width, height + 2,
249 contour, contourInner, borderScheme);
250
251 result = SubstanceCoreUtilities.blendImagesVertical(result, fade,
252 skin.getSelectedTabFadeStart(), skin
253 .getSelectedTabFadeEnd());
254 }
255 return result;
256 }
257
258 /**
259 * Updates background of the specified button.
260 *
261 * @param g
262 * Graphic context.
263 * @param button
264 * Button to update.
265 */
266 public void updateTaskToggleButtonBackground(Graphics g,
267 JRibbonTaskToggleButton button) {
268 Graphics2D g2d = (Graphics2D) g.create();
269
270 int width = button.getWidth();
271 int height = button.getHeight();
272
273 BufferedImage ribbonBackground = getTaskToggleButtonBackground(button,
274 width, height);
275
276 TransitionAwareUI ui = (TransitionAwareUI) button.getUI();
277 StateTransitionTracker stateTransitionTracker = ui
278 .getTransitionTracker();
279
280 float extraActionAlpha = 0.0f;
281 for (Map.Entry<ComponentState, StateTransitionTracker.StateContributionInfo> activeEntry : stateTransitionTracker
282 .getModelStateInfo().getStateContributionMap().entrySet()) {
283 ComponentState activeState = activeEntry.getKey();
284 if (activeState.isDisabled())
285 continue;
286 if (activeState == ComponentState.ENABLED)
287 continue;
288 extraActionAlpha += activeEntry.getValue().getContribution();
289 }
290
291 g2d.setComposite(LafWidgetUtilities.getAlphaComposite(button,
292 extraActionAlpha, g));
293 g2d.drawImage(ribbonBackground, 0, 0, null);
294
295 g2d.dispose();
296 }
297 }