Path: blob/master/src/demo/share/jfc/SampleTree/SampleTreeCellRenderer.java
41149 views
/*1* Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.2*3* Redistribution and use in source and binary forms, with or without4* modification, are permitted provided that the following conditions5* are met:6*7* - Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9*10* - Redistributions in binary form must reproduce the above copyright11* notice, this list of conditions and the following disclaimer in the12* documentation and/or other materials provided with the distribution.13*14* - Neither the name of Oracle nor the names of its15* contributors may be used to endorse or promote products derived16* from this software without specific prior written permission.17*18* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS19* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,20* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR21* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR22* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,23* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,24* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR25* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF26* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING27* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS28* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.29*/3031/*32* This source code is provided to illustrate the usage of a given feature33* or technique and has been deliberately simplified. Additional steps34* required for a production-quality application, such as security checks,35* input validation and proper error handling, might not be present in36* this sample code.37*/38394041import javax.swing.Icon;42import javax.swing.ImageIcon;43import javax.swing.JLabel;44import javax.swing.JTree;45import javax.swing.tree.TreeCellRenderer;46import javax.swing.tree.DefaultMutableTreeNode;47import java.awt.Component;48import java.awt.Color;49import java.awt.Font;50import java.awt.Graphics;51import javax.swing.UIManager;525354@SuppressWarnings("serial")55public class SampleTreeCellRenderer extends JLabel implements TreeCellRenderer {5657/** Font used if the string to be displayed isn't a font. */58protected static Font defaultFont;59/** Icon to use when the item is collapsed. */60protected static ImageIcon collapsedIcon;61/** Icon to use when the item is expanded. */62protected static ImageIcon expandedIcon;63/** Color to use for the background when selected. */64protected static final Color SELECTED_BACKGROUND_COLOR;6566static {67if ("Nimbus".equals(UIManager.getLookAndFeel().getName())) {68SELECTED_BACKGROUND_COLOR = new Color(0, 0,690, 0);70} else {71SELECTED_BACKGROUND_COLOR = Color.YELLOW;72}73try {74defaultFont = new Font("SansSerif", 0, 12);75} catch (Exception e) {76}77try {78collapsedIcon = new ImageIcon(SampleTreeCellRenderer.class.79getResource("/resources/images/collapsed.gif"));80expandedIcon = new ImageIcon(SampleTreeCellRenderer.class.81getResource("/resources/images/expanded.gif"));82} catch (Exception e) {83System.out.println("Couldn't load images: " + e);84}85}86/** Whether or not the item that was last configured is selected. */87protected boolean selected;8889/**90* This is messaged from JTree whenever it needs to get the size91* of the component or it wants to draw it.92* This attempts to set the font based on value, which will be93* a TreeNode.94*/95public Component getTreeCellRendererComponent(JTree tree, Object value,96boolean selected, boolean expanded,97boolean leaf, int row,98boolean hasFocus) {99String stringValue = tree.convertValueToText(value, selected,100expanded, leaf, row, hasFocus);101102/* Set the text. */103setText(stringValue);104/* Tooltips used by the tree. */105setToolTipText(stringValue);106107/* Set the image. */108if (expanded) {109setIcon(expandedIcon);110} else if (!leaf) {111setIcon(collapsedIcon);112} else {113setIcon(null);114}115116/* Set the color and the font based on the SampleData userObject. */117SampleData userObject = (SampleData) ((DefaultMutableTreeNode) value).118getUserObject();119if (hasFocus) {120setForeground(UIManager.getColor("Tree.selectionForeground"));121} else {122setForeground(userObject.getColor());123}124if (userObject.getFont() == null) {125setFont(defaultFont);126} else {127setFont(userObject.getFont());128}129130/* Update the selected flag for the next paint. */131this.selected = selected;132133return this;134}135136/**137* paint is subclassed to draw the background correctly. JLabel138* currently does not allow backgrounds other than white, and it139* will also fill behind the icon. Something that isn't desirable.140*/141@Override142public void paint(Graphics g) {143Color bColor;144Icon currentI = getIcon();145146if (selected) {147bColor = SELECTED_BACKGROUND_COLOR;148} else if (getParent() != null) /* Pick background color up from parent (which will come from149the JTree we're contained in). */ {150bColor = getParent().getBackground();151} else {152bColor = getBackground();153}154g.setColor(bColor);155if (currentI != null && getText() != null) {156int offset = (currentI.getIconWidth() + getIconTextGap());157158if (getComponentOrientation().isLeftToRight()) {159g.fillRect(offset, 0, getWidth() - 1 - offset,160getHeight() - 1);161} else {162g.fillRect(0, 0, getWidth() - 1 - offset, getHeight() - 1);163}164} else {165g.fillRect(0, 0, getWidth() - 1, getHeight() - 1);166}167super.paint(g);168}169}170171172