| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 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 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
public class SubstanceRibbonBandUI extends BasicRibbonBandUI { |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
public static ComponentUI createUI(JComponent comp) { |
| 68 |
SubstanceCoreUtilities.testComponentCreationThreadingViolation(comp); |
| 69 |
return new SubstanceRibbonBandUI(); |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 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 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
@Override |
| 98 |
protected void uninstallDefaults() { |
| 99 |
DecorationPainterUtils.clearDecorationType(this.ribbonBand); |
| 100 |
super.uninstallDefaults(); |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 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 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
@Override |
| 131 |
protected void paintBandTitle(Graphics graphics, Rectangle titleRectangle, |
| 132 |
String title) { |
| 133 |
|
| 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 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 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 |
|
| 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 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 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 |
|
| 210 |
outline.moveTo(0, 0); |
| 211 |
|
| 212 |
outline.lineTo(titleRectangle.width, 0); |
| 213 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 234 |
g2d.setColor(colorScheme.getMidColor()); |
| 235 |
g2d.setStroke(new BasicStroke(1.2f)); |
| 236 |
g2d.draw(outline); |
| 237 |
|
| 238 |
|
| 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 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 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 |
|
| 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 |
|
| 305 |
|
| 306 |
|
| 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 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
@Override |
| 320 |
protected JCommandButton createExpandButton() { |
| 321 |
RibbonBandExpandButton result = new RibbonBandExpandButton(); |
| 322 |
|
| 323 |
|
| 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 |
} |