📄 foldertreecellrenderer.java
字号:
/*** $Id: FolderTreeCellRenderer.java,v 1.3 2001/05/07 12:37:21 kunugi Exp $**** Copyright (c) 2000-2001 Jeff Gay** on behalf of ICEMail.org <http://www.icemail.org>** Copyright (c) 1998-2000 by Timothy Gerard Endres** ** This program is free software.** ** You may redistribute it and/or modify it under the terms of the GNU** General Public License as published by the Free Software Foundation.** Version 2 of the license should be included with this distribution in** the file LICENSE, as well as License.html. If the license is not** included with this distribution, you may find a copy at the FSF web** site at 'www.gnu.org' or 'www.fsf.org', or you may write to the** Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139 USA.**** THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND,** NOT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR** OF THIS SOFTWARE, ASSUMES _NO_ RESPONSIBILITY FOR ANY** CONSEQUENCE RESULTING FROM THE USE, MODIFICATION, OR** REDISTRIBUTION OF THIS SOFTWARE. */package org.icemail.mail;import java.awt.Color;import java.awt.Component;import java.awt.Font;import java.awt.Image;import javax.swing.Icon;import javax.swing.ImageIcon;import javax.swing.JLabel;import javax.swing.JTree;import javax.swing.tree.TreeCellRenderer;import javax.swing.tree.DefaultMutableTreeNode;import org.icemail.util.AWTUtilities;import org.icemail.util.UserProperties;/** * This class renders the nodes of the various trees in the application. * Nodes are rendered based on the type of underlying object and state of the node. */public class FolderTreeCellRenderer extends JLabel implements TreeCellRenderer{ static private ImageIcon rootExpIcon; static private ImageIcon rootColIcon; static private ImageIcon storeExpIcon; static private ImageIcon storeColIcon; static private ImageIcon folderExpIcon; static private ImageIcon folderColIcon; static private ImageIcon mailboxExpIcon; static private ImageIcon mailboxColIcon; static private Font selectedFont; static private Font unselectedFont; static private Color selectedColor; static private Color unSelectedColor; static { try { Image iconImg; iconImg = AWTUtilities.getImageResource( "/org/icemail/mail/images/rootexp.gif" ); rootExpIcon = new ImageIcon( iconImg ); iconImg = AWTUtilities.getImageResource( "/org/icemail/mail/images/rootcol.gif" ); rootColIcon = new ImageIcon( iconImg ); iconImg = AWTUtilities.getImageResource( "/org/icemail/mail/images/storexp.gif" ); storeExpIcon = new ImageIcon( iconImg ); iconImg = AWTUtilities.getImageResource( "/org/icemail/mail/images/storcol.gif" ); storeColIcon = new ImageIcon( iconImg ); iconImg = AWTUtilities.getImageResource( "/org/icemail/mail/images/foldexp.gif" ); folderExpIcon = new ImageIcon( iconImg ); iconImg = AWTUtilities.getImageResource( "/org/icemail/mail/images/foldcol.gif" ); folderColIcon = new ImageIcon( iconImg ); iconImg = AWTUtilities.getImageResource( "/org/icemail/mail/images/mboxexp.gif" ); mailboxExpIcon = new ImageIcon( iconImg ); iconImg = AWTUtilities.getImageResource( "/org/icemail/mail/images/mboxcol.gif" ); mailboxColIcon = new ImageIcon( iconImg ); } catch ( Exception ex ) { System.err.println( "FolderTreeCellRenderer.static: Couldn't load images: " + ex.getMessage() ); } selectedColor = UserProperties.getColor( "folderTree.colors.selected", new Color( 150, 0, 0 ) ); unSelectedColor = UserProperties.getColor( "folderTree.colors.unselected", Color.black ); selectedFont = new Font( "SansSerif", Font.BOLD, 12 ); unselectedFont = new Font( "SansSerif", Font.BOLD, 12 ); } /** * Default constructor. */ public FolderTreeCellRenderer() { super(); } /** * Sets the value of the current tree cell to the given value. * <p> * If selected is true, the cell will be drawn as if selected. * If expanded is true the node is currently expanded and * if leaf is true the node represets a leaf and * if hasFocus is true the node currently has focus. * * This is messaged from JTree whenever it needs to get the size * of the component or it wants to draw it. * This attempts to set the font based on value, which will be * a TreeNode. * <p> * Implementation of java.swing.tree.TreeCellRenderer.getTreeCellRendererComponent() * * @param tree the JTree the receiver is being configured for * @return component that the renderer uses to draw the value */ public Component getTreeCellRendererComponent( JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus ) { Font font; String stringValue = tree.convertValueToText( value, selected, expanded, leaf, row, hasFocus ); /* Set the image. */ if ( expanded ) { if ( value instanceof RootTreeNode ) { this.setIcon( rootExpIcon ); } else if ( value instanceof StoreTreeNode ) { this.setIcon( storeExpIcon ); } else if ( value instanceof FolderTreeNode ) { if ( leaf ) { this.setIcon( mailboxExpIcon ); } else { this.setIcon( folderExpIcon ); } } else { this.setIcon( folderColIcon ); } } else { if ( value instanceof RootTreeNode ) { this.setIcon( rootColIcon ); } else if ( value instanceof StoreTreeNode ) { this.setIcon( storeColIcon ); } else if ( value instanceof FolderTreeNode) { if ( leaf ) { this.setIcon( mailboxColIcon ); } else { this.setIcon( folderColIcon ); } } else { this.setIcon( folderColIcon ); } } if ( selected ) { setForeground( selectedColor ); } else { setForeground( unSelectedColor ); } /* Set the text. */ this.setText( stringValue ); return this; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -