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.BorderLayout;
026 import java.util.HashSet;
027 import java.util.Set;
028 import java.util.Vector;
029
030 import javax.swing.JPanel;
031 import javax.swing.JScrollPane;
032 import javax.swing.event.TableModelEvent;
033 import javax.swing.event.TableModelListener;
034 import javax.swing.table.DefaultTableModel;
035
036 import csheets.core.Cell;
037 import csheets.ext.test.TestCaseParam;
038 import csheets.ext.test.TestExtension;
039 import csheets.ext.test.TestableCell;
040 import csheets.ui.ctrl.SelectionEvent;
041 import csheets.ui.ctrl.SelectionListener;
042 import csheets.ui.ctrl.UIController;
043
044 /**
045 * A panel for displaying and providing editing of test case parameters
046 * @author Björn Lanneskog
047 * @author Einar Pehrson
048 */
049 @SuppressWarnings("serial")
050 public class TestCaseParamPanel extends JPanel implements SelectionListener {
051
052 /** Array which contains labels of columns in the table.*/
053 private static final String[] columnNames =
054 {"Precedent", "", "", "", "", "", "", "", "", ""};
055
056 /** The table model for this table.*/
057 private DefaultTableModel tableModel;
058
059 /** The Visible table for entering test case params.*/
060 private TestCaseParamTable table;
061
062 /** The test case parameter controller.*/
063 private TestCaseParamController controller;
064
065 /** Determines whether the table arranger should respond to events. */
066 private boolean listening = true;
067
068 /**
069 * Creates a new test case parameter panel.
070 * @param uiController the user interface controller
071 */
072 public TestCaseParamPanel(UIController uiController) {
073 // Creates and configures table
074 controller = new TestCaseParamController(uiController);
075 tableModel = new DefaultTableModel(columnNames,0);
076 tableModel.addTableModelListener(new TestCaseParamSetter());
077 table = new TestCaseParamTable(tableModel);
078
079 // Configures layout and adds components
080 setLayout(new BorderLayout());
081 JScrollPane tableScrollPane = new JScrollPane(table);
082 add(tableScrollPane, BorderLayout.CENTER);
083 }
084
085 /**
086 * Updates the table of precedents when the active cell is changed.
087 * @param event the selection event that was fired
088 */
089 public void selectionChanged(SelectionEvent event) {
090 listening = false;
091
092 // Clears the table
093 tableModel.setRowCount(0);
094
095 Cell cell = event.getCell();
096 if (cell != null) {
097 // Resizes the table
098 Set<Cell> precedents = cell.getPrecedents();
099 tableModel.setRowCount(precedents.size());
100
101 // Adds the test case parameters of each precedent to the table
102 int row = 0;
103 for (Cell precedent : precedents) {
104 TestableCell testablePrecedent = (TestableCell)precedent.getExtension(TestExtension.NAME);
105 tableModel.setValueAt(testablePrecedent, row, 0);
106 int column = 1;
107 for (TestCaseParam param : testablePrecedent.getTestCaseParams()) {
108 if (tableModel.getColumnCount() - 1 == column)
109 tableModel.addColumn("");
110 tableModel.setValueAt(param, row, column++);
111 }
112 row++;
113 }
114 table.packColumns();
115 }
116
117 listening = true;
118 }
119
120 /**
121 * A table model listener for setting the test case parameters of a cell.
122 * @author Björn Lanneskog
123 * @author Einar Pehrson
124 */
125 protected class TestCaseParamSetter implements TableModelListener {
126
127 public void tableChanged(TableModelEvent e) {
128 // If there has been an update on any column but the first
129 if(listening && e.getType() == TableModelEvent.UPDATE && e.getColumn() != 0 && listening == true) {
130
131 // Fetches the parameter and the cell
132 String param = tableModel.getValueAt(e.getFirstRow(), e.getColumn()).toString().trim();
133 TestableCell cell = (TestableCell)tableModel.getValueAt(e.getFirstRow(), 0);
134
135 // Checks if the selected parameter was removed
136 if(param.equals(""))
137 param = null;
138
139 // Collects the unchanged parameters
140 Set<TestCaseParam> unChangedParams = new HashSet<TestCaseParam>();
141 Vector params = (Vector)tableModel.getDataVector().elementAt(e.getFirstRow());
142 for (Object o : params)
143 if(o instanceof TestCaseParam)
144 unChangedParams.add((TestCaseParam)o);
145
146 // Stores the parameter
147 TestCaseParam added = controller.setTestCaseParams(cell, param, unChangedParams);
148 listening = false;
149 if (added != null) {
150 tableModel.setValueAt(added, e.getFirstRow(), e.getColumn());
151 // If the last column was filled, adds another
152 } else
153 tableModel.setValueAt("", e.getFirstRow(), e.getColumn());
154 listening = true;
155
156 // Packs the column
157 table.packColumn(e.getColumn());
158 }
159 }
160 }
161 }