Eclipseplugins
ProgressServiceUtil.java
1 package com.proalpha.pds.ui;
2 
3 
4 import org.eclipse.core.resources.ResourcesPlugin;
5 import org.eclipse.core.runtime.jobs.ISchedulingRule;
6 import org.eclipse.jface.dialogs.MessageDialog;
7 import org.eclipse.jface.operation.IRunnableContext;
8 import org.eclipse.jface.operation.IRunnableWithProgress;
9 import org.eclipse.swt.widgets.Display;
10 import org.eclipse.ui.PlatformUI;
11 
12 public class ProgressServiceUtil {
13 
14  public static void runInProgressService(String taskName,
15  IRunnableWithProgress runnableWithProgress) {
16  try {
17 
18  PlatformUI.getWorkbench().getProgressService()
19  .busyCursorWhile(runnableWithProgress);
20 
21  } catch (Exception e) {
22  showAndLogError(taskName, e);
23  }
24  }
25 
26  public static void runInUI(String taskName,
27  IRunnableWithProgress runnableWithProgress) {
28  try {
29 
30  ISchedulingRule rule = ResourcesPlugin.getWorkspace().getRoot();
31  IRunnableContext context = PlatformUI.getWorkbench()
32  .getProgressService();
33 
34  PlatformUI.getWorkbench().getProgressService()
35  .runInUI(context, runnableWithProgress, rule);
36 
37  } catch (Exception e) {
38  showAndLogError(taskName, e);
39  }
40  }
41 
42  public static void runInNewThread(String taskName,
43  IRunnableWithProgress runnableWithProgress) {
44  try {
45 
46  PlatformUI.getWorkbench().getProgressService()
47  .run(Boolean.TRUE, Boolean.TRUE, runnableWithProgress);
48 
49  } catch (Exception e) {
50  showAndLogError(taskName, e);
51  }
52  }
53 
54  private static void showAndLogError(String taskName, Exception e) {
55  String message = e.getMessage();
56  if (message == null || message.isEmpty()){
57  message = e.getCause().getMessage();
58  }
59 
60  MessageDialog.openError(Display.getCurrent().getActiveShell(),
61  taskName, message);
62 
63  //Activator.getDefault().logErrorMessage(e);
64  }
65 }