]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.document.swt.core/src/org/simantics/document/swt/core/SWTViews.java
Improvements to modelled SWT documents
[simantics/platform.git] / bundles / org.simantics.document.swt.core / src / org / simantics / document / swt / core / SWTViews.java
index 468bf7bdaec4295d561e25b2f32e1212dcc06eef..3ea5da8a6d76ecfd3762a3133eb4d8e7c2d087b4 100644 (file)
@@ -1,6 +1,20 @@
+/*******************************************************************************
+ * 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;
 
+import java.util.ArrayList;
+import java.util.Collection;
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 
 import org.eclipse.swt.widgets.Composite;
@@ -8,8 +22,11 @@ import org.eclipse.swt.widgets.Control;
 import org.simantics.document.server.JSONObject;
 import org.simantics.document.server.client.CommandMapping;
 import org.simantics.document.server.client.CommandMappingImpl;
+import org.simantics.document.server.client.Document;
+import org.simantics.document.server.client.WidgetData;
 import org.simantics.document.server.client.WidgetMapping;
 import org.simantics.document.server.client.WidgetMappingImpl;
+import org.simantics.document.server.io.ICommand;
 import org.simantics.document.swt.core.base.ScrolledCompositeContent;
 import org.simantics.document.swt.core.widget.BrowserWidget;
 import org.simantics.document.swt.core.widget.ButtonWidget;
@@ -23,13 +40,14 @@ import org.simantics.document.swt.core.widget.LabelWidget;
 import org.simantics.document.swt.core.widget.SCLTextEditor;
 import org.simantics.document.swt.core.widget.ScrolledCompositeWidget;
 import org.simantics.document.swt.core.widget.TrackedTextWidget;
+import org.simantics.utils.datastructures.Pair;
 
 public class SWTViews {
 
        private static WidgetMappingImpl mapping = null;
-       
+
        public static WidgetMapping getMapping() {
-               
+
                if(mapping == null) {
                        mapping = new WidgetMappingImpl();
                        mapping.register("Root", new FillComposite());
@@ -45,25 +63,40 @@ public class SWTViews {
                        mapping.register("Browser", new BrowserWidget());
                        mapping.register("SCLTextEditor", new SCLTextEditor());
                }
-               
+
                return mapping;
-               
+
+       }
+
+       private static CommandMappingImpl commandMapping = null;
+
+       public static CommandMapping getCommandMapping() {
+
+               if(commandMapping == null) {
+                       commandMapping = new CommandMappingImpl();
+                       commandMapping.register("Button", new ButtonWidget.ButtonCommandManager());
+                       commandMapping.register("Explorer", new Explorer.ExplorerCommandManager());
+               }
+
+               return commandMapping;
+
+       }
+
+       public static List<Pair<WidgetData, ICommand>> getTriggeredCommands(Document document, Collection<ICommand> commands, String trigger) {
+               // Nulls should not get this far
+               assert(commands != null);
+               List<Pair<WidgetData, ICommand>> data = new ArrayList<>();
+               for(ICommand c : commands) {
+                       if(c.getCommand() == null || c.getTargetId() == null || c.getTrigger() == null)
+                               continue;
+                       if(trigger.equals(c.getTrigger())) {
+                               WidgetData wd = document.getWidgetData().get(c.getTargetId());
+                               if(wd != null)
+                                       data.add(new Pair<WidgetData, ICommand>(wd, c));
+                       }
+               }
+               return data;
        }
-       
-    private static CommandMappingImpl commandMapping = null;
-    
-    public static CommandMapping getCommandMapping() {
-        
-        if(commandMapping == null) {
-            
-            commandMapping = new CommandMappingImpl();
-            commandMapping.register("Button", new ButtonWidget.ButtonCommandManager());
-            
-        }
-        
-        return commandMapping;
-        
-    }
 
        public static void notifyScrolledComposite(Control c) {
                if(c instanceof ScrolledCompositeContent) {
@@ -75,17 +108,17 @@ public class SWTViews {
                if(parent == null) return;
                notifyScrolledComposite(parent);
        }
-       
+
        public static Map<String, Object> encoded = new HashMap<String, Object>();
-       
+
        public static String encode(JSONObject object, String property, Object data) {
            String key = object.getId() + "#" + property;
            encoded.put(key, data);
            return key;
        }
-       
+
        public static Object decode(String key) {
            return encoded.get(key);
        }
-       
+
 }