1
2
3
4
5
6 package org.dbe.studio.tools.ontologyviewer.utils;
7
8 import java.net.NoRouteToHostException;
9
10 import org.eclipse.jface.dialogs.MessageDialog;
11 import org.eclipse.swt.widgets.Display;
12 import org.eclipse.swt.widgets.Shell;
13
14 public class Runner extends Thread {
15
16 private Object obj;
17 private IRunner func = null;
18 private String errMsg = null;
19 private MyMessageDialog dialog;
20 private boolean functionReturned;
21
22 /***
23 *
24 */
25 public Runner() {
26 super();
27 }
28
29
30
31
32
33
34 public void run() {
35 try {
36 obj = func.run();
37 if (dialog != null && dialog.getShell() != null) {
38 functionReturned = true;
39 dialog.getShell().getDisplay().wake();
40 }
41 }
42 catch (ThreadDeath e) {
43 errMsg = "Cancel by user";
44 }
45 catch (NoRouteToHostException e) {
46 errMsg = "No route to host";
47 }
48 catch (Exception e) {
49 e.printStackTrace();
50 errMsg = "Error occured while trying to submit query. "
51 + ((e.getMessage() != null) ? e.getMessage() : "");
52 }
53
54 }
55
56 public Object run(IRunner runnerFunc, String message) {
57 functionReturned = false;
58 func = runnerFunc;
59
60 errMsg = null;
61
62 dialog = new MyMessageDialog(this, message);
63 start();
64
65 dialog.open();
66
67 dialog.close();
68
69 if (errMsg != null)
70 throw new RuntimeException(errMsg);
71
72 return obj;
73 }
74
75 class MyMessageDialog extends MessageDialog {
76 Thread runner;
77
78 public MyMessageDialog(Thread runner, String message) {
79 super(null, "Ontology Viewer - Wait", MessageDialog.getDefaultImage(), message,
80 MessageDialog.INFORMATION, new String[] { "Cancel" }, 0);
81 setBlockOnOpen(false);
82 this.runner = runner;
83 }
84
85 protected void buttonPressed(int buttonId) {
86 try {
87 runner.interrupt();
88 } catch (Exception e) {
89
90 }
91 errMsg = "Cancel by user";
92 super.buttonPressed(buttonId);
93 }
94
95 /***
96 * Opens this window, creating it first if it has not yet been created.
97 * <p>
98 * If this window has been configured to block on open (
99 * <code>setBlockOnOpen</code>), this method waits until the window
100 * is closed by the end user, and then it returns the window's return
101 * code; otherwise, this method returns immediately. A window's return
102 * codes are window-specific, although two standard return codes are
103 * predefined: <code>OK</code> and <code>CANCEL</code>.
104 * </p>
105 *
106 * @return the return code
107 *
108 * @see #create()
109 */
110 public int open() {
111 super.open();
112
113 runEventLoop(getShell());
114
115 return getReturnCode();
116 }
117
118 /***
119 * Runs the event loop for the given shell.
120 *
121 * @param shell
122 * the shell
123 */
124 private void runEventLoop(Shell shell) {
125
126
127 Display display;
128 if (shell == null)
129 display = Display.getCurrent();
130 else
131 display = shell.getDisplay();
132
133 while (shell != null && !shell.isDisposed() && !functionReturned) {
134 try {
135 if (!display.readAndDispatch() && !functionReturned) {
136 display.sleep();
137 }
138 } catch (Throwable e) {
139
140 }
141 }
142 display.update();
143 }
144 }
145 }