001: package org.uispec4j;
002:
003: import junit.framework.Assert;
004: import org.uispec4j.assertion.Assertion;
005: import org.uispec4j.assertion.UISpecAssert;
006: import org.uispec4j.utils.KeyUtils;
007:
008: import javax.swing.*;
009: import javax.swing.text.BadLocationException;
010: import javax.swing.text.Document;
011: import javax.swing.text.JTextComponent;
012: import java.awt.*;
013:
014: abstract class AbstractTextBoxHandlerForTextComponent implements
015: TextBox.Handler {
016: protected JTextComponent jTextComponent;
017:
018: public AbstractTextBoxHandlerForTextComponent(
019: JTextComponent textComponent) {
020: this .jTextComponent = textComponent;
021: }
022:
023: public Component getAwtComponent() {
024: return jTextComponent;
025: }
026:
027: public void setText(String text) {
028: UISpecAssert.assertTrue(isEditable());
029: jTextComponent.setText(text);
030: if (jTextComponent instanceof JTextField) {
031: ((JTextField) jTextComponent).postActionEvent();
032: }
033: }
034:
035: public void insertText(String text, int position) {
036: UISpecAssert.assertTrue(isEditable());
037: Document document = jTextComponent.getDocument();
038: try {
039: document.insertString(position, text, document
040: .getDefaultRootElement().getAttributes());
041: } catch (BadLocationException e) {
042: Assert.fail("Position should be between 0 and "
043: + document.getLength());
044: }
045: }
046:
047: public String getText() {
048: return jTextComponent.getText();
049: }
050:
051: public Assertion textContains(final String text) {
052: return new Assertion() {
053: public void check() {
054: String actual = jTextComponent.getText().replaceAll(
055: "\n ", "").replaceAll("\n </body>",
056: "</body>");
057: Assert.assertTrue(
058: "The component text does not contain '" + text
059: + "' - actual content is:" + actual,
060: actual.indexOf(text.trim()) >= 0);
061: }
062: };
063: }
064:
065: public Assertion textDoesNotContain(final String text) {
066: return new Assertion() {
067: public void check() {
068: String actual = jTextComponent.getText();
069: Assert.assertTrue(
070: "The component text should not contain '"
071: + text + "' - actual content is:"
072: + actual, actual.indexOf(text) < 0);
073: }
074: };
075: }
076:
077: public Assertion isEditable() {
078: return new Assertion() {
079: public void check() {
080: Assert.assertTrue("The text box is not editable",
081: jTextComponent.isEditable());
082: }
083: };
084: }
085:
086: public void pressKey(Key key) {
087: KeyUtils.pressKey(jTextComponent, key);
088: if (jTextComponent.isEditable() && key.isPrintable()) {
089: Document document = jTextComponent.getDocument();
090: try {
091: int position = jTextComponent.getCaretPosition();
092: document.insertString(position, new Character(
093: (char) key.getCode()).toString(), document
094: .getDefaultRootElement().getAttributes());
095: jTextComponent.moveCaretPosition(document
096: .getEndPosition().getOffset() - 1);
097: // jTextComponent.moveCaretPosition(position + 1);
098: } catch (BadLocationException e) {
099: Assert.fail(e.getMessage());
100: }
101: }
102: }
103:
104: public Assertion iconEquals(Icon icon) {
105: throw new UnsupportedOperationException(
106: "assertIconEquals is not supported for JTextComponent components");
107: }
108: }
|