Package simplesheet.model.defaults

Source Code of simplesheet.model.defaults.DefaultCellRenderer

/*
* (swing1.1beta3)
*/
package simplesheet.model.defaults;

import simplesheet.model.CellRenderer;
import simplesheet.*;
import simplesheet.style.Style;
import simplesheet.model.cell.TableCell;
import simplesheet.style.EdgeType;
import simplesheet.style.TextVAlign;
import simplesheet.style.TextAlign;
import simplesheet.style.CellBorder;
import simplesheet.style.CellEdge;
import java.awt.*;
import javax.swing.*;
import simplesheet.style.Margin;

/**
* @version 1.0 11/22/98
*/
public class DefaultCellRenderer extends JComponent implements CellRenderer
        {

    protected static final Margin defaultMargin = new Margin(1, 1, 1, 1);
    protected static final CellBorder focusedBorder = new CellBorder(
        new CellEdge(EdgeType.solid, 2),
        new CellEdge(EdgeType.solid, 2),
        new CellEdge(EdgeType.solid, 2),
        new CellEdge(EdgeType.solid, 2)
    );


    private Color foreground = null;
    private Color background = null;
    private Font font = null;
    private Object cellValue = null;
    private TextAlign textAlign = TextAlign.undefined;
    private TextVAlign textVAlign = TextVAlign.undefined;
    private CellBorder border = null;
    private Margin margin = null;

    public DefaultCellRenderer() {
    }

    @Override
    public void paint(Graphics g) {
        Rectangle rc = getBounds();
        Color oldColor = g.getColor();
        Font oldFont = null;

        //draw background
        if(background != null) {
            g.setColor(background);
            g.fillRect(rc.x, rc.y, rc.width, rc.height);
        }

        //draw text
        String txt = cellValue != null ? cellValue.toString() : null;
        if(txt != null) {
            //font
            oldFont = g.getFont();
            g.setFont(font);
            //foreground
            g.setColor(foreground);
            //calc position
            FontMetrics fontMetrics = g.getFontMetrics();
            int fontHeight = fontMetrics.getHeight();
            int x = rc.x + scaleInset(margin.left, fontHeight);
            if(textAlign == TextAlign.center) {
                x += (rc.width - scaleInset(margin.left + margin.right, fontHeight)
                        - fontMetrics.stringWidth(txt)) / 2;
            } else if(textAlign == TextAlign.right) {
                x += rc.width - scaleInset(margin.left + margin.right, fontHeight)
                        - fontMetrics.stringWidth(txt);
            }
            int y = rc.y + scaleInset(margin.top, fontHeight) + fontMetrics.getMaxAscent();
            if(textVAlign == TextVAlign.middle) {
                y += (rc.height - scaleInset(margin.top + margin.bottom, fontHeight)
                        - fontMetrics.getHeight()) / 2;
            } else if(textVAlign == TextVAlign.bottom) {
                y += rc.height - scaleInset(margin.top + margin.bottom, fontHeight)
                        - fontMetrics.getHeight();
            }
            g.drawString(txt, x, y);
        }
        //border
        if(border != null) {
            g.setColor(Color.BLACK);
            Graphics2D g2 = (Graphics2D)g;
            Stroke oldStroke = g2.getStroke();
            if(border.top != null) {
                drawEdge(g2, border.top, rc.x, rc.y, rc.width-1, 0);
            }
            if(border.right != null) {
                drawEdge(g2, border.right, rc.x+rc.width-1, rc.y, 0, rc.height-1);
            }
            if(border.bottom != null) {
                drawEdge(g2, border.bottom, rc.x+rc.width-1, rc.y+rc.height-1, -rc.width+1, 0);
            }
            if(border.left != null) {
                drawEdge(g2, border.left, rc.x, rc.y+rc.height-1, 0, -rc.height+1);
            }
            g2.setStroke(oldStroke);
        }

        if(oldFont != null) {
            g.setFont(oldFont);
        }
        g.setColor(oldColor);

    }

    private int scaleInset(int value, int fontHeight) {
        return fontHeight*value/10;
    }

    private void drawEdge(Graphics2D g2, CellEdge edge, int x, int y, int cx, int cy) {
        Stroke stroke = getStroke(edge);
        if(stroke == null) {
            return;
        }
        g2.setStroke(stroke);
        g2.drawLine(x, y, x+cx, y+cy);
    }

    private Stroke getStroke(CellEdge edge) {
        if(edge.getType() == EdgeType.invisible
                || edge.getWidth() == 0) {
            return null;
        }
        return new BasicStroke(edge.getWidth(), BasicStroke.CAP_SQUARE,
                BasicStroke.JOIN_MITER, 10.0f);
    }


    @Override
    public Component getSheetCellRendererComponent(SheetTable table, TableCell value,
            boolean isSelected, boolean hasFocus) {

        Style style = value.getStyle();
        cellValue = value.getValue();

        foreground = (style != null && style.getForeground() != null)
                ? style.getForeground() : table.getForeground();

        background = (style != null)
                ? style.getBackground() : null;

        font = (style != null && style.getFont() != null)
                ? style.getFont() : table.getFont();

        textAlign = (style != null)
                ? style.getTextAlign() : TextAlign.undefined;

        textVAlign = (style != null)
                ? style.getTextVAlign(): TextVAlign.undefined;

        border = (style != null && style.getBorder() != null)
                ? style.getBorder() : null;

        margin = (style != null && style.getMargin() != null)
                ? style.getMargin() : defaultMargin;

        if(hasFocus) {
            border = focusedBorder;
        }
        if (isSelected && !hasFocus) {
            if(background != null) {
                background =  invert(background);
            } else {
                background =  invert(table.getBackground());
            }

            if(foreground != null) {
                foreground = invert(foreground);
            } else {
                foreground = invert(background);
            }

        } else {
            if(foreground == null) {
                foreground = table.getForeground();
            }
        }
       
        return this;
    }

    private static Color invert(Color color) {
        return new Color(255-color.getRed(), 255-color.getGreen(), 255-color.getBlue());
    }

    /**
     *
     * @param table
     * @param g
     * @param cell
     * @return
     */
    @Override
    public int getPreferredWidth(SheetTable table, Graphics g, TableCell cell) {
        if(cell == null || cell.getValue() == null) {
            return -1;
        }
        FontMetrics fontMetrics = getFontMetrics(table, g, cell);
        Style style = cell.getStyle();
        margin = (style != null  &&  style.getMargin() != null)
                ? style.getMargin() :  defaultMargin;

        return scaleInset(margin.left + margin.right, fontMetrics.getHeight())
               + fontMetrics.stringWidth(cell.getValue().toString());
    }


    /**
     *
     * @param table
     * @param g
     * @param cell
     * @param width
     * @return
     */
    @Override
    public int getPreferredHeight(SheetTable table, Graphics g, TableCell cell, int width) {
        if(cell == null || cell.getValue() == null) {
            return -1;
        }
        FontMetrics fontMetrics = getFontMetrics(table, g, cell);
        Style style = cell.getStyle();
        margin = (style != null  &&  style.getMargin() != null)
                ? style.getMargin() :  defaultMargin;
        return scaleInset(margin.top + margin.bottom, fontMetrics.getHeight())
               + fontMetrics.getHeight();
    }

    /**
     *
     * @param table
     * @param g
     * @param cell
     * @return
     */
    private static FontMetrics getFontMetrics(SheetTable table, Graphics g, TableCell cell) {
        Font oldFont = g.getFont();
        Style style = cell.getStyle();
        Font cellFont = (style != null  &&  style.getFont() != null)
                ? style.getFont() : table.getFont();
        g.setFont(cellFont);
        FontMetrics fontMetrics = g.getFontMetrics();
        g.setFont(oldFont);
        return fontMetrics;
    }
}

TOP

Related Classes of simplesheet.model.defaults.DefaultCellRenderer

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.