1 33 34 package edu.rice.cs.drjava.model; 35 36 import java.util.List ; 37 import java.util.LinkedList ; 38 import java.util.Hashtable ; 39 import javax.swing.text.*; 40 41 import edu.rice.cs.drjava.DrJava; 42 import edu.rice.cs.drjava.config.OptionConstants; 43 import edu.rice.cs.drjava.model.definitions.*; 44 import edu.rice.cs.util.Log; 45 import edu.rice.cs.util.swing.Utilities; 46 47 50 public class DefaultLightWeightParsingControl implements LightWeightParsingControl { 51 52 private AbstractGlobalModel _model; 53 54 55 private long _beginUpdates; 56 57 58 private long _lastDelay = System.currentTimeMillis(); 59 60 61 private Hashtable <OpenDefinitionsDocument, Long > _lastUpdates = new Hashtable <OpenDefinitionsDocument, Long >(); 62 63 64 private Hashtable <OpenDefinitionsDocument, String > _enclosingClassNames = new Hashtable <OpenDefinitionsDocument, String >(); 65 66 67 private volatile boolean _running = false; 68 69 70 private Object _restart = new Object (); 71 72 73 private LinkedList <LightWeightParsingListener> _listeners = new LinkedList <LightWeightParsingListener>(); 74 75 76 private static final Log _log = new Log("LightWeightParsing", false); 77 78 79 private ThreadGroup _updaterThreadGroup = new ThreadGroup ("Light-weight parsing updater thread group") { 80 public void uncaughtException(Thread t, Throwable e) { 81 _log.log("Uncaught exception in updater; disabled for rest of session", e); 82 new edu.rice.cs.drjava.ui.DrJavaErrorHandler().handle(e); 83 } 84 }; 85 86 87 private Thread _updater = new Thread (_updaterThreadGroup, new Runnable () { 88 public void run() { 89 while(true) { while (!_running) { 91 _log.log("Waiting..."); 92 try { 93 synchronized(_restart) { 94 if (!_running) { 95 _restart.wait(); 96 } 97 } 98 } 99 catch(InterruptedException e) { } 100 } 101 long current = System.currentTimeMillis(); 102 long delta = (_beginUpdates-current); 103 if (current>=_beginUpdates) { 105 OpenDefinitionsDocument doc = _model.getActiveDocument(); 106 Long last = _lastUpdates.get(doc); 107 if ((last==null) || (last<_lastDelay)) { 108 update(doc); 109 } 111 else { 112 } 114 delta = DrJava.getConfig().getSetting(OptionConstants.DIALOG_LIGHTWEIGHT_PARSING_DELAY).intValue(); 115 } 116 try { 118 Thread.sleep(delta); 119 } 120 catch (InterruptedException e) { } 121 } 122 } 123 }); 124 125 127 public DefaultLightWeightParsingControl(AbstractGlobalModel model) { 128 _model = model; 129 _updater.setDaemon(true); 130 _updater.start(); 131 } 132 133 134 public synchronized void update(final OpenDefinitionsDocument doc) { 135 _log.log("Update for "+doc); 136 try { 137 _lastUpdates.put(doc, System.currentTimeMillis()); 138 final String old = _enclosingClassNames.get(doc); 139 final String updated = doc.getEnclosingClassName(doc.getCaretPosition(), true); 140 if ((old==null) || (!old.equals(updated))) { 141 _enclosingClassNames.put(doc, updated); 142 Utilities.invokeLater(new Runnable () { 143 public void run() { 144 List <LightWeightParsingListener> listeners = getListeners(); 145 for (LightWeightParsingListener l: listeners) { l.enclosingClassNameUpdated(doc, old, updated); } 146 } 147 }); 148 } 149 } 150 catch(BadLocationException e) { } 151 catch(ClassNameNotFoundException e) { } 152 } 153 154 157 public void setAutomaticUpdates(boolean b) { 158 _log.log("setAutomaticUpdates("+b+")"); 159 _running = b; 160 if (b) { 161 delay(); 162 synchronized(_restart) { 163 _restart.notify(); 164 } 165 } 166 } 167 168 169 public void delay() { 170 _lastDelay = System.currentTimeMillis(); 171 _beginUpdates = _lastDelay + (DrJava.getConfig().getSetting(OptionConstants.DIALOG_LIGHTWEIGHT_PARSING_DELAY).intValue()); 172 } 173 174 175 public synchronized void reset() { 176 for(final OpenDefinitionsDocument doc: _enclosingClassNames.keySet()) { 177 final String old = _enclosingClassNames.get(doc); 178 Utilities.invokeLater(new Runnable () { 179 public void run() { 180 List <LightWeightParsingListener> listeners = getListeners(); 181 for (LightWeightParsingListener l: listeners) { l.enclosingClassNameUpdated(doc, old, null); } 182 } 183 }); 184 } 185 _enclosingClassNames.clear(); 186 _lastUpdates.clear(); 187 } 188 189 195 public synchronized String getEnclosingClassName(OpenDefinitionsDocument doc) { return _enclosingClassNames.get(doc); } 196 197 199 public synchronized void addListener(LightWeightParsingListener l) { 200 _listeners.add(l); 201 } 202 203 204 public synchronized void removeListener(LightWeightParsingListener l) { 205 _listeners.remove(l); 206 } 207 208 209 public synchronized void removeAllListeners() { 210 _listeners.clear(); 211 } 212 213 214 public synchronized List <LightWeightParsingListener> getListeners() { 215 return new LinkedList <LightWeightParsingListener>(_listeners); 216 } 217 } | Popular Tags |