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.Component;
027
028import javax.swing.JTable;
029import javax.swing.table.DefaultTableCellRenderer;
030
031import csheets.core.Value;
032import csheets.ext.assertion.AssertableCell;
033import csheets.ext.assertion.Assertion;
034import csheets.ext.assertion.AssertionExtension;
035import csheets.ext.test.TestCase;
036import csheets.ext.test.TestableCell;
037
038/**
039 * A table cell renderer for test case results.
040 * @author Einar Pehrson
041 */
042@SuppressWarnings("serial")
043public class TestCaseResultRenderer extends DefaultTableCellRenderer {
044
045        /**
046         * Creates a new test case result renderer.
047         */
048        public TestCaseResultRenderer() {}
049
050        public Component getTableCellRendererComponent(JTable table, Object o,
051                boolean isSelected, boolean hasFocus, int row, int column) {
052
053                // Defines variables
054                Color color = Color.black;
055                String tooltip = null;
056
057                // Get result, and cell of test case at column 0 in this row
058                Value value = (Value)o;
059                TestableCell cell = ((TestCase)table.getValueAt(row, 0)).getCell();
060
061                // Assert value against cells assertion, if possible
062                AssertableCell assertableCell
063                        = (AssertableCell)cell.getExtension(AssertionExtension.NAME);
064                if (assertableCell != null) {
065                        Assertion.Result result = assertableCell.assertAny(value);
066                        if (result == Assertion.Result.NAN || result == Assertion.Result.FAILED) {
067                                color = Color.RED;
068                                tooltip = "Entered test case param violates assertion";
069                        }
070                }
071
072                // Render component
073                setForeground(color);
074                setToolTipText(tooltip);
075                return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
076        }
077}