Skip to main content
summaryrefslogtreecommitdiffstats
blob: 1f7d6d29fe9c6b1110611745351497c3f2c98917 (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
package org.eclipse.compare.contentmergeviewer;

import org.eclipse.swt.widgets.Shell;
import org.eclipse.core.runtime.IProgressMonitor;
import java.lang.reflect.InvocationTargetException;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.operation.ModalContext;
import org.eclipse.jface.dialogs.ProgressMonitorDialog;


class DelayedProgressMonitor implements IProgressMonitor {
	
	ProgressMonitorDialog fProgressDialog;
	IProgressMonitor fRealProgressMonitor;
	String fTaskName;
	String fSubTaskName;
	int fTotalWork;
	int fWorked;
	boolean fCancelable;
	Shell fShell;
	int fTime;
	
	
	DelayedProgressMonitor(Shell shell) {
		fShell= shell;
	}

	/*
	 * @see IProgressMonitor#beginTask(String, int)
	 */
	public void beginTask(String name, int totalWork) {
		fTaskName= name;
		fTotalWork= totalWork;
		fTime= 0;
	}

	/*
	 * @see IProgressMonitor#done()
	 */
	public void done() {
		if (fRealProgressMonitor != null)
			fRealProgressMonitor.done();
	}

	/*
	 * @see IProgressMonitor#internalWorked(double)
	 */
	public void internalWorked(double work) {
		if (fRealProgressMonitor != null) {
			fRealProgressMonitor.internalWorked(work);
		}
	}
	
	private void checkTimeout() {
		if (fRealProgressMonitor == null) {
			
			//if (fTime++ < 100)
			//	return;
			
			fProgressDialog= new ProgressMonitorDialog(fShell);
			fProgressDialog.setCancelable(true);
			fProgressDialog.open();
			fRealProgressMonitor= fProgressDialog.getProgressMonitor();
			fRealProgressMonitor.beginTask(fTaskName, fTotalWork);
			if (fSubTaskName != null)
				fRealProgressMonitor.subTask(fSubTaskName);
			fRealProgressMonitor.worked(fWorked);
		}
	}

	/*
	 * @see IProgressMonitor#isCanceled()
	 */
	public boolean isCanceled() {
		checkTimeout();
		if (fRealProgressMonitor != null)
			return fRealProgressMonitor.isCanceled();
		return false;
	}

	/*
	 * @see IProgressMonitor#setCanceled(boolean)
	 */
	public void setCanceled(boolean value) {
		if (fRealProgressMonitor != null)
			fRealProgressMonitor.setCanceled(value);
		else
			fCancelable= value;
	}

	/*
	 * @see IProgressMonitor#setTaskName(String)
	 */
	public void setTaskName(String name) {
		if (fRealProgressMonitor != null)
			fRealProgressMonitor.setTaskName(name);
		else
			fTaskName= name;
	}

	/*
	 * @see IProgressMonitor#subTask(String)
	 */
	public void subTask(String name) {
		if (fRealProgressMonitor != null)
			fRealProgressMonitor.subTask(name);
		else
			fSubTaskName= name;
	}

	/*
	 * @see IProgressMonitor#worked(int)
	 */
	public void worked(int work) {
		if (fRealProgressMonitor != null)
			fRealProgressMonitor.internalWorked(work);
		else {
			fWorked+= work;
			checkTimeout();
		}
	}
	
	public static void run(Shell shell, boolean fork, boolean cancelable, IRunnableWithProgress runnable)
						throws InvocationTargetException, InterruptedException {
		
		DelayedProgressMonitor pm= new DelayedProgressMonitor(shell);
		pm.checkTimeout();
		try {
			ModalContext.run(runnable, fork, pm, shell.getDisplay());
		} finally {
			if (pm.fProgressDialog != null)
				pm.fProgressDialog.close();
		}
	}
}

Back to the top