KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > logger > listeners > DocManager


1 /*
2  * DocManager.java
3  *
4  * Created on May 3, 2005, 10:23 AM
5  */

6
7 package org.netbeans.modules.logger.listeners;
8
9 import java.util.Hashtable JavaDoc;
10 import java.io.IOException JavaDoc;
11 import org.openide.loaders.DataObject;
12 import org.openide.cookies.*;
13
14 /**
15  * Listens to user keystrokes, computing his typing frequency. Also manages the
16  * mapping of user filenames to dummy names, keeping only the extension.
17  * @author loicsegapelli
18  */

19 public class DocManager extends ListenerTools {
20     
21     /**
22      * Mapping of real user filenames to dummy names
23      */

24     private Hashtable JavaDoc hashNames;
25     int counter;
26     long totalTime;
27     double sumFreq;
28     
29     /**
30      * Creates a new instance of DocManager
31      */

32     public DocManager() {
33         super("DOC");
34         hashNames = new Hashtable JavaDoc();
35         counter = 0;
36     }
37     
38     /**
39      * returns the dummy name associated with a <CODE>DataObject</CODE>
40      * @param d the original <CODE>DataObject</CODE>
41      * @return the name associated with <CODE>d</CODE>
42      */

43     public String JavaDoc getName(DataObject d){
44         if(hashNames.containsKey((Object JavaDoc)d)) return (String JavaDoc)hashNames.get((Object JavaDoc)d);
45         else {
46             String JavaDoc codeName = assignName(d);
47             EditorCookie ec = (EditorCookie)d.getCookie(EditorCookie.class);
48             ec.getDocument().addDocumentListener(new DocListener(this));
49             return codeName;
50         }
51     }
52     
53     /**
54      * Creates a new name for <CODE>d</CODE>
55      * @param d <CODE>DataObject</CODE> that needs a new name
56      * @return the new name
57      */

58     private String JavaDoc assignName(DataObject d){
59         String JavaDoc fileName = d.getPrimaryFile().getNameExt();
60         int dotIndex = fileName.lastIndexOf(".");
61         String JavaDoc extension = dotIndex!=-1 ? fileName.substring(dotIndex,fileName.length()) : "";
62         String JavaDoc out = "f"+new Integer JavaDoc(counter).toString()+extension;
63         hashNames.put((Object JavaDoc)d,(Object JavaDoc)out);
64         counter++;
65         return out;
66     }
67     
68     /**
69      * retrieves a name already assigned
70      * @param d the <CODE>DataObject</CODE> for wich we want a name
71      * @return the name assigned
72      */

73     public String JavaDoc findName(DataObject d){
74         if(hashNames.containsKey((Object JavaDoc)d)) return (String JavaDoc)hashNames.get((Object JavaDoc)d);
75         else return "";
76     }
77     
78     /*
79      *logs infos from the doc listener
80      */

81     public synchronized void logDoc(double freq, long time){
82         newBuffer();
83         append("\n"+System.currentTimeMillis());
84         append("f"+Double.toString(freq));
85         append("t"+Long.toString(time));
86         flush();
87     }
88 }
89
Popular Tags