1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package net.sf.jameleon.ui;
20
21 import java.io.BufferedReader;
22 import java.io.File;
23 import java.io.FileReader;
24 import java.io.IOException;
25
26 import javax.swing.JTree;
27 import javax.swing.event.TreeExpansionEvent;
28 import javax.swing.event.TreeSelectionEvent;
29 import javax.swing.event.TreeSelectionListener;
30 import javax.swing.event.TreeWillExpandListener;
31 import javax.swing.tree.DefaultMutableTreeNode;
32 import javax.swing.tree.DefaultTreeModel;
33 import javax.swing.tree.TreePath;
34
35 import net.sf.jameleon.TestCaseTagLibrary;
36 import net.sf.jameleon.bean.TestCase;
37 import net.sf.jameleon.util.Configurator;
38 import net.sf.jameleon.util.JameleonDefaultValues;
39
40 public class TestCaseTree extends JTree{
41
42 protected Configurator config;
43 protected TestCasePane testCasePane;
44 private final int SECONDS_TO_WAIT = 3;
45
46 public TestCaseTree(TestCasePane testCasePn){
47 super();
48 testCasePane = testCasePn;
49 testCasePane.setTestCaseTree(this);
50
51 FileNode rootFileNode = createRootFileNode();
52 TCTreeNode rootNode = new TCTreeNode(rootFileNode, true);
53 DefaultTreeModel tm = new DefaultTreeModel(rootNode);
54 setModel(tm);
55 generateScriptsTree(rootFileNode);
56 TCTreeListener listener = new TCTreeListener();
57 addTreeWillExpandListener(listener);
58 addTreeSelectionListener(listener);
59 }
60
61 public TCTreeNode generateScriptsTree(){
62 return generateScriptsTree(createRootFileNode());
63 }
64
65 public TCTreeNode generateScriptsTree(FileNode rootFileNode){
66 TCTreeNode rootNode = (TCTreeNode)getModel().getRoot();
67 rootNode.removeAllChildren();
68 rootNode.setUserObject(rootFileNode);
69 addTreeToDir(rootFileNode.getFile(), rootNode);
70 updateUI();
71 return rootNode;
72 }
73
74 protected FileNode createRootFileNode(){
75 Configurator config = Configurator.getInstance();
76 String scriptDir = config.getValue(JameleonDefaultValues.SCRIPT_DIR_CONFIG_NAME,
77 JameleonDefaultValues.SCRIPT_DIR.getPath());
78 FileNode tcFn = new FileNode(new File(scriptDir), "Test Cases");
79 return tcFn;
80 }
81
82 protected void addTreeToDir(File file, DefaultMutableTreeNode parentNode){
83 if ( file.isDirectory() ) {
84 File[] childFiles = file.listFiles();
85 for (int i = 0; i < childFiles.length; i++) {
86 addChildToTree(childFiles[i], parentNode);
87 }
88 }
89 }
90
91 protected void addChildToTree(File file, DefaultMutableTreeNode parent){
92 FileNode fn = new FileNode(file, file.getName());
93 TCTreeNode node = new TCTreeNode(fn, file.isDirectory());
94 final int NON_EXISTENT = -1;
95 if ( parent.getIndex(node) == NON_EXISTENT &&
96 !"CVS".equals(file.getName()) &&
97 !".svn".equalsIgnoreCase(file.getName()) ) {
98 parent.add(node);
99 }
100 }
101
102 protected void genTestCaseDocs(final FileNode fn){
103 disableLogging();
104 new ProgressDialog(testCasePane.getJameleonUI(), SECONDS_TO_WAIT, testCasePane, fn.getFile());
105 }
106
107 protected void setTestCaseSource(final File f, final boolean activateSourceTab){
108 if (f.exists() && f.canRead()) {
109 StringBuffer contents = new StringBuffer();
110 BufferedReader in = null;
111 try{
112 in = new BufferedReader(new FileReader(f));
113 while (in.ready()) {
114 contents.append(in.readLine()).append("\n");
115 }
116 testCasePane.setTestCaseSource(contents.toString());
117 if (activateSourceTab) {
118 testCasePane.getTabbedPane().setSelectedIndex(TestCasePane.SOURCE_INDEX);
119 }
120 }catch(IOException ioe){
121 ioe.printStackTrace();
122 }finally{
123 try{
124 if (in != null){
125 in.close();
126 }
127 }catch(IOException ioe){
128
129 }
130 }
131 }
132 }
133
134 protected void disableLogging(){
135
136
137
138
139 TestCaseTagLibrary.setWarnOnNoPluginsFound(false);
140 }
141
142
143
144
145 public class TCTreeListener implements TreeWillExpandListener, TreeSelectionListener{
146
147 public void treeWillCollapse(TreeExpansionEvent event){
148 }
149
150 protected void addNodesToDir(TreePath path){
151 Object obj = path.getLastPathComponent();
152 if (obj instanceof TCTreeNode) {
153 TCTreeNode tn = (TCTreeNode)obj;
154 FileNode fn = (FileNode)tn.getUserObject();
155 if (fn.isDir()) {
156 tn.removeAllChildren();
157 addTreeToDir(fn.getFile(), tn);
158 ((DefaultTreeModel)getModel()).reload(tn);
159 }
160
161 }
162 }
163
164 public void treeWillExpand(TreeExpansionEvent event){
165 TreePath path = event.getPath();
166 addNodesToDir(path);
167 }
168
169 public void valueChanged(TreeSelectionEvent e) {
170 TreePath path = e.getNewLeadSelectionPath();
171 if (path != null) {
172 Object obj = path.getLastPathComponent();
173
174 if (obj instanceof TCTreeNode) {
175 TCTreeNode tn = (TCTreeNode)obj;
176 FileNode fn = (FileNode)tn.getUserObject();
177 if (!fn.isDir()) {
178 boolean isFrag = false;
179 if (fn.getFile().getName().endsWith("ml")) {
180 genTestCaseDocs(fn);
181 }else{
182 testCasePane.setTestCaseInfo(new TestCase());
183 isFrag = true;
184 }
185 setTestCaseSource(fn.getFile(), isFrag);
186 }
187 }
188 }
189 }
190 }
191
192 }