Project

General

Profile

Download (18.5 KB) Statistics
| Branch: | Tag: | Revision:
1
// $Id$
2
/**
3
* Copyright (C) 2007 EDIT
4
* European Distributed Institute of Taxonomy 
5
* http://www.e-taxonomy.eu
6
* 
7
* The contents of this file are subject to the Mozilla Public License Version 1.1
8
* See LICENSE.TXT at the top of this package for the full license terms.
9
*/
10

    
11
package eu.etaxonomy.taxeditor.model;
12

    
13
import java.lang.reflect.InvocationTargetException;
14

    
15
import org.apache.log4j.Logger;
16
import org.eclipse.core.commands.ExecutionException;
17
import org.eclipse.core.commands.operations.IOperationHistory;
18
import org.eclipse.core.runtime.IAdaptable;
19
import org.eclipse.core.runtime.ILog;
20
import org.eclipse.core.runtime.IProgressMonitor;
21
import org.eclipse.core.runtime.IStatus;
22
import org.eclipse.core.runtime.NullProgressMonitor;
23
import org.eclipse.core.runtime.OperationCanceledException;
24
import org.eclipse.core.runtime.Status;
25
import org.eclipse.core.runtime.SubProgressMonitor;
26
import org.eclipse.core.runtime.jobs.ISchedulingRule;
27
import org.eclipse.jface.action.IStatusLineManager;
28
import org.eclipse.jface.dialogs.MessageDialog;
29
import org.eclipse.jface.operation.IRunnableWithProgress;
30
import org.eclipse.jface.resource.ColorRegistry;
31
import org.eclipse.jface.resource.FontRegistry;
32
import org.eclipse.jface.window.ApplicationWindow;
33
import org.eclipse.swt.graphics.Color;
34
import org.eclipse.swt.graphics.Font;
35
import org.eclipse.swt.widgets.Display;
36
import org.eclipse.swt.widgets.Shell;
37
import org.eclipse.ui.IViewPart;
38
import org.eclipse.ui.IViewReference;
39
import org.eclipse.ui.IWorkbench;
40
import org.eclipse.ui.IWorkbenchPage;
41
import org.eclipse.ui.IWorkbenchPart;
42
import org.eclipse.ui.PartInitException;
43
import org.eclipse.ui.PlatformUI;
44
import org.eclipse.ui.ide.undo.WorkspaceUndoUtil;
45
import org.eclipse.ui.progress.IProgressService;
46
import org.eclipse.ui.progress.IWorkbenchSiteProgressService;
47
import org.eclipse.ui.themes.ITheme;
48
import org.eclipse.ui.themes.IThemeManager;
49

    
50
import eu.etaxonomy.taxeditor.operations.AbstractPostOperation;
51
import eu.etaxonomy.taxeditor.operations.IPostOperationEnabled;
52
import eu.etaxonomy.taxeditor.store.internal.TaxeditorStorePlugin;
53

    
54
/**
55
 * <p>Abstract AbstractUtility class.</p>
56
 *
57
 * @author n.hoffmann
58
 * @created 11.05.2009
59
 * @version 1.0
60
 */
