jdk/src/share/demo/jfc/SampleTree/SampleTreeCellRenderer.java
author nloodin
Wed, 31 Aug 2011 13:48:04 +0200
changeset 10292 ed7db6a12c2a
parent 8966 ccf9551ddbd8
permissions -rw-r--r--
7067811: Update demo/sample code to state it should not be used for production Summary: Added comment block after copyright block stating that code is unfit for production. Reviewed-by: ohair
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
10292
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 8966
diff changeset
    32
/*
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 8966
diff changeset
    33
 * This source code is provided to illustrate the usage of a given feature
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 8966
diff changeset
    34
 * or technique and has been deliberately simplified. Additional steps
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 8966
diff changeset
    35
 * required for a production-quality application, such as security checks,
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 8966
diff changeset
    36
 * input validation and proper error handling, might not be present in
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 8966
diff changeset
    37
 * this sample code.
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 8966
diff changeset
    38
 */
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 8966
diff changeset
    39
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 8966
diff changeset
    40
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import javax.swing.Icon;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import javax.swing.ImageIcon;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import javax.swing.JLabel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import javax.swing.JTree;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import javax.swing.tree.TreeCellRenderer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import javax.swing.tree.DefaultMutableTreeNode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import java.awt.Component;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import java.awt.Color;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import java.awt.Font;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
import java.awt.Graphics;
8966
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
    52
import javax.swing.UIManager;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
8966
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
    54
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
    55
@SuppressWarnings("serial")
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
    56
public class SampleTreeCellRenderer extends JLabel implements TreeCellRenderer {
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
    57
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    /** 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
    59
    protected static Font defaultFont;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    /** Icon to use when the item is collapsed. */
8966
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
    61
    protected static ImageIcon collapsedIcon;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    /** Icon to use when the item is expanded. */
8966
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
    63
    protected static ImageIcon expandedIcon;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    /** Color to use for the background when selected. */
8966
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
    65
    protected static final Color SELECTED_BACKGROUND_COLOR;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
8966
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
    67
    static {
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
    68
        if ("Nimbus".equals(UIManager.getLookAndFeel().getName())) {
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
    69
            SELECTED_BACKGROUND_COLOR = new Color(0, 0,
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
    70
                0, 0);
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
    71
        } else {
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
    72
            SELECTED_BACKGROUND_COLOR = Color.YELLOW;
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
    73
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            defaultFont = new Font("SansSerif", 0, 12);
8966
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
    76
        } catch (Exception e) {
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
    77
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        try {
8966
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
    79
            collapsedIcon = new ImageIcon(SampleTreeCellRenderer.class.
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
    80
                    getResource("/resources/images/collapsed.gif"));
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
    81
            expandedIcon = new ImageIcon(SampleTreeCellRenderer.class.
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
    82
                    getResource("/resources/images/expanded.gif"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            System.out.println("Couldn't load images: " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    /** 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
    88
    protected boolean selected;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    /**
8966
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
    91
     * 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
    92
     * of the component or it wants to draw it.
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
    93
     * 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
    94
     * a TreeNode.
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
    95
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    public Component getTreeCellRendererComponent(JTree tree, Object value,
8966
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
    97
            boolean selected, boolean expanded,
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
    98
            boolean leaf, int row,
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
    99
            boolean hasFocus) {
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
   100
        String stringValue = tree.convertValueToText(value, selected,
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
   101
                expanded, leaf, row, hasFocus);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        /* Set the text. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        setText(stringValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        /* Tooltips used by the tree. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        setToolTipText(stringValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        /* Set the image. */
8966
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
   109
        if (expanded) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            setIcon(expandedIcon);
8966
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
   111
        } else if (!leaf) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            setIcon(collapsedIcon);
8966
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
            setIcon(null);
8966
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
   115
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        /* 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
   118
        SampleData userObject = (SampleData) ((DefaultMutableTreeNode) value).
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
   119
                getUserObject();
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
   120
        if (hasFocus) {
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
   121
            setForeground(UIManager.getColor("Tree.selectionForeground"));
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
   122
        } else {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            setForeground(userObject.getColor());
8966
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
   124
        }
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
   125
        if (userObject.getFont() == null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            setFont(defaultFont);
8966
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
   127
        } else {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            setFont(userObject.getFont());
8966
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
   129
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        /* Update the selected flag for the next paint. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        this.selected = selected;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    /**
8966
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
   138
     * paint is subclassed to draw the background correctly.  JLabel
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
   139
     * currently does not allow backgrounds other than white, and it
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
   140
     * 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
   141
     */
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
   142
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    public void paint(Graphics g) {
8966
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
   144
        Color bColor;
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
   145
        Icon currentI = getIcon();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
8966
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
   147
        if (selected) {
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
   148
            bColor = SELECTED_BACKGROUND_COLOR;
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
   149
        } 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
   150
        the JTree we're contained in). */ {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            bColor = getParent().getBackground();
8966
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
   152
        } else {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            bColor = getBackground();
8966
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
   154
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        g.setColor(bColor);
8966
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
   156
        if (currentI != null && getText() != null) {
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
   157
            int offset = (currentI.getIconWidth() + getIconTextGap());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            if (getComponentOrientation().isLeftToRight()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                g.fillRect(offset, 0, getWidth() - 1 - offset,
8966
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
   161
                        getHeight() - 1);
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
   162
            } else {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                g.fillRect(0, 0, getWidth() - 1 - offset, getHeight() - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            }
8966
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
   165
        } else {
ccf9551ddbd8 7027698: /jfc/SampleTree demo needs to be improved
mrkam
parents: 5506
diff changeset
   166
            g.fillRect(0, 0, getWidth() - 1, getHeight() - 1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        super.paint(g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
}