KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > piaget > analyze > TCRAnalyzer


1 /*
2  * TCRInspector.java
3  *
4  * Created on April 18, 2005, 12:01 PM
5  */

6
7 package org.netbeans.modules.piaget.analyze;
8
9 import java.util.Hashtable JavaDoc;
10 import java.util.Enumeration JavaDoc;
11 import java.io.File JavaDoc;
12 import java.util.HashSet JavaDoc;
13 import java.util.Iterator JavaDoc;
14 import org.netbeans.modules.piagetproject.property.Property;
15
16 /**
17  *
18  * @author loicsegapelli
19  */

20 public class TCRAnalyzer extends TCRParser{
21     
22     boolean editorInUse;
23     int tabSwitch, fields, constructors, methods, beanPatterns, tabCounter, maxTabs;
24     private Hashtable JavaDoc extensions, windows;
25     HashSet JavaDoc openedWin;
26     WindowData editorSum, previousWindow, activeB4LostFocus;
27     
28     /** Creates a new instance of TCRInspector */
29     public TCRAnalyzer(File JavaDoc f) {
30         super(f);
31     }
32     
33     protected void init(){
34         editorInUse = false;
35         windows = new Hashtable JavaDoc();
36         extensions = new Hashtable JavaDoc();
37         maxTabs = fields = constructors = methods = beanPatterns = tabCounter = tabSwitch = 0;
38         
39         editorSum = new WindowData(false, Property.EDITOR);
40         windows.put(Property.EDITOR, editorSum);
41         editorSum.chartColor = java.awt.Color.RED;
42         
43         WindowData outNB = new WindowData(false, Property.OUT);
44         windows.put(Property.OUT, outNB);
45         outNB.chartColor = java.awt.Color.RED;
46     }
47     
48     protected void analyzeNode(String JavaDoc parent, String JavaDoc node){
49         matchFieldName(parent);
50         matchFieldName(node);
51     }
52     
53     protected void analyzeOpen(String JavaDoc windowId, boolean editor){
54         WindowData w;
55         if(windows.containsKey((Object JavaDoc)windowId)){
56             w = (WindowData)windows.get((Object JavaDoc)windowId);
57         } else {
58             w = new WindowData(editor,windowId);
59             windows.put((Object JavaDoc)windowId,w);
60             if(editor){
61                 editorSum.open(timestamp);
62                 w.chartColor = java.awt.Color.BLUE;
63                 tabCounter++;
64                 if(tabCounter>maxTabs)maxTabs = tabCounter;
65                 extension(windowId);
66             }
67         }
68         w.open(timestamp);
69     }
70      
71     /* IDE has lost focus
72      */

73     protected void analyzeLOST(){
74         // artifact: we pretend that when the IDE is losing focus
75
// all windows are closed. We "re-open" them when focus
76
// is gained
77
Enumeration JavaDoc e = windows.elements();
78         openedWin = new HashSet JavaDoc();
79         while(e.hasMoreElements()){
80             WindowData w = (WindowData)e.nextElement();
81             if(w.openB==false) continue;
82             w.close(timestamp);
83             openedWin.add(w);
84         }
85         
86         activeB4LostFocus = previousWindow;
87         analyzeActivated(Property.OUT, false);
88     }
89     
90      /* IDE has gained focus
91      */

92     protected void analyzeGAINED(){
93         if(openedWin!=null){
94             Iterator JavaDoc it = openedWin.iterator();
95             while(it.hasNext()){
96                 WindowData w = (WindowData)it.next();
97                 w.open(timestamp);
98             }
99         }
100         if(activeB4LostFocus!=null) analyzeActivated(activeB4LostFocus.name,activeB4LostFocus.editor);
101         else if(previousWindow!=null)analyzeActivated(previousWindow.name,previousWindow.editor);
102     }
103
104     
105     protected void analyzeClose(String JavaDoc windowId, boolean editor){
106         WindowData w;
107         if(editor)tabCounter--;
108         if(windows.containsKey((Object JavaDoc)windowId)){
109             w = (WindowData)windows.get((Object JavaDoc)windowId);
110             w.close(timestamp);
111         } else {
112             myError("close without open:"+windowId);
113         }
114     }
115     
116     protected void analyzeActivated(String JavaDoc windowId, boolean editor){
117         WindowData currentWindow;
118         if(previousWindow!=null){
119             previousWindow.deactivated(timestamp);
120             editorSum.deactivated(timestamp);
121         }
122         if(!windows.containsKey((Object JavaDoc)windowId)) analyzeOpen(windowId,editor);
123         currentWindow = (WindowData)windows.get((Object JavaDoc)windowId);
124         currentWindow.activated(timestamp);
125         previousWindow = currentWindow;
126         if(editorInUse&&editor)tabSwitch++;
127         if(editor)editorSum.activated(timestamp);
128         editorInUse = editor;
129     }
130     
131     protected void eof(){
132         Enumeration JavaDoc e = windows.elements();
133         WindowData w;
134         while(e.hasMoreElements()){
135             w = (WindowData)e.nextElement();
136             w.end(timestamp);
137             //w.close(timestamp);
138
}
139         print();
140     }
141     
142         protected void myError(String JavaDoc msg){
143         error("ANALYZER:"+msg);
144     }
145     
146     private void matchFieldName(String JavaDoc s){
147         if(s==null) return;
148         if(s.equalsIgnoreCase("Fields")) {
149             fields++;
150             return;
151         }
152         if(s.equalsIgnoreCase("Constructors")) {
153             constructors++;
154             return;
155         }
156         if(s.equalsIgnoreCase("Methods")) {
157             methods++;
158             return;
159         }
160         if(s.equalsIgnoreCase("BeanPatterns")) {
161             beanPatterns++;
162             return;
163         }
164     }
165     
166     private void extension(String JavaDoc s){
167         int index = s.lastIndexOf('.');
168         if(index>0){
169             Object JavaDoc ext;
170             ext = (Object JavaDoc)s.substring(index+1);
171             int i = 1;
172             if(extensions.containsKey(ext)){
173                 i = ((Integer JavaDoc)extensions.get(ext)).intValue();
174                 i++;
175             }
176             extensions.put(ext,(Object JavaDoc)new Integer JavaDoc(i));
177         }
178     }
179     
180     private void printWindows(){
181         Enumeration JavaDoc e = windows.elements();
182         if(e.hasMoreElements()) Analyzer.writeComment("WINDOWS:");
183         while(e.hasMoreElements()){
184             WindowData w = (WindowData)e.nextElement();
185             Analyzer.writeComment(" * "+w.name+"\topen: "+prettyTime(w.open)+"\tactivated: "+prettyTime(w.activated));
186             String JavaDoc win = w.name;
187             // characters ':' and '=' are key characters for a property file,
188
// hence I remove them here
189
win = win.replaceAll(":", "");
190             win = win.replaceAll("=", "");
191             Analyzer.writeProperty(Property.WINDOW + win, w.open + " " + w.activated);
192         }
193     }
194     
195     private void printEditor(){
196         
197         Analyzer.writeComment("Max editor tabs simultaneously opened: "+maxTabs);
198         Analyzer.writeProperty(Property.MAX_TABS, Integer.toString(maxTabs));
199         
200         Analyzer.writeComment("Tab switches: " + tabSwitch);
201         Analyzer.writeProperty(Property.TAB_SWITCH, Integer.toString(tabSwitch));
202         
203         if(tabSwitch>0){
204             WindowData editor = (WindowData)windows.get(Property.EDITOR);
205             double time = new Long JavaDoc(editor.activated).doubleValue();
206             long freq = new Double JavaDoc(time/tabSwitch).longValue();
207             Analyzer.writeComment("tab switch period: "+prettyTime(freq));
208             Analyzer.writeProperty(Property.TAB_PERIOD, Long.toString(freq));
209         }
210     }
211     
212     private void printExtension(){
213         Enumeration JavaDoc e = extensions.keys();
214         String JavaDoc ext, count;
215         while(e.hasMoreElements()){
216             ext = (String JavaDoc)e.nextElement();
217             count = ((Integer JavaDoc)extensions.get((Object JavaDoc)ext)).toString();
218             Analyzer.writeComment("filetype extension: "+ext+"\t# of occurences:"+count);
219             Analyzer.writeProperty(Property.EXTENSION + ext, count);
220         }
221     }
222     
223     private void printGal(){
224         long duration = timestamp-startTime;
225         Analyzer.writeComment("Duration of session:" + prettyTime(duration));
226         Analyzer.writeProperty(Property.TOTAL_TIME, Long.toString(duration));
227     }
228     
229     private void printFields(){
230         Analyzer.writeComment("usage of Java class nodes - fields: "+fields+" constructors: "+constructors+" methods: "+methods+" beans: "+beanPatterns);
231         Analyzer.writeProperty(Property.CN_FIELDS, Integer.toString(fields));
232         Analyzer.writeProperty(Property.CN_METHODS, Integer.toString(methods));
233         Analyzer.writeProperty(Property.CN_CONS, Integer.toString(constructors));
234         Analyzer.writeProperty(Property.CN_BEANS, Integer.toString(beanPatterns));
235     }
236     
237     private void print(){
238         printGal();
239         printWindows();
240         printEditor();
241         printExtension();
242         printFields();
243     }
244     
245 }
246
Popular Tags