61
public abstract class AbstractUtility {
62
	
63
	/** Constant <code>statusLineManager</code> */
64
	protected static IStatusLineManager statusLineManager;
65
	
66
	/**
67
	 * <p>getShell</p>
68
	 *
69
	 * @return a {@link org.eclipse.swt.widgets.Shell} object.
70
	 */
71
	public static Shell getShell() {
72
		
73
		return TaxeditorStorePlugin.getDefault().getWorkbench()
74
				.getActiveWorkbenchWindow().getShell();
75
	}
76
	
77
	/**
78
	 * <p>getActivePage</p>
79
	 *
80
	 * @return a {@link org.eclipse.ui.IWorkbenchPage} object.
81
	 */
82
	public static IWorkbenchPage getActivePage(){
83
		
84
		return TaxeditorStorePlugin.getDefault().getWorkbench()
85
			.getActiveWorkbenchWindow().getActivePage();
86
	}
87
	
88
	/**
89
	 * <p>getActivePart</p>
90
	 *
91
	 * @return a {@link org.eclipse.ui.IWorkbenchPart} object.
92
	 */
93
	public static IWorkbenchPart getActivePart(){
94
		return getActivePage() != null ? getActivePage().getActivePart() : null;
95
	}
96
	
97
	/**
98
	 * <p>getWorkbenchWindow</p>
99
	 *
100
	 * @return a {@link org.eclipse.jface.window.ApplicationWindow} object.
101
	 */
102
	public static ApplicationWindow getWorkbenchWindow(){
103
		if(TaxeditorStorePlugin.getDefault().getWorkbench().getWorkbenchWindowCount() > 1){
104
			throw new IllegalStateException("More than one workbench window");
105
		}
106
		return (ApplicationWindow) TaxeditorStorePlugin.getDefault().getWorkbench().getWorkbenchWindows()[0];
107
	}
108
	
109
	/**
110
	 * <p>showView</p>
111
	 *
112
	 * @param id a {@link java.lang.String} object.
113
	 * @return a {@link org.eclipse.ui.IViewPart} object.
114
	 */
115
	public static IViewPart showView(String id){
116
		try {
117
			return PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().showView(id);
118
		} catch (PartInitException e) {
119
			error(AbstractUtility.class, "Could not open view: " + id, e);
120
			throw new RuntimeException(e);
121
		}
122
	}
123
	
124
	/**
125
	 * <p>hideView</p>
126
	 *
127
	 * @param view a {@link org.eclipse.ui.IViewPart} object.
128
	 */
129
	public static void hideView(IViewPart view){
130
		PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().hideView(view);
131
	}
132
	
133
	
134
	/**
135
	 * <p>getView</p>
136
	 *
137
	 * @param id a {@link java.lang.String} object.
138
	 * @param restore a boolean.
139
	 * @return a {@link org.eclipse.ui.IViewPart} object.
140
	 */
141
	public static IViewPart getView(String id, boolean restore){
142
		IViewReference[] references = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getViewReferences();
143
		for (IViewReference reference : references){
144
			if(reference.getId().equals(id)){
145
				return reference.getView(restore);
146
			}
147
		}
148
		return null;
149
	}
150
		
151
	/**
152
	 * <p>closeAll</p>
153
	 *
154
	 * @return a boolean.
155
	 */
156
	public static boolean closeAll() {
157
		return getActivePage().closeAllEditors(true);
158
	}
159
	
160
	/**
161
	 * <p>getService</p>
162
	 *
163
	 * @param api a {@link java.lang.Class} object.
164
	 * @return a {@link java.lang.Object} object.
165
	 */
166
	public static Object getService(Class api){
167
		return TaxeditorStorePlugin.getDefault().getWorkbench().getService(api);
168
	}
169
	
170
	/**
171
	 * <p>getCurrentTheme</p>
172
	 *
173
	 * @return a {@link org.eclipse.ui.themes.ITheme} object.
174
	 */
175
	public static ITheme getCurrentTheme(){
176
		IThemeManager themeManager = TaxeditorStorePlugin.getDefault().getWorkbench().getThemeManager();
177
		return themeManager.getCurrentTheme();
178
	}
179
	
180
	/**
181
	 * Fonts registered to the plugin may be obtained with the Eclipse themeing functionality.
182
	 * Thus fonts are chooseable by the user via Preferences->General->Appearance->Colors and Fonts
183
	 *
184
	 * @return the FontRegistry for the current theme
185
	 */
186
	public static FontRegistry getFontRegistry(){
187
		return getCurrentTheme().getFontRegistry();
188
	}
189
	
190
	/**
191
	 * <p>getFont</p>
192
	 *
193
	 * @param symbolicName a {@link java.lang.String} object.
194
	 * @return a {@link org.eclipse.swt.graphics.Font} object.
195
	 */
196
	public static Font getFont(String symbolicName){
197
		return getFontRegistry().get(symbolicName);
198
	}
199
	
200
	/**
201
	 * Color registered to the plugin may be obtained with the Eclipse themeing functionality.
202
	 * Thus colors are editable by the user via Preferences->General->Appearance->Colors and Fonts
203
	 *
204
	 * @return the ColorRegistry for the current theme
205
	 */
206
	public static ColorRegistry getColorRegistry(){
207
		return getCurrentTheme().getColorRegistry();
208
	}
209
	
210
	/**
211
	 * <p>getColor</p>
212
	 *
213
	 * @param symbolicName a {@link java.lang.String} object.
214
	 * @return a {@link org.eclipse.swt.graphics.Color} object.
215
	 */
216
	public static Color getColor(String symbolicName){
217
		return getColorRegistry().get(symbolicName);
218
	}
219
	
220
	/**
221
	 * Open a message box that informs the user about unimplemented functionality.
222
	 * This method is for developer convenience.
223
	 *
224
	 * @param source a {@link java.lang.Object} object.
225
	 */
226
	public static void notImplementedMessage(Object source){
227
		warningDialog("Not yet implemented", source, "This functionality is not yet implemented.");
228
	}
229
	
230
	/**
231
	 * <p>informationDialog</p>
232
	 *
233
	 * @param title a {@link java.lang.String} object.
234
	 * @param message a {@link java.lang.String} object.
235
	 */
236
	public static void informationDialog(final String title, final String message){
237
		Display.getDefault().asyncExec(new Runnable(){
238

    
239
			public void run() {
240
				MessageDialog.openInformation(getShell(), title, message);
241
			}
242
		});
243
	}
244
	
245
	/**
246
	 * <p>warningDialog</p>
247
	 *
248
	 * @param title The dialogs title
249
	 * @param source The object where the warning was generated (used by log4j)
250
	 * @param message An informative String to be presented to the user
251
	 */
252
	public static void warningDialog(final String title, final Object source, final String message){
253
		Display.getDefault().asyncExec(new Runnable(){
254

    
255
			public void run() {
256
				MessageDialog.openWarning(getShell(), title, message);
257
				warn(AbstractUtility.class, message);
258
			}
259
		});
260
	}
261
	
262
	/**
263
	 * <p>errorDialog</p>
264
	 *
265
	 * @param title The dialogs title
266
	 * @param source The object where the warning was generated (used by log4j)
267
	 * @param message An informative String to be presented to the user
268
	 * @param title The dialogs title
269
	 * @param t a Throwable if one exists or null
270
	 */
271
	public static void errorDialog(final String title, final Object source, final String message, final Throwable t){
272
		Display.getDefault().asyncExec(new Runnable(){
273

    
274
			public void run() {
275
				MessageDialog.openError(getShell(), title, message);
276
				error(AbstractUtility.class, message, t);
277
			}
278
		});
279
	}
280
	
281
	/**
282
	 * <p>errorDialog</p>
283
	 *
284
	 * @param title a {@link java.lang.String} object.
285
	 * @param source a {@link java.lang.Object} object.
286
	 * @param status a {@link org.eclipse.core.runtime.IStatus} object.
287
	 */
288
	public static void errorDialog(final String title, final Object source, final IStatus status){
289
		
290
	}
291

    
292
	/**
293
	 * <p>confirmDialog</p>
294
	 *
295
	 * @param title a {@link java.lang.String} object.
296
	 * @param message a {@link java.lang.String} object.
297
	 * @return a boolean.
298
	 */
299
	public static boolean confirmDialog(String title, String message) {
300
		return MessageDialog.openQuestion(getShell(), title, message);
301
	}
302
	
303
	/**
304
	 * <p>executeOperation</p>
305
	 *
306
	 * @param operation a {@link eu.etaxonomy.taxeditor.operations.AbstractPostOperation} object.
307
	 * @return a {@link org.eclipse.core.runtime.IStatus} object.
308
	 */
309
	public static IStatus executeOperation(final AbstractPostOperation operation){
310
		if(getOperationHistory() == null){
311
			throw new IllegalArgumentException("There is no operation history for this context");
312
		}
313
		
314
		final IAdaptable uiInfoAdapter = WorkspaceUndoUtil.getUIInfoAdapter(getShell());
315
		
316
		
317
		
318
		IRunnableWithProgress runnable = new IRunnableWithProgress() {
319
			
320
			public void run(IProgressMonitor monitor) throws InvocationTargetException,
321
					InterruptedException {
322
				monitor.beginTask(operation.getLabel(), 100);
323
				IStatus status;
324
				try {
325
					status = getOperationHistory().execute(operation, monitor, uiInfoAdapter);
326
				} catch (ExecutionException e) {
327
					throw new RuntimeException(e);
328
				}
329
				monitor.done();
330
		        String statusString = status.equals(Status.OK_STATUS) ? "completed" : "cancelled";
331
				setStatusLine(operation.getLabel() + " " + statusString + ".");
332
				
333
			}
334
		};
335
		
336
		try {
337
			runInUI(runnable, null);
338
		} catch (InvocationTargetException e) {
339
			throw new RuntimeException(e);
340
		} catch (InterruptedException e) {
341
			throw new RuntimeException(e);
342
		}
343
		
344
//		// Start the main progress monitor.
345
//        IProgressMonitor newMonitor = startMainMonitor(getMonitor(),operation.getLabel(), 100);
346
//
347
//		// Check whether operation was canceled and do some steps.
348
//        workedChecked(newMonitor, 10);
349
//
350
//        try {
351
//			IStatus status = getOperationHistory().execute(operation, newMonitor,
352
//					WorkspaceUndoUtil.getUIInfoAdapter(getShell()));
353
//
354
//			// Check whether operation was canceled and do some steps.
355
//			workedChecked(newMonitor, 30);
356
//
357
//	        String statusString = status.equals(Status.OK_STATUS) ? "completed" : "cancelled";
358
//			setStatusLine(operation.getLabel() + " " + statusString + ".");
359
//
360
//	        return status;
361
//		} catch (ExecutionException e) {
362
//			logger.error("Error executing operation: " + operation.getLabel(), e);
363
//			errorDialog("Error executing operation: " + operation.getLabel(), "Please refer to the error log.");
364
//		}
365
//        finally {
366
//        	
367
//        	// Stop the progress monitor.
368
//            newMonitor.done();
369
//        }
370
		
371
		IPostOperationEnabled postOperationEnabled = operation.getPostOperationEnabled();
372
		postOperationEnabled.onComplete();
373
		return Status.OK_STATUS;
374
	}
375
	
376
	/**
377
	 * <p>getOperationHistory</p>
378
	 *
379
	 * @return a {@link org.eclipse.core.commands.operations.IOperationHistory} object.
380
	 */
381
	public static IOperationHistory getOperationHistory(){
382
		return TaxeditorStorePlugin.getDefault().getWorkbench().
383
							getOperationSupport().getOperationHistory();
384
	}
385
	
386
	/**
387
	 * <p>setStatusLine</p>
388
	 *
389
	 * @param message a {@link java.lang.String} object.
390
	 */
391
	public static void setStatusLine(final String message) {
392
		Display.getDefault().asyncExec(new Runnable(){
393

    
394
			public void run() {
395
				statusLineManager.setMessage(message);
396
			}
397
			
398
		});
399
		
400
	}
401
	
402
	/**
403
	 * <p>getMonitor</p>
404
	 *
405
	 * @return a {@link org.eclipse.core.runtime.IProgressMonitor} object.
406
	 */
407
	public static IProgressMonitor getMonitor() {
408
		statusLineManager.setCancelEnabled(false);
409
		return statusLineManager.getProgressMonitor();
410
	}
411
	
412
    /**
413
     * Starts either the given {@link IProgressMonitor} if it's not <code>null</code> or a new {@link NullProgressMonitor}.
414
     *
415
     * @param progressMonitor The {@link IProgressMonitor} or <code>null</code> if no progress should be reported.
416
     * @param taskName The name of the main task.
417
     * @param steps The number of steps this task is subdivided into.
418
     * @return The {@link IProgressMonitor}.
419
     */
420
    public static IProgressMonitor startMainMonitor(IProgressMonitor progressMonitor, String taskName, int steps) {
421
        IProgressMonitor newMonitor = progressMonitor;
422
        if (newMonitor == null) {
423
            newMonitor = new NullProgressMonitor();
424
        }
425
        newMonitor.beginTask(taskName == null ? "" : taskName, steps);
426
        newMonitor.subTask(" ");
427
        return newMonitor;
428
    }
429

    
430
    /**
431
     * Creates a {@link SubProgressMonitor} if the given {@link IProgressMonitor} is not <code>null</code> and not a {@link NullProgressMonitor}.
432
     *
433
     * @param progressMonitor The parent {@link IProgressMonitor} of the {@link SubProgressMonitor} to be created.
434
     * @param ticks The number of steps this subtask is subdivided into. Must be a positive number and must not be {@link IProgressMonitor#UNKNOWN}.
435
     * @return The {@link IProgressMonitor}.
436
     */
437
    public static IProgressMonitor getSubProgressMonitor(IProgressMonitor progressMonitor, int ticks) {
438
        if (progressMonitor == null) {
439
            return new NullProgressMonitor();
440
        }
441
        if (progressMonitor instanceof NullProgressMonitor) {
442
            return progressMonitor;
443
        }
444
        
445
        return new SubProgressMonitor(progressMonitor, ticks);
446
    }
447
    
448
	/**
449
	 * Checks whether the user canceled this operation. If not canceled, the given number of steps are declared as done.
450
	 *
451
	 * @param newMonitor a {@link org.eclipse.core.runtime.IProgressMonitor} object.
452
	 * @param steps a int.
453
	 */
454
	public static void workedChecked(IProgressMonitor newMonitor, int steps) {
455
		// In case the progress monitor was canceled throw an exception.
456
		if (newMonitor.isCanceled()) {
457
			throw new OperationCanceledException();
458
		}
459
		// Otherwise declare this step as done.
460
		newMonitor.worked(steps);
461
	}
462

    
463
	/**
464
	 * Present a progress dialog to the user. This dialog will block the UI
465
	 *
466
	 * @param runnable an implementation of {@link IRunnableWithProgress}
467
	 * @throws java.lang.InterruptedException if any.
468
	 * @throws java.lang.reflect.InvocationTargetException if any.
469
	 */
470
	public static void busyCursorWhile(IRunnableWithProgress runnable) throws InvocationTargetException, InterruptedException{
471
		getProgressService().busyCursorWhile(runnable);
472
	}
473
	
474
	/**
475
	 * <p>runInUI</p>
476
	 *
477
	 * @see {@link IProgressService#runInUI(org.eclipse.jface.operation.IRunnableContext, IRunnableWithProgress, ISchedulingRule)}
478
	 * @param runnable a {@link org.eclipse.jface.operation.IRunnableWithProgress} object.
479
	 * @param rule a {@link org.eclipse.core.runtime.jobs.ISchedulingRule} object.
480
	 * @throws java.lang.reflect.InvocationTargetException if any.
481
	 * @throws java.lang.InterruptedException if any.
482
	 */
483
	public static void runInUI(IRunnableWithProgress runnable, ISchedulingRule rule) throws InvocationTargetException, InterruptedException{
484
		getProgressService().runInUI(getWorkbenchWindow(), runnable, rule);		
485
	}
486
	
487
	/**
488
	 * <p>run</p>
489
	 *
490
	 * @param fork a boolean.
491
	 * @param cancelable a boolean.
492
	 * @param runnable a {@link org.eclipse.jface.operation.IRunnableWithProgress} object.
493
	 * @throws java.lang.reflect.InvocationTargetException if any.
494
	 * @throws java.lang.InterruptedException if any.
495
	 */
496
	public static void run(boolean fork, boolean cancelable, IRunnableWithProgress runnable) throws InvocationTargetException, InterruptedException{
497
		getProgressService().run(fork, cancelable, runnable);
498
	}
499
	
500
	/**
501
	 * <p>getProgressService</p>
502
	 *
503
	 * @return a {@link org.eclipse.ui.progress.IProgressService} object.
504
	 */
505
	public static IProgressService getProgressService(){
506
		IWorkbench workbench = PlatformUI.getWorkbench();
507
		return workbench.getProgressService();
508
	}
509
	
510
	/**
511
	 * <p>getProgressService2</p>
512
	 *
513
	 * @return a {@link org.eclipse.ui.progress.IWorkbenchSiteProgressService} object.
514
	 */
515
	public static IWorkbenchSiteProgressService getProgressService2(){
516
		return (IWorkbenchSiteProgressService) getService(IWorkbenchSiteProgressService.class);
517
	}
518
		
519
	/**
520
	 * <p>info</p>
521
	 *
522
	 * @param message a {@link java.lang.String} object.
523
	 */
524
	public static void info(String message){
525
		IStatus status = new Status(IStatus.INFO, getPluginId(), message);
526
		info(status);
527
	}
528
	
529
	/**
530
	 * <p>info</p>
531
	 *
532
	 * @param status a {@link org.eclipse.core.runtime.IStatus} object.
533
	 */
534
	public static void info(IStatus status){
535
		log(status);
536
	}
537
	
538
	/**
539
	 * <p>warn</p>
540
	 *
541
	 * @param source a {@link java.lang.Class} object.
542
	 * @param message a {@link java.lang.String} object.
543
	 */
544
	public static void warn(Class source, String message){
545
		IStatus status = new Status(IStatus.WARNING, getPluginId(), message);
546
		getLog4JLogger(source).warn(message);
547
		log(status);
548
	}
549
	
550
	/**
551
	 * <p>error</p>
552
	 *
553
	 * @param source a {@link java.lang.Class} object.
554
	 * @param t a {@link java.lang.Throwable} object.
555
	 */
556
	public static void error(Class source, Throwable t){
557
		error(source.getClass(), t.getMessage(), t);
558
	}
559
	
560
	/**
561
	 * <p>error</p>
562
	 *
563
	 * @param source a {@link java.lang.Class} object.
564
	 * @param message a {@link java.lang.String} object.
565
	 * @param t a {@link java.lang.Throwable} object.
566
	 */
567
	public static void error(Class source, String message, Throwable t){
568
		IStatus status = new Status(IStatus.ERROR, getPluginId(), message, t);
569
		error(source, status);
570
	}
571
	
572
	/**
573
	 * <p>error</p>
574
	 *
575
	 * @param source a {@link java.lang.Class} object.
576
	 * @param status a {@link org.eclipse.core.runtime.IStatus} object.
577
	 */
578
	public static void error(Class source, IStatus status){
579
		getLog4JLogger(source).error(status.getMessage(), status.getException());
580
		log(status);
581
	}
582
	
583

    
584
	/**
585
	 * <p>getLog4JLogger</p>
586
	 *
587
	 * @param clazz a {@link java.lang.Class} object.
588
	 * @return a {@link org.apache.log4j.Logger} object.
589
	 */
590
	public static Logger getLog4JLogger(
591
			Class clazz) {
592
		return Logger.getLogger(clazz);
593
	}
594
	
595
	/**
596
	 * @see {@link ILog#log(IStatus)}
597
	 * 
598
	 * @param status
599
	 */
600
	private static void log(IStatus status){
601
		TaxeditorStorePlugin.getDefault().getLog().log(status);
602
	}
603
	
604
	/**
605
	 * <p>getPluginId</p>
606
	 *
607
	 * @return a {@link java.lang.String} object.
608
	 */
609
	protected static String getPluginId(){
610
		return "eu.taxeditor";
611
	}
612
}
(3-3/27)