-
Notifications
You must be signed in to change notification settings - Fork 51
/
Thumbnail.java
634 lines (562 loc) · 17.4 KB
/
Thumbnail.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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
/*******************************************************************************
* 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.parts;
import java.util.Map;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Display;
import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.SWTGraphics;
import org.eclipse.draw2d.UpdateListener;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Rectangle;
/**
* A Thumbnail is a Figure that displays an image of its source Figure at a
* smaller size. The Thumbnail will maintain the aspect ratio of the source
* Figure.
*
* @author Eric Bordeau
* @author Alexander Nyßen (anyssen)
*/
public class Thumbnail extends Figure implements UpdateListener {
// Bug on Mac - images are cached
// See https://bugs.eclipse.org/bugs/show_bug.cgi?id=543796
// This affects macOS 10.14 and greater but we'll patch it for all macOS
// versions
private static final boolean IS_MAC = "cocoa".equals(SWT.getPlatform()); //$NON-NLS-1$
/**
* This updates the Thumbnail by breaking the thumbnail {@link Image} into
* several tiles and updating each tile individually.
*/
class ThumbnailUpdater implements Runnable {
private static final int MIN_TILE_SIZE = 256;
private static final int MAX_NUMBER_OF_TILES = 16;
private int currentHTile;
private int currentVTile;
private int hTiles;
private int vTiles;
private Dimension tileSize;
private Dimension sourceSize; // the source size that was used for the
// tileSize computation
private boolean isActive = true;
private boolean isRunning = false;
private Image tileImage;
private Dimension tileImageSize;
// GC and Graphics to let the source figure paint on the tile image
private GC tileGC;
private SWTGraphics tileGraphics;
// GC used to copy from the tile image into the thumbnail image
private GC thumbnailGC;
/** Stops the updater and disposes of any resources. */
public void deactivate() {
setActive(false);
stop();
if (thumbnailImage != null) {
thumbnailImage.dispose();
thumbnailImage = null;
thumbnailImageSize = null;
}
}
/**
* Returns the current horizontal tile index.
*
* @return current horizontal tile index.
*/
protected int getCurrentHTile() {
return currentHTile;
}
/**
* Returns the current vertical tile index.
*
* @return current vertical tile index.
*/
protected int getCurrentVTile() {
return currentVTile;
}
/**
* Returns <code>true</code> if this ThumbnailUpdater is active. An inactive
* updater has disposed of its {@link Image}. The updater may be active and not
* currently running.
*
* @return <code>true</code> if this ThumbnailUpdater is active
*/
public boolean isActive() {
return isActive;
}
/**
* Returns <code>true</code> if this is currently running and updating at least
* one tile on the thumbnail {@link Image}.
*
* @return <code>true</code> if this is currently running
*/
public boolean isRunning() {
return isRunning;
}
/**
* Resets the number of vertical and horizontal tiles, as well as the tile size
* and current tile index.
*/
public void resetTileValues() {
// Keep track of source size that matches the computed tile size.
sourceSize = getSourceRectangle().getSize();
// Compute number of horizontal and vertical tiles and the size of
// each tile (while the last tile in horizontal and vertical
// direction may be smaller); ensure that all tiles except those on
// the bottom and right border will have at least a size of
// MIN_TILE_SIZE size and that at most MAX_NUMBER_OF_TILES tiles
// will be created.
hTiles = Math.min((int) Math.ceil((float) sourceSize.width / (float) MIN_TILE_SIZE), MAX_NUMBER_OF_TILES);
vTiles = Math.min((int) Math.ceil((float) sourceSize.height / (float) MIN_TILE_SIZE), MAX_NUMBER_OF_TILES);
tileSize = new Dimension((int) Math.ceil((float) sourceSize.width / (float) hTiles),
(int) Math.ceil((float) sourceSize.height / (float) vTiles));
// Reset the current indices so that the next update will start with
// the first tile in horizontal and vertical direction
currentHTile = 0;
currentVTile = 0;
}
/** Restarts the updater. */
public void restart() {
stop();
start();
}
/**
* Updates the current tile on the Thumbnail. An area of the source Figure is
* painted to an {@link Image}. That Image is then drawn on the Thumbnail.
* Scaling of the source Image is done inside
* {@link GC#drawImage(Image, int, int, int, int, int, int, int, int)} since the
* source and target sizes are different. The current tile indexes are
* incremented and if more updating is necesary, this {@link Runnable} is called
* again in a {@link Display#timerExec(int, Runnable)}. If no more updating is
* required, {@link #stop()} is called.
*/
@Override
public void run() {
if (!isActive() || !isRunning() || tileGraphics == null) {
return;
}
int v = getCurrentVTile();
int sy1 = v * tileSize.height;
int sy2 = Math.min((v + 1) * tileSize.height, sourceSize.height);
int h = getCurrentHTile();
int sx1 = h * tileSize.width;
int sx2 = Math.min((h + 1) * tileSize.width, sourceSize.width);
// Mac fix - create new Tile Graphics instances
if (IS_MAC) {
createTileGraphics();
}
tileGraphics.pushState();
// clear the background (by filling with the background color)
Rectangle rect = new Rectangle(0, 0, sx2 - sx1, sy2 - sy1);
tileGraphics.fillRectangle(rect);
// Let the source figure paint into the tile image.
// IMPORTANT (fix for bug #309912): we do not let the source figure
// paint directly into the thumbnail image, because we cannot ensure
// that it paints completely inside the current tile area (it may
// set its own clip inside paint(Graphics) and overwrite areas of
// tiles that have already been rendered. By providing an own tile
// image and copying from it into the thumbnail image, we are safe.
org.eclipse.draw2d.geometry.Point p = getSourceRectangle().getLocation();
tileGraphics.translate(-p.x * getScaleX() - sx1, -p.y * getScaleY() - sy1);
tileGraphics.scale(getScaleX());
sourceFigure.paint(tileGraphics);
tileGraphics.popState();
// Copy the painted tile image into the thumbnail image.
thumbnailGC.drawImage(tileImage, 0, 0, sx2 - sx1, sy2 - sy1, sx1, sy1, sx2 - sx1, sy2 - sy1);
if (getCurrentHTile() < (hTiles - 1)) {
setCurrentHTile(getCurrentHTile() + 1);
} else {
setCurrentHTile(0);
if (getCurrentVTile() < (vTiles - 1)) {
setCurrentVTile(getCurrentVTile() + 1);
} else {
setCurrentVTile(0);
}
}
if (getCurrentHTile() != 0 || getCurrentVTile() != 0) {
Display.getCurrent().asyncExec(this);
} else if (isDirty()) {
setDirty(false);
Display.getCurrent().asyncExec(this);
repaint();
} else {
stop();
repaint();
}
}
/**
* Sets the active flag.
*
* @param value The active value
*/
public void setActive(boolean value) {
isActive = value;
}
/**
* Sets the current horizontal tile index.
*
* @param count current horizontal tile index
*/
protected void setCurrentHTile(int count) {
currentHTile = count;
}
/**
* Sets the current vertical tile index.
*
* @param count current vertical tile index
*/
protected void setCurrentVTile(int count) {
currentVTile = count;
}
/**
* Starts this updater. This method initializes all the necessary resources and
* puts this {@link Runnable} on the asynch queue. If this updater is not active
* or is already running, this method just returns.
*/
public void start() {
if (!isActive() || isRunning()) {
return;
}
isRunning = true;
setDirty(false);
resetTileValues();
if (!targetSize.equals(thumbnailImageSize)) {
resetThumbnailImage();
}
if (targetSize.isEmpty()) {
return;
}
thumbnailGC = new GC(thumbnailImage, SWT.NONE);
if (!tileSize.equals(tileImageSize)) {
resetTileImage();
}
createTileGraphics();
setScales(targetSize.width / (float) sourceSize.width, targetSize.height / (float) sourceSize.height);
if (refreshRate <= 0) {
Display.getCurrent().asyncExec(this);
} else {
Display.getCurrent().timerExec(refreshRate, this);
}
}
/** Create new GC, SWTGraphics, and ScaledGraphics instances */
private void createTileGraphics() {
// For the Mac fixe we have to create a new GC instance to flush the previous
// tile image...
if (tileGC != null && !tileGC.isDisposed()) {
tileGC.dispose();
}
tileGC = new GC(tileImage, sourceFigure.isMirrored() ? SWT.RIGHT_TO_LEFT : SWT.NONE);
// ...and this means we need a new SWTGraphics instance
if (tileGraphics != null) {
tileGraphics.dispose();
}
tileGraphics = new SWTGraphics(tileGC);
Color color = sourceFigure.getForegroundColor();
if (color != null) {
tileGraphics.setForegroundColor(color);
}
color = sourceFigure.getBackgroundColor();
if (color != null) {
tileGraphics.setBackgroundColor(color);
}
tileGraphics.setFont(sourceFigure.getFont());
}
private void resetThumbnailImage() {
if (thumbnailImage != null) {
thumbnailImage.dispose();
}
if (!targetSize.isEmpty()) {
thumbnailImage = new Image(Display.getDefault(), targetSize.width, targetSize.height);
thumbnailImageSize = new Dimension(targetSize);
} else {
thumbnailImage = null;
thumbnailImageSize = new Dimension(0, 0);
}
}
private void resetTileImage() {
if (tileImage != null) {
tileImage.dispose();
}
if (!tileSize.isEmpty()) {
tileImage = new Image(Display.getDefault(), tileSize.width, tileSize.height);
tileImageSize = new Dimension(tileSize);
} else {
tileImage = null;
tileImageSize = new Dimension(0, 0);
}
}
/**
* Stops this updater. Also disposes of resources (except the thumbnail image
* which is still needed for painting).
*/
public void stop() {
isRunning = false;
if (tileGraphics != null) {
tileGraphics.dispose();
tileGraphics = null;
}
if (tileGC != null) {
tileGC.dispose();
tileGC = null;
}
if (thumbnailGC != null) {
thumbnailGC.dispose();
thumbnailGC = null;
}
if (tileImage != null) {
tileImage.dispose();
tileImage = null;
tileImageSize = null;
}
// Don't dispose of the thumbnail image since it is needed to paint
// the figure when the source is not dirty (i.e. showing/hiding the
// dock).
}
}
private boolean isDirty;
private float scaleX;
private float scaleY;
private IFigure sourceFigure;
Dimension targetSize = new Dimension(0, 0);
private Image thumbnailImage;
private Dimension thumbnailImageSize;
private final ThumbnailUpdater updater = new ThumbnailUpdater();
private int refreshRate = -1;
/**
* Creates a new Thumbnail. The source Figure must be set separately if you use
* this constructor.
*/
public Thumbnail() {
}
/**
* Creates a new Thumbnail with the given IFigure as its source figure.
*
* @param fig The source figure
*/
public Thumbnail(IFigure fig) {
this();
setSource(fig);
}
private Dimension adjustToAspectRatio(Dimension size, boolean adjustToMaxDimension) {
Dimension sourceSize = getSourceRectangle().getSize();
Dimension borderSize = new Dimension(getInsets().getWidth(), getInsets().getHeight());
size.expand(borderSize.getNegated());
int width;
int height;
if (adjustToMaxDimension) {
width = Math.max(size.width, (int) (size.height * sourceSize.width / (float) sourceSize.height + 0.5));
height = Math.max(size.height, (int) (size.width * sourceSize.height / (float) sourceSize.width + 0.5));
} else {
width = Math.min(size.width, (int) (size.height * sourceSize.width / (float) sourceSize.height + 0.5));
height = Math.min(size.height, (int) (size.width * sourceSize.height / (float) sourceSize.width + 0.5));
}
size.width = width;
size.height = height;
return size.expand(borderSize);
}
/** Deactivates this Thumbnail. */
public void deactivate() {
sourceFigure.getUpdateManager().removeUpdateListener(this);
updater.deactivate();
}
/**
* Returns the preferred size of this Thumbnail. The preferred size will be
* calculated in a way that maintains the source Figure's aspect ratio.
*
* @param wHint The width hint
* @param hHint The height hint
* @return The preferred size
*/
@Override
public Dimension getPreferredSize(int wHint, int hHint) {
if (prefSize == null) {
return adjustToAspectRatio(getBounds().getSize(), false);
}
Dimension preferredSize = adjustToAspectRatio(prefSize.getCopy(), true);
if (maxSize == null) {
return preferredSize;
}
Dimension maximumSize = adjustToAspectRatio(maxSize.getCopy(), true);
if (preferredSize.contains(maximumSize)) {
return maximumSize;
}
return preferredSize;
}
/**
* Returns the scale factor on the X-axis.
*
* @return X scale
*/
protected float getScaleX() {
return scaleX;
}
/**
* Returns the scale factor on the Y-axis.
*
* @return Y scale
*/
protected float getScaleY() {
return scaleY;
}
/**
* Returns the source figure being used to generate a thumbnail.
*
* @return the source figure
*/
protected IFigure getSource() {
return sourceFigure;
}
/**
* Returns the rectangular region relative to the source figure which will be
* the basis of the thumbnail. The value may be returned by reference and should
* not be modified by the caller.
*
* @since 3.1
* @return the region of the source figure being used for the thumbnail
*/
protected Rectangle getSourceRectangle() {
return sourceFigure.getBounds();
}
/**
* Returns the scaled Image of the source Figure. If the Image needs to be
* updated, the ThumbnailUpdater will notified.
*
* @return The thumbnail image
*/
protected Image getThumbnailImage() {
Dimension oldSize = targetSize;
targetSize = getPreferredSize();
targetSize.expand(new Dimension(getInsets().getWidth(), getInsets().getHeight()).negate());
setScales(targetSize.width / (float) getSourceRectangle().width,
targetSize.height / (float) getSourceRectangle().height);
if ((isDirty()) && !updater.isRunning()) {
updater.start();
} else if (oldSize != null && !targetSize.equals(oldSize)) {
revalidate();
updater.restart();
}
return thumbnailImage;
}
/**
* Returns <code>true</code> if the source figure has changed.
*
* @return <code>true</code> if the source figure has changed
*/
protected boolean isDirty() {
return isDirty;
}
/**
* @see org.eclipse.draw2d.UpdateListener#notifyPainting(Rectangle, Map)
*/
@Override
public void notifyPainting(Rectangle damage, Map<IFigure, Rectangle> dirtyRegions) {
for (IFigure current : dirtyRegions.keySet()) {
while (current != null) {
if (current == getSource()) {
setDirty(true);
repaint();
return;
}
current = current.getParent();
}
}
}
/**
* @see org.eclipse.draw2d.UpdateListener#notifyValidating()
*/
@Override
public void notifyValidating() {
// setDirty(true);
// revalidate();
}
/**
* @see org.eclipse.draw2d.Figure#paintFigure(Graphics)
*/
@Override
protected void paintFigure(Graphics graphics) {
Image thumbnail = getThumbnailImage();
if (thumbnail == null) {
return;
}
graphics.drawImage(thumbnail, getClientArea().getLocation());
}
/**
* Sets the dirty flag.
*
* @param value The dirty value
*/
public void setDirty(boolean value) {
isDirty = value;
}
/**
* Sets the X and Y scales for the Thumbnail. These scales represent the ratio
* between the source figure and the Thumbnail.
*
* @param x The X scale
* @param y The Y scale
*/
protected void setScales(float x, float y) {
scaleX = x;
scaleY = y;
}
/**
* Sets the source Figure. Also sets the scales and creates the necessary update
* manager.
*
* @param fig The source figure
*/
public void setSource(IFigure fig) {
if (sourceFigure == fig) {
return;
}
if (sourceFigure != null) {
sourceFigure.getUpdateManager().removeUpdateListener(this);
}
sourceFigure = fig;
if (sourceFigure != null) {
setScales((float) getSize().width / (float) getSourceRectangle().width,
(float) getSize().height / (float) getSourceRectangle().height);
sourceFigure.getUpdateManager().addUpdateListener(this);
repaint();
}
}
/**
* Returns the target size of the thumbnail.
*
* @return the target size
* @since 3.14
*/
protected Dimension getTargetSize() {
return targetSize;
}
/**
* Sets the rate with which the thumbnail is updated. If set to either {@code 0}
* or a negative value, the update is done as fast as possible (default
* behavior), otherwise every {@code refreshRate}ms.
*
* Example:
*
* <pre>
* setRefreshRate(500); // Update every 500ms
* </pre>
*
* @param refreshRate The rate with which the thumbnail is updated.
* @since 3.18
*/
public void setRefreshRate(int refreshRate) {
this.refreshRate = refreshRate;
}
}