]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.document.swt.core/src/org/simantics/document/swt/core/widget/ButtonWidget.java
Improvements to modelled SWT documents
[simantics/platform.git] / bundles / org.simantics.document.swt.core / src / org / simantics / document / swt / core / widget / ButtonWidget.java
index 384ef88cd8638f23e0f17dec7005cedc52b7ca49..72c7c360b0566529d0941f58557ed9cd93d4c4eb 100644 (file)
@@ -1,11 +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.widget;
 
+import java.util.ArrayList;
 import java.util.Collection;
-import java.util.Collections;
 import java.util.HashSet;
-import java.util.LinkedHashMap;
 import java.util.List;
-import java.util.Map;
 
 import org.eclipse.swt.SWT;
 import org.eclipse.swt.events.SelectionListener;
@@ -17,11 +26,18 @@ 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.handler.EventHandler;
+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.utils.datastructures.Pair;
+import org.simantics.utils.ui.SWTUtils;
 
 public class ButtonWidget extends LeafWidgetManager<Button> {
 
@@ -55,23 +71,13 @@ public class ButtonWidget extends LeafWidgetManager<Button> {
         public Collection<Object> updateCommandListeners(final SWTDocument document, final JSONObject object,
                 WidgetContainer<Button> container) {
             
-            List<Command> commands = object.getJSONField("commands");
+            WidgetData wd = document.getWidget(JSONObjectUtils.getId(object));
+            List<ICommand> commands = object.getJSONField("commands");
             HashSet<Object> listeners = new HashSet<Object>();
-            LinkedHashMap<WidgetData, String> data = new LinkedHashMap<WidgetData, String>();
-            if(commands != null) {
-                   for(Command c : commands) {
-                       if(c.getCommand() == null || c.getTargetId() == null || c.getTrigger() == null)
-                           continue;
-                       String trigger = c.getTrigger();
-                       if("click".equals(trigger)) {
-                           WidgetData wd = document.getWidgetData().get(c.getTargetId());
-                           if(wd != null)
-                               data.put(wd, c.getCommand());
-                       }
-                   }
-            }
-            data.put(document.getWidgetData().get(object.getId()), "onPress");
-            SelectionListener listener = new ButtonSelectionListener(data);
+            List<Pair<WidgetData, ICommand>> data = new ArrayList<>();
+            data.addAll(SWTViews.getTriggeredCommands(document, commands, "eventOut"));
+            data.add(new Pair<WidgetData, ICommand>(wd, new Command("onPress")));
+            SelectionListener listener = new ButtonSelectionListener(wd, data);
             Button button = container.getControl();
             if(!button.isDisposed()) {
                button.addSelectionListener(listener);
@@ -92,11 +98,11 @@ public class ButtonWidget extends LeafWidgetManager<Button> {
     }
 
     @Override
-    public IEventCommand eventCommand(SWTDocument document, JSONObject object, WidgetContainer widget, String command) {
-        if("onPress".equals(command)) {
+    public IEventCommand eventCommand(SWTDocument document, JSONObject object, WidgetContainer widget, ICommand command, CommandContext p) {
+        if("onPress".equals(command.getCommand())) {
+            CommandContextMutable context = new CommandContextImpl().merge(p);
             AbstractEventHandler onPress = object.getJSONField("onPress");
-            Map<String, String> data = Collections.emptyMap();
-            return new PostEventCommand(document, onPress, data);
+            return new PostEventCommand(document, onPress, context);
         }
         return null;
     }