001/*
002 * Copyright (c) 2005 Jens Schou, Staffan Gustafsson, Bjorn Lanneskog, 
003 * Einar Pehrson and Sebastian Kekkonen
004 *
005 * This file is part of
006 * CleanSheets Extension for Test Cases
007 *
008 * CleanSheets Extension for Test Cases 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 Test Cases 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 Test Cases; if not, write to the
020 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
021 * Boston, MA  02111-1307  USA
022 */
023package csheets.ext.test.ui;
024
025import java.awt.Color;
026import java.awt.Font;
027import java.awt.Graphics;
028import java.awt.Graphics2D;
029
030import javax.swing.JComponent;
031
032import csheets.core.Cell;
033import csheets.ext.test.TestExtension;
034import csheets.ext.test.TestableCell;
035import csheets.ui.ext.CellDecorator;
036
037/**
038 * A decorator for testable cells.
039 * @author Einar Pehrson
040 */
041public class TestableCellDecorator 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        /** The color used to indicate a warning */
047        private static final Color warningColor = new Color(0.7f, 0.7f, 0f);
048
049        /**
050         * Creates a new cell decorator.
051         */
052        public TestableCellDecorator() {}
053
054        /**
055         * Decorates the given graphics context if the cell being rendered
056         * has a test error.
057         * @param component the cell renderer component
058         * @param g the graphics context on which drawing should be done
059         * @param cell the cell being rendered
060         * @param selected whether the cell is selected
061         * @param hasFocus whether the cell has focus
062         */
063        public void decorate(JComponent component, Graphics g, Cell cell,
064                        boolean selected, boolean hasFocus) {
065                if (enabled) {
066                        // Checks for error
067                        TestableCell testableCell = (TestableCell)cell.getExtension(TestExtension.NAME);
068                        boolean hasError = testableCell.hasTestError();
069                        boolean hasWarning = testableCell.hasTestCases()
070                                && testableCell.getTestedness() < 1.0;
071                        if (hasError || hasWarning) {
072                                // Stores current graphics context properties
073                                Graphics2D g2 = (Graphics2D)g;
074                                Color oldPaint = g2.getColor();
075                                Font oldFont = g2.getFont();
076
077                                // Selects color
078                                if (hasError)
079                                        g2.setColor(Color.red);
080                                else if (hasWarning)
081                                        g2.setColor(warningColor);
082        
083                                // Prints 'T' using own font, then restores the old font
084                                g2.setFont(font);
085                                g2.drawString("T", 12, 12);
086        
087                                // Restores graphics context properties
088                                g2.setColor(oldPaint);
089                                g2.setFont(oldFont);
090                        }
091                }
092        }
093}