📄 abstractuicomponent.java
字号:
package org.uispec4j;
import junit.framework.Assert;
import org.uispec4j.assertion.Assertion;
import org.uispec4j.utils.ColorUtils;
import org.uispec4j.utils.UIComponentFactory;
import org.uispec4j.xml.XmlWriter;
import javax.swing.*;
import java.awt.*;
import java.io.StringWriter;
/**
* Base class for UIComponent implementations.
*/
public abstract class AbstractUIComponent implements UIComponent {
public final String getDescription() {
StringWriter writer = new StringWriter();
XmlWriter.Tag tag = XmlWriter.startTag(writer, getDescriptionTypeName());
Component component = getAwtComponent();
addAttributes(component, tag);
if (component instanceof Container) {
getSubDescription((Container)component, tag);
}
tag.end();
return writer.toString();
}
protected void getSubDescription(Container container, XmlWriter.Tag tag) {
Component[] components = container.getComponents();
for (int i = 0; i < components.length; i++) {
getDescription(components[i], tag, true);
}
}
protected void getDescription(Component component, XmlWriter.Tag tag, boolean showVisibleOnly) {
if (!JComponent.class.isAssignableFrom(component.getClass())) {
return;
}
JComponent jComponent = (JComponent)component;
if (showVisibleOnly && !jComponent.isVisible()) {
return;
}
if (jComponent instanceof JScrollPane) {
getSubDescription(((JScrollPane)jComponent).getViewport(), tag);
return;
}
AbstractUIComponent guiComponent =
(AbstractUIComponent)UIComponentFactory.createUIComponent(jComponent);
if ((guiComponent == null) || isPanelWithNoName(guiComponent)) {
getSubDescription(jComponent, tag);
return;
}
XmlWriter.Tag childTag = tag.start(guiComponent.getDescriptionTypeName());
guiComponent.addAttributes(jComponent, childTag);
guiComponent.getSubDescription(jComponent, childTag);
childTag.end();
}
protected void addAttributes(Component component, XmlWriter.Tag tag) {
if ((component.getName() != null) && (component.getName().length() != 0)) {
tag.addAttribute("name", computeComponentName(component));
}
String label = getLabel();
if ((label != null) && (label.length() > 0)) {
tag.addAttribute("label", label);
}
}
public String getName() {
return getAwtComponent().getName();
}
public String getLabel() {
return null;
}
public Assertion isVisible() {
return new Assertion() {
public void check() {
Assert.assertTrue(getAwtComponent().isVisible());
}
};
}
public Assertion isEnabled() {
return new Assertion() {
public void check() {
Assert.assertTrue(getAwtComponent().isEnabled());
}
};
}
/**
* Checks the foreground color of the component. <p/>
* The color can be given in either hexadecimal ("FF45C0") or human-readable ("red") format.
*
* @see <a href="http://www.uispec4j.org/usingcolors.html">Using colors</a>
*/
public Assertion foregroundEquals(final String expectedColor) {
return new Assertion() {
public void check() {
Color foreground = getAwtComponent().getForeground();
if (foreground == null) {
foreground = Color.BLACK;
}
ColorUtils.assertEquals(expectedColor, foreground);
}
};
}
/**
* Checks the background color of the component
* The color can be given in either hexadecimal ("FF45C0") or human-readable ("red") format.
*
* @see <a href="http://www.uispec4j.org/usingcolors.html">Using colors</a>
*/
public Assertion backgroundEquals(final String expectedColor) {
return new Assertion() {
public void check() {
ColorUtils.assertEquals(expectedColor, getAwtComponent().getBackground());
}
};
}
private String computeComponentName(Component component) {
String name = component.getName();
return (name == null) ? "" : name;
}
private boolean isPanelWithNoName(UIComponent component) {
return ((component instanceof Panel) && computeComponentName(component.getAwtComponent()).equals(""));
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -