| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
package org.pushingpixels.substance.swingx; |
| 20 |
|
| 21 |
import java.awt.Color; |
| 22 |
import java.awt.Component; |
| 23 |
import java.awt.Graphics; |
| 24 |
import java.awt.Graphics2D; |
| 25 |
import java.awt.Insets; |
| 26 |
import java.awt.Rectangle; |
| 27 |
import java.awt.event.MouseEvent; |
| 28 |
import java.util.Map; |
| 29 |
|
| 30 |
import javax.swing.ButtonModel; |
| 31 |
import javax.swing.DefaultButtonModel; |
| 32 |
import javax.swing.Icon; |
| 33 |
import javax.swing.JComponent; |
| 34 |
import javax.swing.SwingConstants; |
| 35 |
import javax.swing.border.Border; |
| 36 |
import javax.swing.border.CompoundBorder; |
| 37 |
import javax.swing.border.EmptyBorder; |
| 38 |
import javax.swing.plaf.ComponentUI; |
| 39 |
|
| 40 |
import org.jdesktop.swingx.JXTaskPane; |
| 41 |
import org.jdesktop.swingx.icon.EmptyIcon; |
| 42 |
import org.jdesktop.swingx.plaf.basic.BasicTaskPaneUI; |
| 43 |
import org.pushingpixels.lafwidget.LafWidgetUtilities; |
| 44 |
import org.pushingpixels.substance.api.ColorSchemeAssociationKind; |
| 45 |
import org.pushingpixels.substance.api.ComponentState; |
| 46 |
import org.pushingpixels.substance.api.DecorationAreaType; |
| 47 |
import org.pushingpixels.substance.api.SubstanceColorScheme; |
| 48 |
import org.pushingpixels.substance.api.SubstanceLookAndFeel; |
| 49 |
import org.pushingpixels.substance.internal.animation.StateTransitionTracker; |
| 50 |
import org.pushingpixels.substance.internal.animation.TransitionAwareUI; |
| 51 |
import org.pushingpixels.substance.internal.painter.DecorationPainterUtils; |
| 52 |
import org.pushingpixels.substance.internal.painter.HighlightPainterUtils; |
| 53 |
import org.pushingpixels.substance.internal.utils.RolloverControlListener; |
| 54 |
import org.pushingpixels.substance.internal.utils.SubstanceColorSchemeUtilities; |
| 55 |
import org.pushingpixels.substance.internal.utils.SubstanceColorUtilities; |
| 56 |
import org.pushingpixels.substance.internal.utils.SubstanceCoreUtilities; |
| 57 |
import org.pushingpixels.substance.internal.utils.SubstanceImageCreator; |
| 58 |
import org.pushingpixels.substance.internal.utils.SubstanceSizeUtils; |
| 59 |
import org.pushingpixels.substance.internal.utils.SubstanceTextUtilities; |
| 60 |
import org.pushingpixels.substance.internal.utils.icon.TransitionAwareIcon; |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
public class SubstanceTaskPaneUI extends BasicTaskPaneUI implements |
| 68 |
TransitionAwareUI { |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
private SubstanceSwingxFillBackgroundDelegate bgDelegate; |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
private RolloverControlListener substanceRolloverListener; |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
protected StateTransitionTracker stateTransitionTracker; |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
protected ButtonModel taskPaneModel; |
| 88 |
|
| 89 |
public static ComponentUI createUI(JComponent comp) { |
| 90 |
SubstanceCoreUtilities.testComponentCreationThreadingViolation(comp); |
| 91 |
return new SubstanceTaskPaneUI((JXTaskPane) comp); |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
public SubstanceTaskPaneUI(JXTaskPane taskPane) { |
| 98 |
super(); |
| 99 |
this.bgDelegate = new SubstanceSwingxFillBackgroundDelegate(); |
| 100 |
|
| 101 |
this.taskPaneModel = new DefaultButtonModel(); |
| 102 |
this.taskPaneModel.setArmed(false); |
| 103 |
this.taskPaneModel.setSelected(false); |
| 104 |
this.taskPaneModel.setPressed(false); |
| 105 |
this.taskPaneModel.setRollover(false); |
| 106 |
|
| 107 |
this.stateTransitionTracker = new StateTransitionTracker(taskPane, |
| 108 |
this.taskPaneModel); |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
@Override |
| 117 |
protected void installListeners() { |
| 118 |
super.installListeners(); |
| 119 |
|
| 120 |
this.substanceRolloverListener = new RolloverControlListener(this, |
| 121 |
this.taskPaneModel); |
| 122 |
this.group.addMouseListener(this.substanceRolloverListener); |
| 123 |
this.group.addMouseMotionListener(this.substanceRolloverListener); |
| 124 |
|
| 125 |
this.stateTransitionTracker.registerModelListeners(); |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
@Override |
| 134 |
protected void uninstallListeners() { |
| 135 |
this.group.removeMouseListener(this.substanceRolloverListener); |
| 136 |
this.group.removeMouseMotionListener(this.substanceRolloverListener); |
| 137 |
this.substanceRolloverListener = null; |
| 138 |
|
| 139 |
this.stateTransitionTracker.unregisterModelListeners(); |
| 140 |
|
| 141 |
super.uninstallListeners(); |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
@Override |
| 150 |
protected void installDefaults() { |
| 151 |
SubstanceLookAndFeel.setDecorationType(this.group, |
| 152 |
DecorationAreaType.GENERAL); |
| 153 |
|
| 154 |
super.installDefaults(); |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
@Override |
| 165 |
public void uninstallUI(JComponent c) { |
| 166 |
DecorationPainterUtils.clearDecorationType(this.group); |
| 167 |
super.uninstallUI(c); |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
@Override |
| 176 |
protected Border createPaneBorder() { |
| 177 |
return new SubstancePaneBorder(); |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
@Override |
| 187 |
protected Border createContentPaneBorder() { |
| 188 |
SubstanceColorScheme colorScheme = SubstanceColorSchemeUtilities |
| 189 |
.getColorScheme(group, ColorSchemeAssociationKind.BORDER, |
| 190 |
ComponentState.ENABLED); |
| 191 |
|
| 192 |
final Color borderColor = colorScheme.isDark() ? colorScheme |
| 193 |
.getDarkColor() : SubstanceColorUtilities.getInterpolatedColor( |
| 194 |
colorScheme.getMidColor(), colorScheme.getDarkColor(), 0.4); |
| 195 |
Border outer = new Border() { |
| 196 |
@Override |
| 197 |
public void paintBorder(Component c, Graphics g, int x, int y, |
| 198 |
int width, int height) { |
| 199 |
Graphics2D g2d = (Graphics2D) g.create(); |
| 200 |
|
| 201 |
g2d.setColor(borderColor); |
| 202 |
|
| 203 |
g2d.drawLine(x, y, x, y + height - 1); |
| 204 |
|
| 205 |
g2d.drawLine(x + width - 1, y, x + width - 1, y + height - 1); |
| 206 |
|
| 207 |
g2d.drawLine(x, y + height - 1, x + width - 1, y + height - 1); |
| 208 |
|
| 209 |
g2d.dispose(); |
| 210 |
} |
| 211 |
|
| 212 |
@Override |
| 213 |
public boolean isBorderOpaque() { |
| 214 |
return true; |
| 215 |
} |
| 216 |
|
| 217 |
@Override |
| 218 |
public Insets getBorderInsets(Component c) { |
| 219 |
|
| 220 |
return new Insets(0, 1, 1, 1); |
| 221 |
} |
| 222 |
}; |
| 223 |
return new CompoundBorder(outer, new EmptyBorder(1, 2, 2, 2)); |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
protected class SubstancePaneBorder extends PaneBorder { |
| 232 |
protected Icon expandedIcon; |
| 233 |
|
| 234 |
protected Icon collapsedIcon; |
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
public SubstancePaneBorder() { |
| 240 |
this.borderColor = SubstanceColorSchemeUtilities.getColorScheme( |
| 241 |
group, ColorSchemeAssociationKind.BORDER, |
| 242 |
ComponentState.ENABLED).getMidColor(); |
| 243 |
TransitionAwareIcon.ColorSchemeAssociationKindDelegate colorSchemeAssociationDelegate = new TransitionAwareIcon.ColorSchemeAssociationKindDelegate() { |
| 244 |
@Override |
| 245 |
public ColorSchemeAssociationKind getColorSchemeAssociationKind( |
| 246 |
ComponentState state) { |
| 247 |
if (!state.isDisabled() |
| 248 |
&& (state != ComponentState.ENABLED)) { |
| 249 |
|
| 250 |
return ColorSchemeAssociationKind.HIGHLIGHT; |
| 251 |
} |
| 252 |
return ColorSchemeAssociationKind.MARK; |
| 253 |
} |
| 254 |
}; |
| 255 |
this.expandedIcon = new TransitionAwareIcon(group, |
| 256 |
new TransitionAwareIcon.TransitionAwareUIDelegate() { |
| 257 |
@Override |
| 258 |
public TransitionAwareUI getTransitionAwareUI() { |
| 259 |
return (TransitionAwareUI) group.getUI(); |
| 260 |
} |
| 261 |
}, new TransitionAwareIcon.Delegate() { |
| 262 |
public Icon getColorSchemeIcon( |
| 263 |
SubstanceColorScheme scheme) { |
| 264 |
return SubstanceImageCreator |
| 265 |
.getDoubleArrowIconDelta(SubstanceSizeUtils |
| 266 |
.getComponentFontSize(group), 0, 3, |
| 267 |
0, SwingConstants.NORTH, scheme); |
| 268 |
} |
| 269 |
}, colorSchemeAssociationDelegate, |
| 270 |
"substance.swingx.taskpane.expanded"); |
| 271 |
this.collapsedIcon = new TransitionAwareIcon(group, |
| 272 |
new TransitionAwareIcon.TransitionAwareUIDelegate() { |
| 273 |
@Override |
| 274 |
public TransitionAwareUI getTransitionAwareUI() { |
| 275 |
return (TransitionAwareUI) group.getUI(); |
| 276 |
} |
| 277 |
}, new TransitionAwareIcon.Delegate() { |
| 278 |
public Icon getColorSchemeIcon( |
| 279 |
SubstanceColorScheme scheme) { |
| 280 |
return SubstanceImageCreator |
| 281 |
.getDoubleArrowIconDelta(SubstanceSizeUtils |
| 282 |
.getComponentFontSize(group), 0, 3, |
| 283 |
0, SwingConstants.SOUTH, scheme); |
| 284 |
} |
| 285 |
}, colorSchemeAssociationDelegate, |
| 286 |
"substance.swingx.taskpane.collapsed"); |
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
SubstanceLookAndFeel.setDecorationType(this.label, |
| 291 |
DecorationAreaType.GENERAL); |
| 292 |
|
| 293 |
this.label.putClientProperty( |
| 294 |
SubstanceTextUtilities.ENFORCE_FG_COLOR, Boolean.TRUE); |
| 295 |
} |
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
@Override |
| 305 |
protected void paintTitleBackground(JXTaskPane group, Graphics g) { |
| 306 |
Graphics2D graphics = (Graphics2D) g.create(); |
| 307 |
|
| 308 |
StateTransitionTracker.ModelStateInfo modelStateInfo = stateTransitionTracker |
| 309 |
.getModelStateInfo(); |
| 310 |
Map<ComponentState, StateTransitionTracker.StateContributionInfo> activeStates = modelStateInfo |
| 311 |
.getStateContributionMap(); |
| 312 |
ComponentState currState = stateTransitionTracker |
| 313 |
.getModelStateInfo().getCurrModelState(); |
| 314 |
|
| 315 |
SubstanceColorScheme baseFillScheme = SubstanceColorSchemeUtilities |
| 316 |
.getColorScheme(group, |
| 317 |
ColorSchemeAssociationKind.HIGHLIGHT, currState); |
| 318 |
SubstanceColorScheme baseBorderScheme = SubstanceColorSchemeUtilities |
| 319 |
.getColorScheme(group, |
| 320 |
ColorSchemeAssociationKind.HIGHLIGHT_BORDER, |
| 321 |
currState); |
| 322 |
|
| 323 |
HighlightPainterUtils |
| 324 |
.paintHighlight(graphics, null, group, new Rectangle(0, 0, |
| 325 |
group.getWidth(), getTitleHeight(group)), 0.5f, |
| 326 |
null, baseFillScheme, baseBorderScheme); |
| 327 |
|
| 328 |
for (Map.Entry<ComponentState, StateTransitionTracker.StateContributionInfo> stateEntry : activeStates |
| 329 |
.entrySet()) { |
| 330 |
ComponentState activeState = stateEntry.getKey(); |
| 331 |
if (activeState == currState) |
| 332 |
continue; |
| 333 |
|
| 334 |
float contribution = stateEntry.getValue().getContribution(); |
| 335 |
if (contribution == 0.0f) |
| 336 |
continue; |
| 337 |
|
| 338 |
SubstanceColorScheme fillScheme = SubstanceColorSchemeUtilities |
| 339 |
.getColorScheme(group, |
| 340 |
ColorSchemeAssociationKind.HIGHLIGHT, |
| 341 |
activeState); |
| 342 |
SubstanceColorScheme borderScheme = SubstanceColorSchemeUtilities |
| 343 |
.getColorScheme(group, |
| 344 |
ColorSchemeAssociationKind.HIGHLIGHT_BORDER, |
| 345 |
activeState); |
| 346 |
|
| 347 |
graphics.setComposite(LafWidgetUtilities.getAlphaComposite( |
| 348 |
group, contribution, g)); |
| 349 |
HighlightPainterUtils.paintHighlight(graphics, null, group, |
| 350 |
new Rectangle(0, 0, group.getWidth(), |
| 351 |
getTitleHeight(group)), 0.5f, null, fillScheme, |
| 352 |
borderScheme); |
| 353 |
} |
| 354 |
graphics.dispose(); |
| 355 |
} |
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
@Override |
| 365 |
protected Color getPaintColor(JXTaskPane group) { |
| 366 |
StateTransitionTracker.ModelStateInfo modelStateInfo = stateTransitionTracker |
| 367 |
.getModelStateInfo(); |
| 368 |
Map<ComponentState, StateTransitionTracker.StateContributionInfo> activeStates = modelStateInfo |
| 369 |
.getStateContributionMap(); |
| 370 |
ComponentState currState = stateTransitionTracker |
| 371 |
.getModelStateInfo().getCurrModelState(); |
| 372 |
|
| 373 |
if (currState.isDisabled() || (activeStates.size() == 1)) |
| 374 |
return getColorSchemeForState(group, currState) |
| 375 |
.getForegroundColor(); |
| 376 |
|
| 377 |
float aggrRed = 0; |
| 378 |
float aggrGreen = 0; |
| 379 |
float aggrBlue = 0; |
| 380 |
|
| 381 |
for (Map.Entry<ComponentState, StateTransitionTracker.StateContributionInfo> activeEntry : modelStateInfo |
| 382 |
.getStateContributionMap().entrySet()) { |
| 383 |
ComponentState activeState = activeEntry.getKey(); |
| 384 |
SubstanceColorScheme scheme = getColorSchemeForState(group, |
| 385 |
activeState); |
| 386 |
Color schemeFg = scheme.getForegroundColor(); |
| 387 |
float contribution = activeEntry.getValue().getContribution(); |
| 388 |
aggrRed += schemeFg.getRed() * contribution; |
| 389 |
aggrGreen += schemeFg.getGreen() * contribution; |
| 390 |
aggrBlue += schemeFg.getBlue() * contribution; |
| 391 |
} |
| 392 |
return new Color((int) aggrRed, (int) aggrGreen, (int) aggrBlue); |
| 393 |
} |
| 394 |
|
| 395 |
private SubstanceColorScheme getColorSchemeForState(JXTaskPane group, |
| 396 |
ComponentState state) { |
| 397 |
ColorSchemeAssociationKind assocKind = ColorSchemeAssociationKind.FILL; |
| 398 |
if (!state.isDisabled() && (state != ComponentState.ENABLED)) { |
| 399 |
|
| 400 |
assocKind = ColorSchemeAssociationKind.HIGHLIGHT; |
| 401 |
} |
| 402 |
SubstanceColorScheme currScheme = SubstanceColorSchemeUtilities |
| 403 |
.getColorScheme(group, assocKind, state); |
| 404 |
return currScheme; |
| 405 |
} |
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
@Override |
| 415 |
protected void paintExpandedControls(JXTaskPane group, Graphics g, |
| 416 |
int x, int y, int width, int height) { |
| 417 |
Icon arrowIcon = group.isCollapsed() ? collapsedIcon : expandedIcon; |
| 418 |
int dx = (width - arrowIcon.getIconWidth()) / 2; |
| 419 |
int dy = 1 + (height - arrowIcon.getIconHeight()) / 2; |
| 420 |
arrowIcon.paintIcon(group, g, x + dx, y + dy); |
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
} |
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
@Override |
| 435 |
protected boolean isMouseOverBorder() { |
| 436 |
return true; |
| 437 |
} |
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
@Override |
| 447 |
protected void paintFocus(Graphics g, Color paintColor, int x, int y, |
| 448 |
int width, int height) { |
| 449 |
SubstanceCoreUtilities.paintFocus(g, group, group, |
| 450 |
SubstanceTaskPaneUI.this, new Rectangle(x, y, width - 1, |
| 451 |
height - 1), label.getBounds(), 1.0f, 0); |
| 452 |
} |
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
@Override |
| 462 |
protected void configureLabel(JXTaskPane group) { |
| 463 |
label.applyComponentOrientation(group.getComponentOrientation()); |
| 464 |
label.setFont(group.getFont()); |
| 465 |
label.setText(group.getTitle()); |
| 466 |
label.setIcon(group.getIcon() == null ? new EmptyIcon() : group |
| 467 |
.getIcon()); |
| 468 |
} |
| 469 |
} |
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
@Override |
| 478 |
public void paint(Graphics g, JComponent c) { |
| 479 |
this.bgDelegate.paint(c, (Graphics2D) g, false); |
| 480 |
} |
| 481 |
|
| 482 |
@Override |
| 483 |
public boolean isInside(MouseEvent me) { |
| 484 |
return this.isInBorder(me); |
| 485 |
} |
| 486 |
|
| 487 |
@Override |
| 488 |
public StateTransitionTracker getTransitionTracker() { |
| 489 |
return this.stateTransitionTracker; |
| 490 |
} |
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
@Override |
| 500 |
protected int getTitleHeight(Component c) { |
| 501 |
return SubstanceSizeUtils.getAdjustedSize(SubstanceSizeUtils |
| 502 |
.getComponentFontSize(c), super.getTitleHeight(c), 2, 3, false); |
| 503 |
} |
| 504 |
} |