-
-
Notifications
You must be signed in to change notification settings - Fork 256
/
FlatTreeUI.java
751 lines (649 loc) · 26.4 KB
/
FlatTreeUI.java
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
/*
* Copyright 2019 FormDev Software GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.formdev.flatlaf.ui;
import static com.formdev.flatlaf.FlatClientProperties.*;
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.geom.Rectangle2D;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Map;
import javax.swing.CellRendererPane;
import javax.swing.Icon;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JTree;
import javax.swing.LookAndFeel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.JTree.DropLocation;
import javax.swing.event.TreeSelectionListener;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.basic.BasicTreeUI;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.TreePath;
import com.formdev.flatlaf.ui.FlatStylingSupport.Styleable;
import com.formdev.flatlaf.ui.FlatStylingSupport.StyleableUI;
import com.formdev.flatlaf.util.LoggingFacade;
import com.formdev.flatlaf.util.UIScale;
/**
* Provides the Flat LaF UI delegate for {@link javax.swing.JTree}.
*
* <!-- BasicTreeUI -->
*
* @uiDefault Tree.font Font
* @uiDefault Tree.background Color
* @uiDefault Tree.foreground Color unused
* @uiDefault Tree.hash Color
* @uiDefault Tree.dropLineColor Color
* @uiDefault Tree.expandedIcon Icon
* @uiDefault Tree.collapsedIcon Icon
* @uiDefault Tree.leftChildIndent int
* @uiDefault Tree.rightChildIndent int
* @uiDefault Tree.rowHeight int
* @uiDefault Tree.scrollsOnExpand boolean
* @uiDefault Tree.scrollsHorizontallyAndVertically boolean
* @uiDefault Tree.paintLines boolean
* @uiDefault Tree.lineTypeDashed boolean
* @uiDefault Tree.showsRootHandles boolean
* @uiDefault Tree.repaintWholeRow boolean
*
* <!-- DefaultTreeCellRenderer -->
*
* @uiDefault Tree.leafIcon Icon
* @uiDefault Tree.closedIcon Icon
* @uiDefault Tree.openIcon Icon
* @uiDefault Tree.textBackground Color
* @uiDefault Tree.textForeground Color
* @uiDefault Tree.selectionBackground Color
* @uiDefault Tree.selectionForeground Color
* @uiDefault Tree.selectionBorderColor Color focus indicator border color
* @uiDefault Tree.drawsFocusBorderAroundIcon boolean
* @uiDefault Tree.drawDashedFocusIndicator boolean
* @uiDefault Tree.rendererFillBackground boolean default is true
* @uiDefault Tree.rendererMargins Insets
* @uiDefault Tree.dropCellBackground Color
* @uiDefault Tree.dropCellForeground Color
*
* <!-- DefaultTreeCellEditor -->
*
* @uiDefault Tree.editorBorder Border
* @uiDefault Tree.editorBorderSelectionColor Color
*
* <!-- FlatTreeUI -->
*
* @uiDefault Tree.border Border
* @uiDefault Tree.selectionBackground Color
* @uiDefault Tree.selectionForeground Color
* @uiDefault Tree.selectionInactiveBackground Color
* @uiDefault Tree.selectionInactiveForeground Color
* @uiDefault Tree.selectionInsets Insets
* @uiDefault Tree.selectionArc int
* @uiDefault Tree.wideSelection boolean
* @uiDefault Tree.showCellFocusIndicator boolean
*
* <!-- FlatTreeExpandedIcon -->
*
* @uiDefault Component.arrowType String chevron (default) or triangle
* @uiDefault Tree.icon.expandedColor Color
*
* <!-- FlatTreeCollapsedIcon -->
*
* @uiDefault Component.arrowType String chevron (default) or triangle
* @uiDefault Tree.icon.collapsedColor Color
*
* <!-- FlatTreeLeafIcon -->
*
* @uiDefault Tree.icon.leafColor Color
*
* <!-- FlatTreeClosedIcon -->
*
* @uiDefault Tree.icon.closedColor Color
*
* <!-- FlatTreeOpenIcon -->
*
* @uiDefault Tree.icon.openColor Color
*
* @author Karl Tauber
*/
public class FlatTreeUI
extends BasicTreeUI
implements StyleableUI
{
@Styleable protected Color selectionBackground;
@Styleable protected Color selectionForeground;
@Styleable protected Color selectionInactiveBackground;
@Styleable protected Color selectionInactiveForeground;
@Styleable protected Color selectionBorderColor;
/** @since 3 */ @Styleable protected Insets selectionInsets;
/** @since 3 */ @Styleable protected int selectionArc;
@Styleable protected boolean wideSelection;
@Styleable protected boolean showCellFocusIndicator;
// for icons
// (needs to be public because icon classes are in another package)
/** @since 2 */ @Styleable(dot=true) public String iconArrowType;
/** @since 2 */ @Styleable(dot=true) public Color iconExpandedColor;
/** @since 2 */ @Styleable(dot=true) public Color iconCollapsedColor;
/** @since 2 */ @Styleable(dot=true) public Color iconLeafColor;
/** @since 2 */ @Styleable(dot=true) public Color iconClosedColor;
/** @since 2 */ @Styleable(dot=true) public Color iconOpenColor;
// only used via styling (not in UI defaults, but has likewise client properties)
/** @since 2 */ @Styleable protected boolean paintSelection = true;
private boolean paintLines;
private Color defaultCellNonSelectionBackground;
private Color defaultSelectionBackground;
private Color defaultSelectionForeground;
private Color defaultSelectionBorderColor;
private Map<String, Object> oldStyleValues;
public static ComponentUI createUI( JComponent c ) {
return new FlatTreeUI();
}
@Override
public void installUI( JComponent c ) {
super.installUI( c );
installStyle();
}
@Override
protected void installDefaults() {
super.installDefaults();
LookAndFeel.installBorder( tree, "Tree.border" );
selectionBackground = UIManager.getColor( "Tree.selectionBackground" );
selectionForeground = UIManager.getColor( "Tree.selectionForeground" );
selectionInactiveBackground = UIManager.getColor( "Tree.selectionInactiveBackground" );
selectionInactiveForeground = UIManager.getColor( "Tree.selectionInactiveForeground" );
selectionBorderColor = UIManager.getColor( "Tree.selectionBorderColor" );
selectionInsets = UIManager.getInsets( "Tree.selectionInsets" );
selectionArc = UIManager.getInt( "Tree.selectionArc" );
wideSelection = UIManager.getBoolean( "Tree.wideSelection" );
showCellFocusIndicator = UIManager.getBoolean( "Tree.showCellFocusIndicator" );
paintLines = UIManager.getBoolean( "Tree.paintLines" );
defaultCellNonSelectionBackground = UIManager.getColor( "Tree.textBackground" );
defaultSelectionBackground = selectionBackground;
defaultSelectionForeground = selectionForeground;
defaultSelectionBorderColor = selectionBorderColor;
// scale
int rowHeight = FlatUIUtils.getUIInt( "Tree.rowHeight", 16 );
if( rowHeight > 0 )
LookAndFeel.installProperty( tree, "rowHeight", UIScale.scale( rowHeight ) );
setLeftChildIndent( UIScale.scale( getLeftChildIndent() ) );
setRightChildIndent( UIScale.scale( getRightChildIndent() ) );
}
@Override
protected void uninstallDefaults() {
super.uninstallDefaults();
LookAndFeel.uninstallBorder( tree );
selectionBackground = null;
selectionForeground = null;
selectionInactiveBackground = null;
selectionInactiveForeground = null;
selectionBorderColor = null;
defaultCellNonSelectionBackground = null;
defaultSelectionBackground = null;
defaultSelectionForeground = null;
defaultSelectionBorderColor = null;
oldStyleValues = null;
}
@Override
protected MouseListener createMouseListener() {
return new BasicTreeUI.MouseHandler() {
@Override
public void mousePressed( MouseEvent e ) {
super.mousePressed( handleWideMouseEvent( e ) );
}
@Override
public void mouseReleased( MouseEvent e ) {
super.mouseReleased( handleWideMouseEvent( e ) );
}
@Override
public void mouseDragged( MouseEvent e ) {
super.mouseDragged( handleWideMouseEvent( e ) );
}
private MouseEvent handleWideMouseEvent( MouseEvent e ) {
if( !isWideSelection() || !tree.isEnabled() || !SwingUtilities.isLeftMouseButton( e ) || e.isConsumed() )
return e;
int x = e.getX();
int y = e.getY();
TreePath path = getClosestPathForLocation( tree, x, y );
if( path == null || isLocationInExpandControl( path, x, y ) )
return e;
Rectangle bounds = getPathBounds( tree, path );
if( bounds == null || y < bounds.y || y >= (bounds.y + bounds.height) )
return e;
int newX = Math.max( bounds.x, Math.min( x, bounds.x + bounds.width - 1 ) );
if( newX == x )
return e;
// clone mouse event, but with new X coordinate
return new MouseEvent( e.getComponent(), e.getID(), e.getWhen(),
e.getModifiers() | e.getModifiersEx(), newX, e.getY(),
e.getClickCount(), e.isPopupTrigger(), e.getButton() );
}
};
}
@Override
protected PropertyChangeListener createPropertyChangeListener() {
PropertyChangeListener superListener = super.createPropertyChangeListener();
return e -> {
superListener.propertyChange( e );
if( e.getSource() == tree ) {
switch( e.getPropertyName() ) {
case TREE_WIDE_SELECTION:
case TREE_PAINT_SELECTION:
tree.repaint();
break;
case "dropLocation":
if( isWideSelection() ) {
JTree.DropLocation oldValue = (JTree.DropLocation) e.getOldValue();
repaintWideDropLocation( oldValue );
repaintWideDropLocation( tree.getDropLocation() );
}
break;
case STYLE:
case STYLE_CLASS:
installStyle();
tree.revalidate();
tree.repaint();
break;
}
}
};
}
private void repaintWideDropLocation(JTree.DropLocation loc) {
if( loc == null || isDropLine( loc ) )
return;
Rectangle r = tree.getPathBounds( loc.getPath() );
if( r != null )
tree.repaint( 0, r.y, tree.getWidth(), r.height );
}
@Override
protected TreeSelectionListener createTreeSelectionListener() {
TreeSelectionListener superListener = super.createTreeSelectionListener();
return e -> {
superListener.valueChanged( e );
// for united rounded selection, repaint parts of the rows that adjoin to the changed rows
TreePath[] changedPaths;
if( useUnitedRoundedSelection() &&
tree.getSelectionCount() > 1 &&
(changedPaths = e.getPaths()) != null )
{
if( changedPaths.length > 4 ) {
// same is done in BasicTreeUI.Handler.valueChanged()
tree.repaint();
} else {
int arc = (int) Math.ceil( UIScale.scale( selectionArc / 2f ) );
for( TreePath path : changedPaths ) {
Rectangle r = getPathBounds( tree, path );
if( r != null )
tree.repaint( r.x, r.y - arc, r.width, r.height + (arc * 2) );
}
}
}
};
}
@Override
public Rectangle getPathBounds( JTree tree, TreePath path ) {
Rectangle bounds = super.getPathBounds( tree, path );
// If this method was invoked from JTree.getPathForLocation(int x, int y) to check whether
// the location is within tree node bounds, then return the bounds of a wide node.
// This changes the behavior of JTree.getPathForLocation(int x, int y) and
// JTree.getRowForLocation(int x, int y), which now return the path/row even
// if [x,y] is in the wide row area outside of the actual tree node.
if( bounds != null &&
isWideSelection() &&
UIManager.getBoolean( "FlatLaf.experimental.tree.widePathForLocation" ) &&
StackUtils.wasInvokedFrom( JTree.class.getName(), "getPathForLocation", 5 ) )
{
bounds.x = 0;
bounds.width = tree.getWidth();
}
return bounds;
}
/** @since 2 */
protected void installStyle() {
try {
applyStyle( FlatStylingSupport.getResolvedStyle( tree, "Tree" ) );
} catch( RuntimeException ex ) {
LoggingFacade.INSTANCE.logSevere( null, ex );
}
}
/** @since 2 */
protected void applyStyle( Object style ) {
oldStyleValues = FlatStylingSupport.parseAndApply( oldStyleValues, style, this::applyStyleProperty );
}
/** @since 2 */
protected Object applyStyleProperty( String key, Object value ) {
return FlatStylingSupport.applyToAnnotatedObjectOrComponent( this, tree, key, value );
}
/** @since 2 */
@Override
public Map<String, Class<?>> getStyleableInfos( JComponent c ) {
return FlatStylingSupport.getAnnotatedStyleableInfos( this );
}
/** @since 2.5 */
@Override
public Object getStyleableValue( JComponent c, String key ) {
return FlatStylingSupport.getAnnotatedStyleableValue( this, key );
}
@Override
public void paint( Graphics g, JComponent c ) {
if( treeState == null )
return;
// use clip bounds to limit painting to needed rows
Rectangle clipBounds = g.getClipBounds();
TreePath firstPath = getClosestPathForLocation( tree, 0, clipBounds.y );
Enumeration<TreePath> visiblePaths = treeState.getVisiblePathsFrom( firstPath );
if( visiblePaths != null ) {
Insets insets = tree.getInsets();
HashSet<TreePath> verticalLinePaths = paintLines ? new HashSet<>() : null;
ArrayList<Runnable> paintLinesLater = paintLines ? new ArrayList<>() : null;
ArrayList<Runnable> paintExpandControlsLater = paintLines ? new ArrayList<>() : null;
// add parents for later painting of vertical lines
if( paintLines ) {
for( TreePath path = firstPath.getParentPath(); path != null; path = path.getParentPath() )
verticalLinePaths.add( path );
}
Rectangle boundsBuffer = new Rectangle();
boolean rootVisible = isRootVisible();
int row = treeState.getRowForPath( firstPath );
boolean leftToRight = tree.getComponentOrientation().isLeftToRight();
int treeWidth = tree.getWidth();
// iterate over visible rows and paint rows, expand control and lines
while( visiblePaths.hasMoreElements() ) {
TreePath path = visiblePaths.nextElement();
if( path == null )
break;
// compute path bounds
Rectangle bounds = treeState.getBounds( path, boundsBuffer );
if( bounds == null )
break;
// add tree insets to path bounds
if( leftToRight )
bounds.x += insets.left;
else
bounds.x = treeWidth - insets.right - (bounds.x + bounds.width);
bounds.y += insets.top;
boolean isLeaf = treeModel.isLeaf( path.getLastPathComponent() );
boolean isExpanded = isLeaf ? false : treeState.getExpandedState( path );
boolean hasBeenExpanded = isLeaf ? false : tree.hasBeenExpanded( path );
// paint row (including selection)
paintRow( g, clipBounds, insets, bounds, path, row, isExpanded, hasBeenExpanded, isLeaf );
// collect lines for later painting
if( paintLines ) {
TreePath parentPath = path.getParentPath();
// add parent for later painting of vertical lines
if( parentPath != null )
verticalLinePaths.add( parentPath );
// paint horizontal line later (for using rendering hints)
if( parentPath != null || (rootVisible && row == 0) ) {
Rectangle bounds2 = new Rectangle( bounds );
int row2 = row;
paintLinesLater.add( () -> {
paintHorizontalPartOfLeg( g, clipBounds, insets, bounds2, path, row2, isExpanded, hasBeenExpanded, isLeaf );
} );
}
}
// paint expand control
if( shouldPaintExpandControl( path, row, isExpanded, hasBeenExpanded, isLeaf ) ) {
if( paintLines ) {
// need to paint after painting lines
Rectangle bounds2 = new Rectangle( bounds );
int row2 = row;
paintExpandControlsLater.add( () -> {
paintExpandControl( g, clipBounds, insets, bounds2, path, row2, isExpanded, hasBeenExpanded, isLeaf );
} );
} else
paintExpandControl( g, clipBounds, insets, bounds, path, row, isExpanded, hasBeenExpanded, isLeaf );
}
if( bounds.y + bounds.height >= clipBounds.y + clipBounds.height )
break;
row++;
}
if( paintLines ) {
// enable antialiasing for line painting
Object[] oldRenderingHints = FlatUIUtils.setRenderingHints( g );
// paint horizontal lines
for( Runnable r : paintLinesLater )
r.run();
// paint vertical lines
g.setColor( Color.green );
for( TreePath path : verticalLinePaths )
paintVerticalPartOfLeg( g, clipBounds, insets, path );
// restore rendering hints
if( oldRenderingHints != null )
FlatUIUtils.resetRenderingHints( g, oldRenderingHints );
// paint expand controls
for( Runnable r : paintExpandControlsLater )
r.run();
}
}
paintDropLine( g );
rendererPane.removeAll();
}
/**
* Similar to super.paintRow(), but supports wide selection and uses
* inactive selection background/foreground if tree is not focused.
*/
@Override
protected void paintRow( Graphics g, Rectangle clipBounds, Insets insets, Rectangle bounds,
TreePath path, int row, boolean isExpanded, boolean hasBeenExpanded, boolean isLeaf )
{
boolean isEditing = (editingComponent != null && editingRow == row);
boolean isSelected = tree.isRowSelected( row );
boolean isDropRow = isDropRow( row );
boolean needsSelectionPainting = (isSelected || isDropRow) && isPaintSelection();
// do not paint row if editing
if( isEditing ) {
// paint wide selection
// (do not access cell renderer here to avoid side effect
// if renderer component is also used as editor component)
if( isSelected && isWideSelection() ) {
Color oldColor = g.getColor();
g.setColor( selectionInactiveBackground );
paintWideSelection( g, clipBounds, insets, bounds, path, row, isExpanded, hasBeenExpanded, isLeaf );
g.setColor( oldColor );
}
return;
}
boolean hasFocus = FlatUIUtils.isPermanentFocusOwner( tree );
boolean cellHasFocus = hasFocus && (row == getLeadSelectionRow());
// if tree is used as cell renderer in another component (e.g. in Rhino JavaScript debugger),
// check whether that component is focused to get correct selection colors
if( !hasFocus && isSelected && tree.getParent() instanceof CellRendererPane )
hasFocus = FlatUIUtils.isPermanentFocusOwner( tree.getParent().getParent() );
// get renderer component
Component rendererComponent = currentCellRenderer.getTreeCellRendererComponent( tree,
path.getLastPathComponent(), isSelected, isExpanded, isLeaf, row, cellHasFocus );
// renderer background/foreground
Color oldBackgroundSelectionColor = null;
if( isSelected && !hasFocus && !isDropRow ) {
// apply inactive selection background/foreground if tree is not focused
oldBackgroundSelectionColor = setRendererBackgroundSelectionColor( rendererComponent, selectionInactiveBackground );
setRendererForeground( rendererComponent, selectionInactiveForeground );
} else if( isSelected ) {
// update background/foreground if set via style
if( selectionBackground != defaultSelectionBackground )
oldBackgroundSelectionColor = setRendererBackgroundSelectionColor( rendererComponent, selectionBackground );
if( selectionForeground != defaultSelectionForeground )
setRendererForeground( rendererComponent, selectionForeground );
}
// update focus selection border
Color oldBorderSelectionColor = null;
if( isSelected && hasFocus &&
(!showCellFocusIndicator || tree.getMinSelectionRow() == tree.getMaxSelectionRow()) )
{
// remove focus selection border if exactly one item is selected or if showCellFocusIndicator is false
oldBorderSelectionColor = setRendererBorderSelectionColor( rendererComponent, null );
} else if( hasFocus && selectionBorderColor != defaultSelectionBorderColor ) {
// update focus selection border if set via style
oldBorderSelectionColor = setRendererBorderSelectionColor( rendererComponent, selectionBorderColor );
}
// paint selection background
if( needsSelectionPainting ) {
// set selection color
Color oldColor = g.getColor();
g.setColor( isDropRow
? UIManager.getColor( "Tree.dropCellBackground" )
: (rendererComponent instanceof DefaultTreeCellRenderer
? ((DefaultTreeCellRenderer)rendererComponent).getBackgroundSelectionColor()
: (hasFocus ? selectionBackground : selectionInactiveBackground)) );
if( isWideSelection() ) {
// wide selection
paintWideSelection( g, clipBounds, insets, bounds, path, row, isExpanded, hasBeenExpanded, isLeaf );
} else {
// non-wide selection
paintCellBackground( g, rendererComponent, bounds, row, true );
}
// this is actually not necessary because renderer should always set color
// before painting, but doing anyway to avoid any side effect (in bad renderers)
g.setColor( oldColor );
} else {
// paint cell background if DefaultTreeCellRenderer.getBackgroundNonSelectionColor() is set
if( rendererComponent instanceof DefaultTreeCellRenderer ) {
DefaultTreeCellRenderer renderer = (DefaultTreeCellRenderer) rendererComponent;
Color bg = renderer.getBackgroundNonSelectionColor();
if( bg != null && !bg.equals( defaultCellNonSelectionBackground ) ) {
Color oldColor = g.getColor();
g.setColor( bg );
paintCellBackground( g, rendererComponent, bounds, row, false );
g.setColor( oldColor );
}
}
}
// paint renderer
rendererPane.paintComponent( g, rendererComponent, tree, bounds.x, bounds.y, bounds.width, bounds.height, true );
// restore background selection color and border selection color
if( oldBackgroundSelectionColor != null )
((DefaultTreeCellRenderer)rendererComponent).setBackgroundSelectionColor( oldBackgroundSelectionColor );
if( oldBorderSelectionColor != null )
((DefaultTreeCellRenderer)rendererComponent).setBorderSelectionColor( oldBorderSelectionColor );
}
private Color setRendererBackgroundSelectionColor( Component rendererComponent, Color color ) {
Color oldColor = null;
if( rendererComponent instanceof DefaultTreeCellRenderer ) {
DefaultTreeCellRenderer renderer = (DefaultTreeCellRenderer) rendererComponent;
if( renderer.getBackgroundSelectionColor() == defaultSelectionBackground ) {
oldColor = renderer.getBackgroundSelectionColor();
renderer.setBackgroundSelectionColor( color );
}
} else {
if( rendererComponent.getBackground() == defaultSelectionBackground )
rendererComponent.setBackground( color );
}
return oldColor;
}
private void setRendererForeground( Component rendererComponent, Color color ) {
if( rendererComponent.getForeground() == defaultSelectionForeground )
rendererComponent.setForeground( color );
}
private Color setRendererBorderSelectionColor( Component rendererComponent, Color color ) {
Color oldColor = null;
if( rendererComponent instanceof DefaultTreeCellRenderer ) {
DefaultTreeCellRenderer renderer = (DefaultTreeCellRenderer) rendererComponent;
if( renderer.getBorderSelectionColor() == defaultSelectionBorderColor ) {
oldColor = renderer.getBorderSelectionColor();
renderer.setBorderSelectionColor( color );
}
}
return oldColor;
}
private void paintWideSelection( Graphics g, Rectangle clipBounds, Insets insets, Rectangle bounds,
TreePath path, int row, boolean isExpanded, boolean hasBeenExpanded, boolean isLeaf )
{
float arcTop, arcBottom;
arcTop = arcBottom = UIScale.scale( selectionArc / 2f );
if( useUnitedRoundedSelection() ) {
if( row > 0 && tree.isRowSelected( row - 1 ) )
arcTop = 0;
if( row < tree.getRowCount() - 1 && tree.isRowSelected( row + 1 ) )
arcBottom = 0;
}
FlatUIUtils.paintSelection( (Graphics2D) g, 0, bounds.y, tree.getWidth(), bounds.height,
UIScale.scale( selectionInsets ), arcTop, arcTop, arcBottom, arcBottom, 0 );
}
private void paintCellBackground( Graphics g, Component rendererComponent, Rectangle bounds,
int row, boolean paintSelection )
{
int xOffset = 0;
int imageOffset = 0;
if( rendererComponent instanceof JLabel ) {
JLabel label = (JLabel) rendererComponent;
Icon icon = label.getIcon();
imageOffset = (icon != null && label.getText() != null)
? icon.getIconWidth() + Math.max( label.getIconTextGap() - 1, 0 )
: 0;
xOffset = label.getComponentOrientation().isLeftToRight() ? imageOffset : 0;
}
if( paintSelection ) {
float arcTopLeft, arcTopRight, arcBottomLeft, arcBottomRight;
arcTopLeft = arcTopRight = arcBottomLeft = arcBottomRight = UIScale.scale( selectionArc / 2f );
if( useUnitedRoundedSelection() ) {
if( row > 0 && tree.isRowSelected( row - 1 ) ) {
Rectangle r = getPathBounds( tree, tree.getPathForRow( row - 1 ) );
arcTopLeft = Math.min( arcTopLeft, r.x - bounds.x );
arcTopRight = Math.min( arcTopRight, (bounds.x + bounds.width) - (r.x + r.width) );
}
if( row < tree.getRowCount() - 1 && tree.isRowSelected( row + 1 ) ) {
Rectangle r = getPathBounds( tree, tree.getPathForRow( row + 1 ) );
arcBottomLeft = Math.min( arcBottomLeft, r.x - bounds.x );
arcBottomRight = Math.min( arcBottomRight, (bounds.x + bounds.width) - (r.x + r.width) );
}
}
FlatUIUtils.paintSelection( (Graphics2D) g, bounds.x + xOffset, bounds.y, bounds.width - imageOffset, bounds.height,
UIScale.scale( selectionInsets ), arcTopLeft, arcTopRight, arcBottomLeft, arcBottomRight, 0 );
} else
g.fillRect( bounds.x + xOffset, bounds.y, bounds.width - imageOffset, bounds.height );
}
private boolean useUnitedRoundedSelection() {
return selectionArc > 0 &&
(selectionInsets == null || (selectionInsets.top == 0 && selectionInsets.bottom == 0));
}
@Override
protected void paintVerticalLine( Graphics g, JComponent c, int x, int top, int bottom ) {
((Graphics2D)g).fill( new Rectangle2D.Float( x, top, UIScale.scale( 1f ), bottom - top ) );
}
@Override
protected void paintHorizontalLine( Graphics g, JComponent c, int y, int left, int right ) {
((Graphics2D)g).fill( new Rectangle2D.Float( left, y, right - left, UIScale.scale( 1f ) ) );
}
/**
* Checks whether dropping on a row.
* See DefaultTreeCellRenderer.getTreeCellRendererComponent().
*/
private boolean isDropRow( int row ) {
JTree.DropLocation dropLocation = tree.getDropLocation();
return dropLocation != null &&
dropLocation.getChildIndex() == -1 &&
tree.getRowForPath( dropLocation.getPath() ) == row;
}
@Override
protected Rectangle getDropLineRect( DropLocation loc ) {
Rectangle r = super.getDropLineRect( loc );
return isWideSelection() ? new Rectangle( 0, r.y, tree.getWidth(), r.height ) : r;
}
protected boolean isWideSelection() {
return clientPropertyBoolean( tree, TREE_WIDE_SELECTION, wideSelection );
}
protected boolean isPaintSelection() {
return clientPropertyBoolean( tree, TREE_PAINT_SELECTION, paintSelection );
}
}