]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.document.swt.core/src/org/simantics/document/swt/core/widget/Explorer.java
Improvements to modelled SWT documents
[simantics/platform.git] / bundles / org.simantics.document.swt.core / src / org / simantics / document / swt / core / widget / Explorer.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.Collections;
17 import java.util.HashMap;
18 import java.util.HashSet;
19 import java.util.List;
20 import java.util.Map;
21
22 import org.eclipse.jface.viewers.ISelection;
23 import org.eclipse.jface.viewers.ISelectionProvider;
24 import org.eclipse.swt.SWT;
25 import org.eclipse.swt.widgets.Composite;
26 import org.eclipse.swt.widgets.Event;
27 import org.eclipse.swt.widgets.Listener;
28 import org.eclipse.swt.widgets.Tree;
29 import org.eclipse.swt.widgets.TreeItem;
30 import org.eclipse.ui.IWorkbenchSite;
31 import org.simantics.Simantics;
32 import org.simantics.browsing.ui.Column;
33 import org.simantics.browsing.ui.Column.Align;
34 import org.simantics.browsing.ui.StatePersistor;
35 import org.simantics.browsing.ui.swt.InputSourceImpl;
36 import org.simantics.browsing.ui.swt.widgets.DragSourceListenerFactory;
37 import org.simantics.browsing.ui.swt.widgets.ModelBrowser;
38 import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupportImpl;
39 import org.simantics.db.layer0.variable.VariableBean;
40 import org.simantics.db.management.ISessionContext;
41 import org.simantics.document.server.IEventCommand;
42 import org.simantics.document.server.JSONObject;
43 import org.simantics.document.server.bean.Command;
44 import org.simantics.document.server.client.CommandManager;
45 import org.simantics.document.server.client.WidgetData;
46 import org.simantics.document.server.handler.AbstractEventHandler;
47 import org.simantics.document.server.io.CommandContext;
48 import org.simantics.document.server.io.CommandContextImpl;
49 import org.simantics.document.server.io.CommandContextMutable;
50 import org.simantics.document.server.io.ICommand;
51 import org.simantics.document.server.io.JSONObjectUtils;
52 import org.simantics.document.swt.core.SWTDocument;
53 import org.simantics.document.swt.core.SWTViews;
54 import org.simantics.document.swt.core.base.LeafWidgetManager;
55 import org.simantics.document.swt.core.base.PostEventCommand;
56 import org.simantics.document.swt.core.base.WidgetContainer;
57 import org.simantics.document.swt.core.bean.ColumnBean;
58 import org.simantics.document.swt.core.bean.ColumnsBean;
59 import org.simantics.scl.runtime.function.Function1;
60 import org.simantics.scl.runtime.function.Function2;
61 import org.simantics.ui.selection.WorkbenchSelectionUtils;
62 import org.simantics.utils.datastructures.Pair;
63
64 public class Explorer extends LeafWidgetManager<ModelBrowser> {
65
66         @Override
67         protected void doUpdateProperties(SWTDocument document, ModelBrowser control, JSONObject object) {
68                 //                      String text = object.getField("text");
69                 //                      control.setText(text);
70         }
71
72         protected Column[] getColumns(ColumnsBean bean) {
73                 Column[] result = new Column[bean.columns.length];
74                 for(int i=0;i<bean.columns.length;i++) {
75                         ColumnBean cb = bean.columns[i];
76                         result[i] = new Column(cb.key, cb.label, Align.valueOf(cb.alignment), cb.width, cb.tooltip, cb.grab, cb.weight);
77                 }
78                 return result;
79         }
80
81         @Override
82         protected ModelBrowser doCreateControl(SWTDocument document, Composite parent, JSONObject object) {
83
84                 String browseContext = object.getJSONField("browseContext");
85                 if(browseContext == null) return null;
86
87                 String contextMenuId = object.getJSONField("contextMenuId");
88
89                 Boolean displayFilter = object.getJSONField("displayFilter");
90                 Boolean displayHeader = object.getJSONField("displayHeader");
91                 Integer style = object.getJSONField("style");
92                 if(style == null) style = SWT.FULL_SELECTION;
93
94         Boolean vscroll = object.getJSONFieldDefault("VScroll", false);
95         if(Boolean.TRUE.equals(vscroll))
96             style |= SWT.V_SCROLL;
97
98         Boolean hscroll = object.getJSONFieldDefault("HScroll", false);
99         if(Boolean.TRUE.equals(hscroll))
100             style |= SWT.H_SCROLL;
101
102         Boolean noscroll = object.getJSONFieldDefault("NoScroll", false);
103         if(Boolean.TRUE.equals(noscroll))
104             style |= SWT.NO_SCROLL;
105
106         Boolean check = object.getJSONFieldDefault("Check", false);
107         if(Boolean.TRUE.equals(check))
108             style |= SWT.CHECK;
109
110                 StatePersistor persistor = object.getJSONField("persistor");
111                 final Function1<Object, Boolean> selectionListener = object.getJSONField("selectionListener");
112
113                 final Function2<Object, Boolean, Boolean> checkStateListener = object.getJSONField("checkStateListener");
114
115                 DragSourceListenerFactory dragSourceListenerFactory = object.getJSONField("dragSourceListenerFactory");
116
117                 final IWorkbenchSite site = document.getSite();
118                 final ISelectionProvider selectionProvider = document.getSelectionProvider();
119
120                 Map<String, Object> args = new HashMap<String, Object>();
121                 args.put("displaySelectors", Boolean.FALSE);
122                 args.put("displayFilter", displayFilter != null ? displayFilter : Boolean.FALSE);
123
124 //              GridLayoutFactory.fillDefaults().applyTo(parent);
125
126                 ColumnsBean columns = object.getBeanJSONFieldDefault("columns", ColumnsBean.BINDING, null);
127                 String editingColumn = object.getJSONFieldDefault("editingColumn", null);
128
129 //              Column[] COLUMNS = new Column[] {
130 //                              new Column("HasDisplayProperty", "Parameter description", Align.LEFT, 80, "Input parameter name", true),
131 //                              new Column("HasDisplayValue", "Value", Align.RIGHT, 65, "Value"),
132 //                              new Column("HasDisplayUnit", "Unit", Align.RIGHT, 100, "Unit") 
133 //              };
134
135 //              parent.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_RED));
136
137                 final ModelBrowser control = new ModelBrowser(Collections.singleton(browseContext), args, site, parent, new WidgetSupportImpl(), style);
138
139                 Tree tree = control.getExplorerControl();
140                 tree.addListener(SWT.SetData, new Listener() {
141
142                         @Override
143                         public void handleEvent(Event event) {
144                                 SWTViews.notifyScrolledComposite(control);
145                         }
146
147                 });
148
149                 if(columns != null)
150                         control.setColumns(getColumns(columns));
151                 if(editingColumn != null)
152                         control.setEditingColumn(editingColumn);
153                 if(contextMenuId != null)
154                         control.setContextMenuId(contextMenuId);
155                 if(dragSourceListenerFactory != null)
156                         control.setDragSourceListenerFactory(dragSourceListenerFactory);
157                 if(displayHeader != null)
158                         tree.setHeaderVisible(displayHeader);
159
160                 control.setStatePersistor(persistor);
161
162                 control.finish();
163
164                 control.setInputSource(new InputSourceImpl<Object>() {
165
166                         @Override
167                         public Object get(ISessionContext ctx, Object selection) {
168                                 return selection;
169                         }
170
171                 });
172
173                 // TODO: fixme!
174                 Object i = object.getJSONField("explorerInput");
175                 if(i instanceof VariableBean) {
176                         control.setInput(Simantics.getSessionContext(), ((VariableBean)i).getVariable());
177                 } else {
178                         control.setInput(Simantics.getSessionContext(), i);
179                 }
180
181                 control.addListenerToControl(SWT.Selection, new Listener() {
182
183                         @Override
184                         public void handleEvent(Event event) {
185
186                                 switch (event.type) {
187                                 case SWT.Selection:
188                                         if (event.detail == SWT.CHECK && event.item != null) {
189                                                 TreeItem item = (TreeItem) event.item;
190                                                 boolean checked = item.getChecked();
191                                                 if(checkStateListener != null)
192                                                         selectionListener.apply(event);
193                                         }
194                                         break;
195                                 }
196
197                                 if(selectionListener != null)
198                                         selectionListener.apply(event);
199
200                                 ISelection selection = (ISelection)control.getExplorer().getWidgetSelection();
201
202                                 // TODO: refactor this!
203
204                                 if(selectionProvider != null) {
205                                         selectionProvider.setSelection(selection);
206                                 }
207
208 //                              if (site != null) {
209 //                                      ISelectionProvider sp = site.getSelectionProvider();
210 //                                      if (sp != null) {
211 //                                              sp.setSelection(selection);
212 //                                      }
213 //                              }
214
215                         }
216
217                 });
218
219                 return control;
220
221         }
222
223         @Override
224         public String getProperty(SWTDocument document, JSONObject object, WidgetContainer<ModelBrowser> widget, String property) {
225             if("selection".equals(property)) {
226             ISelection selection = (ISelection)widget.getControl().getExplorer().getWidgetSelection();
227             return SWTViews.encode(object, property, WorkbenchSelectionUtils.getWorkbenchSelectionElements(selection));
228             }
229             return null;
230         }
231
232     public static class ExplorerCommandManager implements CommandManager<SWTDocument, WidgetContainer<ModelBrowser>> {
233
234         @Override
235         public Collection<Object> updateCommandListeners(final SWTDocument document, final JSONObject object,
236                 WidgetContainer<ModelBrowser> container) {
237
238             WidgetData wd = document.getWidget(JSONObjectUtils.getId(object));
239             List<ICommand> commands = object.getJSONField("commands");
240             HashSet<Object> listeners = new HashSet<Object>();
241             List<Pair<WidgetData, ICommand>> data = new ArrayList<>();
242             data.addAll(SWTViews.getTriggeredCommands(document, commands, "eventOut"));
243             data.add(new Pair<WidgetData, ICommand>(wd, new Command("onCheck")));
244             Listener listener = new ExplorerListener(wd, data);
245             ModelBrowser browser = container.getControl();
246             if(!browser.isDisposed()) {
247                 browser.addListenerToControl(SWT.Selection, listener);
248                 listeners.add(listener);
249             }
250
251             return listeners;
252
253         }
254
255         @Override
256         public void removeListener(WidgetContainer<ModelBrowser> container, Object listener) {
257             if(container.getControl().isDisposed()) return;
258             if(listener instanceof ExplorerListener)
259                 container.getControl().removeListenerFromControl(SWT.Selection, (Listener)listener);
260         }
261
262     }
263
264     @Override
265     public IEventCommand eventCommand(SWTDocument document, JSONObject object, WidgetContainer widget, ICommand command, CommandContext p) {
266         if("onCheck".equals(command.getCommand())) {
267             CommandContextMutable context = new CommandContextImpl().merge(p);
268             AbstractEventHandler onCheck = object.getJSONField("onCheck");
269             return new PostEventCommand(document, onCheck, context);
270         }
271         return null;
272     }
273
274 }