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.JComponent;
034:
035: /**
036: * This class implements test functionality for {@link javax.swing.JComponent} instances and is
037: * extended by all test classes for swing components. The following criteria are tested by this
038: * class:
039: *
040: * <ul>
041: * <li>
042: * <b>is enabled</b> - testes whether the JComponent is enabled
043: * </li>
044: * <li>
045: * <b>is focused</b> - testes whether the JComponent has the focus
046: * </li>
047: * </ul>
048: *
049: * If you extend this class you should override the two protected methods {@link
050: * #doTest(Component)} and {@link #correct(Component)}, call super() and add specific
051: * functionality afterwards.
052: *
053: * @author <a href="mailto:frommer@rbg.informatik.tu-darmstadt.de">Juri Frommer</a>
054: * @version 1.2
055: */
056: public class JComponentTest extends Test {
057: /** Contains whether the tested component should have the focus */
058: private boolean focus;
059:
060: /** Contains whether the tested component should be enabled */
061: private boolean enabled;
062:
063: /**
064: * Creates a new test with the specified values.
065: *
066: * @param env env the environment
067: * @param componentName the name of the component
068: * @param isIgnoring if the test result should be ignored
069: * @param isCorrecting if the values of the component should be corrected when the test has
070: * failed
071: * @param hasFocus if the component has the focus
072: * @param isEnabled if the component is set enabled
073: */
074: public JComponentTest(Environment env, String componentName,
075: boolean isIgnoring, boolean isCorrecting, boolean hasFocus,
076: boolean isEnabled) {
077: super (env, componentName, isIgnoring, isCorrecting);
078: setFocus(hasFocus);
079: setEnabled(isEnabled);
080: }
081:
082: /**
083: * Creates a new test with the values of the given component and default values.
084: *
085: * @param env the environment.
086: * @param components the components instance
087: * @param component the component to test. Must be of type <code>javax.swing.JComponent</code>.
088: */
089: public JComponentTest(Environment env, Components components,
090: Component component) {
091: super (env, components, component);
092: setEnabled(((JComponent) component).isEnabled());
093: setFocus(((JComponent) component).hasFocus());
094: }
095:
096: /**
097: * Creates a new test with default values and no environment. The environment should be defined
098: * with the method {@link jacareto.system.EnvironmentMember#setEnvironment(Environment)}
099: * before environment instances will be accessed.
100: */
101: public JComponentTest() {
102: this (null, "", false, false, false, true);
103: }
104:
105: /**
106: * @see jacareto.test.Test#evaluate(jacareto.comp.Components)
107: */
108: public boolean evaluate(Components components) {
109: JComponent jComponent = null;
110: setLastIgnored(isIgnoring());
111: setLastResult(false);
112: setLastCorrected(false);
113:
114: // get the JComponent with the componentName
115: Component component = components
116: .getComponent(getComponentName());
117:
118: if (component == null) {
119: setEvaluationMessage(language
120: .getString("Tests.Test.Failure.NoComponent"));
121:
122: return false;
123: }
124:
125: if (!(component instanceof JComponent)) {
126: setEvaluationMessage(language
127: .getString("Tests.Test.Failure.WrongComponentType"));
128:
129: return false;
130: }
131:
132: jComponent = (JComponent) component;
133:
134: if (doTest(jComponent)) {
135: boolean result = true;
136: Iterator childIter = childrenIterator();
137:
138: while (childIter.hasNext() && result) {
139: result &= evaluateChild((Test) childIter.next(),
140: components);
141: }
142:
143: setLastResult(result);
144:
145: return result;
146: }
147:
148: if (isCorrecting()) {
149: //appendToEvaluationMessage("\n" + language.getString("Test.Correct"));
150: correct(jComponent);
151: setLastCorrected(true);
152: }
153:
154: //if(isIgnoring()) appendToEvaluationMessage("\n" + language.getString("Test.Ignored"));
155: return false;
156: }
157:
158: /**
159: * Evaluates a subtest
160: *
161: * @param child the subtest to evaluate
162: * @param components the components containing the component which will be tested by
163: * <code>child</code>
164: *
165: * @return <code>true</code> if the test succeeds, <code>false</code> otherwise
166: */
167: protected boolean evaluateChild(Test child, Components components) {
168: boolean result = child.evaluate(components);
169:
170: if (!result) {
171: setChildEvaluationMessage(child.getEvaluationMessage());
172: }
173:
174: return result;
175: }
176:
177: /**
178: * @see jacareto.struct.StructureElement#getElementName()
179: */
180: public String getElementName() {
181: return language.getString("Tests.JComponentTest.Name");
182: }
183:
184: /**
185: * @see jacareto.struct.StructureElement#getElementDescription()
186: */
187: public String getElementDescription() {
188: return language.getString("Tests.JComponentTest.Description");
189: }
190:
191: /**
192: * @see jacareto.struct.StructureElement#toShortString()
193: */
194: public String toShortString() {
195: return getElementName();
196: }
197:
198: /**
199: * DOCUMENT ME!
200: *
201: * @return <code>true</code> if the tested component must have the focus, if not
202: * <code>false</code>.
203: */
204: public boolean hasFocus() {
205: return focus;
206: }
207:
208: /**
209: * DOCUMENT ME!
210: *
211: * @param b if the tested component should have the focus.
212: */
213: public void setFocus(boolean b) {
214: focus = b;
215: }
216:
217: /**
218: * DOCUMENT ME!
219: *
220: * @return <code>true</code> if the tested component is expected to be enabled, if not
221: * <code>false</code>.
222: */
223: public boolean isEnabled() {
224: return enabled;
225: }
226:
227: /**
228: * DOCUMENT ME!
229: *
230: * @param isEnabled if the tested component is expected to be be enabled.
231: */
232: public void setEnabled(boolean isEnabled) {
233: enabled = isEnabled;
234: }
235:
236: /**
237: * Performs the test. Should be overridden by subclasses and then called via
238: * <code>super</code>. Called by the method {@link #evaluate(Components)}.
239: *
240: * @param component the comonent to perform the test on.
241: *
242: * @return <code>true</code> if the test on <code>component</code> succeeds, <code>otherwise
243: * false</code>
244: */
245: protected boolean doTest(Component component) {
246: JComponent jComponent = (JComponent) component;
247:
248: // Testing the focus does not work anymore because selecting components
249: // in the components frame will cause the focus to change because of the selection
250: //if (component.hasFocus () == hasFocus ()) {
251: if (jComponent.isEnabled() == isEnabled()) {
252: return true;
253: } else {
254: setEvaluationMessage(language
255: .getString("Tests.JComponentTest.Failure.WrongEnabled")
256: + "\n"
257: + language.getString("Tests.Test.Failure.Expected")
258: + ": "
259: + isEnabled()
260: + "\n"
261: + language.getString("Tests.Test.Failure.Detected")
262: + ": " + jComponent.isEnabled());
263:
264: return false;
265: }
266:
267: /*} else {
268: setEvaluationMessage (language.getString ("Tests.JComponentTest.Failure.WrongFocus") +
269: "\n" + language.getString ("Tests.Test.Failure.Expected") + ": " + hasFocus () +
270: "\n" + language.getString ("Tests.Test.Failure.Detected") + ": " +
271: jComponent.hasFocus ());
272:
273: return false;
274: } */
275: }
276:
277: /**
278: * Sets the expected values on the tested component. Should be overridden by subclasses and
279: * then called via <code>super</code>. Called by the method {@link #evaluate(Components)}.
280: *
281: * @param component the component to correct
282: */
283: protected void correct(Component component) {
284: if (hasFocus()) {
285: component.requestFocus();
286: appendToEvaluationMessage("\n"
287: + getElementName()
288: + ": "
289: + language
290: .getString("Tests.JComponentTest.Correct.Focus"));
291: } else {
292: appendToEvaluationMessage("\n"
293: + getElementName()
294: + ": "
295: + language
296: .getString("Tests.JComponentTest.Correct.FocusFailed"));
297: }
298:
299: component.setEnabled(isEnabled());
300: appendToEvaluationMessage("\n"
301: + getElementName()
302: + ": "
303: + language
304: .getString("Tests.JComponentTest.Correct.Enabled"));
305: }
306:
307: /**
308: * @see jacareto.test.Test#setEvaluationMessage(java.lang.String)
309: */
310: protected void setEvaluationMessage(String evaluationMessage) {
311: super .setEvaluationMessage( /*getComponentName()+": "+*/
312: evaluationMessage);
313: }
314:
315: /**
316: * Sets the evaluation message without adding the component name
317: *
318: * @param evaluationMessage the evaluaton message
319: */
320: private void setChildEvaluationMessage(String evaluationMessage) {
321: super .setEvaluationMessage(evaluationMessage);
322: }
323:
324: /**
325: * Appends the argument to the evaluation message
326: *
327: * @param appendix the string to append tpo the evaluation message
328: */
329: protected void appendToEvaluationMessage(String appendix) {
330: super .setEvaluationMessage(getEvaluationMessage() + appendix);
331: }
332:
333: public boolean hasProcTime() {
334: return true;
335: }
336: }
|