-
Notifications
You must be signed in to change notification settings - Fork 51
/
DeferredUpdateManager.java
382 lines (346 loc) · 9.1 KB
/
DeferredUpdateManager.java
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
/*******************************************************************************
* Copyright (c) 2000, 2024 IBM Corporation and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.draw2d;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.eclipse.swt.SWT;
import org.eclipse.swt.SWTException;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.widgets.Display;
import org.eclipse.draw2d.geometry.Rectangle;
/**
* An UpdateManager that asynchronously updates the affected figures.
*/
public class DeferredUpdateManager extends UpdateManager {
/**
* Calls {@link DeferredUpdateManager#performUpdate()}.
*/
protected class UpdateRequest implements Runnable {
public UpdateRequest() {
// this constructor is here for the visibility of the constructor of this class
}
/**
* Calls {@link DeferredUpdateManager#performUpdate()}.
*/
@Override
public void run() {
performUpdate();
}
}
private Rectangle damage;
private Map<IFigure, Rectangle> dirtyRegions = new HashMap<>();
private GraphicsSource graphicsSource;
private final List<IFigure> invalidFigures = new ArrayList<>();
private IFigure root;
private boolean updateQueued;
private boolean updating;
private boolean validating;
private RunnableChain afterUpdate;
private int refreshRate = -1;
private static class RunnableChain {
RunnableChain next;
Runnable run;
RunnableChain(Runnable run, RunnableChain next) {
this.run = run;
this.next = next;
}
void run() {
if (next != null) {
next.run();
}
run.run();
}
}
/**
* Empty constructor.
*/
public DeferredUpdateManager() {
}
/**
* Constructs a new DererredUpdateManager with the given GraphicsSource.
*
* @param gs the graphics source
*/
public DeferredUpdateManager(GraphicsSource gs) {
setGraphicsSource(gs);
}
/**
* Adds a dirty region (defined by the rectangle <i>x, y, w, h</i>) to the
* update queue. If the figure isn't visible or either the width or height are
* 0, the method returns without queueing the dirty region.
*
* @param figure the figure that contains the dirty region
* @param x the x coordinate of the dirty region
* @param y the y coordinate of the dirty region
* @param w the width of the dirty region
* @param h the height of the dirty region
*/
@Override
public synchronized void addDirtyRegion(IFigure figure, int x, int y, int w, int h) {
if (w == 0 || h == 0 || !figure.isShowing()) {
return;
}
Rectangle rect = dirtyRegions.get(figure);
if (rect == null) {
rect = new Rectangle(x, y, w, h);
dirtyRegions.put(figure, rect);
} else {
rect.union(x, y, w, h);
}
queueWork();
}
/**
* Adds the given figure to the update queue. Invalid figures will be validated
* before the damaged regions are repainted.
*
* @param f the invalid figure
*/
@Override
public synchronized void addInvalidFigure(IFigure f) {
if (invalidFigures.contains(f)) {
return;
}
queueWork();
invalidFigures.add(f);
}
/**
* Returns a Graphics object for the given region.
*
* @param region the region to be repainted
* @return the Graphics object
*/
protected Graphics getGraphics(Rectangle region) {
if (graphicsSource == null) {
return null;
}
return graphicsSource.getGraphics(region);
}
/**
* @since 3.10
*/
@Override
protected void paint(GC gc) {
if (!validating) {
SWTGraphics graphics = new SWTGraphics(gc);
if (!updating) {
/**
* If a paint occurs not as part of an update, we should notify that the region
* is being painted. Otherwise, notification already occurs in repairDamage().
*/
Rectangle rect = graphics.getClip(new Rectangle());
HashMap<IFigure, Rectangle> map = new HashMap<>();
map.put(root, rect);
firePainting(rect, map);
}
performValidation();
root.paint(graphics);
graphics.dispose();
} else {
/*
* If figures are being validated then we can simply add a dirty region here and
* update will repaint this region with other dirty regions when it gets to
* painting. We can't paint if we're not sure that all figures are valid.
*/
addDirtyRegion(root, new Rectangle(gc.getClipping()));
}
}
/**
* Performs the update. Validates the invalid figures and then repaints the
* dirty regions.
*
* @see #validateFigures()
* @see #repairDamage()
*/
@Override
public synchronized void performUpdate() {
if (isDisposed() || updating) {
return;
}
updating = true;
try {
performValidation();
updateQueued = false;
repairDamage();
if (afterUpdate != null) {
RunnableChain chain = afterUpdate;
afterUpdate = null;
chain.run(); // chain may queue additional Runnable.
if (afterUpdate != null) {
queueWork();
}
}
} finally {
updating = false;
}
}
/**
* @see UpdateManager#performValidation()
*/
@Override
public synchronized void performValidation() {
if (invalidFigures.isEmpty() || validating) {
return;
}
try {
IFigure fig;
validating = true;
fireValidating();
for (int i = 0; i < invalidFigures.size(); i++) {
fig = invalidFigures.get(i);
invalidFigures.set(i, null);
fig.validate();
}
} finally {
invalidFigures.clear();
validating = false;
}
}
/**
* Adds the given exposed region to the update queue and then performs the
* update.
*
* @param exposed the exposed region
*/
@Override
public synchronized void performUpdate(Rectangle exposed) {
addDirtyRegion(root, exposed);
performUpdate();
}
/**
* Posts an {@link UpdateRequest} using {@link Display#asyncExec(Runnable)}. If
* work has already been queued, a new request is not needed.
*/
protected void queueWork() {
if (!updateQueued) {
sendUpdateRequest();
updateQueued = true;
}
}
/**
* Fires the <code>UpdateRequest</code> to the current display asynchronously.
*
* @since 3.2
*/
protected void sendUpdateRequest() {
Display display = Display.getCurrent();
if (display == null) {
throw new SWTException(SWT.ERROR_THREAD_INVALID_ACCESS);
}
if (refreshRate <= 0) {
display.asyncExec(new UpdateRequest());
} else {
display.timerExec(refreshRate, new UpdateRequest());
}
}
/**
* Releases the graphics object, which causes the GraphicsSource to flush.
*
* @param graphics the graphics object
*/
protected void releaseGraphics(Graphics graphics) {
graphics.dispose();
graphicsSource.flushGraphics(damage);
}
/**
* Repaints the dirty regions on the update queue and calls
* {@link UpdateManager#firePainting(Rectangle, Map)}, unless there are no dirty
* regions.
*/
protected void repairDamage() {
dirtyRegions.forEach((figure, contribution) -> {
IFigure walker = figure.getParent();
// A figure can't paint beyond its own bounds
contribution.intersect(figure.getBounds());
while (!contribution.isEmpty() && walker != null) {
walker.translateToParent(contribution);
contribution.intersect(walker.getBounds());
walker = walker.getParent();
}
if (damage == null) {
damage = new Rectangle(contribution);
} else {
damage.union(contribution);
}
});
if (!dirtyRegions.isEmpty()) {
Map<IFigure, Rectangle> oldRegions = dirtyRegions;
dirtyRegions = new HashMap<>();
firePainting(damage, oldRegions);
}
if (damage != null && !damage.isEmpty()) {
Graphics graphics = getGraphics(damage);
if (graphics != null) {
root.paint(graphics);
releaseGraphics(graphics);
}
}
damage = null;
}
/**
* Adds the given runnable and queues an update if an update is not under
* progress.
*
* @param runnable the runnable
*/
@Override
public synchronized void runWithUpdate(Runnable runnable) {
afterUpdate = new RunnableChain(runnable, afterUpdate);
if (!updating) {
queueWork();
}
}
/**
* Sets the graphics source.
*
* @param gs the graphics source
*/
@Override
public void setGraphicsSource(GraphicsSource gs) {
graphicsSource = gs;
}
/**
* Sets the root figure.
*
* @param figure the root figure
*/
@Override
public void setRoot(IFigure figure) {
root = figure;
}
/**
* Sets the rate with paint requests are executed. If set to either {@code 0} or
* a negative value, requests are executed as fast as possible (default
* behavior), otherwise every {@code refreshRate}ms.
*
* Example:
*
* <pre>
* setRefreshRate(500); // Paints every 500ms
* </pre>
*
* @param refreshRate The rate with which paint requests are executed.
* @since 3.18
*/
public void setRefreshRate(int refreshRate) {
this.refreshRate = refreshRate;
}
/**
* Validates all invalid figures on the update queue and calls
* {@link UpdateManager#fireValidating()} unless there are no invalid figures.
*/
protected void validateFigures() {
performValidation();
}
}