Revision 2563

tmp/org.txm.rcp/src/main/java/org/txm/rcp/utils/JobHandler.java (revision 2563)
48 48
 * 
49 49
 */
50 50
public abstract class JobHandler extends Job implements IProgressMonitor {
51

  
51
	
52 52
	/** The parent. */
53 53
	IProgressMonitor currentMonitor;
54

  
54
	
55 55
	protected Semaphore mySemaphore;
56
	
56 57
	protected boolean canBeInterrupted = false;
57 58
	
58 59
	Object result = null;
......
71 72
	// FIXME: SJ: parent is not used anymore, need to remove or modify this constructor
72 73
	public JobHandler(String title, Composite parent) {
73 74
		super(title);
74
		//this.parent = parent;
75
		// this.parent = parent;
75 76
		this.setUser(true);
76 77
	}
77

  
78

  
78
	
79
	
79 80
	/**
80 81
	 * Create a job in RCP scheduler.
81 82
	 * 
......
85 86
	public JobHandler(String title) {
86 87
		this(title, true);
87 88
	}
88

  
89
	
89 90
	/**
90 91
	 * Instantiates a new job handler.
91 92
	 * 
......
98 99
		super(title);
99 100
		this.setUser(show);
100 101
	}
101

  
102 102
	
103
	
103 104
	/**
104 105
	 * You must set the current monitor to be available to manage the process
105 106
	 * progress
......
115 116
			currentMonitor.beginTask(name, (int) totalWork);
116 117
		}
117 118
	}
118

  
119
	
119 120
	@Override
120 121
	public void subTask(String name) {
121 122
		if (currentMonitor != null) {
122 123
			currentMonitor.subTask(name);
123 124
		}
124 125
	}
125

  
126 126
	
127
	
127 128
	@Override
128 129
	public void worked(int amount) {
129 130
		if (currentMonitor != null) {
......
143 144
			currentMonitor.worked(1);
144 145
		}
145 146
	}
146

  
147
	
147 148
	@Override
148 149
	public boolean isCanceled() {
149 150
		if (currentMonitor != null) {
......
151 152
		}
152 153
		return false;
153 154
	}
154

  
155
	
155 156
	@Override
156 157
	public void done() {
157 158
		if (currentMonitor != null) {
......
163 164
	public void acquireSemaphore() {
164 165
		try {
165 166
			this.mySemaphore.acquire();
166
		} catch (InterruptedException e) {
167
		}
168
		catch (InterruptedException e) {
167 169
			org.txm.utils.logger.Log.printStackTrace(e);
168 170
		}
169 171
	}
170

  
172
	
171 173
	public boolean tryAcquireSemaphore() {
172 174
		return this.mySemaphore.tryAcquire();
173 175
	}
174

  
176
	
175 177
	public void releaseSemaphore() {
176 178
		this.mySemaphore.release();
177 179
		if (isCanceled()) {
178 180
			canceling();
179 181
		}
180 182
	}
181

  
183
	
182 184
	/*
183 185
	 * use this function to do something with SWT objects
184 186
	 */
......
191 193
	public void syncExec(Runnable runable) {
192 194
		Display.getDefault().syncExec(runable);
193 195
	}
194

  
196
	
195 197
	public void startJob() {
196 198
		startJob(false);
197 199
	}
......
206 208
		this.setUser(true);// show popup
207 209
		this.schedule();// start
208 210
	}
209

  
211
	
210 212
	/**
211 213
	 * Allows the job to be interrupted
212 214
	 * 
......
217 219
		this.currentMonitor = monitor;
218 220
		this.canBeInterrupted = true;
219 221
	}
220

  
222
	
221 223
	/**
222 224
	 * Action when user requests for interruption
223 225
	 */
......
229 231
		try {
230 232
			Log.info(TXMUIMessages.canceling_2 + " " + this.getName() + ".");
231 233
			this.syncExec(new Runnable() {
234
				
232 235
				@Override
233 236
				public void run() {
234 237
					StatusLine.setMessage(TXMUIMessages.canceling);
......
237 240
			if (currentMonitor != null) this.currentMonitor.setTaskName(TXMUIMessages.canceling);
238 241
			this.mySemaphore.acquire();
239 242
			this.getThread().stop();
240
		} catch (Exception e) {
243
			
244
		}
245
		catch (Exception e) {
241 246
			Log.printStackTrace(e);
242 247
			Log.severe(TXMUIMessages.failedToCancel + this.getName());
243 248
		}
249
		finally {
250
			super.canceling();
251
		}
244 252
	}
245

  
253
	
246 254
	/**
247 255
	 * can be used to store the job result after join() has been called
248 256
	 * 
......
265 273
	public void beginTask(String name, int totalWork) {
266 274
		if (currentMonitor != null) currentMonitor.beginTask(name, totalWork);
267 275
	}
268

  
276
	
269 277
	@Override
270 278
	public void internalWorked(double work) {
271 279
		if (currentMonitor != null) currentMonitor.internalWorked(work);
272 280
	}
273

  
281
	
274 282
	@Override
275 283
	public void setCanceled(boolean value) {
276 284
		this.cancel();
277 285
	}
278

  
286
	
279 287
	@Override
280 288
	public void setTaskName(String name) {
281 289
		if (currentMonitor != null) currentMonitor.setTaskName(name);
282 290
	}
283
}
291
}

Also available in: Unified diff