]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.document.swt.core/src/org/simantics/document/swt/core/widget/CommandEventWidget.java
Improvements to modelled SWT documents
[simantics/platform.git] / bundles / org.simantics.document.swt.core / src / org / simantics / document / swt / core / widget / CommandEventWidget.java
1 /*******************************************************************************
2  * Copyright (c) 2019 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     Semantum Oy - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.document.swt.core.widget;
13
14 import java.util.List;
15 import java.util.TreeMap;
16
17 import org.simantics.document.server.IEventCommand;
18 import org.simantics.document.server.JSONObject;
19 import org.simantics.document.server.bean.DataDefinition;
20 import org.simantics.document.server.client.WidgetData;
21 import org.simantics.document.server.client.WidgetManager;
22 import org.simantics.document.server.handler.AbstractEventHandler;
23 import org.simantics.document.server.io.CommandContext;
24 import org.simantics.document.server.io.CommandContextImpl;
25 import org.simantics.document.server.io.CommandContextMutable;
26 import org.simantics.document.server.io.ICommand;
27 import org.simantics.document.swt.core.SWTDocument;
28 import org.simantics.document.swt.core.base.PostEventCommand;
29 import org.simantics.document.swt.core.base.PropertyWidgetManager;
30
31 public class CommandEventWidget  extends PropertyWidgetManager<Object> {
32
33     @Override
34     public Object createWidget(JSONObject object) {
35         return null;
36     }
37
38     @Override
39     public void updateProperties(SWTDocument document, JSONObject object, Object widget) {
40         // No properties
41     }
42
43     @Override
44     public void updateChildren(SWTDocument document, JSONObject object, Object widget,
45             TreeMap<String, WidgetData> childMap) {
46         // No children
47     }
48
49     @Override
50     public IEventCommand eventCommand(SWTDocument document, JSONObject object, Object component, ICommand command, CommandContext p) {
51         if("event".equals(command.getCommand())) {
52             List<DataDefinition> dataDefinitions = object.getJSONField("dataDefinitions");
53             CommandContextMutable context = new CommandContextImpl();
54             context.merge(p);
55             for(DataDefinition dd : dataDefinitions) {
56                 WidgetData wd = document.getWidgetData().get(dd.getElementId());
57                 if(wd != null && wd.object != null) {
58                     @SuppressWarnings("rawtypes")
59                     WidgetManager manager = document.getManager(wd.object);
60                     if(manager != null) {
61                         @SuppressWarnings("unchecked")
62                         String result = manager.getProperty(document, wd.object, wd.widget, dd.getProperty());
63                         context.putValue(dd.getTarget(), result);
64                     }
65                 }
66             }
67             AbstractEventHandler handler = object.getJSONField("SCLFunction");
68             return new PostEventCommand(document, handler, context);
69         } else {
70             return null;
71         }
72     }
73     
74 }