View Javadoc

1   /*
2       Jameleon - An automation testing tool..
3       Copyright (C) 2003 Christian W. Hargraves (engrean@hotmail.com)
4   
5       This library is free software; you can redistribute it and/or
6       modify it under the terms of the GNU Lesser General Public
7       License as published by the Free Software Foundation; either
8       version 2.1 of the License, or (at your option) any later version.
9   
10      This library is distributed in the hope that it will be useful,
11      but WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13      Lesser General Public License for more details.
14  
15      You should have received a copy of the GNU Lesser General Public
16      License along with this library; if not, write to the Free Software
17      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */
19  package net.sf.jameleon.ui;
20  
21  import java.io.IOException;
22  import java.io.InvalidClassException;
23  import java.util.Collections;
24  import java.util.Enumeration;
25  import java.util.HashSet;
26  import java.util.Iterator;
27  import java.util.Map;
28  import java.util.Set;
29  import java.util.TreeSet;
30  
31  import javax.swing.JTree;
32  import javax.swing.event.TreeExpansionEvent;
33  import javax.swing.event.TreeSelectionEvent;
34  import javax.swing.event.TreeSelectionListener;
35  import javax.swing.event.TreeWillExpandListener;
36  import javax.swing.tree.DefaultMutableTreeNode;
37  import javax.swing.tree.DefaultTreeModel;
38  import javax.swing.tree.TreePath;
39  
40  import net.sf.jameleon.TestCaseTagLibrary;
41  import net.sf.jameleon.bean.FunctionalPoint;
42  import net.sf.jameleon.util.InstanceSerializer;
43  import net.sf.jameleon.util.SupportedTags;
44  
45  public class FunctionalPointTree extends JTree {
46  
47      private Set usedClasses;
48  
49      public FunctionalPointTree(FPDisplayer fpDisplayer) {
50          super();
51          final DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode("Functional Points");
52          buildTree(rootNode);
53  
54          DefaultTreeModel tm = new DefaultTreeModel(rootNode);
55          setModel(tm);
56          FPTreeListener listener = new FPTreeListener(fpDisplayer);
57          addTreeWillExpandListener(listener);
58          addTreeSelectionListener(listener);
59      }
60  
61      private void buildTree(final DefaultMutableTreeNode rootNode) {
62          Thread t = new Thread() {
63              public void run() {
64                  rootNode.removeAllChildren();
65                  TestCaseTagLibrary.resetTags();
66                  SupportedTags st = new SupportedTags();
67                  st.setWarnOnNoPluginsFile(true);
68                  //Get all supported tags
69                  Map tags = st.getSupportedTags();
70                  Set orderedClasses = Collections.synchronizedSet(new TreeSet(tags.values()));
71                  createSkeleton(orderedClasses, rootNode);
72                  populateTree(tags, rootNode, Thread.currentThread().getContextClassLoader());
73                  ((DefaultTreeModel)getModel()).reload(rootNode);
74              }
75          };
76          t.setContextClassLoader(Utils.createClassLoader());
77          t.start();
78      }
79  
80      protected void createSkeleton(Set classes, DefaultMutableTreeNode root) {
81          Iterator it = classes.iterator();
82          String packageName = null;
83  
84          String dirNode = null;
85          DefaultMutableTreeNode node, pNode, childNode = null;
86  
87          while ( it.hasNext() ) {
88              pNode = root;
89              packageName = (String)it.next();
90              for ( int index = packageName.indexOf('.'); index > -1; index = packageName.indexOf('.') ) {
91                  dirNode = packageName.substring(0,index);
92                  node = new DefaultMutableTreeNode(dirNode, true);
93                  boolean nodeNotFound = true;
94                  Enumeration e = pNode.children();
95                  String childString;
96                  while ( e.hasMoreElements() && nodeNotFound ) {
97                      childNode = (DefaultMutableTreeNode)e.nextElement();
98                      childString = (String)childNode.getUserObject();
99                      if ( childString.equals(dirNode) ) {
100                         nodeNotFound = false;
101                     }
102                 }
103                 if ( nodeNotFound ) {
104                     pNode.add(node);
105                     pNode = node;
106                 } else {
107                     pNode = childNode;
108                 }
109                 packageName = packageName.substring(index+1);
110             }
111         }
112 
113     }
114 
115     protected void populateTree(Map tags, DefaultMutableTreeNode root, ClassLoader cl) {
116         usedClasses = new HashSet();
117         Iterator it = tags.keySet().iterator();
118         String className = null;
119         while ( it.hasNext() ) {
120             className = (String)tags.get((String)it.next());
121             if ( usedClasses.add(className) ) {
122                 addFunctionalPointToTree(className,root, cl);
123             }
124         }
125     }
126 
127     protected void addFunctionalPointToTree(String qName, DefaultMutableTreeNode parent, ClassLoader cl) {
128         String dirNode = null;
129         String className = qName;
130         DefaultMutableTreeNode node = parent;
131         DefaultMutableTreeNode pNode = parent;
132         for ( int index = className.indexOf('.'); index > -1; index = className.indexOf('.') ) {
133             dirNode = className.substring(0,index);
134             node = new DefaultMutableTreeNode(dirNode, true);
135             DefaultMutableTreeNode nodeFound = null;
136             Enumeration e = pNode.children();
137             while ( e.hasMoreElements() ) {
138                 nodeFound = (DefaultMutableTreeNode)e.nextElement();
139                 if ( !nodeFound.isLeaf() || nodeFound.getUserObject() instanceof String ) {
140                     if ( ((String)nodeFound.getUserObject()).equals(dirNode) ) {
141                         node = nodeFound;
142                     }
143                 }
144             }
145             if ( pNode.getIndex(node) == -1 ) {
146                 pNode.add(node);
147             }
148             pNode = node;
149             className = className.substring(index+1);
150         }
151         String fileName = qName.replace('.', '/')+InstanceSerializer.SERIALIZED_EXT;
152         try {
153             FunctionalPoint fp = (FunctionalPoint)InstanceSerializer.deserialize(cl.getResourceAsStream(fileName));
154             node = new DefaultMutableTreeNode(fp, false);
155             if ( pNode.getIndex(node) == -1 ) {
156                 pNode.add(node);
157             }
158         } catch (InvalidClassException ice){
159             System.err.println("Could not load tag for "+qName+"!");
160             ice.printStackTrace();
161         } catch ( IOException ioe ) {
162             ioe.printStackTrace();//Simply can't read in the source dat file
163         } catch ( ClassNotFoundException cnfe ) {
164             cnfe.printStackTrace();
165             System.err.print(cnfe.getMessage());
166         } 
167     }
168 
169     public class FPTreeListener implements TreeWillExpandListener, TreeSelectionListener {
170 
171         protected FPDisplayer fpDisplayer;
172 
173         public FPTreeListener (FPDisplayer fpDisplayer) {
174             this.fpDisplayer = fpDisplayer;
175         }
176 
177         public void treeWillCollapse(TreeExpansionEvent event) {
178         }
179 
180         public void treeWillExpand(TreeExpansionEvent event) {
181             TreePath path = event.getPath();
182             if ( path.getParentPath() == null ) {
183                 DefaultMutableTreeNode rootNode = (DefaultMutableTreeNode)path.getLastPathComponent();
184                 buildTree(rootNode);
185             }
186         }
187 
188         public void valueChanged(TreeSelectionEvent e) {
189             TreePath path = e.getPath();
190             DefaultMutableTreeNode tn = (DefaultMutableTreeNode)path.getLastPathComponent();
191             Object obj = tn.getUserObject();
192             if ( obj instanceof FunctionalPoint ) {
193                 FunctionalPoint fp = (FunctionalPoint)obj;
194                 fpDisplayer.sendFunctionalPointInfoToUI(fp);
195             }
196         }
197     }
198 
199 }