]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.document.swt.core/src/org/simantics/document/swt/core/widget/Explorer.java
Improvements to modelled SWT documents
[simantics/platform.git] / bundles / org.simantics.document.swt.core / src / org / simantics / document / swt / core / widget / Explorer.java
index deabd7573d3196eb9a0cd2a059f1c1bb20c64ae4..99f5cbd241272728f49de64a7290d7b333d245bb 100644 (file)
@@ -1,7 +1,22 @@
+/*******************************************************************************
+ * Copyright (c) 2019 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     Semantum Oy - initial API and implementation
+ *******************************************************************************/
 package org.simantics.document.swt.core.widget;
 
+import java.util.ArrayList;
+import java.util.Collection;
 import java.util.Collections;
 import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
 import java.util.Map;
 
 import org.eclipse.jface.viewers.ISelection;
@@ -11,6 +26,7 @@ import org.eclipse.swt.widgets.Composite;
 import org.eclipse.swt.widgets.Event;
 import org.eclipse.swt.widgets.Listener;
 import org.eclipse.swt.widgets.Tree;
+import org.eclipse.swt.widgets.TreeItem;
 import org.eclipse.ui.IWorkbenchSite;
 import org.simantics.Simantics;
 import org.simantics.browsing.ui.Column;
@@ -22,15 +38,28 @@ import org.simantics.browsing.ui.swt.widgets.ModelBrowser;
 import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupportImpl;
 import org.simantics.db.layer0.variable.VariableBean;
 import org.simantics.db.management.ISessionContext;
+import org.simantics.document.server.IEventCommand;
 import org.simantics.document.server.JSONObject;
+import org.simantics.document.server.bean.Command;
+import org.simantics.document.server.client.CommandManager;
+import org.simantics.document.server.client.WidgetData;
+import org.simantics.document.server.handler.AbstractEventHandler;
+import org.simantics.document.server.io.CommandContext;
+import org.simantics.document.server.io.CommandContextImpl;
+import org.simantics.document.server.io.CommandContextMutable;
+import org.simantics.document.server.io.ICommand;
+import org.simantics.document.server.io.JSONObjectUtils;
 import org.simantics.document.swt.core.SWTDocument;
 import org.simantics.document.swt.core.SWTViews;
 import org.simantics.document.swt.core.base.LeafWidgetManager;
+import org.simantics.document.swt.core.base.PostEventCommand;
 import org.simantics.document.swt.core.base.WidgetContainer;
 import org.simantics.document.swt.core.bean.ColumnBean;
 import org.simantics.document.swt.core.bean.ColumnsBean;
 import org.simantics.scl.runtime.function.Function1;
+import org.simantics.scl.runtime.function.Function2;
 import org.simantics.ui.selection.WorkbenchSelectionUtils;
+import org.simantics.utils.datastructures.Pair;
 
 public class Explorer extends LeafWidgetManager<ModelBrowser> {
 
@@ -48,24 +77,24 @@ public class Explorer extends LeafWidgetManager<ModelBrowser> {
                }
                return result;
        }
