001    /*
002     * Copyright (c) 2005 Peter Palotas, Fredrik Johansson, Einar Pehrson,
003     * Sebastian Kekkonen, Lars Magnus Lång, Malin Johansson and Sofia Nilsson
004     *
005     * This file is part of
006     * CleanSheets Extension for Assertions
007     *
008     * CleanSheets Extension for Assertions is free software; you can
009     * redistribute it and/or modify it under the terms of the GNU General Public
010     * License as published by the Free Software Foundation; either version 2 of
011     * the License, or (at your option) any later version.
012     *
013     * CleanSheets Extension for Assertions is distributed in the hope that
014     * it will be useful, but WITHOUT ANY WARRANTY; without even the implied
015     * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
016     * See the GNU General Public License for more details.
017     *
018     * You should have received a copy of the GNU General Public License
019     * along with CleanSheets Extension for Assertions; if not, write to the
020     * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
021     * Boston, MA  02111-1307  USA
022     */
023    package csheets.ext.assertion.ui;
024    
025    import java.awt.Color;
026    import java.awt.Font;
027    import java.awt.Graphics;
028    import java.awt.Graphics2D;
029    
030    import javax.swing.JComponent;
031    
032    import csheets.core.Cell;
033    import csheets.ext.assertion.AssertableCell;
034    import csheets.ext.assertion.AssertionExtension;
035    import csheets.ui.ext.CellDecorator;
036    
037    /**
038     * A decorator for assertable cells.
039     * @author Einar Pehrson
040     */
041    public class AssertableCellDecorator extends CellDecorator {
042    
043            /** The font used to render the 'A' */
044            private static final Font font = new Font("Dialog", Font.PLAIN, 10);
045    
046            /**
047             * Creates a new cell decorator.
048             */
049            public AssertableCellDecorator() {}
050    
051            /**
052             * Decorates the given graphics context if the cell being rendered
053             * has an assertion error.
054             * @param component the cell renderer component
055             * @param g the graphics context on which drawing should be done
056             * @param cell the cell being rendered
057             * @param selected whether the cell is selected
058             * @param hasFocus whether the cell has focus
059             */
060            public void decorate(JComponent component, Graphics g, Cell cell,
061                            boolean selected, boolean hasFocus) {
062                    if (enabled) {
063                            AssertableCell assertableCell = (AssertableCell)cell.getExtension(AssertionExtension.NAME);
064                            if (assertableCell.hasAssertionError()) {
065                                    // Stores current graphics context properties
066                                    Graphics2D g2 = (Graphics2D)g;
067                                    Color oldPaint = g2.getColor();
068                                    Font oldFont = g2.getFont();
069    
070                                    // Prints 'A' using own font, then restores the old font
071                                    g2.setColor(Color.red);
072                                    g2.setFont(font);
073                                    g2.drawString("A", 4, 12);
074    
075                                    // Restores graphics context properties
076                                    g2.setColor(oldPaint);
077                                    g2.setFont(oldFont);
078                            }
079                    }
080            }
081    }