jdk/src/share/demo/jfc/SampleTree/SampleTreeCellRenderer.java
author mrkam
Fri, 25 Mar 2011 13:23:09 +0100
changeset 8966 ccf9551ddbd8
parent 5506 202f599c92aa
child 10292 ed7db6a12c2a
permissions -rw-r--r--
7027698: /jfc/SampleTree demo needs to be improved Reviewed-by: rupashka
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
8966
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
     2
 * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 * Redistribution and use in source and binary forms, with or without
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * modification, are permitted provided that the following conditions
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * are met:
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *   - Redistributions of source code must retain the above copyright
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 *     notice, this list of conditions and the following disclaimer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 *   - Redistributions in binary form must reproduce the above copyright
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 *     notice, this list of conditions and the following disclaimer in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 *     documentation and/or other materials provided with the distribution.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    15
 *   - Neither the name of Oracle nor the names of its
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *     contributors may be used to endorse or promote products derived
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 *     from this software without specific prior written permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.swing.Icon;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.swing.ImageIcon;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.swing.JLabel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import javax.swing.JTree;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import javax.swing.tree.TreeCellRenderer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import javax.swing.tree.DefaultMutableTreeNode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.awt.Component;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.awt.Color;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.awt.Font;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.awt.Graphics;
8966
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
    43
import javax.swing.UIManager;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
8966
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
    45
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
    46
@SuppressWarnings("serial")
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
    47
public class SampleTreeCellRenderer extends JLabel implements TreeCellRenderer {
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
    48
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    /** Font used if the string to be displayed isn't a font. */
8966
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
    50
    protected static Font defaultFont;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    /** Icon to use when the item is collapsed. */
8966
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
    52
    protected static ImageIcon collapsedIcon;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    /** Icon to use when the item is expanded. */
8966
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
    54
    protected static ImageIcon expandedIcon;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    /** Color to use for the background when selected. */
8966
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
    56
    protected static final Color SELECTED_BACKGROUND_COLOR;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
8966
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
    58
    static {
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
    59
        if ("Nimbus".equals(UIManager.getLookAndFeel().getName())) {
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
    60
            SELECTED_BACKGROUND_COLOR = new Color(0, 0,
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
    61
                0, 0);
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
    62
        } else {
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
    63
            SELECTED_BACKGROUND_COLOR = Color.YELLOW;
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
    64
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            defaultFont = new Font("SansSerif", 0, 12);
8966
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
    67
        } catch (Exception e) {
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
    68
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        try {
8966
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
    70
            collapsedIcon = new ImageIcon(SampleTreeCellRenderer.class.
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
    71
                    getResource("/resources/images/collapsed.gif"));
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
    72
            expandedIcon = new ImageIcon(SampleTreeCellRenderer.class.
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
    73
                    getResource("/resources/images/expanded.gif"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            System.out.println("Couldn't load images: " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    /** Whether or not the item that was last configured is selected. */
8966
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
    79
    protected boolean selected;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    /**
8966
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
    82
     * This is messaged from JTree whenever it needs to get the size
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
    83
     * of the component or it wants to draw it.
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
    84
     * This attempts to set the font based on value, which will be
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
    85
     * a TreeNode.
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
    86
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    public Component getTreeCellRendererComponent(JTree tree, Object value,
8966
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
    88
            boolean selected, boolean expanded,
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
    89
            boolean leaf, int row,
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
    90
            boolean hasFocus) {
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
    91
        String stringValue = tree.convertValueToText(value, selected,
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
    92
                expanded, leaf, row, hasFocus);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        /* Set the text. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        setText(stringValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        /* Tooltips used by the tree. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        setToolTipText(stringValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        /* Set the image. */
8966
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
   100
        if (expanded) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            setIcon(expandedIcon);
8966
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
   102
        } else if (!leaf) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            setIcon(collapsedIcon);
8966
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
   104
        } else {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            setIcon(null);
8966
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
   106
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        /* Set the color and the font based on the SampleData userObject. */
8966
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
   109
        SampleData userObject = (SampleData) ((DefaultMutableTreeNode) value).
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
   110
                getUserObject();
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
   111
        if (hasFocus) {
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
   112
            setForeground(UIManager.getColor("Tree.selectionForeground"));
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
   113
        } else {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            setForeground(userObject.getColor());
8966
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
   115
        }
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
   116
        if (userObject.getFont() == null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            setFont(defaultFont);
8966
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
   118
        } else {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            setFont(userObject.getFont());
8966
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
   120
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        /* Update the selected flag for the next paint. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        this.selected = selected;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    /**
8966
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
   129
     * paint is subclassed to draw the background correctly.  JLabel
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
   130
     * currently does not allow backgrounds other than white, and it
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
   131
     * will also fill behind the icon.  Something that isn't desirable.
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
   132
     */
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
   133
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    public void paint(Graphics g) {
8966
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
   135
        Color bColor;
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
   136
        Icon currentI = getIcon();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
8966
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
   138
        if (selected) {
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
   139
            bColor = SELECTED_BACKGROUND_COLOR;
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
   140
        } else if (getParent() != null) /* Pick background color up from parent (which will come from
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
   141
        the JTree we're contained in). */ {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            bColor = getParent().getBackground();
8966
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
   143
        } else {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            bColor = getBackground();
8966
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
   145
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        g.setColor(bColor);
8966
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
   147
        if (currentI != null && getText() != null) {
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
   148
            int offset = (currentI.getIconWidth() + getIconTextGap());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            if (getComponentOrientation().isLeftToRight()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                g.fillRect(offset, 0, getWidth() - 1 - offset,
8966
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
   152
                        getHeight() - 1);
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
   153
            } else {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                g.fillRect(0, 0, getWidth() - 1 - offset, getHeight() - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            }
8966
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
   156
        } else {
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
   157
            g.fillRect(0, 0, getWidth() - 1, getHeight() - 1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        super.paint(g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
}