]> git.argeo.org Git - lgpl/argeo-commons.git/blob - ui/rcp/CmsRcpDisplayFactory.java
Prepare next development cycle
[lgpl/argeo-commons.git] / ui / rcp / CmsRcpDisplayFactory.java
1 package org.argeo.cms.ui.rcp;
2
3 import java.lang.System.Logger;
4 import java.lang.System.Logger.Level;
5 import java.nio.file.Path;
6
7 import org.argeo.api.cms.CmsApp;
8 import org.argeo.cms.util.OS;
9 import org.eclipse.swt.events.DisposeListener;
10 import org.eclipse.swt.widgets.Display;
11
12 /** Creates the SWT {@link Display} in a dedicated thread. */
13 public class CmsRcpDisplayFactory {
14 private final static Logger logger = System.getLogger(CmsRcpDisplayFactory.class.getName());
15
16 /** File name in a run directory */
17 private final static String ARGEO_RCP_URL = "argeo.rcp.url";
18
19 /** There is only one display in RCP mode */
20 private static Display display;
21
22 private CmsUiThread uiThread;
23
24 private boolean shutdown = false;
25
26 public void init() {
27 uiThread = new CmsUiThread();
28 uiThread.start();
29 while (display == null)
30 try {
31 Thread.sleep(100);
32 } catch (InterruptedException e) {
33 // silent
34 }
35 }
36
37 public void destroy() {
38 shutdown = true;
39 display.wake();
40 try {
41 uiThread.join();
42 } catch (InterruptedException e) {
43 // silent
44 } finally {
45 uiThread = null;
46 }
47 }
48
49 class CmsUiThread extends Thread {
50
51 public CmsUiThread() {
52 super("CMS UI");
53 }
54
55 @Override
56 public void run() {
57 try {
58 display = Display.getDefault();
59 display.setRuntimeExceptionHandler((e) -> e.printStackTrace());
60 display.setErrorHandler((e) -> e.printStackTrace());
61
62 while (!shutdown) {
63 if (!display.readAndDispatch())
64 display.sleep();
65 }
66 display.dispose();
67 display = null;
68 } catch (UnsatisfiedLinkError e) {
69 logger.log(Level.ERROR,
70 "Cannot load SWT, probably because the OSGi framework has been refresh. Restart the application.",
71 e);
72 }
73 }
74 }
75
76 public static Display getDisplay() {
77 return display;
78 }
79
80 public static void openCmsApp(CmsApp cmsApp, String uiName, DisposeListener disposeListener) {
81 CmsRcpDisplayFactory.getDisplay().syncExec(() -> {
82 CmsRcpApp cmsRcpApp = new CmsRcpApp(uiName);
83 cmsRcpApp.setCmsApp(cmsApp, null);
84 cmsRcpApp.initRcpApp();
85 if (disposeListener != null)
86 cmsRcpApp.getShell().addDisposeListener(disposeListener);
87 });
88 }
89
90 public static Path getUrlRunFile() {
91 return OS.getRunDir().resolve(CmsRcpDisplayFactory.ARGEO_RCP_URL);
92 }
93 }