📄 jmtreerenderer.java
字号:
package jm.framework.gui.module.render ;
import java.awt.Color;
import java.awt.Component;
import java.io.Serializable;
import javax.swing.JLabel;
import javax.swing.JTree;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
import javax.swing.tree.TreeCellRenderer;
import jm.framework.gui.module.JMItemCode;
import jm.framework.gui.module.jmtree.JMTree;
import jm.framework.gui.module.jmtree.model.JMTreeNode;
import jm.util.JMVal;
public class JMTreeRenderer
extends JLabel implements TreeCellRenderer,Serializable
{
/**
*
*/
private static final long serialVersionUID = 7793851920839126255L;
public static final String CLASS_NAME = "JLabel" ;
Color[] backcolor = null ;
protected static Border noFocusBorder = new EmptyBorder ( 1 , 1 , 1 , 1 ) ;
// We need a place to store the color the JLabel should be returned
// to after its foreground and background colors have been set
// to the selection background color.
// These ivars will be made protected when their names are finalized.
private Color unselectedForeground ;
private Color unselectedBackground ;
private boolean dsel = false ;
/**
* Creates a default table cell renderer.
*/
public JMTreeRenderer ( Color[] color )
{
super () ;
backcolor = color ;
setOpaque ( true ) ;
setBorder ( noFocusBorder ) ;
}
/**
* Overrides <code>JComponent.setForeground</code> to assign
* the unselected-foreground color to the specified color.
*
* @param c set the foreground color to this value
*/
public void setForeground ( Color c )
{
super.setForeground ( c ) ;
unselectedForeground = c ;
}
/**
* Overrides <code>JComponent.setBackground</code> to assign
* the unselected-background color to the specified color.
*
* @param c set the background color to this value
*/
public void setBackground ( Color c )
{
super.setBackground ( c ) ;
unselectedBackground = c ;
}
/**
* Notification from the <code>UIManager</code> that the look and feel
* [L&F] has changed.
* Replaces the current UI object with the latest version from the
* <code>UIManager</code>.
*
* @see JComponent#updateUI
*/
public void updateUI ()
{
super.updateUI () ;
setForeground ( null ) ;
setBackground ( null ) ;
}
public Component getTreeCellRendererComponent (
JTree tree ,
Object value ,
boolean selected ,
boolean expanded ,
boolean leaf ,
int row ,
boolean hasFocus )
{
JMTreeNode tvalue = (JMTreeNode) value;
// boolean s = ((JMTree)tree).getDoubleSelect();
// if(s)
// {
// dsel = true;
// }
// if(dsel)
// {
// dsel = false;
// }
if ( backcolor != null && backcolor.length > 0 )
{
setBackground ( backcolor[ row % backcolor.length ] ) ;
}
if ( selected&&!dsel)
{
super.setForeground ( ((JMTree)tree).getSelectionForeground() ) ;
super.setBackground ( ((JMTree)tree).getSelectionBackground() ) ;
setFont ( JMItemCode.FT_TREE_SELECT ) ;
dsel = true;
}
else
{
setFont ( JMItemCode.FT_TREE ) ;
dsel = false;
super.setForeground ( ( unselectedForeground != null ) ? unselectedForeground
: tree.getForeground () ) ;
super.setBackground ( ( unselectedBackground != null ) ? unselectedBackground
: tree.getBackground () ) ;
}
setValue ( new JMVal( tvalue.toString()) ) ;
return this ;
}
/*
* The following methods are overridden as a performance measure to
* to prune code-paths are often called in the case of renders
* but which we know are unnecessary. Great care should be taken
* when writing your own renderer to weigh the benefits and
* drawbacks of overriding methods like these.
*/
/**
* Overridden for performance reasons.
* See the <a href="#override">Implementation Note</a>
* for more information.
*/
public boolean isOpaque ()
{
Color back = getBackground () ;
Component p = getParent () ;
if ( p != null )
{
p = p.getParent () ;
}
// p should now be the JTable.
boolean colorMatch = ( back != null ) && ( p != null ) &&
back.equals ( p.getBackground () ) &&
p.isOpaque () ;
return!colorMatch && super.isOpaque () ;
}
/**
* Sets the <code>String</code> object for the cell being rendered to
* <code>value</code>.
*
* @param value the string value for this cell; if value is
* <code>null</code> it sets the text value to an empty string
* @see JLabel#setText
*
*/
protected void setValue ( Object value )
{
setText ( ( value == null ) ? "" : ((JMVal)value).getStringValue() ) ;
}
// /**
// * A subclass of <code>DefaultTableCellRenderer</code> that
// * implements <code>UIResource</code>.
// * <code>DefaultTableCellRenderer</code> doesn't implement
// * <code>UIResource</code>
// * directly so that applications can safely override the
// * <code>cellRenderer</code> property with
// * <code>DefaultTableCellRenderer</code> subclasses.
// * <p>
// * <strong>Warning:</strong>
// * Serialized objects of this class will not be compatible with
// * future Swing releases. The current serialization support is
// * appropriate for short term storage or RMI between applications running
// * the same version of Swing. As of 1.4, support for long term storage
// * of all JavaBeans<sup><font size="-2">TM</font></sup>
// * has been added to the <code>java.beans</code> package.
// * Please see {@link java.beans.XMLEncoder}.
// */
// public static class UIResource extends DefaultTableCellRenderer
// implements javax.swing.plaf.UIResource
// {
// }
//
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -