下面的例子中将获取到JTree中的每一个节点并按树状结构打印出来:
public void testMain(Object[] args)
{
//Turn off Log Viewer for this example
setOption(IOptionName.BRING_UP_LOGVIEWER, false);
//Start Classics Java Application
startApp("ClassicsJavaA");
// Frame: ClassicsCD
tree2().waitForExistence();
//Display available test data types available from tree
System.out.println ("Available Tree Data Types: " + tree2().getTestDataTypes());
//Declare variables for tree
ITestDataTree cdTree;
ITestDataTreeNodes cdTreeNodes;
ITestDataTreeNode[] cdTreeNode;
//Variables to hold tree data
cdTree = (ITestDataTree)tree2().getTestData("tree");
cdTreeNodes = cdTree.getTreeNodes();
cdTreeNode = cdTreeNodes.getRootNodes();
//Print out total number of nodes
System.out.println ("Tree Total Node Count: " + cdTreeNodes.getNodeCount());
System.out.println ("Tree Root Node Count : " + cdTreeNodes.getRootNodeCount());
//Iterate through tree branches; this is a recursive method.
for (int i = 0;i
showTree(cdTreeNode[i], 0);
//Shut down Classics Java Application
classicsJava(ANY,MAY_EXIT).close();
}
void showTree(ITestDataTreeNode node, int indent)
{
//Recursive method to print out tree nodes with proper indenting.
//Determine number of tabs to use - to properly indent tree
int tabCount = ( indent < tabs.length() ? indent :
tabs.length() );
//Print out node name + number of children
System.out.println(tabs.substring(0, tabCount) + node.getNode() + " (" + node.getChildCount() + "children)" );
//Determine if node has children; recursively call this same
//method to print out child nodes.
ITestDataTreeNode[] children = node.getChildren();
int childCount = ( children != null ? children.length : 0 );
for ( int i = 0; i < childCount; ++i )
showTree(children[i], indent+1);
}
//String of tabs used to indent tree view
final String tabs = "/t/t/t/t/t/t/t/t/t/t/t/t/t/t/t/t/t/t/t/t";
输出:
Available Tree Data Types: {selected=选中的树层次结构, tree=树层次结构}
Tree Total Node Count: 20
Tree Root Node Count : 1
Composers (5children)
Schubert (3children)
String Quartets Nos. 4 & 14 (0children)
Die schone Mullerin, Op. 25 (0children)
Symphonies Nos. 5 & 9 (0children)
Haydn (3children)
Symphonies Nos. 99 & 101 (0children)
Symphonies Nos. 94 & 98 (0children)
Violin Concertos (0children)
Bach (2children)
Brandenburg Concertos Nos. 1 & 3 (0children)
Violin Concertos (0children)
Beethoven (3children)
Symphony No. 7 (0children)
Symphony No. 9 (0children)
Symphony No. 5 (0children)
Mozart (3children)
Symphony No. 34 (0children)
Symphony in C, No. 41: Jupiter (0children)
Concerto in D for Piano (0children)
关于ITestDataTree 、 ITestDataTreeNodes 、ItestDataTreeNode的使用方法可参考RFT的帮助文档:
All Superinterfaces:
ITestData
public interface ITestDataTree
extends ITestData
The base-level tree verification-point interface. This interface represents a property set and entry points to the tree nodes.
Since:
RFT1.0
Method Summary |
|
|
|
|
|
|
|
|
|
public interface ITestDataTreeNodes
Enables an appropriate display object to be associated with the tree nodes. This is the container class for the Nodes
of a tree-type verification point. Without this container class, the generic property display does not know how to display the nodes.
Since:
RFT1.0
Method Summary |
|
|
|
|
|
|
|
|
|
|
|
|
|
public interface ITestDataTreeNode
Provides the necessary methods for representing a node in a Tree
test-data object.
Since:
RFT1.0
Method Summary |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|