+ AppUi ui = (AppUi) cmsUi;
+ if (state == null)
+ return;
+ if (!state.startsWith("/")) {
+// if (cmsUi instanceof SwtAppUi) {
+// SwtAppUi ui = (SwtAppUi) cmsUi;
+ if (LOGIN.equals(state)) {
+ String appTitle = "";
+ if (ui.getTitle() != null)
+ appTitle = ui.getTitle().lead();
+ ui.getCmsView().stateChanged(state, appTitle);
+ return;
+ }
+ Map<String, Object> properties = new HashMap<>();
+ String layerId = HOME_STATE.equals(state) ? defaultLayerPid : state;
+ properties.put(SuiteUxEvent.LAYER, layerId);
+ properties.put(SuiteUxEvent.CONTENT_PATH, HOME_STATE);
+ ui.getCmsView().sendEvent(SuiteUxEvent.switchLayer.topic(), properties);
+// }
+ return;
+ }
+// SwtAppUi suiteUi = (SwtAppUi) cmsUi;
+ if (ui.isLoginScreen()) {
+ return;
+ }
+
+ Content node = stateToNode(ui, state);
+ if (node == null) {
+ ui.getCmsView().navigateTo(HOME_STATE);
+ } else {
+ ui.getCmsView().sendEvent(SuiteUxEvent.switchLayer.topic(), SuiteUxEvent.eventProperties(node));
+ ui.getCmsView().sendEvent(SuiteUxEvent.refreshPart.topic(), SuiteUxEvent.eventProperties(node));
+ }
+ }
+
+ // TODO move it to an internal package?
+ private static String nodeToState(Content node) {
+ return node.getPath();
+ }
+
+ private Content stateToNode(CmsUi suiteUi, String state) {
+ if (suiteUi == null)
+ return null;
+ if (state == null || !state.startsWith("/"))
+ return null;
+
+ String path = state;
+
+ ProvidedSession contentSession = (ProvidedSession) CmsUxUtils.getContentSession(contentRepository,
+ suiteUi.getCmsView());
+ return contentSession.get(path);
+ }
+
+ /*
+ * Events management
+ */
+
+ @Override
+ public void onEvent(String topic, Map<String, Object> event) {
+
+ // Specific UI related events
+ SwtAppUi ui = getRelatedUi(event);
+ if (ui == null)
+ return;
+ ui.updateLastAccess();
+ ui.getCmsView().runAs(() -> {
+ try {
+ String appTitle = "";
+ if (ui.getTitle() != null)
+ appTitle = ui.getTitle().lead();
+
+ if (isTopic(topic, SuiteUxEvent.refreshPart)) {
+ Content node = getContentFromEvent(ui, event);
+ if (node == null)
+ return;
+ SwtUiProvider uiProvider = findByType(uiProvidersByType, node);
+ SwtAppLayer layer = findByType(layersByType, node);
+ ui.switchToLayer(layer, node);
+ layer.view(uiProvider, ui.getCurrentWorkArea(), node);
+ ui.getCmsView().stateChanged(nodeToState(node), stateTitle(appTitle, CmsUxUtils.getTitle(node)));
+ } else if (isTopic(topic, SuiteUxEvent.openNewPart)) {
+ Content node = getContentFromEvent(ui, event);
+ if (node == null)
+ return;
+ SwtUiProvider uiProvider = findByType(uiProvidersByType, node);
+ SwtAppLayer layer = findByType(layersByType, node);
+ ui.switchToLayer(layer, node);
+ layer.open(uiProvider, ui.getCurrentWorkArea(), node);
+ ui.getCmsView().stateChanged(nodeToState(node), stateTitle(appTitle, CmsUxUtils.getTitle(node)));
+ } else if (isTopic(topic, SuiteUxEvent.switchLayer)) {
+ String layerId = get(event, SuiteUxEvent.LAYER);
+ if (layerId != null) {
+ SwtAppLayer suiteLayer = findLayer(layerId);
+ if (suiteLayer == null)
+ throw new IllegalArgumentException("No layer '" + layerId + "' available.");
+ Localized layerTitle = suiteLayer.getTitle();
+ // FIXME make sure we don't rebuild the work area twice
+ Composite workArea = ui.switchToLayer(layerId, ui.getUserDir());
+ String title = null;
+ if (layerTitle != null)
+ title = layerTitle.lead();
+ Content nodeFromState = getContentFromEvent(ui, event);
+ if (nodeFromState != null && nodeFromState.getPath().equals(ui.getUserDir().getPath())) {
+ // default layer view is forced
+ String state = defaultLayerPid.equals(layerId) ? "~" : layerId;
+ ui.getCmsView().stateChanged(state, stateTitle(appTitle, title));
+ suiteLayer.view(null, workArea, nodeFromState);
+ } else {
+ Content layerCurrentContext = suiteLayer.getCurrentContext(workArea);
+ if (layerCurrentContext != null && !layerCurrentContext.equals(ui.getUserDir())) {
+ // layer was already showing a context so we set the state to it
+ ui.getCmsView().stateChanged(nodeToState(layerCurrentContext),
+ stateTitle(appTitle, CmsUxUtils.getTitle(layerCurrentContext)));
+ } else {
+ // no context was shown
+ ui.getCmsView().stateChanged(layerId, stateTitle(appTitle, title));
+ }
+ }
+ } else {
+ Content node = getContentFromEvent(ui, event);
+ if (node != null) {
+ SwtAppLayer layer = findByType(layersByType, node);
+ ui.switchToLayer(layer, node);
+ }
+ }
+ }
+ } catch (Exception e) {
+ CmsFeedback.error("Cannot handle event " + topic + " " + event, e);
+// log.error("Cannot handle event " + event, e);
+ }
+ });
+ }
+
+ private String stateTitle(String appTitle, String additionalTitle) {
+ return additionalTitle == null ? appTitle : appTitle + " - " + additionalTitle;
+ }
+
+ private boolean isTopic(String topic, CmsEvent cmsEvent) {
+ Objects.requireNonNull(topic);
+ return topic.equals(cmsEvent.topic());
+ }
+
+ protected Content getContentFromEvent(SwtAppUi ui, Map<String, Object> event) {
+ ProvidedSession contentSession = (ProvidedSession) CmsUxUtils.getContentSession(contentRepository,
+ ui.getCmsView());
+
+ String path = get(event, SuiteUxEvent.CONTENT_PATH);