01: package org.uispec4j;
02:
03: import org.uispec4j.utils.DummyTreeCellRenderer;
04: import org.uispec4j.utils.UnitTestCase;
05: import org.uispec4j.xml.EventLogger;
06:
07: import javax.swing.*;
08: import javax.swing.tree.DefaultMutableTreeNode;
09: import java.awt.*;
10:
11: public abstract class TreeTestCase extends UnitTestCase {
12: protected JTree jTree;
13: protected Tree tree;
14: protected DefaultMutableTreeNode rootNode;
15: protected DefaultMutableTreeNode child1Node;
16: protected DefaultMutableTreeNode child1_1Node;
17: protected DefaultMutableTreeNode child2Node;
18: protected DummyTreeCellRenderer.UserObject root = new DummyTreeCellRenderer.UserObject(
19: "root");
20: protected DummyTreeCellRenderer.UserObject child1 = new DummyTreeCellRenderer.UserObject(
21: "child1");
22: protected DummyTreeCellRenderer.UserObject child1_1 = new DummyTreeCellRenderer.UserObject(
23: "child1_1");
24: protected DummyTreeCellRenderer.UserObject child2 = new DummyTreeCellRenderer.UserObject(
25: "child2");
26:
27: protected void setUp() throws Exception {
28: super .setUp();
29: rootNode = new DefaultMutableTreeNode(root);
30: child1Node = new DefaultMutableTreeNode(child1);
31: child1_1Node = new DefaultMutableTreeNode(child1_1);
32: child2Node = new DefaultMutableTreeNode(child2);
33:
34: rootNode.add(child1Node);
35: child1Node.add(child1_1Node);
36: rootNode.add(child2Node);
37: initTree();
38: }
39:
40: protected void initTree() {
41: jTree = new JTree(rootNode);
42: jTree.setCellRenderer(new DummyTreeCellRenderer(jTree));
43: jTree.setName("myTree");
44: tree = new Tree(jTree);
45: }
46:
47: protected static class DummyTreeCellValueConverter extends
48: EventLogger implements TreeCellValueConverter {
49: private String boldPattern;
50: private String redFontPattern;
51:
52: public String getValue(Component renderedComponent,
53: Object modelObject) {
54: log("getValue").add("component",
55: renderedComponent.getClass().getName()).add(
56: "object", modelObject.toString());
57: return '_' + modelObject.toString() + '_';
58: }
59:
60: public void setRedFontPattern(String redFontPattern) {
61: this .redFontPattern = redFontPattern;
62: }
63:
64: public Color getForeground(Component renderedComponent,
65: Object modelObject) {
66: if ((redFontPattern != null)
67: && (modelObject.toString().indexOf(redFontPattern) >= 0)) {
68: return Color.RED;
69: }
70:
71: return Color.BLACK;
72: }
73:
74: public void setBoldPattern(String boldPattern) {
75: this .boldPattern = boldPattern;
76: }
77:
78: public boolean isBold(Component renderedComponent,
79: Object modelObject) {
80: if (boldPattern == null) {
81: return false;
82: }
83: return modelObject.toString().indexOf(boldPattern) >= 0;
84: }
85: }
86: }
|