package org.simantics.document.swt.core.widget;
+import java.util.ArrayList;
import java.util.Collection;
-import java.util.Collections;
import java.util.HashSet;
-import java.util.LinkedHashMap;
import java.util.List;
-import java.util.Map;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionListener;
import org.simantics.document.server.client.CommandManager;
import org.simantics.document.server.client.WidgetData;
import org.simantics.document.server.handler.AbstractEventHandler;
-import org.simantics.document.server.handler.EventHandler;
+import org.simantics.document.server.io.CommandContext;
+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.server.io.JSONObjectUtils;
import org.simantics.document.swt.core.SWTDocument;
+import org.simantics.document.swt.core.SWTViews;
import org.simantics.document.swt.core.base.LeafWidgetManager;
import org.simantics.document.swt.core.base.PostEventCommand;
import org.simantics.document.swt.core.base.WidgetContainer;
+import org.simantics.utils.datastructures.Pair;
+import org.simantics.utils.ui.SWTUtils;
public class ButtonWidget extends LeafWidgetManager<Button> {
public Collection<Object> updateCommandListeners(final SWTDocument document, final JSONObject object,
WidgetContainer<Button> container) {
- List<Command> commands = object.getJSONField("commands");
+ WidgetData wd = document.getWidget(JSONObjectUtils.getId(object));
+ List<ICommand> commands = object.getJSONField("commands");
HashSet<Object> listeners = new HashSet<Object>();
- LinkedHashMap<WidgetData, String> data = new LinkedHashMap<WidgetData, String>();
- if(commands != null) {
- for(Command c : commands) {
- if(c.getCommand() == null || c.getTargetId() == null || c.getTrigger() == null)
- continue;
- String trigger = c.getTrigger();
- if("click".equals(trigger)) {
- WidgetData wd = document.getWidgetData().get(c.getTargetId());
- if(wd != null)
- data.put(wd, c.getCommand());
- }
- }
- }
- data.put(document.getWidgetData().get(object.getId()), "onPress");
- SelectionListener listener = new ButtonSelectionListener(data);
+ List<Pair<WidgetData, ICommand>> data = new ArrayList<>();
+ data.addAll(SWTViews.getTriggeredCommands(document, commands, "eventOut"));
+ data.add(new Pair<WidgetData, ICommand>(wd, new Command("onPress")));
+ SelectionListener listener = new ButtonSelectionListener(wd, data);
Button button = container.getControl();
if(!button.isDisposed()) {
button.addSelectionListener(listener);
}
@Override
- public IEventCommand eventCommand(SWTDocument document, JSONObject object, WidgetContainer widget, String command) {
- if("onPress".equals(command)) {
+ public IEventCommand eventCommand(SWTDocument document, JSONObject object, WidgetContainer widget, ICommand command, CommandContext p) {
+ if("onPress".equals(command.getCommand())) {
+ CommandContextMutable context = new CommandContextImpl().merge(p);
AbstractEventHandler onPress = object.getJSONField("onPress");
- Map<String, String> data = Collections.emptyMap();
- return new PostEventCommand(document, onPress, data);
+ return new PostEventCommand(document, onPress, context);
}
return null;
}