]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.document.swt.core/src/org/simantics/document/swt/core/widget/ButtonSelectionListener.java
Improvements to modelled SWT documents
[simantics/platform.git] / bundles / org.simantics.document.swt.core / src / org / simantics / document / swt / core / widget / ButtonSelectionListener.java
index 021ba711215d94a472801db100a76ca0de2cf343..453d1d0a7261d1c80ba0688475490f25e65730cc 100644 (file)
@@ -1,50 +1,55 @@
-package org.simantics.document.swt.core.widget;\r
-\r
-import java.util.ArrayList;\r
-import java.util.LinkedHashMap;\r
-\r
-import org.eclipse.swt.events.SelectionEvent;\r
-import org.eclipse.swt.events.SelectionListener;\r
-import org.simantics.document.server.IEventCommand;\r
-import org.simantics.document.server.client.WidgetData;\r
-import org.simantics.document.swt.core.SWTDocument;\r
-\r
-public class ButtonSelectionListener implements SelectionListener {\r
-    \r
-    private LinkedHashMap<WidgetData, String> data;\r
-    \r
-    public ButtonSelectionListener(LinkedHashMap<WidgetData, String> data) {\r
-        this.data = data;\r
-    }\r
-\r
-    @Override\r
-    public void widgetSelected(SelectionEvent e) {\r
-        widgetDefaultSelected(e);\r
-    }\r
-\r
-    @Override\r
-    public void widgetDefaultSelected(SelectionEvent e) {\r
-        SWTDocument document = null;\r
-        // Build a linked list of commands\r
-        ArrayList<IEventCommand> commands = new ArrayList<IEventCommand>();\r
-        for(WidgetData d : data.keySet()) {\r
-            document = (SWTDocument)d.document; // assume that all widgets are from the same document\r
-            IEventCommand p = d.eventCommand(data.get(d));\r
-            if(p != null) {\r
-                if(!commands.isEmpty())\r
-                    commands.get(commands.size()-1).setNext(p);\r
-                commands.add(p);\r
-            }\r
-        }\r
-\r
-        // empty errors\r
-//        if(document != null)\r
-//            document.displayError("");\r
-\r
-        // Execute the first command, the linked list handles the rest of them\r
-        if(!commands.isEmpty())\r
-            commands.get(0).handleCommand();\r
-    \r
-    }\r
-    \r
+/*******************************************************************************
+ * 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.List;
+
+import org.eclipse.swt.events.SelectionEvent;
+import org.eclipse.swt.events.SelectionListener;
+import org.simantics.document.server.client.WidgetData;
+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.swt.core.SWTDocument;
+import org.simantics.document.swt.core.base.PropertyWidgetManager;
+import org.simantics.utils.datastructures.Pair;
+
+public class ButtonSelectionListener implements SelectionListener {
+
+    private WidgetData wd;
+    private List<Pair<WidgetData, ICommand>> data;
+
+    public ButtonSelectionListener(WidgetData wd, List<Pair<WidgetData, ICommand>> data) {
+        this.wd = wd;
+        this.data = data;
+    }
+
+    @Override
+    public void widgetSelected(SelectionEvent e) {
+        widgetDefaultSelected(e);
+    }
+
+    @Override
+    public void widgetDefaultSelected(SelectionEvent e) {
+
+        CommandContextMutable context = new CommandContextImpl();
+        context.putValue("event", "onPress");
+
+        if(!data.isEmpty()) {
+            ((SWTDocument)wd.document).handleCommands(data, context, e.widget);
+        }
+
+        PropertyWidgetManager.sendEvent((SWTDocument)wd.document, wd, "onPress", e.widget, context);
+
+    }
+
 }
\ No newline at end of file