1 /*******************************************************************************
2 * Copyright (c) 2019 Association for Decentralized Information Management
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
10 * Semantum Oy - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.document.swt.core.widget;
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.HashSet;
17 import java.util.List;
19 import org.eclipse.swt.SWT;
20 import org.eclipse.swt.events.SelectionListener;
21 import org.eclipse.swt.widgets.Button;
22 import org.eclipse.swt.widgets.Composite;
23 import org.simantics.document.server.IEventCommand;
24 import org.simantics.document.server.JSONObject;
25 import org.simantics.document.server.bean.Command;
26 import org.simantics.document.server.client.CommandManager;
27 import org.simantics.document.server.client.WidgetData;
28 import org.simantics.document.server.handler.AbstractEventHandler;
29 import org.simantics.document.server.io.CommandContext;
30 import org.simantics.document.server.io.CommandContextImpl;
31 import org.simantics.document.server.io.CommandContextMutable;
32 import org.simantics.document.server.io.ICommand;
33 import org.simantics.document.server.io.JSONObjectUtils;
34 import org.simantics.document.swt.core.SWTDocument;
35 import org.simantics.document.swt.core.SWTViews;
36 import org.simantics.document.swt.core.base.LeafWidgetManager;
37 import org.simantics.document.swt.core.base.PostEventCommand;
38 import org.simantics.document.swt.core.base.WidgetContainer;
39 import org.simantics.utils.datastructures.Pair;
40 import org.simantics.utils.ui.SWTUtils;
42 public class ButtonWidget extends LeafWidgetManager<Button> {
45 protected void doUpdateProperties(SWTDocument document, Button control, JSONObject object) {
47 if(control.isDisposed()) return;
49 String text = object.getJSONField("text");
51 control.setText(text);
58 protected Button doCreateControl(final SWTDocument document, Composite parent, final JSONObject object) {
60 final Button label = new Button(parent, SWT.NONE);
62 // final EventHandler onPress = object.getJSONField("onPress");
63 // label.addSelectionListener(new );
68 public static class ButtonCommandManager implements CommandManager<SWTDocument, WidgetContainer<Button>> {
71 public Collection<Object> updateCommandListeners(final SWTDocument document, final JSONObject object,
72 WidgetContainer<Button> container) {
74 WidgetData wd = document.getWidget(JSONObjectUtils.getId(object));
75 List<ICommand> commands = object.getJSONField("commands");
76 HashSet<Object> listeners = new HashSet<Object>();
77 List<Pair<WidgetData, ICommand>> data = new ArrayList<>();
78 data.addAll(SWTViews.getTriggeredCommands(document, commands, "eventOut"));
79 data.add(new Pair<WidgetData, ICommand>(wd, new Command("onPress")));
80 SelectionListener listener = new ButtonSelectionListener(wd, data);
81 Button button = container.getControl();
82 if(!button.isDisposed()) {
83 button.addSelectionListener(listener);
84 listeners.add(listener);
92 public void removeListener(WidgetContainer<Button> container, Object listener) {
93 if(container.getControl().isDisposed()) return;
94 if(listener instanceof SelectionListener)
95 container.getControl().removeSelectionListener((SelectionListener) listener);
101 public IEventCommand eventCommand(SWTDocument document, JSONObject object, WidgetContainer widget, ICommand command, CommandContext p) {
102 if("onPress".equals(command.getCommand())) {
103 CommandContextMutable context = new CommandContextImpl().merge(p);
104 AbstractEventHandler onPress = object.getJSONField("onPress");
105 return new PostEventCommand(document, onPress, context);