Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 76e8eaeddbf5bb6739651ad64074addb088698bd (plain) (blame)
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
/*******************************************************************************
 * Copyright (c) 2007, 2016 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *     Friederike Schertel <friederike@schertel.org> - Bug 478336
 ******************************************************************************/

package org.eclipse.ui.internal;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.swt.SWTException;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.internal.misc.StatusUtil;
import org.eclipse.ui.statushandlers.StatusManager;

/**
 * Base class for Cyclic animations.
 *
 * @since 3.3
 *
 */
public abstract class ImageCycleFeedbackBase extends AnimationFeedbackBase {
	protected Image[] images;
	protected Image stoppedImage;
	private Image offScreenImage;
	private GC offScreenImageGC;
	private int imageDataIndex;
	private Image image;
	private ImageData imageData;
	protected Display display;
	protected Color background;

	/**
	 * @param parentShell
	 */
	public ImageCycleFeedbackBase(Shell parentShell) {
		super(parentShell);
		// TODO Auto-generated constructor stub
	}

	/**
	 *
	 * @param parentShell
	 * @param images      : an array of images
	 */
	public ImageCycleFeedbackBase(Shell parentShell, Image[] images) {
		super(parentShell);
		this.images = images;
	}

	/**
	 * Set the image during progress without caching.
	 *
	 * @param image
	 */
	public abstract void showImage(Image image);

	/**
	 * Save initial Image which would be stoppedImage
	 *
	 */
	public abstract void saveStoppedImage();

	/**
	 * Set the stopped Image upon animation completion
	 *
	 * @param image
	 */
	public abstract void setStoppedImage(Image image);

	@Override
	public void dispose() {
		if (stoppedImage == null || stoppedImage.isDisposed())
			return;
		setStoppedImage(stoppedImage);

		if (offScreenImageGC != null && !offScreenImageGC.isDisposed())
			offScreenImageGC.dispose();

		if (offScreenImage != null && !offScreenImage.isDisposed())
			offScreenImage.dispose();
	}

	@Override
	public boolean jobInit(AnimationEngine engine) {
		return super.jobInit(engine);
	}

	@Override
	public void renderStep(AnimationEngine engine) {
		if (offScreenImage == null) {
			offScreenImage = getOffscreenImage();
		}

		try {
			imageDataIndex = (imageDataIndex + 1) % images.length;
			image = images[imageDataIndex];
			imageData = image.getImageData();

			offScreenImageGC.drawImage(image, 0, 0, imageData.width, imageData.height, imageData.x, imageData.y,
					imageData.width, imageData.height);

			final Image finalImage = image;

			display.syncExec(() -> showImage(finalImage));

			/*
			 * Sleep for the specified delay time (adding commonly-used slow-down fudge
			 * factors).
			 */
			// try {
			// Thread.sleep(30);
			// } catch (InterruptedException e) {
			// }
			if (images == null)
				return;
		} catch (SWTException ex) {
			IStatus status = StatusUtil.newStatus(WorkbenchPlugin.PI_WORKBENCH, ex);
			StatusManager.getManager().handle(status);
		}
	}

	private Image getOffscreenImage() {
		saveStoppedImage();
		imageDataIndex = 0;
		image = images[imageDataIndex];
		imageData = image.getImageData();
		/*
		 * Create an off-screen image to draw on, and fill it with the shell background.
		 */
		offScreenImage = new Image(display, imageData.width, imageData.height);

		offScreenImageGC = new GC(offScreenImage);
		offScreenImageGC.setBackground(background);
		offScreenImageGC.fillRectangle(0, 0, imageData.width, imageData.height);

		/*
		 * Create the first image and draw it on the off-screen image.
		 */

		offScreenImageGC.drawImage(image, 0, 0, imageData.width, imageData.height, imageData.x, imageData.y,
				imageData.width, imageData.height);

		return offScreenImage;
	}

}

Back to the top