1 package org.simantics.document.swt.core.widget;
3 import java.util.Collection;
4 import java.util.Collections;
5 import java.util.HashSet;
6 import java.util.LinkedHashMap;
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;
26 public class ButtonWidget extends LeafWidgetManager<Button> {
29 protected void doUpdateProperties(SWTDocument document, Button control, JSONObject object) {
31 if(control.isDisposed()) return;
33 String text = object.getJSONField("text");
35 control.setText(text);
42 protected Button doCreateControl(final SWTDocument document, Composite parent, final JSONObject object) {
44 final Button label = new Button(parent, SWT.NONE);
46 // final EventHandler onPress = object.getJSONField("onPress");
47 // label.addSelectionListener(new );
52 public static class ButtonCommandManager implements CommandManager<SWTDocument, WidgetContainer<Button>> {
55 public Collection<Object> updateCommandListeners(final SWTDocument document, final JSONObject object,
56 WidgetContainer<Button> container) {
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)
65 String trigger = c.getTrigger();
66 if("click".equals(trigger)) {
67 WidgetData wd = document.getWidgetData().get(c.getTargetId());
69 data.put(wd, c.getCommand());
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);
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);
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);