KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * MyDocumentListener.java
3  *
4  * Created on May 2, 2005, 4:14 PM
5  */

6
7 package org.netbeans.modules.logger.listeners;
8
9 import javax.swing.event.DocumentListener JavaDoc;
10 import javax.swing.event.DocumentEvent JavaDoc;
11
12 /**
13  * Listens to changes happening within the Editor on some document. Basically, it
14  * computes the typing frequency of the user and logs it together with the typing
15  * duration every time the user stops typing.
16  * @author loicsegapelli
17  */

18 public class DocListener implements DocumentListener JavaDoc{
19     
20     DocManager manager;
21     DocTimer timer;
22     /**
23      * This boolean tells if it is necessary to restart a Thread to count the time
24      * elapsed
25      */

26     public boolean stopped;
27     
28     
29     /**
30      * Creates a new instance of DocListener
31      * @param manager This reference is needed for logging the information.
32      */

33     public DocListener(DocManager manager) {
34         stopped = true;
35         timer = new DocTimer(this);
36         this.manager = manager;
37     }
38     
39     /**
40      * change fired
41      * @param e even
42      */

43     public void insertUpdate(DocumentEvent JavaDoc e){
44         changes();
45     }
46     
47     /**
48      * change fired
49      * @param e event
50      */

51     public void removeUpdate(DocumentEvent JavaDoc e){
52         changes();
53     }
54     
55     /**
56      * change fired
57      * @param e event
58      */

59     public void changedUpdate(DocumentEvent JavaDoc e){
60     }
61     
62     /*
63      * some typing / insertion has occured: if the timer is stopped we start
64      * it once more
65      */

66     private void changes(){
67         if(stopped){
68             timer.reset();
69             new Thread JavaDoc(timer).start();
70             stopped = false;
71         }
72         timer.action();
73     }
74     
75     /*
76      * The frequency recorded is worth saving
77      */

78     synchronized protected void notifySave(){
79         double strokes = new Integer JavaDoc(timer.strokeCounter).doubleValue();
80         double seconds = new Long JavaDoc(timer.duration).doubleValue() / 1000;
81         double freq = strokes / seconds;
82         manager.logDoc(freq, timer.duration);
83         this.stopped = true;
84     }
85     
86     /*
87      * The frequency recorded is NOT worth saving
88      */

89     synchronized protected void notifyDiscard(){
90         this.stopped = true;
91     }
92     
93     
94     /*
95      * This class is here for counting the time elapsed while the user is typing
96      */

97     private class DocTimer implements Runnable JavaDoc {
98         
99         private int waitCounter;
100         public int strokeCounter;
101         public long duration;
102         private DocListener doc;
103         /*
104          * We wait this amount of time before deciding that the user has stopped
105          * typing
106          */

107         private final static int WAITSEC = 5;
108         /*
109          * minimum duration during which the user must be typing to be
110          * declared woth logging
111          */

112         private static final long MINDURATION = 2*1000;
113         
114         /** Creates a new instance of DocTimer */
115         protected DocTimer(DocListener doc) {
116             this.doc = doc;
117         }
118         
119         synchronized protected void reset(){
120             strokeCounter = 0;
121         }
122         
123         public void run(){
124             long startTime = System.currentTimeMillis();
125             waitCounter = WAITSEC;
126             while(waitCounter>0){
127                 try {
128                     Thread.sleep(1000);
129                     waitCounter--;
130                 } catch (InterruptedException JavaDoc e) {
131                     ListenerTools.logError(e);
132                 }
133             }
134             
135             duration = System.currentTimeMillis() - startTime;
136             duration -= WAITSEC*1000;
137             
138             if(duration>MINDURATION){
139                 doc.notifySave();
140             } else {
141                 doc.notifyDiscard();
142             }
143         }
144         
145      /*
146       * as long as some action occurs the counter is reset to WAIT
147      */

148         synchronized protected void action(){
149             strokeCounter++;
150             waitCounter = WAITSEC;
151         }
152         
153     }
154     
155 }
156
Popular Tags