001: package org.uispec4j;
002:
003: import junit.framework.Assert;
004: import org.uispec4j.assertion.Assertion;
005: import org.uispec4j.utils.ColorUtils;
006: import org.uispec4j.utils.UIComponentFactory;
007: import org.uispec4j.xml.XmlWriter;
008:
009: import javax.swing.*;
010: import java.awt.*;
011: import java.io.StringWriter;
012:
013: /**
014: * Base class for UIComponent implementations.
015: */
016: public abstract class AbstractUIComponent implements UIComponent {
017:
018: public final String getDescription() {
019: StringWriter writer = new StringWriter();
020: XmlWriter.Tag tag = XmlWriter.startTag(writer,
021: getDescriptionTypeName());
022: Component component = getAwtComponent();
023: addAttributes(component, tag);
024: if (component instanceof Container) {
025: getSubDescription((Container) component, tag);
026: }
027: tag.end();
028: return writer.toString();
029: }
030:
031: protected void getSubDescription(Container container,
032: XmlWriter.Tag tag) {
033: Component[] components = container.getComponents();
034: for (int i = 0; i < components.length; i++) {
035: getDescription(components[i], tag, true);
036: }
037: }
038:
039: protected void getDescription(Component component,
040: XmlWriter.Tag tag, boolean showVisibleOnly) {
041: if (!JComponent.class.isAssignableFrom(component.getClass())) {
042: return;
043: }
044: JComponent jComponent = (JComponent) component;
045: if (showVisibleOnly && !jComponent.isVisible()) {
046: return;
047: }
048: if (jComponent instanceof JScrollPane) {
049: getSubDescription(((JScrollPane) jComponent).getViewport(),
050: tag);
051: return;
052: }
053: AbstractUIComponent guiComponent = (AbstractUIComponent) UIComponentFactory
054: .createUIComponent(jComponent);
055: if ((guiComponent == null) || isPanelWithNoName(guiComponent)) {
056: getSubDescription(jComponent, tag);
057: return;
058: }
059: XmlWriter.Tag childTag = tag.start(guiComponent
060: .getDescriptionTypeName());
061: guiComponent.addAttributes(jComponent, childTag);
062: guiComponent.getSubDescription(jComponent, childTag);
063: childTag.end();
064: }
065:
066: protected void addAttributes(Component component, XmlWriter.Tag tag) {
067: if ((component.getName() != null)
068: && (component.getName().length() != 0)) {
069: tag.addAttribute("name", computeComponentName(component));
070: }
071: String label = getLabel();
072: if ((label != null) && (label.length() > 0)) {
073: tag.addAttribute("label", label);
074: }
075: }
076:
077: public String getName() {
078: return getAwtComponent().getName();
079: }
080:
081: public String getLabel() {
082: return null;
083: }
084:
085: public Assertion isVisible() {
086: return new Assertion() {
087: public void check() {
088: Assert.assertTrue(getAwtComponent().isVisible());
089: }
090: };
091: }
092:
093: public Assertion isEnabled() {
094: return new Assertion() {
095: public void check() {
096: Assert.assertTrue(getAwtComponent().isEnabled());
097: }
098: };
099: }
100:
101: /**
102: * Checks the foreground color of the component. <p/>
103: * The color can be given in either hexadecimal ("FF45C0") or human-readable ("red") format.
104: *
105: * @see <a href="http://www.uispec4j.org/usingcolors.html">Using colors</a>
106: */
107: public Assertion foregroundEquals(final String expectedColor) {
108: return new Assertion() {
109: public void check() {
110: Color foreground = getAwtComponent().getForeground();
111: if (foreground == null) {
112: foreground = Color.BLACK;
113: }
114: ColorUtils.assertEquals(expectedColor, foreground);
115: }
116: };
117: }
118:
119: /**
120: * Checks the background color of the component
121: * The color can be given in either hexadecimal ("FF45C0") or human-readable ("red") format.
122: *
123: * @see <a href="http://www.uispec4j.org/usingcolors.html">Using colors</a>
124: */
125: public Assertion backgroundEquals(final String expectedColor) {
126: return new Assertion() {
127: public void check() {
128: ColorUtils.assertEquals(expectedColor,
129: getAwtComponent().getBackground());
130: }
131: };
132: }
133:
134: private String computeComponentName(Component component) {
135: String name = component.getName();
136: return (name == null) ? "" : name;
137: }
138:
139: private boolean isPanelWithNoName(UIComponent component) {
140: return ((component instanceof Panel) && computeComponentName(
141: component.getAwtComponent()).equals(""));
142: }
143: }
|