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