]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.document.swt.core/src/org/simantics/document/swt/core/widget/ButtonWidget.java
Improvements to modelled SWT documents
[simantics/platform.git] / bundles / org.simantics.document.swt.core / src / org / simantics / document / swt / core / widget / ButtonWidget.java
1 /*******************************************************************************
2  * Copyright (c) 2019 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     Semantum Oy - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.document.swt.core.widget;
13
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.HashSet;
17 import java.util.List;
18
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;
41
42 public class ButtonWidget extends LeafWidgetManager<Button> {
43
44         @Override
45         protected void doUpdateProperties(SWTDocument document, Button control, JSONObject object) {
46
47                 if(control.isDisposed()) return;
48                 
49                 String text = object.getJSONField("text");
50                 
51                 control.setText(text);
52                 
53                 document.layout();
54                 
55         }
56     
57         @Override
58         protected Button doCreateControl(final SWTDocument document, Composite parent, final JSONObject object) {
59
60                 final Button label = new Button(parent, SWT.NONE);
61                 
62 //              final EventHandler onPress = object.getJSONField("onPress");
63 //              label.addSelectionListener(new );
64
65                 return label;
66         }
67         
68     public static class ButtonCommandManager implements CommandManager<SWTDocument, WidgetContainer<Button>> {
69
70         @Override
71         public Collection<Object> updateCommandListeners(final SWTDocument document, final JSONObject object,
72                 WidgetContainer<Button> container) {
73             
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);
85             }
86             
87             return listeners;
88             
89         }
90
91         @Override
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);
96         }
97
98     }
99
100     @Override
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);
106         }
107         return null;
108     }   
109         
110 }