/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package pdfdb.gui.customcomponents;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Paint;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.RenderingHints.Key;
import java.awt.Shape;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JTable;
/** The component that paints table cells.
* @author ug22cmg */
public class TableCell extends JLabel
{
// Constants
public static final int H = 30;
private static final Key AA_KEY = RenderingHints.KEY_ANTIALIASING;
private static final Object AA_VALUE = RenderingHints.VALUE_ANTIALIAS_ON;
private static final Color C1 = new Color(180, 180, 180, 80);
private static final Color C2 = new Color(180, 180, 180, 20);
private static final Color TRANSPARENT = new Color(255, 255, 255, 0);
private static final Paint ALTGRAD = new GradientPaint(0, 10, C1, 0, 30, C2);
private static final Paint SELGRAD = new GradientPaint(0, 15, new Color(145,
160, 192, 100), 0, 30, new Color(86, 100, 129, 99));
private static final int TYPE = Transparency.TRANSLUCENT;
private static final Dimension DIMENSIONS = new Dimension(-1, H);
// Fields
private boolean selected;
private Shape rect;
private Paint paint;
private BufferedImage bi;
private int columnIndex;
private JTable table;
/** Initializes the TableCell */
public TableCell()
{
super.setOpaque(false);
super.setSize(DIMENSIONS);
super.setPreferredSize(DIMENSIONS);
super.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
}
/** Sets the new row index.
* @param rowIndex New row index.*/
public void setRowIndex(int rowIndex)
{
if (rowIndex % 2 == 0)
{
this.paint = ALTGRAD;
}
else
{
this.paint = TRANSPARENT;
}
}
/** Sets the column index.
* @param columnIndex The column index. */
public void setColumnIndex(int columnIndex)
{
this.columnIndex = columnIndex;
}
/** Sets the associated table.
* @param table Sets the table. */
public void setTable(JTable table)
{
this.table = table;
}
/** Sets whether the cell is selected.
* @param selected The new selected state. */
public void setSelected(boolean selected)
{
this.selected = selected;
// this.setForeground(selected ? Color.WHITE : Color.BLACK);
}
/** Caches the rectangle and buffered image */
private void createGraphics()
{
GraphicsEnvironment env = GraphicsEnvironment.
getLocalGraphicsEnvironment();
GraphicsDevice gd = env.getDefaultScreenDevice();
GraphicsConfiguration gc = gd.getDefaultConfiguration();
bi = gc.createCompatibleImage(getWidth(), H, TYPE);
rect = new Rectangle(0, 0, getWidth(), H);
}
/** Performs the actual paint onto the buffered graphics object.
* @param g2d The buffered graphics object. */
private void paintOnImage(Graphics2D g2d)
{
g2d.setRenderingHint(AA_KEY, AA_VALUE);
if (selected)
{
g2d.setPaint(SELGRAD);
g2d.fill(rect);
g2d.setPaint(Color.GRAY);
if(columnIndex != table.getColumnCount() - 1)
g2d.drawLine(getWidth() - 1, 0, getWidth() - 1, getHeight() - 1);
g2d.drawLine(0, 0, getWidth() - 1, 0);
if(columnIndex == 0) g2d.drawLine(0, 0, 0, getHeight() - 1);
g2d.drawLine(0, getHeight() - 1, getWidth() - 1, getHeight() - 1);
}
else
{
g2d.setPaint(this.paint);
g2d.fill(rect);
g2d.setPaint(Color.LIGHT_GRAY);
g2d.drawLine(getWidth() - 1, 0, getWidth() - 1, getHeight() - 1);
g2d.drawLine(0, 0, getWidth() - 1, 0);
}
g2d.dispose();
}
/** Attempts to paint using cached resources, and if fails, paints
* normally.
* @param g The graphics object to use to paint. */
@Override
public void paintComponent(Graphics g)
{
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(AA_KEY, AA_VALUE);
createGraphics();
paintOnImage((Graphics2D) bi.getGraphics());
g2d.drawImage(bi, 0, 0, null);
super.paintComponent(g2d);
}
}