Skip to main content
summaryrefslogtreecommitdiffstats
blob: 5bed1f2f031516ed1f2dc1811b227672d3ce141c (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
161
162
163
164
165
166
167
168
169
170
package org.eclipse.mylyn.internal.tasks.ui.editors;

import java.util.Timer;
import java.util.TimerTask;

import org.eclipse.mylyn.internal.tasks.ui.TasksUiImages;
import org.eclipse.swt.SWTException;
import org.eclipse.swt.graphics.Image;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.internal.WorkbenchPlugin;

/**
 * @author Shawn Minto
 */
public class EditorBusyIndicator {

	private boolean showingBusy = false;

	private Image[] images;

	private TimerTask animateTask;

	private IBusyEditor editor;

	private Image oldImage;

	public EditorBusyIndicator(IBusyEditor editor) {
		this.editor = editor;
	}

	/**
	 * Stop showing the busy cursor.
	 */
	public void stopBusy() {

		if (!showingBusy || isDisposed())
			return;

		showingBusy = false;
		if (animateTask != null) {
			animateTask.cancel();
			animateTask = null;
		}

		updateTitleImage(oldImage);
	}

	/**
	 * Start the busy indication.
	 */
	public void startBusy() {

		if (showingBusy || isDisposed())
			return;

		showingBusy = true;

		oldImage = editor.getTitleImage();

		try {
			if (images == null) {
				images = TasksUiImages.getProgressImages();// If we fail to
															// load do not
															// continue
				if (images == null) {
					showingBusy = false;
					return;
				}
			}

			if (images.length > 1) {

				Timer animateTimer = new Timer();
				if (animateTask == null)
					animateTimer.schedule(getTimerTask(), 0);
			}
		} catch (SWTException ex) {
			WorkbenchPlugin.log(ex);
		}
	}

	/**
	 * Get the timer task for the receiver.
	 * 
	 * @param background
	 * @param display
	 * @return TimerTask
	 */
	private TimerTask getTimerTask() {

		animateTask = new TimerTask() {

			@Override
			public void run() {

				try {
					int imageDataIndex = 0;

					Image image = images[imageDataIndex];

					while (showingBusy) {

						updateTitleImage(image);

						imageDataIndex = (imageDataIndex + 1) % images.length;
						image = images[imageDataIndex];

						try {
							Thread.sleep(90);
						} catch (InterruptedException e) {
						}

						if (images == null)
							return;
					}
				} catch (SWTException ex) {
					WorkbenchPlugin.log(ex);
				} catch (NullPointerException e) {
					// we will get this if the timer continues to run after the
					// images have been disposed
				} finally {

				}
			}

		};

		return animateTask;
	}

	private void updateTitleImage(final Image image) {
		if (PlatformUI.getWorkbench().isClosing()) {
			return;
		} else {
			PlatformUI.getWorkbench().getDisplay().syncExec(new Runnable() {
				public void run() {
					if ((image == null || !image.isDisposed()) && !isDisposed() && showingBusy) {
						editor.setTitleImage(image);
					} else {
						if (oldImage != null && !oldImage.isDisposed()) {
							editor.setTitleImage(oldImage);
						}
					}

				}
			});
		}
	}

	private boolean isDisposed = false;

	public void dispose() {
		if (!isDisposed) {
			if (animateTask != null)
				animateTask.cancel();
			// if (images != null) {
			// for (int i = 0; i < images.length; i++) {
			// images[i].dispose();
			// }
			// }
			// images = null;
			isDisposed = true;
		}
	}

	public boolean isDisposed() {
		return isDisposed;
	}

}

Back to the top