package com.xoetrope.svgscanner.color;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.SystemColor;
import java.awt.event.MouseEvent;
import javax.swing.JToolTip;
import javax.swing.JTree;
import javax.swing.tree.DefaultTreeCellRenderer;
import net.xoetrope.swing.tree.XTreeModelAdapter;
public class ColorTreeCellRenderer extends DefaultTreeCellRenderer
{
private Color backgroundColor = Color.blue;
private String fontSize = "10";
private ColorMapping colorMapping;
private int colorMode;
public ColorTreeCellRenderer( int mode )
{
colorMode = mode;
setToolTipText( "Colors" );
closedIcon = null;
openIcon = null;
leafIcon = null;
}
public Component getTreeCellRendererComponent( JTree tree, Object value,
boolean sel, boolean expanded,
boolean leaf, int row,
boolean hasFocus )
{
if (( value != null ) && ( value instanceof XTreeModelAdapter )) {
Object obj = ((XTreeModelAdapter)value).getModel().get();
if ( obj instanceof ColorMapping )
colorMapping = (ColorMapping)obj;
else
colorMapping = null;
}
return super.getTreeCellRendererComponent( tree, value, sel, expanded, leaf, row, hasFocus );
}
/**
* Render the cell by drawing the text and then two colour swatches and the font size
* @param g
*/
public void paint( Graphics g )
{
super.paint( g );
if (( backgroundColor != null ) && ( colorMapping != null )) {
int width = getWidth();
int height = getHeight();
g.setColor( Color.white );
g.drawRect( width - 32 + 9 - 2 * height, 3, height - 6, height - 6 );
g.setColor( backgroundColor );
g.fillRect( width - 32 + 10 - 2 * height, 4, height - 7, height - 7 );
}
}
/**
* Overrides <code>DefaultTreeCellRenderer.getPreferredSize</code> to
* return slightly wider preferred size value.
*/
public Dimension getPreferredSize()
{
Dimension retDimension = super.getPreferredSize();
if ( retDimension != null )
retDimension = new Dimension( Math.max( retDimension.width + 2 * retDimension.height + 32, 100 ),
retDimension.height );
return retDimension;
}
public void setText( String str )
{
if ( str.length() == 0 ) {
super.setText( str );
backgroundColor = null;
}
else if ( colorMapping != null ) {
String text =( colorMode == HsbColorComparator.COMPARE_COLORIZED ) ? colorMapping.getColorizedColor() : colorMapping.getOriginalColor();
backgroundColor = ( colorMode == HsbColorComparator.COMPARE_COLORIZED ) ? colorMapping.getColorized() : colorMapping.getOriginal();
float[] bHSB = new float[ 3 ];
backgroundColor.RGBtoHSB( backgroundColor.getRed(), backgroundColor.getGreen(), backgroundColor.getBlue(), bHSB );
int h = (int)(255*bHSB[ 0 ]);
int s = (int)(255*bHSB[ 1 ]);
int b = (int)(255*bHSB[ 2 ]);
int a = backgroundColor.getAlpha();
String hsba = (( h < 16 ) ? "0" : "" ) + Integer.toString( h, 16 ) +
(( s < 16 ) ? "0" : "" ) + Integer.toString( s, 16 ) +
(( b < 16 ) ? "0" : "" ) + Integer.toString( b, 16 ) +
(( a < 16 ) ? "0" : "" ) + ( a != 255 ? Integer.toString( a, 16 ) : "" );
super.setText( text + " " + hsba.toUpperCase() );
}
else
super.setText( str );
}
/**
* Subclass the tooltip to allow the colours to be changed
*/
class ColorToolTip extends JToolTip
{
public ColorToolTip()
{
}
public void paintComponent( Graphics g )
{
g.setColor( getForeground());
super.paintComponent( g );
}
}
}
|