29 import java.awt.Component;
30 import java.awt.Dimension;
31 import java.awt.Font;
32 import java.awt.Graphics;
33 import java.awt.Insets;
34 import java.awt.Rectangle;
35 import javax.swing.plaf.ColorUIResource;
36 import javax.swing.plaf.FontUIResource;
37 import javax.swing.plaf.UIResource;
38 import javax.swing.plaf.basic.BasicGraphicsUtils;
39 import javax.swing.Icon;
40 import javax.swing.JLabel;
41 import javax.swing.JTree;
42 import javax.swing.LookAndFeel;
43 import javax.swing.UIManager;
44 import javax.swing.border.EmptyBorder;
45 import sun.swing.DefaultLookup;
46
47 /**
48 * Displays an entry in a tree.
49 * <code>DefaultTreeCellRenderer</code> is not opaque and
50 * unless you subclass paint you should not change this.
51 * See <a
52 href="http://docs.oracle.com/javase/tutorial/uiswing/components/tree.html">How to Use Trees</a>
53 * in <em>The Java Tutorial</em>
54 * for examples of customizing node display using this class.
55 * <p>
56 * The set of icons and colors used by {@code DefaultTreeCellRenderer}
57 * can be configured using the various setter methods. The value for
58 * each property is initialized from the defaults table. When the
59 * look and feel changes ({@code updateUI} is invoked), any properties
60 * that have a value of type {@code UIResource} are refreshed from the
61 * defaults table. The following table lists the mapping between
62 * {@code DefaultTreeCellRenderer} property and defaults table key:
63 * <table border="1" cellpadding="1" cellspacing="0" summary="">
64 * <tr valign="top" align="left">
65 * <th style="background-color:#CCCCFF" align="left">Property:
66 * <th style="background-color:#CCCCFF" align="left">Key:
67 * <tr><td>"leafIcon"<td>"Tree.leafIcon"
68 * <tr><td>"closedIcon"<td>"Tree.closedIcon"
69 * <tr><td>"openIcon"<td>"Tree.openIcon"
70 * <tr><td>"textSelectionColor"<td>"Tree.selectionForeground"
71 * <tr><td>"textNonSelectionColor"<td>"Tree.textForeground"
72 * <tr><td>"backgroundSelectionColor"<td>"Tree.selectionBackground"
73 * <tr><td>"backgroundNonSelectionColor"<td>"Tree.textBackground"
74 * <tr><td>"borderSelectionColor"<td>"Tree.selectionBorderColor"
75 * </table>
76 * <p>
77 * <strong><a name="override">Implementation Note:</a></strong>
78 * This class overrides
79 * <code>invalidate</code>,
80 * <code>validate</code>,
81 * <code>revalidate</code>,
82 * <code>repaint</code>,
83 * and
84 * <code>firePropertyChange</code>
85 * solely to improve performance.
86 * If not overridden, these frequently called methods would execute code paths
87 * that are unnecessary for the default tree cell renderer.
88 * If you write your own renderer,
89 * take care to weigh the benefits and
90 * drawbacks of overriding these methods.
91 *
92 * <p>
93 * <strong>Warning:</strong>
94 * Serialized objects of this class will not be compatible with
95 * future Swing releases. The current serialization support is
96 * appropriate for short term storage or RMI between applications running
97 * the same version of Swing. As of 1.4, support for long term storage
98 * of all JavaBeans™
99 * has been added to the <code>java.beans</code> package.
100 * Please see {@link java.beans.XMLEncoder}.
101 *
102 * @author Rob Davis
103 * @author Ray Ryan
104 * @author Scott Violet
105 */
106 @SuppressWarnings("serial") // Same-version serialization only
107 public class DefaultTreeCellRenderer extends JLabel implements TreeCellRenderer
108 {
109 /** Last tree the renderer was painted in. */
110 private JTree tree;
111
112 /** Is the value currently selected. */
113 protected boolean selected;
114 /** True if has focus. */
115 protected boolean hasFocus;
116 /** True if draws focus border around icon as well. */
117 private boolean drawsFocusBorderAroundIcon;
118 /** If true, a dashed line is drawn as the focus indicator. */
119 private boolean drawDashedFocusIndicator;
395
396 /**
397 * Sets the color to use for the border.
398 *
399 * @param newColor color to be used for the border
400 */
401 public void setBorderSelectionColor(Color newColor) {
402 borderSelectionColor = newColor;
403 }
404
405 /**
406 * Returns the color the border is drawn.
407 *
408 * @return the color the border is drawn
409 */
410 public Color getBorderSelectionColor() {
411 return borderSelectionColor;
412 }
413
414 /**
415 * Subclassed to map <code>FontUIResource</code>s to null. If
416 * <code>font</code> is null, or a <code>FontUIResource</code>, this
417 * has the effect of letting the font of the JTree show
418 * through. On the other hand, if <code>font</code> is non-null, and not
419 * a <code>FontUIResource</code>, the font becomes <code>font</code>.
420 */
421 public void setFont(Font font) {
422 if(font instanceof FontUIResource)
423 font = null;
424 super.setFont(font);
425 }
426
427 /**
428 * Gets the font of this component.
429 * @return this component's font; if a font has not been set
430 * for this component, the font of its parent is returned
431 */
432 public Font getFont() {
433 Font font = super.getFont();
434
435 if (font == null && tree != null) {
436 // Strive to return a non-null value, otherwise the html support
437 // will typically pick up the wrong font in certain situations.
438 font = tree.getFont();
439 }
440 return font;
441 }
442
443 /**
444 * Subclassed to map <code>ColorUIResource</code>s to null. If
445 * <code>color</code> is null, or a <code>ColorUIResource</code>, this
446 * has the effect of letting the background color of the JTree show
447 * through. On the other hand, if <code>color</code> is non-null, and not
448 * a <code>ColorUIResource</code>, the background becomes
449 * <code>color</code>.
450 */
451 public void setBackground(Color color) {
452 if(color instanceof ColorUIResource)
453 color = null;
454 super.setBackground(color);
455 }
456
457 /**
458 * Configures the renderer based on the passed in components.
459 * The value is set from messaging the tree with
460 * <code>convertValueToText</code>, which ultimately invokes
461 * <code>toString</code> on <code>value</code>.
462 * The foreground color is set based on the selection and the icon
463 * is set based on the <code>leaf</code> and <code>expanded</code>
464 * parameters.
465 */
466 public Component getTreeCellRendererComponent(JTree tree, Object value,
467 boolean sel,
468 boolean expanded,
469 boolean leaf, int row,
470 boolean hasFocus) {
471 String stringValue = tree.convertValueToText(value, sel,
472 expanded, leaf, row, hasFocus);
473
474 this.tree = tree;
475 this.hasFocus = hasFocus;
476 setText(stringValue);
477
478 Color fg = null;
479 isDropCell = false;
480
481 JTree.DropLocation dropLocation = tree.getDropLocation();
482 if (dropLocation != null
483 && dropLocation.getChildIndex() == -1
584 }
585 if (drawDashedFocusIndicator && notColor != null) {
586 if (treeBGColor != notColor) {
587 treeBGColor = notColor;
588 focusBGColor = new Color(~notColor.getRGB());
589 }
590 g.setColor(focusBGColor);
591 BasicGraphicsUtils.drawDashedRect(g, x, y, w, h);
592 }
593 }
594
595 private int getLabelStart() {
596 Icon currentI = getIcon();
597 if(currentI != null && getText() != null) {
598 return currentI.getIconWidth() + Math.max(0, getIconTextGap() - 1);
599 }
600 return 0;
601 }
602
603 /**
604 * Overrides <code>JComponent.getPreferredSize</code> to
605 * return slightly wider preferred size value.
606 */
607 public Dimension getPreferredSize() {
608 Dimension retDimension = super.getPreferredSize();
609
610 if(retDimension != null)
611 retDimension = new Dimension(retDimension.width + 3,
612 retDimension.height);
613 return retDimension;
614 }
615
616 /**
617 * Overridden for performance reasons.
618 * See the <a href="#override">Implementation Note</a>
619 * for more information.
620 */
621 public void validate() {}
622
623 /**
624 * Overridden for performance reasons.
|
29 import java.awt.Component;
30 import java.awt.Dimension;
31 import java.awt.Font;
32 import java.awt.Graphics;
33 import java.awt.Insets;
34 import java.awt.Rectangle;
35 import javax.swing.plaf.ColorUIResource;
36 import javax.swing.plaf.FontUIResource;
37 import javax.swing.plaf.UIResource;
38 import javax.swing.plaf.basic.BasicGraphicsUtils;
39 import javax.swing.Icon;
40 import javax.swing.JLabel;
41 import javax.swing.JTree;
42 import javax.swing.LookAndFeel;
43 import javax.swing.UIManager;
44 import javax.swing.border.EmptyBorder;
45 import sun.swing.DefaultLookup;
46
47 /**
48 * Displays an entry in a tree.
49 * {@code DefaultTreeCellRenderer} is not opaque and
50 * unless you subclass paint you should not change this.
51 * See <a
52 href="http://docs.oracle.com/javase/tutorial/uiswing/components/tree.html">How to Use Trees</a>
53 * in <em>The Java Tutorial</em>
54 * for examples of customizing node display using this class.
55 * <p>
56 * The set of icons and colors used by {@code DefaultTreeCellRenderer}
57 * can be configured using the various setter methods. The value for
58 * each property is initialized from the defaults table. When the
59 * look and feel changes ({@code updateUI} is invoked), any properties
60 * that have a value of type {@code UIResource} are refreshed from the
61 * defaults table. The following table lists the mapping between
62 * {@code DefaultTreeCellRenderer} property and defaults table key:
63 * <table border="1" cellpadding="1" cellspacing="0" summary="">
64 * <tr valign="top" align="left">
65 * <th style="background-color:#CCCCFF" align="left">Property:
66 * <th style="background-color:#CCCCFF" align="left">Key:
67 * <tr><td>"leafIcon"<td>"Tree.leafIcon"
68 * <tr><td>"closedIcon"<td>"Tree.closedIcon"
69 * <tr><td>"openIcon"<td>"Tree.openIcon"
70 * <tr><td>"textSelectionColor"<td>"Tree.selectionForeground"
71 * <tr><td>"textNonSelectionColor"<td>"Tree.textForeground"
72 * <tr><td>"backgroundSelectionColor"<td>"Tree.selectionBackground"
73 * <tr><td>"backgroundNonSelectionColor"<td>"Tree.textBackground"
74 * <tr><td>"borderSelectionColor"<td>"Tree.selectionBorderColor"
75 * </table>
76 * <p>
77 * <strong><a name="override">Implementation Note:</a></strong>
78 * This class overrides
79 * {@code invalidate},
80 * {@code validate},
81 * {@code revalidate},
82 * {@code repaint},
83 * and
84 * {@code firePropertyChange}
85 * solely to improve performance.
86 * If not overridden, these frequently called methods would execute code paths
87 * that are unnecessary for the default tree cell renderer.
88 * If you write your own renderer,
89 * take care to weigh the benefits and
90 * drawbacks of overriding these methods.
91 *
92 * <p>
93 * <strong>Warning:</strong>
94 * Serialized objects of this class will not be compatible with
95 * future Swing releases. The current serialization support is
96 * appropriate for short term storage or RMI between applications running
97 * the same version of Swing. As of 1.4, support for long term storage
98 * of all JavaBeans™
99 * has been added to the {@code java.beans} package.
100 * Please see {@link java.beans.XMLEncoder}.
101 *
102 * @author Rob Davis
103 * @author Ray Ryan
104 * @author Scott Violet
105 */
106 @SuppressWarnings("serial") // Same-version serialization only
107 public class DefaultTreeCellRenderer extends JLabel implements TreeCellRenderer
108 {
109 /** Last tree the renderer was painted in. */
110 private JTree tree;
111
112 /** Is the value currently selected. */
113 protected boolean selected;
114 /** True if has focus. */
115 protected boolean hasFocus;
116 /** True if draws focus border around icon as well. */
117 private boolean drawsFocusBorderAroundIcon;
118 /** If true, a dashed line is drawn as the focus indicator. */
119 private boolean drawDashedFocusIndicator;
395
396 /**
397 * Sets the color to use for the border.
398 *
399 * @param newColor color to be used for the border
400 */
401 public void setBorderSelectionColor(Color newColor) {
402 borderSelectionColor = newColor;
403 }
404
405 /**
406 * Returns the color the border is drawn.
407 *
408 * @return the color the border is drawn
409 */
410 public Color getBorderSelectionColor() {
411 return borderSelectionColor;
412 }
413
414 /**
415 * Subclassed to map {@code FontUIResource}s to null. If
416 * {@code font} is null, or a {@code FontUIResource}, this
417 * has the effect of letting the font of the JTree show
418 * through. On the other hand, if {@code font} is non-null, and not
419 * a {@code FontUIResource}, the font becomes {@code font}.
420 */
421 public void setFont(Font font) {
422 if(font instanceof FontUIResource)
423 font = null;
424 super.setFont(font);
425 }
426
427 /**
428 * Gets the font of this component.
429 * @return this component's font; if a font has not been set
430 * for this component, the font of its parent is returned
431 */
432 public Font getFont() {
433 Font font = super.getFont();
434
435 if (font == null && tree != null) {
436 // Strive to return a non-null value, otherwise the html support
437 // will typically pick up the wrong font in certain situations.
438 font = tree.getFont();
439 }
440 return font;
441 }
442
443 /**
444 * Subclassed to map {@code ColorUIResource}s to null. If
445 * {@code color} is null, or a {@code ColorUIResource}, this
446 * has the effect of letting the background color of the JTree show
447 * through. On the other hand, if {@code color} is non-null, and not
448 * a {@code ColorUIResource}, the background becomes
449 * {@code color}.
450 */
451 public void setBackground(Color color) {
452 if(color instanceof ColorUIResource)
453 color = null;
454 super.setBackground(color);
455 }
456
457 /**
458 * Configures the renderer based on the passed in components.
459 * The value is set from messaging the tree with
460 * {@code convertValueToText}, which ultimately invokes
461 * {@code toString} on {@code value}.
462 * The foreground color is set based on the selection and the icon
463 * is set based on the {@code leaf} and {@code expanded}
464 * parameters.
465 */
466 public Component getTreeCellRendererComponent(JTree tree, Object value,
467 boolean sel,
468 boolean expanded,
469 boolean leaf, int row,
470 boolean hasFocus) {
471 String stringValue = tree.convertValueToText(value, sel,
472 expanded, leaf, row, hasFocus);
473
474 this.tree = tree;
475 this.hasFocus = hasFocus;
476 setText(stringValue);
477
478 Color fg = null;
479 isDropCell = false;
480
481 JTree.DropLocation dropLocation = tree.getDropLocation();
482 if (dropLocation != null
483 && dropLocation.getChildIndex() == -1
584 }
585 if (drawDashedFocusIndicator && notColor != null) {
586 if (treeBGColor != notColor) {
587 treeBGColor = notColor;
588 focusBGColor = new Color(~notColor.getRGB());
589 }
590 g.setColor(focusBGColor);
591 BasicGraphicsUtils.drawDashedRect(g, x, y, w, h);
592 }
593 }
594
595 private int getLabelStart() {
596 Icon currentI = getIcon();
597 if(currentI != null && getText() != null) {
598 return currentI.getIconWidth() + Math.max(0, getIconTextGap() - 1);
599 }
600 return 0;
601 }
602
603 /**
604 * Overrides {@code JComponent.getPreferredSize} to
605 * return slightly wider preferred size value.
606 */
607 public Dimension getPreferredSize() {
608 Dimension retDimension = super.getPreferredSize();
609
610 if(retDimension != null)
611 retDimension = new Dimension(retDimension.width + 3,
612 retDimension.height);
613 return retDimension;
614 }
615
616 /**
617 * Overridden for performance reasons.
618 * See the <a href="#override">Implementation Note</a>
619 * for more information.
620 */
621 public void validate() {}
622
623 /**
624 * Overridden for performance reasons.
|