]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.document.swt.core/src/org/simantics/document/swt/core/widget/ButtonWidget.java
Playground for Antti.
[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.ArrayList;
4 import java.util.Collection;
5 import java.util.HashSet;
6 import java.util.List;
7
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;
30
31 public class ButtonWidget extends LeafWidgetManager<Button> {
32
33         @Override
34         protected void doUpdateProperties(SWTDocument document, Button control, JSONObject object) {
35
36                 if(control.isDisposed()) return;
37                 
38                 String text = object.getJSONField("text");
39                 
40                 control.setText(text);
41                 
42                 document.layout();
43                 
44         }
45     
46         @Override
47         protected Button doCreateControl(final SWTDocument document, Composite parent, final JSONObject object) {
48
49                 final Button label = new Button(parent, SWT.NONE);
50                 
51 //              final EventHandler onPress = object.getJSONField("onPress");
52 //              label.addSelectionListener(new );
53
54                 return label;
55         }
56         
57     public static class ButtonCommandManager implements CommandManager<SWTDocument, WidgetContainer<Button>> {
58
59         @Override
60         public Collection<Object> updateCommandListeners(final SWTDocument document, final JSONObject object,
61                 WidgetContainer<Button> container) {
62             
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);
74             }
75             
76             return listeners;
77             
78         }
79
80         @Override
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);
85         }
86
87     }
88
89     @Override
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);
95         }
96         return null;
97     }   
98         
99 }