1 package org.simantics.document.swt.core.widget;
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.Collections;
6 import java.util.HashMap;
7 import java.util.HashSet;
8 import java.util.LinkedHashMap;
12 import org.eclipse.jface.viewers.ISelection;
13 import org.eclipse.jface.viewers.ISelectionProvider;
14 import org.eclipse.swt.SWT;
15 import org.eclipse.swt.events.SelectionListener;
16 import org.eclipse.swt.widgets.Button;
17 import org.eclipse.swt.widgets.Composite;
18 import org.eclipse.swt.widgets.Event;
19 import org.eclipse.swt.widgets.Listener;
20 import org.eclipse.swt.widgets.Tree;
21 import org.eclipse.swt.widgets.TreeItem;
22 import org.eclipse.ui.IWorkbenchSite;
23 import org.simantics.Simantics;
24 import org.simantics.browsing.ui.Column;
25 import org.simantics.browsing.ui.Column.Align;
26 import org.simantics.browsing.ui.StatePersistor;
27 import org.simantics.browsing.ui.swt.InputSourceImpl;
28 import org.simantics.browsing.ui.swt.widgets.DragSourceListenerFactory;
29 import org.simantics.browsing.ui.swt.widgets.ModelBrowser;
30 import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupportImpl;
31 import org.simantics.db.layer0.variable.VariableBean;
32 import org.simantics.db.management.ISessionContext;
33 import org.simantics.document.server.IEventCommand;
34 import org.simantics.document.server.JSONObject;
35 import org.simantics.document.server.bean.Command;
36 import org.simantics.document.server.client.CommandManager;
37 import org.simantics.document.server.client.WidgetData;
38 import org.simantics.document.server.handler.AbstractEventHandler;
39 import org.simantics.document.server.io.CommandContext;
40 import org.simantics.document.server.io.CommandContextImpl;
41 import org.simantics.document.server.io.CommandContextMutable;
42 import org.simantics.document.server.io.ICommand;
43 import org.simantics.document.server.io.JSONObjectUtils;
44 import org.simantics.document.swt.core.SWTDocument;
45 import org.simantics.document.swt.core.SWTViews;
46 import org.simantics.document.swt.core.base.LeafWidgetManager;
47 import org.simantics.document.swt.core.base.PostEventCommand;
48 import org.simantics.document.swt.core.base.WidgetContainer;
49 import org.simantics.document.swt.core.bean.ColumnBean;
50 import org.simantics.document.swt.core.bean.ColumnsBean;
51 import org.simantics.scl.runtime.function.Function1;
52 import org.simantics.scl.runtime.function.Function2;
53 import org.simantics.ui.selection.WorkbenchSelectionUtils;
54 import org.simantics.utils.datastructures.Pair;
56 public class Explorer extends LeafWidgetManager<ModelBrowser> {
59 protected void doUpdateProperties(SWTDocument document, ModelBrowser control, JSONObject object) {
60 // String text = object.getField("text");
61 // control.setText(text);
64 protected Column[] getColumns(ColumnsBean bean) {
65 Column[] result = new Column[bean.columns.length];
66 for(int i=0;i<bean.columns.length;i++) {
67 ColumnBean cb = bean.columns[i];
68 result[i] = new Column(cb.key, cb.label, Align.valueOf(cb.alignment), cb.width, cb.tooltip, cb.grab, cb.weight);
74 protected ModelBrowser doCreateControl(SWTDocument document, Composite parent, JSONObject object) {
76 String browseContext = object.getJSONField("browseContext");
77 if(browseContext == null) return null;
79 String contextMenuId = object.getJSONField("contextMenuId");
81 Boolean displayFilter = object.getJSONField("displayFilter");
82 Boolean displayHeader = object.getJSONField("displayHeader");
83 Integer style = object.getJSONField("style");
84 if(style == null) style = SWT.FULL_SELECTION;
86 Boolean vscroll = object.getJSONFieldDefault("VScroll", false);
87 if(Boolean.TRUE.equals(vscroll))
88 style |= SWT.V_SCROLL;
90 Boolean hscroll = object.getJSONFieldDefault("HScroll", false);
91 if(Boolean.TRUE.equals(hscroll))
92 style |= SWT.H_SCROLL;
94 Boolean noscroll = object.getJSONFieldDefault("NoScroll", false);
95 if(Boolean.TRUE.equals(noscroll))
96 style |= SWT.NO_SCROLL;
98 Boolean check = object.getJSONFieldDefault("Check", false);
99 if(Boolean.TRUE.equals(check))
102 StatePersistor persistor = object.getJSONField("persistor");
103 final Function1<Object, Boolean> selectionListener = object.getJSONField("selectionListener");
105 final Function2<Object, Boolean, Boolean> checkStateListener = object.getJSONField("checkStateListener");
107 DragSourceListenerFactory dragSourceListenerFactory = object.getJSONField("dragSourceListenerFactory");
109 final IWorkbenchSite site = document.getSite();
110 final ISelectionProvider selectionProvider = document.getSelectionProvider();
112 Map<String, Object> args = new HashMap<String, Object>();
113 args.put("displaySelectors", Boolean.FALSE);
114 args.put("displayFilter", displayFilter != null ? displayFilter : Boolean.FALSE);
116 // GridLayoutFactory.fillDefaults().applyTo(parent);
118 ColumnsBean columns = object.getBeanJSONFieldDefault("columns", ColumnsBean.BINDING, null);
119 String editingColumn = object.getJSONFieldDefault("editingColumn", null);
121 // Column[] COLUMNS = new Column[] {
122 // new Column("HasDisplayProperty", "Parameter description", Align.LEFT, 80, "Input parameter name", true),
123 // new Column("HasDisplayValue", "Value", Align.RIGHT, 65, "Value"),
124 // new Column("HasDisplayUnit", "Unit", Align.RIGHT, 100, "Unit")
127 // parent.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_RED));
129 final ModelBrowser control = new ModelBrowser(Collections.singleton(browseContext), args, site, parent, new WidgetSupportImpl(), style);
131 Tree tree = control.getExplorerControl();
132 tree.addListener(SWT.SetData, new Listener() {
135 public void handleEvent(Event event) {
136 SWTViews.notifyScrolledComposite(control);
142 control.setColumns(getColumns(columns));
143 if(editingColumn != null)
144 control.setEditingColumn(editingColumn);
145 if(contextMenuId != null)
146 control.setContextMenuId(contextMenuId);
147 if(dragSourceListenerFactory != null)
148 control.setDragSourceListenerFactory(dragSourceListenerFactory);
149 if(displayHeader != null)
150 tree.setHeaderVisible(displayHeader);
152 control.setStatePersistor(persistor);
156 control.setInputSource(new InputSourceImpl<Object>() {
159 public Object get(ISessionContext ctx, Object selection) {
166 Object i = object.getJSONField("explorerInput");
167 if(i instanceof VariableBean) {
168 control.setInput(Simantics.getSessionContext(), ((VariableBean)i).getVariable());
170 control.setInput(Simantics.getSessionContext(), i);
173 control.addListenerToControl(SWT.Selection, new Listener() {
176 public void handleEvent(Event event) {
178 switch (event.type) {
180 if (event.detail == SWT.CHECK && event.item != null) {
181 TreeItem item = (TreeItem) event.item;
182 boolean checked = item.getChecked();
183 if(checkStateListener != null)
184 selectionListener.apply(event);
189 if(selectionListener != null)
190 selectionListener.apply(event);
192 ISelection selection = (ISelection)control.getExplorer().getWidgetSelection();
194 // TODO: refactor this!
196 if(selectionProvider != null) {
197 selectionProvider.setSelection(selection);
200 // if (site != null) {
201 // ISelectionProvider sp = site.getSelectionProvider();
203 // sp.setSelection(selection);
216 public String getProperty(SWTDocument document, JSONObject object, WidgetContainer<ModelBrowser> widget, String property) {
217 if("selection".equals(property)) {
218 ISelection selection = (ISelection)widget.getControl().getExplorer().getWidgetSelection();
219 return SWTViews.encode(object, property, WorkbenchSelectionUtils.getWorkbenchSelectionElements(selection));
224 public static class ExplorerCommandManager implements CommandManager<SWTDocument, WidgetContainer<ModelBrowser>> {
227 public Collection<Object> updateCommandListeners(final SWTDocument document, final JSONObject object,
228 WidgetContainer<ModelBrowser> container) {
230 WidgetData wd = document.getWidget(JSONObjectUtils.getId(object));
231 List<ICommand> commands = object.getJSONField("commands");
232 HashSet<Object> listeners = new HashSet<Object>();
233 List<Pair<WidgetData, ICommand>> data = new ArrayList<>();
234 data.addAll(SWTViews.getTriggeredCommands(document, commands, "eventOut"));
235 data.add(new Pair<WidgetData, ICommand>(wd, new Command("onCheck")));
236 Listener listener = new ExplorerListener(wd, data);
237 ModelBrowser browser = container.getControl();
238 if(!browser.isDisposed()) {
239 browser.addListenerToControl(SWT.Selection, listener);
240 listeners.add(listener);
248 public void removeListener(WidgetContainer<ModelBrowser> container, Object listener) {
249 if(container.getControl().isDisposed()) return;
250 if(listener instanceof ExplorerListener)
251 container.getControl().removeListenerFromControl(SWT.Selection, (Listener)listener);
257 public IEventCommand eventCommand(SWTDocument document, JSONObject object, WidgetContainer widget, ICommand command, CommandContext p) {
258 if("onCheck".equals(command.getCommand())) {
259 CommandContextMutable context = new CommandContextImpl().merge(p);
260 AbstractEventHandler onCheck = object.getJSONField("onCheck");
261 return new PostEventCommand(document, onCheck, context);