Lines 40-48
Link Here
|
40 |
*/ |
40 |
*/ |
41 |
|
41 |
|
42 |
|
42 |
|
43 |
package org.netbeans.progress.module; |
43 |
package org.netbeans.modules.progress.spi; |
44 |
|
44 |
|
45 |
import java.awt.Component; |
45 |
import java.awt.Component; |
|
|
46 |
import java.awt.event.ActionEvent; |
46 |
import java.awt.event.ActionListener; |
47 |
import java.awt.event.ActionListener; |
47 |
import java.util.ArrayList; |
48 |
import java.util.ArrayList; |
48 |
import java.util.Collection; |
49 |
import java.util.Collection; |
Lines 54-72
Link Here
|
54 |
import java.util.logging.Logger; |
55 |
import java.util.logging.Logger; |
55 |
import javax.swing.SwingUtilities; |
56 |
import javax.swing.SwingUtilities; |
56 |
import javax.swing.Timer; |
57 |
import javax.swing.Timer; |
57 |
import org.netbeans.progress.spi.InternalHandle; |
58 |
import org.netbeans.progress.module.TrivialProgressUIWorkerProvider; |
58 |
import org.netbeans.progress.spi.ProgressEvent; |
|
|
59 |
import org.netbeans.progress.spi.ProgressUIWorker; |
60 |
import org.netbeans.progress.spi.ProgressUIWorkerProvider; |
61 |
import org.netbeans.progress.spi.ProgressUIWorkerWithModel; |
62 |
import org.netbeans.progress.spi.TaskModel; |
63 |
import org.openide.util.Lookup; |
59 |
import org.openide.util.Lookup; |
64 |
|
60 |
|
65 |
/** |
61 |
/** |
66 |
* |
62 |
* |
67 |
* @author Milos Kleint (mkleint@netbeans.org) |
63 |
* @author Milos Kleint (mkleint@netbeans.org) |
68 |
*/ |
64 |
*/ |
69 |
public /* final - because of tests */ class Controller implements Runnable, ActionListener { |
65 |
public /* final - because of tests */ class Controller { |
70 |
|
66 |
|
71 |
// non-private so that it can be accessed from the tests |
67 |
// non-private so that it can be accessed from the tests |
72 |
public static Controller defaultInstance; |
68 |
public static Controller defaultInstance; |
Lines 87-112
Link Here
|
87 |
|
83 |
|
88 |
/** Creates a new instance of Controller */ |
84 |
/** Creates a new instance of Controller */ |
89 |
public Controller(ProgressUIWorker comp) { |
85 |
public Controller(ProgressUIWorker comp) { |
90 |
this(); |
|
|
91 |
component = comp; |
86 |
component = comp; |
92 |
} |
|
|
93 |
protected Controller() { |
94 |
model = new TaskModel(); |
87 |
model = new TaskModel(); |
95 |
eventQueue = new ArrayList<ProgressEvent>(); |
88 |
eventQueue = new ArrayList<ProgressEvent>(); |
96 |
dispatchRunning = false; |
89 |
dispatchRunning = false; |
97 |
timer = new Timer(TIMER_QUANTUM, this); |
90 |
timer = new Timer(TIMER_QUANTUM, new ActionListener() { |
|
|
91 |
public void actionPerformed(ActionEvent e) { |
92 |
runNow(); |
93 |
} |
94 |
}); |
98 |
timer.setRepeats(false); |
95 |
timer.setRepeats(false); |
99 |
} |
96 |
} |
100 |
|
97 |
|
101 |
public static synchronized Controller getDefault() { |
98 |
public static synchronized Controller getDefault() { |
102 |
if (defaultInstance == null) { |
99 |
if (defaultInstance == null) { |
103 |
defaultInstance = new Controller(); |
100 |
defaultInstance = new Controller(null); |
104 |
} |
101 |
} |
105 |
return defaultInstance; |
102 |
return defaultInstance; |
106 |
} |
103 |
} |
107 |
|
104 |
|
108 |
// to be called on the default instance only.. |
105 |
// to be called on the default instance only.. |
109 |
Component getVisualComponent() { |
106 |
public Component getVisualComponent() { |
110 |
if (component == null) { |
107 |
if (component == null) { |
111 |
getProgressUIWorker(); |
108 |
getProgressUIWorker(); |
112 |
} |
109 |
} |
Lines 136-142
Link Here
|
136 |
return model; |
133 |
return model; |
137 |
} |
134 |
} |
138 |
|
135 |
|
139 |
public void start(InternalHandle handle) { |
136 |
void start(InternalHandle handle) { |
140 |
ProgressEvent event = new ProgressEvent(handle, ProgressEvent.TYPE_START, isWatched(handle)); |
137 |
ProgressEvent event = new ProgressEvent(handle, ProgressEvent.TYPE_START, isWatched(handle)); |
141 |
if (this == getDefault() && handle.getInitialDelay() > 100) { |
138 |
if (this == getDefault() && handle.getInitialDelay() > 100) { |
142 |
// default controller |
139 |
// default controller |
Lines 146-179
Link Here
|
146 |
} |
143 |
} |
147 |
} |
144 |
} |
148 |
|
145 |
|
149 |
public void finish(InternalHandle handle) { |
146 |
void finish(InternalHandle handle) { |
150 |
ProgressEvent event = new ProgressEvent(handle, ProgressEvent.TYPE_FINISH, isWatched(handle)); |
147 |
ProgressEvent event = new ProgressEvent(handle, ProgressEvent.TYPE_FINISH, isWatched(handle)); |
151 |
postEvent(event); |
148 |
postEvent(event); |
152 |
} |
149 |
} |
153 |
|
150 |
|
154 |
public void toIndeterminate(InternalHandle handle) { |
151 |
void toIndeterminate(InternalHandle handle) { |
155 |
ProgressEvent event = new ProgressEvent(handle, ProgressEvent.TYPE_SWITCH, isWatched(handle)); |
152 |
ProgressEvent event = new ProgressEvent(handle, ProgressEvent.TYPE_SWITCH, isWatched(handle)); |
156 |
postEvent(event); |
153 |
postEvent(event); |
157 |
} |
154 |
} |
158 |
|
155 |
|
159 |
public void toSilent(InternalHandle handle, String message) { |
156 |
void toSilent(InternalHandle handle, String message) { |
160 |
ProgressEvent event = new ProgressEvent(handle, ProgressEvent.TYPE_SILENT, isWatched(handle), message); |
157 |
ProgressEvent event = new ProgressEvent(handle, ProgressEvent.TYPE_SILENT, isWatched(handle), message); |
161 |
postEvent(event); |
158 |
postEvent(event); |
162 |
} |
159 |
} |
163 |
|
160 |
|
164 |
|
161 |
|
165 |
public void toDeterminate(InternalHandle handle) { |
162 |
void toDeterminate(InternalHandle handle) { |
166 |
ProgressEvent event = new ProgressEvent(handle, ProgressEvent.TYPE_SWITCH, isWatched(handle)); |
163 |
ProgressEvent event = new ProgressEvent(handle, ProgressEvent.TYPE_SWITCH, isWatched(handle)); |
167 |
postEvent(event); |
164 |
postEvent(event); |
168 |
} |
165 |
} |
169 |
|
166 |
|
170 |
public void progress(InternalHandle handle, String msg, |
167 |
void progress(InternalHandle handle, String msg, |
171 |
int units, double percentage, long estimate) { |
168 |
int units, double percentage, long estimate) { |
172 |
ProgressEvent event = new ProgressEvent(handle, msg, units, percentage, estimate, isWatched(handle)); |
169 |
ProgressEvent event = new ProgressEvent(handle, msg, units, percentage, estimate, isWatched(handle)); |
173 |
postEvent(event); |
170 |
postEvent(event); |
174 |
} |
171 |
} |
175 |
|
172 |
|
176 |
public ProgressEvent snapshot(InternalHandle handle, String msg, |
173 |
ProgressEvent snapshot(InternalHandle handle, String msg, |
177 |
int units, double percentage, long estimate) { |
174 |
int units, double percentage, long estimate) { |
178 |
if (handle.isInSleepMode()) { |
175 |
if (handle.isInSleepMode()) { |
179 |
return new ProgressEvent(handle, ProgressEvent.TYPE_SILENT, isWatched(handle), msg); |
176 |
return new ProgressEvent(handle, ProgressEvent.TYPE_SILENT, isWatched(handle), msg); |
Lines 182-188
Link Here
|
182 |
} |
179 |
} |
183 |
|
180 |
|
184 |
|
181 |
|
185 |
public void explicitSelection(InternalHandle handle) { |
182 |
void explicitSelection(InternalHandle handle) { |
186 |
InternalHandle old = model.getExplicitSelection(); |
183 |
InternalHandle old = model.getExplicitSelection(); |
187 |
model.explicitlySelect(handle); |
184 |
model.explicitlySelect(handle); |
188 |
Collection<ProgressEvent> evnts = new ArrayList<ProgressEvent>(); |
185 |
Collection<ProgressEvent> evnts = new ArrayList<ProgressEvent>(); |
Lines 194-200
Link Here
|
194 |
runImmediately(evnts); |
191 |
runImmediately(evnts); |
195 |
} |
192 |
} |
196 |
|
193 |
|
197 |
public void displayNameChange(InternalHandle handle, int units, double percentage, long estimate, String display) { |
194 |
void displayNameChange(InternalHandle handle, int units, double percentage, long estimate, String display) { |
198 |
Collection<ProgressEvent> evnts = new ArrayList<ProgressEvent>(); |
195 |
Collection<ProgressEvent> evnts = new ArrayList<ProgressEvent>(); |
199 |
evnts.add(new ProgressEvent(handle, null, units, percentage, estimate, isWatched(handle), display)); |
196 |
evnts.add(new ProgressEvent(handle, null, units, percentage, estimate, isWatched(handle), display)); |
200 |
runImmediately(evnts); |
197 |
runImmediately(evnts); |
Lines 204-212
Link Here
|
204 |
return model.getExplicitSelection() == hndl; |
201 |
return model.getExplicitSelection() == hndl; |
205 |
} |
202 |
} |
206 |
|
203 |
|
207 |
/** |
|
|
208 |
* |
209 |
*/ |
210 |
void runImmediately(Collection<ProgressEvent> events) { |
204 |
void runImmediately(Collection<ProgressEvent> events) { |
211 |
synchronized (this) { |
205 |
synchronized (this) { |
212 |
// need to add to queue immediately in the current thread |
206 |
// need to add to queue immediately in the current thread |
Lines 215-223
Link Here
|
215 |
} |
209 |
} |
216 |
// trigger ui update as fast as possible. |
210 |
// trigger ui update as fast as possible. |
217 |
if (SwingUtilities.isEventDispatchThread()) { |
211 |
if (SwingUtilities.isEventDispatchThread()) { |
218 |
run(); |
212 |
runNow(); |
219 |
} else { |
213 |
} else { |
220 |
SwingUtilities.invokeLater(this); |
214 |
SwingUtilities.invokeLater(new Runnable() { |
|
|
215 |
public void run() { |
216 |
runNow(); |
217 |
} |
218 |
}); |
221 |
} |
219 |
} |
222 |
|
220 |
|
223 |
} |
221 |
} |
Lines 256-265
Link Here
|
256 |
} |
254 |
} |
257 |
|
255 |
|
258 |
|
256 |
|
259 |
/** |
257 |
public void runNow() { |
260 |
* can be run from awt only. |
258 |
// not true in tests: assert EventQueue.isDispatchThread(); |
261 |
*/ |
|
|
262 |
public void run() { |
263 |
HashMap<InternalHandle, ProgressEvent> map = new HashMap<InternalHandle, ProgressEvent>(); |
259 |
HashMap<InternalHandle, ProgressEvent> map = new HashMap<InternalHandle, ProgressEvent>(); |
264 |
boolean hasShortOne = false; |
260 |
boolean hasShortOne = false; |
265 |
long minDiff = TIMER_QUANTUM; |
261 |
long minDiff = TIMER_QUANTUM; |
Lines 284-290
Link Here
|
284 |
{ |
280 |
{ |
285 |
model.removeHandle(event.getSource()); |
281 |
model.removeHandle(event.getSource()); |
286 |
} |
282 |
} |
287 |
ProgressEvent lastEvent = (ProgressEvent)map.get(event.getSource()); |
283 |
ProgressEvent lastEvent = map.get(event.getSource()); |
288 |
if (lastEvent != null && event.getType() == ProgressEvent.TYPE_FINISH && |
284 |
if (lastEvent != null && event.getType() == ProgressEvent.TYPE_FINISH && |
289 |
justStarted.contains(event.getSource()) && isShort) |
285 |
justStarted.contains(event.getSource()) && isShort) |
290 |
{ |
286 |
{ |
Lines 316-322
Link Here
|
316 |
model.addHandle(hndl); |
312 |
model.addHandle(hndl); |
317 |
} else { |
313 |
} else { |
318 |
eventQueue.add(new ProgressEvent(hndl, ProgressEvent.TYPE_START, isWatched(hndl))); |
314 |
eventQueue.add(new ProgressEvent(hndl, ProgressEvent.TYPE_START, isWatched(hndl))); |
319 |
ProgressEvent evnt = (ProgressEvent)map.remove(hndl); |
315 |
ProgressEvent evnt = map.remove(hndl); |
320 |
if (evnt.getType() != ProgressEvent.TYPE_START) { |
316 |
if (evnt.getType() != ProgressEvent.TYPE_START) { |
321 |
eventQueue.add(evnt); |
317 |
eventQueue.add(evnt); |
322 |
} |
318 |
} |
Lines 350-362
Link Here
|
350 |
} |
346 |
} |
351 |
} |
347 |
} |
352 |
|
348 |
|
353 |
/** |
|
|
354 |
* used by Timer |
355 |
*/ |
356 |
public void actionPerformed(java.awt.event.ActionEvent actionEvent) { |
357 |
run(); |
358 |
} |
359 |
|
360 |
|
361 |
|
362 |
} |
349 |
} |