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.Collections;
17 import java.util.HashMap;
18 import java.util.HashSet;
19 import java.util.List;
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;
64 public class Explorer extends LeafWidgetManager<ModelBrowser> {
67 protected void doUpdateProperties(SWTDocument document, ModelBrowser control, JSONObject object) {
68 // String text = object.getField("text");
69 // control.setText(text);
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);
82 protected ModelBrowser doCreateControl(SWTDocument document, Composite parent, JSONObject object) {
84 String browseContext = object.getJSONField("browseContext");
85 if(browseContext == null) return null;
87 String contextMenuId = object.getJSONField("contextMenuId");
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;
94 Boolean vscroll = object.getJSONFieldDefault("VScroll", false);
95 if(Boolean.TRUE.equals(vscroll))
96 style |= SWT.V_SCROLL;
98 Boolean hscroll = object.getJSONFieldDefault("HScroll", false);
99 if(Boolean.TRUE.equals(hscroll))
100 style |= SWT.H_SCROLL;
102 Boolean noscroll = object.getJSONFieldDefault("NoScroll", false);
103 if(Boolean.TRUE.equals(noscroll))
104 style |= SWT.NO_SCROLL;
106 Boolean check = object.getJSONFieldDefault("Check", false);
107 if(Boolean.TRUE.equals(check))
110 StatePersistor persistor = object.getJSONField("persistor");
111 final Function1<Object, Boolean> selectionListener = object.getJSONField("selectionListener");
113 final Function2<Object, Boolean, Boolean> checkStateListener = object.getJSONField("checkStateListener");
115 DragSourceListenerFactory dragSourceListenerFactory = object.getJSONField("dragSourceListenerFactory");
117 final IWorkbenchSite site = document.getSite();
118 final ISelectionProvider selectionProvider = document.getSelectionProvider();
120 Map<String, Object> args = new HashMap<String, Object>();
121 args.put("displaySelectors", Boolean.FALSE);
122 args.put("displayFilter", displayFilter != null ? displayFilter : Boolean.FALSE);
124 // GridLayoutFactory.fillDefaults().applyTo(parent);
126 ColumnsBean columns = object.getBeanJSONFieldDefault("columns", ColumnsBean.BINDING, null);
127 String editingColumn = object.getJSONFieldDefault("editingColumn", null);
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")
135 // parent.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_RED));
137 final ModelBrowser control = new ModelBrowser(Collections.singleton(browseContext), args, site, parent, new WidgetSupportImpl(), style);
139 Tree tree = control.getExplorerControl();
140 tree.addListener(SWT.SetData, new Listener() {
143 public void handleEvent(Event event) {
144 SWTViews.notifyScrolledComposite(control);
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);
160 control.setStatePersistor(persistor);
164 control.setInputSource(new InputSourceImpl<Object>() {
167 public Object get(ISessionContext ctx, Object selection) {
174 Object i = object.getJSONField("explorerInput");
175 if(i instanceof VariableBean) {
176 control.setInput(Simantics.getSessionContext(), ((VariableBean)i).getVariable());
178 control.setInput(Simantics.getSessionContext(), i);
181 control.addListenerToControl(SWT.Selection, new Listener() {
184 public void handleEvent(Event event) {
186 switch (event.type) {
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);
197 if(selectionListener != null)
198 selectionListener.apply(event);
200 ISelection selection = (ISelection)control.getExplorer().getWidgetSelection();
202 // TODO: refactor this!
204 if(selectionProvider != null) {
205 selectionProvider.setSelection(selection);
208 // if (site != null) {
209 // ISelectionProvider sp = site.getSelectionProvider();
211 // sp.setSelection(selection);
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));
232 public static class ExplorerCommandManager implements CommandManager<SWTDocument, WidgetContainer<ModelBrowser>> {
235 public Collection<Object> updateCommandListeners(final SWTDocument document, final JSONObject object,
236 WidgetContainer<ModelBrowser> container) {
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);
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);
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);