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.finder.StringMatcher;
007: import org.uispec4j.utils.ColorUtils;
008: import org.uispec4j.xml.XmlWriter;
009:
010: import javax.swing.*;
011: import java.awt.*;
012:
013: /**
014: * Wrapper for JTabbedPane components.
015: */
016: public class TabGroup extends AbstractUIComponent {
017: public static final String TYPE_NAME = "tabGroup";
018: public static final Class[] SWING_CLASSES = { JTabbedPane.class };
019:
020: private JTabbedPane jTabbedPane;
021:
022: public TabGroup(JTabbedPane jTab) {
023: this .jTabbedPane = jTab;
024: }
025:
026: public String getDescriptionTypeName() {
027: return TYPE_NAME;
028: }
029:
030: public Component getAwtComponent() {
031: return jTabbedPane;
032: }
033:
034: public Panel getSelectedTab() {
035: Component selectedComponent = jTabbedPane
036: .getSelectedComponent();
037: if (!JPanel.class.isInstance(selectedComponent)) {
038: Assert
039: .fail("tabGroup.getSelectedTab() only supports JPanel components inside a JTabbedPane");
040: }
041: return new Panel((JPanel) selectedComponent);
042: }
043:
044: public void selectTab(String tabLabel) {
045: final int index = getTabIndex(tabLabel);
046: Assert.assertTrue(tabNotFound(tabLabel), index >= 0);
047: jTabbedPane.setSelectedIndex(index);
048: UISpecAssert.assertTrue(new Assertion() {
049: public void check() throws Exception {
050: Assert
051: .assertTrue(jTabbedPane.getSelectedIndex() == index);
052: }
053: });
054: }
055:
056: public Assertion tabColorEquals(final String[] colors) {
057: return new Assertion() {
058: public void check() {
059: int tabCount = jTabbedPane.getTabCount();
060: Assert.assertEquals("You specified " + colors.length
061: + " colors but there are " + tabCount
062: + " tabs -", colors.length, tabCount);
063: for (int i = 0; i < colors.length; i++) {
064: String color = colors[i];
065: if (!ColorUtils.equals(color, jTabbedPane
066: .getForegroundAt(i))) {
067: Assert
068: .fail("Unexpected color for tab '"
069: + jTabbedPane.getTitleAt(i)
070: + "' (index "
071: + i
072: + ") - expected "
073: + ColorUtils
074: .getColorDescription(color)
075: + " but was "
076: + ColorUtils
077: .getColorDescription(jTabbedPane
078: .getForegroundAt(i)));
079: }
080: }
081: }
082: };
083: }
084:
085: public Assertion selectedTabEquals(final String tabLabel) {
086: return new Assertion() {
087: public void check() {
088: Assert.assertEquals(tabLabel, jTabbedPane
089: .getTitleAt(jTabbedPane.getSelectedIndex()));
090: }
091: };
092: }
093:
094: public Assertion tabNamesEquals(final String[] tabLabels) {
095: return new Assertion() {
096: public void check() {
097: Assert.assertEquals(tabLabels.length, jTabbedPane
098: .getTabCount());
099: for (int i = 0; i < tabLabels.length; i++) {
100: Assert.assertEquals(tabLabels[i], jTabbedPane
101: .getTitleAt(i));
102: }
103: }
104: };
105: }
106:
107: protected void getSubDescription(Container container,
108: XmlWriter.Tag tag) {
109: if (container == jTabbedPane) {
110: Component selectedComponent = jTabbedPane
111: .getSelectedComponent();
112: if (selectedComponent != null) {
113: getDescription(selectedComponent, tag, false);
114: return;
115: }
116: } else {
117: super .getSubDescription(container, tag);
118: }
119: }
120:
121: private int getTabIndex(String tabLabel) {
122: int index = jTabbedPane.indexOfTab(tabLabel);
123: if (index >= 0) {
124: return index;
125: }
126: for (int i = 0; i < jTabbedPane.getTabCount(); i++) {
127: String title = jTabbedPane.getTitleAt(i);
128: if (StringMatcher.substring(tabLabel).matches(title)) {
129: return i;
130: }
131: }
132: return -1;
133: }
134:
135: static String tabNotFound(String name) {
136: return "There is no tab labelled '" + name + "'";
137: }
138: }
|