import java.io.IOException;
import java.io.InputStream;
+import java.net.URI;
+import java.net.URISyntaxException;
import java.security.PrivilegedAction;
-import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import org.argeo.api.cms.CmsApp;
import org.argeo.api.cms.CmsAuth;
+import org.argeo.api.cms.CmsEventBus;
import org.argeo.api.cms.CmsLog;
import org.argeo.api.cms.CmsSession;
import org.argeo.api.cms.ux.CmsImageManager;
import org.argeo.api.cms.ux.CmsTheme;
-import org.argeo.api.cms.ux.CmsUi;
import org.argeo.api.cms.ux.CmsView;
-import org.argeo.api.cms.ux.UxContext;
+import org.argeo.cms.swt.AbstractSwtCmsView;
import org.argeo.cms.swt.CmsSwtUtils;
+import org.argeo.cms.swt.acr.AcrSwtImageManager;
import org.eclipse.e4.ui.css.core.engine.CSSEngine;
import org.eclipse.e4.ui.css.core.engine.CSSErrorHandler;
import org.eclipse.e4.ui.css.swt.engine.CSSSWTEngineImpl;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
-import org.osgi.service.event.Event;
-import org.osgi.service.event.EventAdmin;
/** Runs a {@link CmsApp} as an SWT desktop application. */
@SuppressWarnings("restriction")
-public class CmsRcpApp implements CmsView {
+public class CmsRcpApp extends AbstractSwtCmsView implements CmsView {
private final static CmsLog log = CmsLog.getLog(CmsRcpApp.class);
- // private BundleContext bundleContext =
- // FrameworkUtil.getBundle(CmsRcpApp.class).getBundleContext();
-
private Shell shell;
private CmsApp cmsApp;
- // CMS View
- private String uid;
- private LoginContext loginContext;
-
- private EventAdmin eventAdmin;
-
private CSSEngine cssEngine;
- private CmsUi ui;
- // TODO make it configurable
- private String uiName = "desktop";
+ private String httpServerBase;
public CmsRcpApp(String uiName) {
+ super(uiName);
uid = UUID.randomUUID().toString();
- this.uiName = uiName;
}
+ @SuppressWarnings("rawtypes")
public void initRcpApp() {
- Display display = Display.getCurrent();
+ imageManager = (CmsImageManager) new AcrSwtImageManager();
+
+ display = Display.getCurrent();
shell = new Shell(display);
shell.setText("Argeo CMS");
Composite parent = shell;
// Styling
CmsTheme theme = CmsSwtUtils.getCmsTheme(parent);
if (theme != null) {
- cssEngine = new CSSSWTEngineImpl(display);
- for (String path : theme.getSwtCssPaths()) {
- try (InputStream in = theme.loadPath(path)) {
- cssEngine.parseStyleSheet(in);
- } catch (IOException e) {
- throw new IllegalStateException("Cannot load stylesheet " + path, e);
+ try {
+ cssEngine = new CSSSWTEngineImpl(display);
+ for (String path : theme.getSwtCssPaths()) {
+ try (InputStream in = theme.loadPath(path)) {
+ cssEngine.parseStyleSheet(in);
+ } catch (IOException e) {
+ throw new IllegalStateException("Cannot load stylesheet " + path, e);
+ }
}
+ cssEngine.setErrorHandler(new CSSErrorHandler() {
+ public void error(Exception e) {
+ log.error("SWT styling error: ", e);
+ }
+ });
+ applyStyles(shell);
+ } catch (Throwable e) {// could be a class not found error
+ log.error("Cannot initialise RCP theming", e);
}
- cssEngine.setErrorHandler(new CSSErrorHandler() {
- public void error(Exception e) {
- log.error("SWT styling error: ", e);
- }
- });
- applyStyles(shell);
}
shell.layout(true, true);
* CMS VIEW
*/
- @Override
- public String getUid() {
- return uid;
- }
-
- @Override
- public UxContext getUxContext() {
- throw new UnsupportedOperationException();
- }
-
@Override
public void navigateTo(String state) {
throw new UnsupportedOperationException();
log.error("Unexpected exception in CMS RCP", e);
}
- @Override
- public CmsImageManager getImageManager() {
- throw new UnsupportedOperationException();
- }
-
@Override
public CmsSession getCmsSession() {
CmsSession cmsSession = cmsApp.getCmsContext().getCmsSession(getSubject());
return cmsSession;
}
- @Override
- public Object getData(String key) {
- if (ui != null) {
- return ui.getData(key);
- } else {
- throw new IllegalStateException("UI is not initialized");
- }
- }
-
- @Override
- public void setData(String key, Object value) {
- if (ui != null) {
- ui.setData(key, value);
- } else {
- throw new IllegalStateException("UI is not initialized");
- }
- }
-
@Override
public boolean isAnonymous() {
return false;
cssEngine.applyStyles(node, true);
}
+ public Shell getShell() {
+ return shell;
+ }
+
@Override
- public void sendEvent(String topic, Map<String, Object> properties) {
- if (properties == null)
- properties = new HashMap<>();
- if (properties.containsKey(CMS_VIEW_UID_PROPERTY) && !properties.get(CMS_VIEW_UID_PROPERTY).equals(uid))
- throw new IllegalArgumentException("Property " + CMS_VIEW_UID_PROPERTY + " is set to another CMS view uid ("
- + properties.get(CMS_VIEW_UID_PROPERTY) + ") then " + uid);
- properties.put(CMS_VIEW_UID_PROPERTY, uid);
- eventAdmin.sendEvent(new Event(topic, properties));
+ public CmsEventBus getCmsEventBus() {
+ return cmsApp.getCmsContext().getCmsEventBus();
}
- public <T> T doAs(PrivilegedAction<T> action) {
- return Subject.doAs(getSubject(), action);
+ @Override
+ public CmsApp getCmsApp() {
+ return cmsApp;
}
- protected Subject getSubject() {
- return loginContext.getSubject();
+ @Override
+ public URI toBackendUri(String url) {
+ try {
+ URI u = new URI(url);
+ if (u.getHost() == null) {
+ // TODO make it more robust
+ u = new URI(httpServerBase + url);
+ }
+ return u;
+ } catch (URISyntaxException e) {
+ throw new IllegalArgumentException("Cannot convert " + url, e);
+ }
}
- public Shell getShell() {
- return shell;
+ public void setHttpServerBase(String httpServerBase) {
+ this.httpServerBase = httpServerBase;
}
/*
this.cmsApp = null;
}
- public void setEventAdmin(EventAdmin eventAdmin) {
- this.eventAdmin = eventAdmin;
- }
-
}