001    /*
002     * Copyright (c) 2005 Jens Schou, Staffan Gustafsson, Björn 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     */
023    package csheets.ext.test.ui;
024    
025    import java.awt.Color;
026    import java.awt.Component;
027    
028    import javax.swing.JTable;
029    import javax.swing.table.DefaultTableCellRenderer;
030    
031    import csheets.core.Value;
032    import csheets.ext.assertion.AssertableCell;
033    import csheets.ext.assertion.Assertion;
034    import csheets.ext.assertion.AssertionExtension;
035    import csheets.ext.test.TestCaseParam;
036    import csheets.ext.test.TestableCell;
037    
038    /**
039     * A table cell renderer for test case parameters.
040     * @author Malin Johansson
041     * @author Sofia Nilsson
042     * @author Einar Pehrson
043     * @author Björn Lanneskog
044     */
045    @SuppressWarnings("serial")
046    public class TestCaseParamRenderer extends DefaultTableCellRenderer {
047    
048            /**
049             * Creates a new test case parameter renderer.
050             */
051            public TestCaseParamRenderer() {}
052    
053            public Component getTableCellRendererComponent(JTable table, Object value,
054                            boolean isSelected, boolean hasFocus, int row, int column) {
055                    
056                    if (value instanceof TestCaseParam) {
057                            // Get param, and cell at column 0 in this row
058                            TestCaseParam param = (TestCaseParam)value;
059                            TestableCell cell = (TestableCell)table.getValueAt(row, 0);
060            
061                            if (param != null && param.getValue().getType() == Value.Type.NUMERIC) {
062                                    AssertableCell assertableCell
063                                            = (AssertableCell)cell.getExtension(AssertionExtension.NAME);
064                                    if (assertableCell != null) {
065                                            // Assert value against cells assertion
066                                            Assertion.Result result = assertableCell.assertAny(param.getValue());
067                                            // check if user entered
068                                            if(param.isUserEntered()) {
069                                                    // set red if testcaseparam breaks against assertion
070                                                    if (result == Assertion.Result.NAN || result == Assertion.Result.FAILED) {
071                                                            setForeground(Color.RED);
072                                                            setToolTipText("Entered test case param violates assertion");
073                                                    }
074                                                    // set black if testcaseparam is within assertion
075                                                    else {
076                                                            setForeground(Color.BLACK);
077                                                            setToolTipText(null);
078                                                    }
079                                            }
080                                            // check if derived or system generated
081                                            else if(param.isDerived() || param.isSystemGenerated()) {
082                                                    // set light red if testcaseparam breaks against assertion
083                                                    if (result == Assertion.Result.NAN || result == Assertion.Result.FAILED) {
084                                                            setForeground(new java.awt.Color(246, 126, 126));
085                                                            setToolTipText("Derived test case param violates assertion");
086                                                    }
087                                                    // set light gray if testcaseparam is within assertion
088                                                    else {
089                                                            setForeground(java.awt.Color.LIGHT_GRAY);
090                                                            setToolTipText(null);
091                                                    }
092                                            }
093                                    }
094                            }
095                    }
096                            
097                    // Render component
098                    return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
099            }
100    }