001: /*
002: * Jacareto Copyright (c) 2002-2005
003: * Applied Computer Science Research Group, Darmstadt University of
004: * Technology, Institute of Mathematics & Computer Science,
005: * Ludwigsburg University of Education, and Computer Based
006: * Learning Research Group, Aachen University. All rights reserved.
007: *
008: * Jacareto is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU General Public
010: * License as published by the Free Software Foundation; either
011: * version 2 of the License, or (at your option) any later version.
012: *
013: * Jacareto is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
016: * General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public
019: * License along with Jacareto; if not, write to the Free
020: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
021: *
022: */
023:
024: package jacareto.test;
025:
026: import jacareto.comp.Components;
027: import jacareto.system.Environment;
028:
029: import java.awt.Component;
030:
031: import java.util.Iterator;
032:
033: import javax.swing.JTable;
034:
035: /**
036: * Test class for testing <code>java.swing.JTable</code> components. The following criteria are
037: * tested by this class:
038: *
039: * <ul>
040: * <li>
041: * <b>selected row</b> - tests if the correct row is selected
042: * </li>
043: * <li>
044: * <b>selected column</b> - tests if the correct column is selected
045: * </li>
046: * </ul>
047: *
048: *
049: * @author <a href="mailto:cspannagel@web.de">Christian Spannagel</a>
050: * @version 1.0
051: */
052: public class JTableTest extends JComponentTest {
053: /** Contains the number of the selected row. */
054: private int selectedRow;
055:
056: /** Contains the number of the selected column. */
057: private int selectedColumn;
058:
059: /**
060: * Creates a new test with the specified values.
061: *
062: * @param env env the environment
063: * @param componentName the name of the component
064: * @param isIgnoring if the test result should be ignored
065: * @param isCorrecting if the values of the component should be corrected when the test has
066: * failed
067: * @param hasFocus if the component has the focus
068: * @param isEnabled if the component is set enabled
069: * @param selectedRow the number of the selected row
070: * @param selectedColumn the number of the selected column
071: */
072: public JTableTest(Environment env, String componentName,
073: boolean isIgnoring, boolean isCorrecting, boolean hasFocus,
074: boolean isEnabled, int selectedRow, int selectedColumn) {
075: super (env, componentName, isIgnoring, isCorrecting, hasFocus,
076: isEnabled);
077: setSelectedRow(selectedRow);
078: setSelectedColumn(selectedColumn);
079: }
080:
081: /**
082: * Creates a new test with the values of the given component and default values.
083: *
084: * @param env the environment.
085: * @param components the components instance
086: * @param component the component to test. Must be of type <code>javax.swing.JTable</code>.
087: */
088: public JTableTest(Environment env, Components components,
089: Component component) {
090: super (env, components, component);
091: setSelectedRow(((JTable) component).getSelectedRow());
092: setSelectedColumn(((JTable) component).getSelectedColumn());
093: }
094:
095: /**
096: * Creates a new test with default values and no environment. The environment should be defined
097: * with the method {@link jacareto.system.EnvironmentMember#setEnvironment(Environment)}
098: * before environment instances will be accessed.
099: */
100: public JTableTest() {
101: this (null, "", false, false, false, true, 0, 0);
102: }
103:
104: /**
105: * Sets the number of the selected row.
106: *
107: * @param selectedRow the index of the selected row
108: */
109: public void setSelectedRow(int selectedRow) {
110: this .selectedRow = selectedRow;
111: }
112:
113: /**
114: * Sets the number of the selected column.
115: *
116: * @param selectedColumn the index of the selected column
117: */
118: public void setSelectedColumn(int selectedColumn) {
119: this .selectedColumn = selectedColumn;
120: }
121:
122: /**
123: * Returns the number of the selected row
124: *
125: * @return the index of the selected row
126: */
127: public int getSelectedRow() {
128: return selectedRow;
129: }
130:
131: /**
132: * Returns the number of the selected column
133: *
134: * @return the index of the selected column
135: */
136: public int getSelectedColumn() {
137: return selectedColumn;
138: }
139:
140: /**
141: * @see jacareto.struct.StructureElement#getElementName()
142: */
143: public String getElementName() {
144: return language.getString("Tests.JTableTest.Name");
145: }
146:
147: /**
148: * @see jacareto.struct.StructureElement#getElementDescription()
149: */
150: public String getElementDescription() {
151: return language.getString("Tests.JTableTest.Description");
152: }
153:
154: /**
155: * @see jacareto.struct.StructureElement#toShortString()
156: */
157: public String toShortString() {
158: return getElementName();
159: }
160:
161: /**
162: * @see jacareto.test.Test#evaluate(jacareto.comp.Components)
163: */
164: public boolean evaluate(Components components) {
165: JTable table = null;
166: setLastIgnored(isIgnoring());
167: setLastResult(false);
168: setLastCorrected(false);
169:
170: // get the button with the componentName
171: Component component = components
172: .getComponent(getComponentName());
173:
174: if (component == null) {
175: setEvaluationMessage(language
176: .getString("Tests.Test.Failure.NoComponent"));
177:
178: return false;
179: }
180:
181: if (!(component instanceof JTable)) {
182: setEvaluationMessage(language
183: .getString("Tests.Test.Failure.WrongComponentType"));
184:
185: return false;
186: }
187:
188: table = (JTable) component;
189:
190: if (doTest(table)) {
191: boolean result = true;
192: Iterator childIter = childrenIterator();
193:
194: while (childIter.hasNext() && result) {
195: result &= evaluateChild((Test) childIter.next(),
196: components);
197: }
198:
199: setLastResult(result);
200:
201: return result;
202: }
203:
204: if (isCorrecting()) {
205: //appendToEvaluationMessage("\n" + language.getString("Test.Correct"));
206: correct(table);
207: setLastCorrected(true);
208: }
209:
210: //if(isIgnoring()) appendToEvaluationMessage("\n" + language.getString("Test.Ignored"));
211: return false;
212: }
213:
214: /**
215: * @see jacareto.test.JComponentTest#doTest(java.awt.Component)
216: */
217: protected boolean doTest(Component component) {
218: if (super .doTest(component)) {
219: JTable table = (JTable) component;
220:
221: // JTable specific evaluation logic
222: if (table.getSelectedRow() != getSelectedRow()) {
223: setEvaluationMessage(language
224: .getString("Tests.JTableTest.Failure.WrongRowSelected")
225: + "\n"
226: + language
227: .getString("Tests.Test.Failure.Expected")
228: + ": "
229: + getSelectedRow()
230: + "\n"
231: + language
232: .getString("Tests.Test.Failure.Detected")
233: + ": " + table.getSelectedRow());
234:
235: return false;
236: }
237:
238: if (table.getSelectedColumn() != getSelectedColumn()) {
239: setEvaluationMessage(language
240: .getString("Tests.JTableTest.Failure.WrongColumnSelected")
241: + "\n"
242: + language
243: .getString("Tests.Test.Failure.Expected")
244: + ": "
245: + getSelectedColumn()
246: + "\n"
247: + language
248: .getString("Tests.Test.Failure.Detected")
249: + ": " + table.getSelectedColumn());
250:
251: return false;
252: }
253: } else {
254: return false;
255: }
256:
257: return true;
258: }
259:
260: /**
261: * @see jacareto.test.JComponentTest#correct(java.awt.Component)
262: */
263: protected void correct(Component component) {
264: super .correct(component);
265:
266: JTable table = (JTable) component;
267:
268: if (table.getSelectedRow() != getSelectedRow()) {
269: table.setRowSelectionInterval(getSelectedRow(),
270: getSelectedRow());
271: appendToEvaluationMessage("\n"
272: + getElementName()
273: + ": "
274: + language
275: .getString("Tests.JTableTest.Correct.SelectedRow"));
276: } else if (table.getSelectedColumn() != getSelectedColumn()) {
277: table.setColumnSelectionInterval(getSelectedColumn(),
278: getSelectedColumn());
279: appendToEvaluationMessage("\n"
280: + getElementName()
281: + ": "
282: + language
283: .getString("Tests.JTableTest.Correct.SelectedColumn"));
284: }
285: }
286: }
|