]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.document.swt.core/src/org/simantics/document/swt/core/widget/ButtonWidget.java
384ef88cd8638f23e0f17dec7005cedc52b7ca49
[simantics/platform.git] / bundles / org.simantics.document.swt.core / src / org / simantics / document / swt / core / widget / ButtonWidget.java
1 package org.simantics.document.swt.core.widget;
2
3 import java.util.Collection;
4 import java.util.Collections;
5 import java.util.HashSet;
6 import java.util.LinkedHashMap;
7 import java.util.List;
8 import java.util.Map;
9
10 import org.eclipse.swt.SWT;
11 import org.eclipse.swt.events.SelectionListener;
12 import org.eclipse.swt.widgets.Button;
13 import org.eclipse.swt.widgets.Composite;
14 import org.simantics.document.server.IEventCommand;
15 import org.simantics.document.server.JSONObject;
16 import org.simantics.document.server.bean.Command;
17 import org.simantics.document.server.client.CommandManager;
18 import org.simantics.document.server.client.WidgetData;
19 import org.simantics.document.server.handler.AbstractEventHandler;
20 import org.simantics.document.server.handler.EventHandler;
21 import org.simantics.document.swt.core.SWTDocument;
22 import org.simantics.document.swt.core.base.LeafWidgetManager;
23 import org.simantics.document.swt.core.base.PostEventCommand;
24 import org.simantics.document.swt.core.base.WidgetContainer;
25
26 public class ButtonWidget extends LeafWidgetManager<Button> {
27
28         @Override
29         protected void doUpdateProperties(SWTDocument document, Button control, JSONObject object) {
30
31                 if(control.isDisposed()) return;
32                 
33                 String text = object.getJSONField("text");
34                 
35                 control.setText(text);
36                 
37                 document.layout();
38                 
39         }
40     
41         @Override
42         protected Button doCreateControl(final SWTDocument document, Composite parent, final JSONObject object) {
43
44                 final Button label = new Button(parent, SWT.NONE);
45                 
46 //              final EventHandler onPress = object.getJSONField("onPress");
47 //              label.addSelectionListener(new );
48
49                 return label;
50         }
51         
52     public static class ButtonCommandManager implements CommandManager<SWTDocument, WidgetContainer<Button>> {
53
54         @Override
55         public Collection<Object> updateCommandListeners(final SWTDocument document, final JSONObject object,
56                 WidgetContainer<Button> container) {
57             
58             List<Command> commands = object.getJSONField("commands");
59             HashSet<Object> listeners = new HashSet<Object>();
60             LinkedHashMap<WidgetData, String> data = new LinkedHashMap<WidgetData, String>();
61             if(commands != null) {
62                     for(Command c : commands) {
63                         if(c.getCommand() == null || c.getTargetId() == null || c.getTrigger() == null)
64                             continue;
65                         String trigger = c.getTrigger();
66                         if("click".equals(trigger)) {
67                             WidgetData wd = document.getWidgetData().get(c.getTargetId());
68                             if(wd != null)
69                                 data.put(wd, c.getCommand());
70                         }
71                     }
72             }
73             data.put(document.getWidgetData().get(object.getId()), "onPress");
74             SelectionListener listener = new ButtonSelectionListener(data);
75             Button button = container.getControl();
76             if(!button.isDisposed()) {
77                 button.addSelectionListener(listener);
78                 listeners.add(listener);
79             }
80             
81             return listeners;
82             
83         }
84
85         @Override
86         public void removeListener(WidgetContainer<Button> container, Object listener) {
87                 if(container.getControl().isDisposed()) return;
88             if(listener instanceof SelectionListener)
89                 container.getControl().removeSelectionListener((SelectionListener) listener);
90         }
91
92     }
93
94     @Override
95     public IEventCommand eventCommand(SWTDocument document, JSONObject object, WidgetContainer widget, String command) {
96         if("onPress".equals(command)) {
97             AbstractEventHandler onPress = object.getJSONField("onPress");
98             Map<String, String> data = Collections.emptyMap();
99             return new PostEventCommand(document, onPress, data);
100         }
101         return null;
102     }   
103         
104 }