001package csheets.ext.comments.ui;
002
003import java.awt.Color;
004import java.awt.Font;
005import java.awt.Graphics;
006import java.awt.Graphics2D;
007
008import javax.swing.JComponent;
009
010import csheets.core.Cell;
011import csheets.ext.comments.CommentableCell;
012import csheets.ext.comments.CommentsExtension;
013import csheets.ui.ext.CellDecorator;
014
015/**
016 * A decorator for commented cells.
017 * @author Alexandre Braganca
018 * @author Einar Pehrson
019 */
020public class CommentedCellDecorator extends CellDecorator {
021
022        /** The font used to render the '+' */
023        private static final Font font = new Font("Dialog", Font.PLAIN, 10);
024
025        /**
026         * Creates a new cell decorator.
027         */
028        public CommentedCellDecorator() {}
029
030        /**
031         * Decorates the given graphics context if the cell being rendered
032         * has a comment.
033         * @param component the cell renderer component
034         * @param g the graphics context on which drawing should be done
035         * @param cell the cell being rendered
036         * @param selected whether the cell is selected
037         * @param hasFocus whether the cell has focus
038         */
039        public void decorate(JComponent component, Graphics g, Cell cell,
040                        boolean selected, boolean hasFocus) {
041                if (enabled) {
042                        CommentableCell commentableCell = (CommentableCell)cell.getExtension(CommentsExtension.NAME);
043                        if (commentableCell.hasComment()) 
044                        {
045                                // Stores current graphics context properties
046                                Graphics2D g2 = (Graphics2D)g;
047                                Color oldPaint = g2.getColor();
048                                Font oldFont = g2.getFont();
049
050                                // Prints 'A' using own font, then restores the old font
051                                g2.setColor(Color.red);
052                                g2.setFont(font);
053                                g2.drawString("+", 4, 12);
054
055                                // Restores graphics context properties
056                                g2.setColor(oldPaint);
057                                g2.setFont(oldFont);
058                        }
059                }
060        }
061}