-       
+
        @Override
        protected ModelBrowser doCreateControl(SWTDocument document, Composite parent, JSONObject object) {
-               
+
                String browseContext = object.getJSONField("browseContext");
                if(browseContext == null) return null;
 
                String contextMenuId = object.getJSONField("contextMenuId");
-               
+
                Boolean displayFilter = object.getJSONField("displayFilter");
                Boolean displayHeader = object.getJSONField("displayHeader");
                Integer style = object.getJSONField("style");
                if(style == null) style = SWT.FULL_SELECTION;
-               
+
         Boolean vscroll = object.getJSONFieldDefault("VScroll", false);
         if(Boolean.TRUE.equals(vscroll))
             style |= SWT.V_SCROLL;
-        
+
         Boolean hscroll = object.getJSONFieldDefault("HScroll", false);
         if(Boolean.TRUE.equals(hscroll))
             style |= SWT.H_SCROLL;
@@ -73,12 +102,18 @@ public class Explorer extends LeafWidgetManager<ModelBrowser> {
         Boolean noscroll = object.getJSONFieldDefault("NoScroll", false);
         if(Boolean.TRUE.equals(noscroll))
             style |= SWT.NO_SCROLL;
-               
+
+        Boolean check = object.getJSONFieldDefault("Check", false);
+        if(Boolean.TRUE.equals(check))
+            style |= SWT.CHECK;
+
                StatePersistor persistor = object.getJSONField("persistor");
                final Function1<Object, Boolean> selectionListener = object.getJSONField("selectionListener");
 
+               final Function2<Object, Boolean, Boolean> checkStateListener = object.getJSONField("checkStateListener");
+
                DragSourceListenerFactory dragSourceListenerFactory = object.getJSONField("dragSourceListenerFactory");
-               
+
                final IWorkbenchSite site = document.getSite();
                final ISelectionProvider selectionProvider = document.getSelectionProvider();
 
@@ -90,27 +125,27 @@ public class Explorer extends LeafWidgetManager<ModelBrowser> {
 
                ColumnsBean columns = object.getBeanJSONFieldDefault("columns", ColumnsBean.BINDING, null);
                String editingColumn = object.getJSONFieldDefault("editingColumn", null);
-               
+
 //             Column[] COLUMNS = new Column[] {
 //                             new Column("HasDisplayProperty", "Parameter description", Align.LEFT, 80, "Input parameter name", true),
 //                             new Column("HasDisplayValue", "Value", Align.RIGHT, 65, "Value"),
 //                             new Column("HasDisplayUnit", "Unit", Align.RIGHT, 100, "Unit") 
 //             };
-               
+
 //             parent.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_RED));
-               
+
                final ModelBrowser control = new ModelBrowser(Collections.singleton(browseContext), args, site, parent, new WidgetSupportImpl(), style);
 
                Tree tree = control.getExplorerControl();
                tree.addListener(SWT.SetData, new Listener() {
-                       
+
                        @Override
                        public void handleEvent(Event event) {
                                SWTViews.notifyScrolledComposite(control);
                        }
-                       
+
                });
-               
+
                if(columns != null)
                        control.setColumns(getColumns(columns));
                if(editingColumn != null)
@@ -121,11 +156,11 @@ public class Explorer extends LeafWidgetManager<ModelBrowser> {
                        control.setDragSourceListenerFactory(dragSourceListenerFactory);
                if(displayHeader != null)
                        tree.setHeaderVisible(displayHeader);
-               
+
                control.setStatePersistor(persistor);
 
                control.finish();
-               
+
                control.setInputSource(new InputSourceImpl<Object>() {
 
                        @Override
@@ -134,7 +169,7 @@ public class Explorer extends LeafWidgetManager<ModelBrowser> {
                        }
 
                });
-               
+
                // TODO: fixme!
                Object i = object.getJSONField("explorerInput");
                if(i instanceof VariableBean) {
@@ -142,23 +177,34 @@ public class Explorer extends LeafWidgetManager<ModelBrowser> {
                } else {
                        control.setInput(Simantics.getSessionContext(), i);
                }
-               
+
                control.addListenerToControl(SWT.Selection, new Listener() {
 
                        @Override
                        public void handleEvent(Event event) {
 
+                               switch (event.type) {
+                               case SWT.Selection:
+                                       if (event.detail == SWT.CHECK && event.item != null) {
+                                               TreeItem item = (TreeItem) event.item;
+                                               boolean checked = item.getChecked();
+                                               if(checkStateListener != null)
+                                                       selectionListener.apply(event);
+                                       }
+                                       break;
+                               }
+
                                if(selectionListener != null)
                                        selectionListener.apply(event);
-                               
+
                                ISelection selection = (ISelection)control.getExplorer().getWidgetSelection();
-                               
+
                                // TODO: refactor this!
 
                                if(selectionProvider != null) {
                                        selectionProvider.setSelection(selection);
                                }
-                               
+
 //                             if (site != null) {
 //                                     ISelectionProvider sp = site.getSelectionProvider();
 //                                     if (sp != null) {
@@ -168,12 +214,12 @@ public class Explorer extends LeafWidgetManager<ModelBrowser> {
 
                        }
 
-               });     
+               });
 
                return control;
 
        }
-       
+
        @Override
        public String getProperty(SWTDocument document, JSONObject object, WidgetContainer<ModelBrowser> widget, String property) {
            if("selection".equals(property)) {
@@ -183,4 +229,46 @@ public class Explorer extends LeafWidgetManager<ModelBrowser> {
            return null;
        }
 
+    public static class ExplorerCommandManager implements CommandManager<SWTDocument, WidgetContainer<ModelBrowser>> {
+
+        @Override
+        public Collection<Object> updateCommandListeners(final SWTDocument document, final JSONObject object,
+                WidgetContainer<ModelBrowser> container) {
+
+            WidgetData wd = document.getWidget(JSONObjectUtils.getId(object));
+            List<ICommand> commands = object.getJSONField("commands");
+            HashSet<Object> listeners = new HashSet<Object>();
+            List<Pair<WidgetData, ICommand>> data = new ArrayList<>();
+            data.addAll(SWTViews.getTriggeredCommands(document, commands, "eventOut"));
+            data.add(new Pair<WidgetData, ICommand>(wd, new Command("onCheck")));
+            Listener listener = new ExplorerListener(wd, data);
+            ModelBrowser browser = container.getControl();
+            if(!browser.isDisposed()) {
+                browser.addListenerToControl(SWT.Selection, listener);
+                listeners.add(listener);
+            }
+
+            return listeners;
+
+        }
+
+        @Override
+        public void removeListener(WidgetContainer<ModelBrowser> container, Object listener) {
+            if(container.getControl().isDisposed()) return;
+            if(listener instanceof ExplorerListener)
+                container.getControl().removeListenerFromControl(SWT.Selection, (Listener)listener);
+        }
+
+    }
+
+    @Override
+    public IEventCommand eventCommand(SWTDocument document, JSONObject object, WidgetContainer widget, ICommand command, CommandContext p) {
+        if("onCheck".equals(command.getCommand())) {
+            CommandContextMutable context = new CommandContextImpl().merge(p);
+            AbstractEventHandler onCheck = object.getJSONField("onCheck");
+            return new PostEventCommand(document, onCheck, context);
+        }
+        return null;
+    }
+
 }