Debian Codegraph @

substance-flamingo/src/main/java/org/pushingpixels/substance/flamingo/ribbon/ui/SubstanceRibbonBandUI.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.geom.Arc2D;
34 import java.awt.geom.GeneralPath;
35
36 import javax.swing.*;
37 import javax.swing.border.EmptyBorder;
38 import javax.swing.plaf.*;
39
40 import org.pushingpixels.flamingo.api.common.AbstractCommandButton;
41 import org.pushingpixels.flamingo.api.common.JCommandButton;
42 import org.pushingpixels.flamingo.api.common.icon.ResizableIcon;
43 import org.pushingpixels.flamingo.internal.ui.ribbon.BasicRibbonBandUI;
44 import org.pushingpixels.lafwidget.LafWidgetUtilities;
45 import org.pushingpixels.substance.api.*;
46 import org.pushingpixels.substance.api.painter.fill.MatteFillPainter;
47 import org.pushingpixels.substance.api.painter.fill.SubstanceFillPainter;
48 import org.pushingpixels.substance.api.watermark.SubstanceWatermark;
49 import org.pushingpixels.substance.flamingo.common.TransitionAwareResizableIcon;
50 import org.pushingpixels.substance.flamingo.common.ui.ActionPopupTransitionAwareUI;
51 import org.pushingpixels.substance.internal.animation.StateTransitionTracker;
52 import org.pushingpixels.substance.internal.painter.DecorationPainterUtils;
53 import org.pushingpixels.substance.internal.painter.SeparatorPainterUtils;
54 import org.pushingpixels.substance.internal.utils.*;
55
56 /**
57 * UI for ribbon bands in <b>Substance</b> look and feel.
58 *
59 * @author Kirill Grouchnikov
60 */
61 public class SubstanceRibbonBandUI extends BasicRibbonBandUI {
62 /*
63 * (non-Javadoc)
64 *
65 * @see javax.swing.plaf.ComponentUI#createUI(javax.swing.JComponent)
66 */
67 public static ComponentUI createUI(JComponent comp) {
68 SubstanceCoreUtilities.testComponentCreationThreadingViolation(comp);
69 return new SubstanceRibbonBandUI();
70 }
71
72 /*
73 * (non-Javadoc)
74 *
75 * @see org.jvnet.flamingo.ribbon.ui.BasicRibbonBandUI#installDefaults()
76 */
77 @Override
78 protected void installDefaults() {
79 super.installDefaults();
80 SubstanceLookAndFeel.setDecorationType(this.ribbonBand,
81 DecorationAreaType.GENERAL);
82
83 Color backgr = this.ribbonBand.getBackground();
84 if (backgr == null || backgr instanceof UIResource) {
85 Color toSet = SubstanceColorSchemeUtilities.getColorScheme(
86 this.ribbonBand, ComponentState.ENABLED)
87 .getBackgroundFillColor();
88 this.ribbonBand.setBackground(new ColorUIResource(toSet));
89 }
90 }
91
92 /*
93 * (non-Javadoc)
94 *
95 * @see org.jvnet.flamingo.ribbon.ui.BasicRibbonBandUI#uninstallDefaults()
96 */
97 @Override
98 protected void uninstallDefaults() {
99 DecorationPainterUtils.clearDecorationType(this.ribbonBand);
100 super.uninstallDefaults();
101 }
102
103 /*
104 * (non-Javadoc)
105 *
106 * @see org.jvnet.flamingo.ribbon.ui.BasicRibbonBandUI#installComponents()
107 */
108 @Override
109 protected void installComponents() {
110 super.installComponents();
111
112 SubstanceLookAndFeel.setDecorationType(this.ribbonBand,
113 DecorationAreaType.GENERAL);
114
115 if (this.expandButton != null) {
116 this.expandButton.putClientProperty(
117 SubstanceLookAndFeel.USE_THEMED_DEFAULT_ICONS,
118 Boolean.FALSE);
119 this.expandButton.setFocusable(false);
120 }
121 }
122
123 /*
124 * (non-Javadoc)
125 *
126 * @see
127 * org.jvnet.flamingo.ribbon.ui.BasicRibbonBandUI#paintBandTitle(java.awt
128 * .Graphics, java.awt.Rectangle, java.lang.String)
129 */
130 @Override
131 protected void paintBandTitle(Graphics graphics, Rectangle titleRectangle,
132 String title) {
133 // fix for issue 28 - empty ribbon band
134 if (titleRectangle.width <= 0)
135 return;
136
137 Graphics2D g2d = (Graphics2D) graphics.create();
138 g2d.setFont(UIManager.getFont("Ribbon.font"));
139
140 FontMetrics fm = graphics.getFontMetrics();
141
142 int currLength = (int) fm.getStringBounds(title, g2d).getWidth();
143 String titleToPaint = title;
144 while (currLength > titleRectangle.width) {
145 title = title.substring(0, title.length() - 1);
146 titleToPaint = title + "...";
147 currLength = (int) fm.getStringBounds(titleToPaint, g2d).getWidth();
148 }
149
150 SubstanceSkin skin = SubstanceCoreUtilities.getSkin(this.ribbonBand);
151
152 // compute the FG color for the ribbon band
153 // SubstanceColorScheme fgColorScheme = skin.getColorScheme(
154 // this.expandButton, ComponentState.ENABLED);
155
156 // make the title color blend a little with the background
157 SubstanceColorScheme bgColorScheme = skin
158 .getBackgroundColorScheme(DecorationAreaType.HEADER);
159 Color bgFillColor = bgColorScheme.getBackgroundFillColor();
160 Color fgColor = bgColorScheme.getForegroundColor();
161 fgColor = SubstanceColorUtilities.getInterpolatedColor(fgColor,
162 bgFillColor, 0.8f);
163
164 g2d.setColor(fgColor);
165
166 // restrict the title rectangle so that it's painted centered
167 int deltaX = (titleRectangle.width - currLength) / 2;
168 int deltaY = (titleRectangle.height - fm.getAscent() - fm.getDescent()) / 2;
169 Rectangle smallTitleRectangle = new Rectangle(
170 titleRectangle.x + deltaX, titleRectangle.y + deltaY,
171 titleRectangle.width - 2 * deltaX, titleRectangle.height - 2
172 * deltaY);
173 SubstanceTextUtilities.paintText(g2d, this.ribbonBand,
174 smallTitleRectangle, titleToPaint, -1, g2d.getFont(), g2d
175 .getColor(), g2d.getClipBounds());
176
177 g2d.dispose();
178 }
179
180 /*
181 * (non-Javadoc)
182 *
183 * @see
184 * org.jvnet.flamingo.ribbon.ui.BasicRibbonBandUI#paintBandTitleBackground
185 * (java.awt.Graphics, java.awt.Rectangle, java.lang.String)
186 */
187 @Override
188 protected void paintBandTitleBackground(Graphics g,
189 Rectangle titleRectangle, String title) {
190 SubstanceFillPainter gradientPainter = new MatteFillPainter();
191
192 Graphics2D g2d = (Graphics2D) g.create();
193
194 SubstanceSkin skin = SubstanceCoreUtilities.getSkin(this.ribbonBand);
195 SubstanceColorScheme colorScheme = skin
196 .getBackgroundColorScheme(DecorationAreaType.HEADER);
197
198 boolean isDark = colorScheme.isDark();
199 float alpha = 0.85f - (isDark ? 0.15f : 0.35f) * this.rolloverAmount;
200 g2d.setComposite(LafWidgetUtilities.getAlphaComposite(this.ribbonBand,
201 alpha, g));
202
203 SubstanceRibbonBandBorder border = (SubstanceRibbonBandBorder) this.ribbonBand
204 .getBorder();
205 float cornerRadius = border.getCornerRadius(this.ribbonBand);
206
207 GeneralPath outline = new GeneralPath();
208
209 // top left
210 outline.moveTo(0, 0);
211 // top right
212 outline.lineTo(titleRectangle.width, 0);
213 // bottom right
214 outline.lineTo(titleRectangle.width, titleRectangle.height
215 - cornerRadius - 1);
216 outline.append(new Arc2D.Double(titleRectangle.width - 2 * cornerRadius
217 - 1, titleRectangle.height - 1 - 2 * cornerRadius,
218 2 * cornerRadius, 2 * cornerRadius, 0, -90, Arc2D.OPEN), true);
219 // bottom left
220 outline.lineTo(cornerRadius, titleRectangle.height - 1);
221 outline.append(new Arc2D.Double(0, titleRectangle.height - 2
222 * cornerRadius - 1, 2 * cornerRadius, 2 * cornerRadius, 270,
223 -90, Arc2D.OPEN), true);
224 // top left
225 outline.lineTo(0, 0);
226
227 g2d.translate(titleRectangle.x, titleRectangle.y);
228
229 gradientPainter.paintContourBackground(g2d, this.ribbonBand,
230 titleRectangle.width, titleRectangle.height, outline, false,
231 colorScheme, false);
232
233 // outline
234 g2d.setColor(colorScheme.getMidColor());
235 g2d.setStroke(new BasicStroke(1.2f));
236 g2d.draw(outline);
237
238 // top line
239 SubstanceColorScheme separatorScheme = SubstanceLookAndFeel
240 .getCurrentSkin(this.ribbonBand).getColorScheme(
241 DecorationAreaType.HEADER,
242 ColorSchemeAssociationKind.SEPARATOR,
243 ComponentState.ENABLED);
244 g2d.setComposite(LafWidgetUtilities.getAlphaComposite(this.ribbonBand,
245 alpha * 0.7f, g));
246 SeparatorPainterUtils.paintSeparator(this.ribbonBand, g2d,
247 separatorScheme, titleRectangle.width, 1,
248 SwingConstants.HORIZONTAL, false, 0, 0, true);
249 g2d.dispose();
250 }
251
252 /*
253 * (non-Javadoc)
254 *
255 * @see
256 * org.jvnet.flamingo.ribbon.ui.BasicRibbonBandUI#paintBandBackground(java
257 * .awt.Graphics, java.awt.Rectangle)
258 */
259 @Override
260 protected void paintBandBackground(Graphics graphics, Rectangle toFill) {
261 paintRibbonBandBackground(graphics, this.ribbonBand,
262 this.rolloverAmount, 0);
263 }
264
265 public static void paintRibbonBandBackground(Graphics graphics,
266 Component comp, float rolloverAmount, int dy) {
267
268 SubstanceSkin skin = SubstanceCoreUtilities.getSkin(comp);
269 SubstanceColorScheme bgScheme = skin
270 .getBackgroundColorScheme(SubstanceLookAndFeel
271 .getDecorationType(comp));
272
273 int offset = 20 - dy;
274 float bp = (float) offset / (float) comp.getHeight();
275
276 Color c1 = bgScheme.getUltraLightColor();
277 Color c2 = SubstanceColorUtilities.getInterpolatedColor(bgScheme
278 .getUltraLightColor(), bgScheme.getExtraLightColor(),
279 rolloverAmount);
280 Color c3 = SubstanceColorUtilities
281 .getInterpolatedColor(bgScheme.getExtraLightColor(), bgScheme
282 .getLightColor(), rolloverAmount);
283 Color c4 = SubstanceColorUtilities.getInterpolatedColor(bgScheme
284 .getUltraLightColor(), bgScheme.getExtraLightColor(),
285 rolloverAmount);
286
287 LinearGradientPaint fillPaint = new LinearGradientPaint(0, 0, 0, comp
288 .getHeight(), new float[] { 0.0f, bp - 0.00001f, bp, 1.0f },
289 new Color[] { c1, c2, c3, c4 });
290
291 Graphics2D g2d = (Graphics2D) graphics.create();
292 g2d.setPaint(fillPaint);
293 g2d.fillRect(0, 0, comp.getWidth(), comp.getHeight());
294
295 // stamp watermark
296 SubstanceWatermark watermark = skin.getWatermark();
297 if ((watermark != null) && SubstanceCoreUtilities.toDrawWatermark(comp))
298 watermark.drawWatermarkImage(g2d, comp, 0, 0, comp.getWidth(), comp
299 .getHeight());
300 g2d.dispose();
301 }
302
303 /*
304 * (non-Javadoc)
305 *
306 * @see org.jvnet.flamingo.ribbon.ui.BasicRibbonBandUI#getBandTitleHeight()
307 */
308 @Override
309 public int getBandTitleHeight() {
310 return 1 + SubstanceSizeUtils.getAdjustedSize(SubstanceSizeUtils
311 .getComponentFontSize(this.ribbonBand), 17, 1, 1, false);
312 }
313
314 /*
315 * (non-Javadoc)
316 *
317 * @see org.jvnet.flamingo.ribbon.ui.BasicRibbonBandUI#createExpandButton()
318 */
319 @Override
320 protected JCommandButton createExpandButton() {
321 RibbonBandExpandButton result = new RibbonBandExpandButton();
322 // since paintBandTitleBackground uses HEADER, mark this button with
323 // HEADER as well to sync the mark color
324 SubstanceLookAndFeel.setDecorationType(result,
325 DecorationAreaType.HEADER);
326 result.setIcon(getExpandButtonIcon(result));
327 return result;
328 }
329
330 private static ResizableIcon getExpandButtonIcon(
331 final AbstractCommandButton button) {
332 final int fontSize = SubstanceSizeUtils.getComponentFontSize(button);
333 int arrowIconHeight = (int) SubstanceSizeUtils
334 .getSmallArrowIconHeight(fontSize) + 2;
335 int arrowIconWidth = (int) SubstanceSizeUtils
336 .getSmallArrowIconWidth(fontSize);
337 final ResizableIcon arrowIcon = new TransitionAwareResizableIcon(
338 button,
339 new TransitionAwareResizableIcon.StateTransitionTrackerDelegate() {
340 @Override
341 public StateTransitionTracker getStateTransitionTracker() {
342 return ((ActionPopupTransitionAwareUI) button.getUI())
343 .getActionTransitionTracker();
344 }
345 }, new TransitionAwareResizableIcon.Delegate() {
346 @Override
347 public Icon getColorSchemeIcon(SubstanceColorScheme scheme,
348 int width, int height) {
349 return SubstanceImageCreator
350 .getDoubleArrowIcon(
351 SubstanceSizeUtils
352 .getComponentFontSize(button),
353 width,
354 height,
355 SubstanceSizeUtils
356 .getDoubleArrowStrokeWidth(fontSize),
357 button.getComponentOrientation()
358 .isLeftToRight() ? SwingConstants.EAST
359 : SwingConstants.WEST, scheme);
360 }
361 }, new Dimension(arrowIconWidth, arrowIconHeight));
362 return arrowIcon;
363 }
364
365 @Override
366 protected void syncExpandButtonIcon() {
367 ResizableIcon icon = getExpandButtonIcon(this.expandButton);
368 this.expandButton.setIcon(icon);
369 }
370
371 private class RibbonBandExpandButton extends JCommandButton implements
372 SubstanceInternalButton {
373 public RibbonBandExpandButton() {
374 super(null, null);
375 this.setFocusable(false);
376
377 this.setBorder(new EmptyBorder(3, 2, 3, 2));
378 this.setActionKeyTip(ribbonBand.getExpandButtonKeyTip());
379 this.setActionRichTooltip(ribbonBand.getExpandButtonRichTooltip());
380 }
381 }
382 }