1 package org.simantics.document.swt.core.widget;
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.HashSet;
8 import org.eclipse.swt.SWT;
9 import org.eclipse.swt.events.SelectionListener;
10 import org.eclipse.swt.widgets.Button;
11 import org.eclipse.swt.widgets.Composite;
12 import org.simantics.document.server.IEventCommand;
13 import org.simantics.document.server.JSONObject;
14 import org.simantics.document.server.bean.Command;
15 import org.simantics.document.server.client.CommandManager;
16 import org.simantics.document.server.client.WidgetData;
17 import org.simantics.document.server.handler.AbstractEventHandler;
18 import org.simantics.document.server.io.CommandContext;
19 import org.simantics.document.server.io.CommandContextImpl;
20 import org.simantics.document.server.io.CommandContextMutable;
21 import org.simantics.document.server.io.ICommand;
22 import org.simantics.document.server.io.JSONObjectUtils;
23 import org.simantics.document.swt.core.SWTDocument;
24 import org.simantics.document.swt.core.SWTViews;
25 import org.simantics.document.swt.core.base.LeafWidgetManager;
26 import org.simantics.document.swt.core.base.PostEventCommand;
27 import org.simantics.document.swt.core.base.WidgetContainer;
28 import org.simantics.utils.datastructures.Pair;
29 import org.simantics.utils.ui.SWTUtils;
31 public class ButtonWidget extends LeafWidgetManager<Button> {
34 protected void doUpdateProperties(SWTDocument document, Button control, JSONObject object) {
36 if(control.isDisposed()) return;
38 String text = object.getJSONField("text");
40 control.setText(text);
47 protected Button doCreateControl(final SWTDocument document, Composite parent, final JSONObject object) {
49 final Button label = new Button(parent, SWT.NONE);
51 // final EventHandler onPress = object.getJSONField("onPress");
52 // label.addSelectionListener(new );
57 public static class ButtonCommandManager implements CommandManager<SWTDocument, WidgetContainer<Button>> {
60 public Collection<Object> updateCommandListeners(final SWTDocument document, final JSONObject object,
61 WidgetContainer<Button> container) {
63 WidgetData wd = document.getWidget(JSONObjectUtils.getId(object));
64 List<ICommand> commands = object.getJSONField("commands");
65 HashSet<Object> listeners = new HashSet<Object>();
66 List<Pair<WidgetData, ICommand>> data = new ArrayList<>();
67 data.addAll(SWTViews.getTriggeredCommands(document, commands, "eventOut"));
68 data.add(new Pair<WidgetData, ICommand>(wd, new Command("onPress")));
69 SelectionListener listener = new ButtonSelectionListener(wd, data);
70 Button button = container.getControl();
71 if(!button.isDisposed()) {
72 button.addSelectionListener(listener);
73 listeners.add(listener);
81 public void removeListener(WidgetContainer<Button> container, Object listener) {
82 if(container.getControl().isDisposed()) return;
83 if(listener instanceof SelectionListener)
84 container.getControl().removeSelectionListener((SelectionListener) listener);
90 public IEventCommand eventCommand(SWTDocument document, JSONObject object, WidgetContainer widget, ICommand command, CommandContext p) {
91 if("onPress".equals(command.getCommand())) {
92 CommandContextMutable context = new CommandContextImpl().merge(p);
93 AbstractEventHandler onPress = object.getJSONField("onPress");
94 return new PostEventCommand(document, onPress, context);