]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.document.swt.core/src/org/simantics/document/swt/core/widget/Explorer.java
deabd7573d3196eb9a0cd2a059f1c1bb20c64ae4
[simantics/platform.git] / bundles / org.simantics.document.swt.core / src / org / simantics / document / swt / core / widget / Explorer.java
1 package org.simantics.document.swt.core.widget;
2
3 import java.util.Collections;
4 import java.util.HashMap;
5 import java.util.Map;
6
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;
34
35 public class Explorer extends LeafWidgetManager<ModelBrowser> {
36
37         @Override
38         protected void doUpdateProperties(SWTDocument document, ModelBrowser control, JSONObject object) {
39                 //                      String text = object.getField("text");
40                 //                      control.setText(text);
41         }
42
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);
48                 }
49                 return result;
50         }
51         
52         @Override
53         protected ModelBrowser doCreateControl(SWTDocument document, Composite parent, JSONObject object) {
54                 
55                 String browseContext = object.getJSONField("browseContext");
56                 if(browseContext == null) return null;
57
58                 String contextMenuId = object.getJSONField("contextMenuId");
59                 
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;
64                 
65         Boolean vscroll = object.getJSONFieldDefault("VScroll", false);
66         if(Boolean.TRUE.equals(vscroll))
67             style |= SWT.V_SCROLL;
68         
69         Boolean hscroll = object.getJSONFieldDefault("HScroll", false);
70         if(Boolean.TRUE.equals(hscroll))
71             style |= SWT.H_SCROLL;
72
73         Boolean noscroll = object.getJSONFieldDefault("NoScroll", false);
74         if(Boolean.TRUE.equals(noscroll))
75             style |= SWT.NO_SCROLL;
76                 
77                 StatePersistor persistor = object.getJSONField("persistor");
78                 final Function1<Object, Boolean> selectionListener = object.getJSONField("selectionListener");
79
80                 DragSourceListenerFactory dragSourceListenerFactory = object.getJSONField("dragSourceListenerFactory");
81                 
82                 final IWorkbenchSite site = document.getSite();
83                 final ISelectionProvider selectionProvider = document.getSelectionProvider();
84
85                 Map<String, Object> args = new HashMap<String, Object>();
86                 args.put("displaySelectors", Boolean.FALSE);
87                 args.put("displayFilter", displayFilter != null ? displayFilter : Boolean.FALSE);
88
89 //              GridLayoutFactory.fillDefaults().applyTo(parent);
90
91                 ColumnsBean columns = object.getBeanJSONFieldDefault("columns", ColumnsBean.BINDING, null);
92                 String editingColumn = object.getJSONFieldDefault("editingColumn", null);
93                 
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") 
98 //              };
99                 
100 //              parent.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_RED));
101                 
102                 final ModelBrowser control = new ModelBrowser(Collections.singleton(browseContext), args, site, parent, new WidgetSupportImpl(), style);
103
104                 Tree tree = control.getExplorerControl();
105                 tree.addListener(SWT.SetData, new Listener() {
106                         
107                         @Override
108                         public void handleEvent(Event event) {
109                                 SWTViews.notifyScrolledComposite(control);
110                         }
111                         
112                 });
113                 
114                 if(columns != null)
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);
124                 
125                 control.setStatePersistor(persistor);
126
127                 control.finish();
128                 
129                 control.setInputSource(new InputSourceImpl<Object>() {
130
131                         @Override
132                         public Object get(ISessionContext ctx, Object selection) {
133                                 return selection;
134                         }
135
136                 });
137                 
138                 // TODO: fixme!
139                 Object i = object.getJSONField("explorerInput");
140                 if(i instanceof VariableBean) {
141                         control.setInput(Simantics.getSessionContext(), ((VariableBean)i).getVariable());
142                 } else {
143                         control.setInput(Simantics.getSessionContext(), i);
144                 }
145                 
146                 control.addListenerToControl(SWT.Selection, new Listener() {
147
148                         @Override
149                         public void handleEvent(Event event) {
150
151                                 if(selectionListener != null)
152                                         selectionListener.apply(event);
153                                 
154                                 ISelection selection = (ISelection)control.getExplorer().getWidgetSelection();
155                                 
156                                 // TODO: refactor this!
157
158                                 if(selectionProvider != null) {
159                                         selectionProvider.setSelection(selection);
160                                 }
161                                 
162 //                              if (site != null) {
163 //                                      ISelectionProvider sp = site.getSelectionProvider();
164 //                                      if (sp != null) {
165 //                                              sp.setSelection(selection);
166 //                                      }
167 //                              }
168
169                         }
170
171                 });     
172
173                 return control;
174
175         }
176         
177         @Override
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));
182             }
183             return null;
184         }
185
186 }