]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.procore.ui/src/org/simantics/db/procore/ui/ProCoreUserAgent.java
dc93b2f9d9a45918a546a42383fa396523104422
[simantics/platform.git] / bundles / org.simantics.db.procore.ui / src / org / simantics / db / procore / ui / ProCoreUserAgent.java
1 package org.simantics.db.procore.ui;\r
2 \r
3 import org.eclipse.swt.widgets.Display;\r
4 import org.eclipse.swt.widgets.Shell;\r
5 import org.eclipse.ui.IWorkbench;\r
6 import org.eclipse.ui.IWorkbenchWindow;\r
7 import org.simantics.db.DatabaseUserAgent;\r
8 import org.simantics.db.exception.InternalException;\r
9 import org.simantics.db.procore.ProCoreDriver;\r
10 \r
11 public final class ProCoreUserAgent implements DatabaseUserAgent {\r
12     private static Shell getShell(IWorkbench workbench) {\r
13         IWorkbenchWindow wbw = workbench.getActiveWorkbenchWindow();\r
14         Shell shell = null;\r
15         if (null != wbw) {\r
16             shell = wbw.getShell();\r
17         } else {\r
18             Display d = getDisplay();\r
19             if (d == null)\r
20                 return null;\r
21             shell = d.getActiveShell();\r
22             if (null == shell) {\r
23                 Shell[] shells = d.getShells();\r
24                 if (null != shells && shells.length > 0)\r
25                     shell = shells[0];\r
26             }\r
27         }\r
28         return shell;\r
29     }\r
30     private static Display getDisplay() {\r
31         Display d = Display.getCurrent();\r
32         if (d == null)\r
33             d = Display.getDefault();\r
34         return d;\r
35     }\r
36     private IWorkbench workbench;\r
37     public ProCoreUserAgent(IWorkbench workbench) {\r
38         this.workbench = workbench;\r
39     }\r
40     private Shell getShell() {\r
41         return getShell(workbench);\r
42     }\r
43     @Override\r
44     public boolean handleStart(InternalException exception) {\r
45         Shell shell = getShell();\r
46         if (null == shell)\r
47             return false; // no can do\r
48         try {\r
49             return Auxiliary.handleStart(shell, exception);\r
50         } catch (InternalException e) {\r
51             return false; // no could do\r
52         }\r
53     }\r
54     \r
55     @Override\r
56     public String getId() {\r
57         return ProCoreDriver.ProCoreDriverName;\r
58     }\r
59 }