package net.xoetrope.optional.laf.synth;
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Insets;
import java.util.HashMap;
import java.util.HashSet;
import javax.swing.CellRendererPane;
import javax.swing.JComboBox;
import javax.swing.JEditorPane;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JToolTip;
import javax.swing.UIManager;
import javax.swing.plaf.synth.SynthContext;
import javax.swing.plaf.synth.SynthPainter;
/**
* Synth painters for Labels and other Text components
* <p>Copyright: Copyright (c) Xoetrope Ltd., 2001-2005<br>
* License: see license.txt
* $Revision: 1.3 $
*/
public class TextPainter extends SynthPainter
{
private static HashSet opaqueTips = new HashSet();
private static HashMap painters = new HashMap();
public TextPainter()
{
}
/**
* Paints the border of a text field
*
* @param context SynthContext identifying the <code>JComponent</code> and
* <code>Region</code> to paint to
* @param g <code>Graphics</code> to paint to
* @param x X coordinate of the area to paint to
* @param y Y coordinate of the area to paint to
* @param w Width of the area to paint to
* @param h Height of the area to paint to
*/
public void paintTextFieldBorder( SynthContext context, Graphics g, int x, int y, int w, int h )
{
String imagePath = ( String ) UIManager.get( "TextField.border" );
Insets sourceInsets = new Insets( 1, 1, 1, 1 );
Insets destInsets = new Insets( 1, 1, 1, 1 );
String key = imagePath + "_" + w + "_" + h;
ImagePainter imagePainter = (ImagePainter)painters.get( key );
if ( imagePainter == null ) {
imagePainter = new ImagePainter( context, g, x, y, w, h, imagePath, sourceInsets, destInsets, true, false );
painters.put( key, imagePainter );
}
imagePainter.paint( g );
}
/**
* Paints the background of a text field.
*
* @param context SynthContext identifying the <code>JComponent</code> and
* <code>Region</code> to paint to
* @param g <code>Graphics</code> to paint to
* @param x X coordinate of the area to paint to
* @param y Y coordinate of the area to paint to
* @param w Width of the area to paint to
* @param h Height of the area to paint to
*/
public void paintTextFieldBackground(SynthContext context,
Graphics g, int x, int y,
int w, int h)
{
g.setColor( Color.white );
g.fillRoundRect( 1, 1, w-2, h-2, 3, 3 );
}
/**
* Paints the background of an label.
*
* @param context SynthContext identifying the <code>JComponent</code> and
* <code>Region</code> to paint to
* @param g <code>Graphics</code> to paint to
* @param x X coordinate of the area to paint to
* @param y Y coordinate of the area to paint to
* @param w Width of the area to paint to
* @param h Height of the area to paint to
*/
public void paintLabelBackground( SynthContext context, Graphics g, int x, int y, int w, int h )
{
JLabel label = ( JLabel ) context.getComponent();
Component parent = label.getParent();
String lableName = label.getName();
if (( lableName != null ) && lableName.startsWith( "ComboBox." ))
label.setOpaque( true );
if (( lableName != null ) && lableName.startsWith( "TableHeader." )) {
label.setFont( new Font( label.getName(), 1, label.getFont().getSize() ) );
Color color = ( Color ) UIManager.get( "TableHeader.gridColor" );
g.setColor( color );
g.drawLine( ( x + w ) - 1, y, ( x + w ) - 1, ( y + h ) - 1 );
return;
}
if ( ! ( parent instanceof CellRendererPane ) || ! ( parent.getParent()instanceof JComboBox ) ) {
super.paintLabelBackground( context, g, x, y, w, h );
return;
}
JComboBox cb = ( JComboBox ) parent.getParent();
Color textColor = null;
Color backColor = null;
if ( !cb.isEnabled() ) {
textColor = UIManager.getColor( "Label.TextColor_disabled" );
backColor = UIManager.getColor( "Label.BackgroundColor_disabled" );
}
else if ( !cb.isEditable() ) {
textColor = UIManager.getColor( "Label.lockedTextColor_" );
backColor = UIManager.getColor( "Label.lockedBackgroundColor" );
}
label.setForeground( textColor );
label.setBackground( backColor );
g.setColor( backColor );
g.fillRect( x, y, w, h );
}
/**
* Paints the background of an editor pane.
*
* @param context SynthContext identifying the <code>JComponent</code> and
* <code>Region</code> to paint to
* @param g <code>Graphics</code> to paint to
* @param x X coordinate of the area to paint to
* @param y Y coordinate of the area to paint to
* @param w Width of the area to paint to
* @param h Height of the area to paint to
*/
public void paintEditorPaneBackground( SynthContext context, Graphics g, int x, int y, int w, int h )
{
JEditorPane editorPane = ( JEditorPane ) context.getComponent();
if ( !editorPane.isEditable() ) {
g.setColor( UIManager.getColor( "EditorPane.lockedColor" ) );
g.fillRect( x, y, w, h );
}
//super.paintEditorPaneBackground( context, g, x, y, w, h );
}
/**
* Paints the background of a text area
*
* @param context SynthContext identifying the <code>JComponent</code> and
* <code>Region</code> to paint to
* @param g <code>Graphics</code> to paint to
* @param x X coordinate of the area to paint to
* @param y Y coordinate of the area to paint to
* @param w Width of the area to paint to
* @param h Height of the area to paint to
*/
public void paintTextAreaBackground( SynthContext context, Graphics g, int x, int y, int w, int h )
{
JTextArea textArea = ( JTextArea ) context.getComponent();
if ( !textArea.isEnabled() ) {
Color color = UIManager.getColor( "TextArea.disabledColor" );
textArea.setBackground( color );
}
else if ( !textArea.isEditable() ) {
Color color = UIManager.getColor( "TextArea.lockedColor" );
textArea.setBackground( color );
g.setColor( color );
g.fillRect( x, y, w, h );
}
}
/**
* Paints the background of a tooltip
*
* @param context SynthContext identifying the <code>JComponent</code> and
* <code>Region</code> to paint to
* @param g <code>Graphics</code> to paint to
* @param x X coordinate of the area to paint to
* @param y Y coordinate of the area to paint to
* @param w Width of the area to paint to
* @param h Height of the area to paint to
*/
public void paintToolTipBackground( SynthContext context, Graphics g, int x, int y, int w, int h )
{
JToolTip toolTip = ( JToolTip ) context.getComponent();
JPanel panel = ( JPanel ) toolTip.getParent();
if ( !opaqueTips.contains( toolTip ) ) {
toolTip.setOpaque( false );
panel.setOpaque( false );
panel.repaint();
toolTip.repaint();
opaqueTips.add( toolTip );
}
String imagePath = ( String ) UIManager.get( "ToolTip.background" );
Insets sInsets = new Insets( 7, 7, 7, 7 );
Insets dInsets = new Insets( 7, 7, 7, 7 );
String key = imagePath + "_" + w + "_" + h;
ImagePainter imagePainter = (ImagePainter)painters.get( key );
if ( imagePainter == null ) {
imagePainter = new ImagePainter( context, g, x, y, w, h, imagePath, sInsets, dInsets, true, true );
painters.put( key, imagePainter );
}
imagePainter.paint( g );
}
}