1 package org.simantics.document.swt.core.widget;
3 import java.util.Collections;
4 import java.util.HashMap;
7 import org.eclipse.jface.viewers.ISelection;
8 import org.eclipse.jface.viewers.ISelectionProvider;
9 import org.eclipse.swt.SWT;
10 import org.eclipse.swt.widgets.Composite;
11 import org.eclipse.swt.widgets.Event;
12 import org.eclipse.swt.widgets.Listener;
13 import org.eclipse.swt.widgets.Tree;
14 import org.eclipse.ui.IWorkbenchSite;
15 import org.simantics.Simantics;
16 import org.simantics.browsing.ui.Column;
17 import org.simantics.browsing.ui.Column.Align;
18 import org.simantics.browsing.ui.StatePersistor;
19 import org.simantics.browsing.ui.swt.InputSourceImpl;
20 import org.simantics.browsing.ui.swt.widgets.DragSourceListenerFactory;
21 import org.simantics.browsing.ui.swt.widgets.ModelBrowser;
22 import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupportImpl;
23 import org.simantics.db.layer0.variable.VariableBean;
24 import org.simantics.db.management.ISessionContext;
25 import org.simantics.document.server.JSONObject;
26 import org.simantics.document.swt.core.SWTDocument;
27 import org.simantics.document.swt.core.SWTViews;
28 import org.simantics.document.swt.core.base.LeafWidgetManager;
29 import org.simantics.document.swt.core.base.WidgetContainer;
30 import org.simantics.document.swt.core.bean.ColumnBean;
31 import org.simantics.document.swt.core.bean.ColumnsBean;
32 import org.simantics.scl.runtime.function.Function1;
33 import org.simantics.ui.selection.WorkbenchSelectionUtils;
35 public class Explorer extends LeafWidgetManager<ModelBrowser> {
38 protected void doUpdateProperties(SWTDocument document, ModelBrowser control, JSONObject object) {
39 // String text = object.getField("text");
40 // control.setText(text);
43 protected Column[] getColumns(ColumnsBean bean) {
44 Column[] result = new Column[bean.columns.length];
45 for(int i=0;i<bean.columns.length;i++) {
46 ColumnBean cb = bean.columns[i];
47 result[i] = new Column(cb.key, cb.label, Align.valueOf(cb.alignment), cb.width, cb.tooltip, cb.grab, cb.weight);
53 protected ModelBrowser doCreateControl(SWTDocument document, Composite parent, JSONObject object) {
55 String browseContext = object.getJSONField("browseContext");
56 if(browseContext == null) return null;
58 String contextMenuId = object.getJSONField("contextMenuId");
60 Boolean displayFilter = object.getJSONField("displayFilter");
61 Boolean displayHeader = object.getJSONField("displayHeader");
62 Integer style = object.getJSONField("style");
63 if(style == null) style = SWT.FULL_SELECTION;
65 Boolean vscroll = object.getJSONFieldDefault("VScroll", false);
66 if(Boolean.TRUE.equals(vscroll))
67 style |= SWT.V_SCROLL;
69 Boolean hscroll = object.getJSONFieldDefault("HScroll", false);
70 if(Boolean.TRUE.equals(hscroll))
71 style |= SWT.H_SCROLL;
73 Boolean noscroll = object.getJSONFieldDefault("NoScroll", false);
74 if(Boolean.TRUE.equals(noscroll))
75 style |= SWT.NO_SCROLL;
77 StatePersistor persistor = object.getJSONField("persistor");
78 final Function1<Object, Boolean> selectionListener = object.getJSONField("selectionListener");
80 DragSourceListenerFactory dragSourceListenerFactory = object.getJSONField("dragSourceListenerFactory");
82 final IWorkbenchSite site = document.getSite();
83 final ISelectionProvider selectionProvider = document.getSelectionProvider();
85 Map<String, Object> args = new HashMap<String, Object>();
86 args.put("displaySelectors", Boolean.FALSE);
87 args.put("displayFilter", displayFilter != null ? displayFilter : Boolean.FALSE);
89 // GridLayoutFactory.fillDefaults().applyTo(parent);
91 ColumnsBean columns = object.getBeanJSONFieldDefault("columns", ColumnsBean.BINDING, null);
92 String editingColumn = object.getJSONFieldDefault("editingColumn", null);
94 // Column[] COLUMNS = new Column[] {
95 // new Column("HasDisplayProperty", "Parameter description", Align.LEFT, 80, "Input parameter name", true),
96 // new Column("HasDisplayValue", "Value", Align.RIGHT, 65, "Value"),
97 // new Column("HasDisplayUnit", "Unit", Align.RIGHT, 100, "Unit")
100 // parent.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_RED));
102 final ModelBrowser control = new ModelBrowser(Collections.singleton(browseContext), args, site, parent, new WidgetSupportImpl(), style);
104 Tree tree = control.getExplorerControl();
105 tree.addListener(SWT.SetData, new Listener() {
108 public void handleEvent(Event event) {
109 SWTViews.notifyScrolledComposite(control);
115 control.setColumns(getColumns(columns));
116 if(editingColumn != null)
117 control.setEditingColumn(editingColumn);
118 if(contextMenuId != null)
119 control.setContextMenuId(contextMenuId);
120 if(dragSourceListenerFactory != null)
121 control.setDragSourceListenerFactory(dragSourceListenerFactory);
122 if(displayHeader != null)
123 tree.setHeaderVisible(displayHeader);
125 control.setStatePersistor(persistor);
129 control.setInputSource(new InputSourceImpl<Object>() {
132 public Object get(ISessionContext ctx, Object selection) {
139 Object i = object.getJSONField("explorerInput");
140 if(i instanceof VariableBean) {
141 control.setInput(Simantics.getSessionContext(), ((VariableBean)i).getVariable());
143 control.setInput(Simantics.getSessionContext(), i);
146 control.addListenerToControl(SWT.Selection, new Listener() {
149 public void handleEvent(Event event) {
151 if(selectionListener != null)
152 selectionListener.apply(event);
154 ISelection selection = (ISelection)control.getExplorer().getWidgetSelection();
156 // TODO: refactor this!
158 if(selectionProvider != null) {
159 selectionProvider.setSelection(selection);
162 // if (site != null) {
163 // ISelectionProvider sp = site.getSelectionProvider();
165 // sp.setSelection(selection);
178 public String getProperty(SWTDocument document, JSONObject object, WidgetContainer<ModelBrowser> widget, String property) {
179 if("selection".equals(property)) {
180 ISelection selection = (ISelection)widget.getControl().getExplorer().getWidgetSelection();
181 return SWTViews.encode(object, property, WorkbenchSelectionUtils.getWorkbenchSelectionElements(selection));