1 6 7 package org.netbeans.modules.logger.listeners; 8 9 import javax.swing.event.DocumentListener ; 10 import javax.swing.event.DocumentEvent ; 11 12 18 public class DocListener implements DocumentListener { 19 20 DocManager manager; 21 DocTimer timer; 22 26 public boolean stopped; 27 28 29 33 public DocListener(DocManager manager) { 34 stopped = true; 35 timer = new DocTimer(this); 36 this.manager = manager; 37 } 38 39 43 public void insertUpdate(DocumentEvent e){ 44 changes(); 45 } 46 47 51 public void removeUpdate(DocumentEvent e){ 52 changes(); 53 } 54 55 59 public void changedUpdate(DocumentEvent e){ 60 } 61 62 66 private void changes(){ 67 if(stopped){ 68 timer.reset(); 69 new Thread (timer).start(); 70 stopped = false; 71 } 72 timer.action(); 73 } 74 75 78 synchronized protected void notifySave(){ 79 double strokes = new Integer (timer.strokeCounter).doubleValue(); 80 double seconds = new Long (timer.duration).doubleValue() / 1000; 81 double freq = strokes / seconds; 82 manager.logDoc(freq, timer.duration); 83 this.stopped = true; 84 } 85 86 89 synchronized protected void notifyDiscard(){ 90 this.stopped = true; 91 } 92 93 94 97 private class DocTimer implements Runnable { 98 99 private int waitCounter; 100 public int strokeCounter; 101 public long duration; 102 private DocListener doc; 103 107 private final static int WAITSEC = 5; 108 112 private static final long MINDURATION = 2*1000; 113 114 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 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 148 synchronized protected void action(){ 149 strokeCounter++; 150 waitCounter = WAITSEC; 151 } 152 153 } 154 155 } 156 | Popular Tags |