]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.document.swt.core/src/org/simantics/document/swt/core/widget/Explorer.java
Playground for Antti.
[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.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;
9 import java.util.List;
10 import java.util.Map;
11
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;
55
56 public class Explorer extends LeafWidgetManager<ModelBrowser> {
57
58         @Override
59         protected void doUpdateProperties(SWTDocument document, ModelBrowser control, JSONObject object) {
60                 //                      String text = object.getField("text");
61                 //                      control.setText(text);
62         }
63
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);
69                 }
70                 return result;
71         }
72         
73         @Override
74         protected ModelBrowser doCreateControl(SWTDocument document, Composite parent, JSONObject object) {
75                 
76                 String browseContext = object.getJSONField("browseContext");
77                 if(browseContext == null) return null;
78
79                 String contextMenuId = object.getJSONField("contextMenuId");
80                 
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;
85                 
86         Boolean vscroll = object.getJSONFieldDefault("VScroll", false);
87         if(Boolean.TRUE.equals(vscroll))
88             style |= SWT.V_SCROLL;
89         
90         Boolean hscroll = object.getJSONFieldDefault("HScroll", false);
91         if(Boolean.TRUE.equals(hscroll))
92             style |= SWT.H_SCROLL;
93
94         Boolean noscroll = object.getJSONFieldDefault("NoScroll", false);
95         if(Boolean.TRUE.equals(noscroll))
96             style |= SWT.NO_SCROLL;
97                 
98         Boolean check = object.getJSONFieldDefault("Check", false);
99         if(Boolean.TRUE.equals(check))
100             style |= SWT.CHECK;
101
102         StatePersistor persistor = object.getJSONField("persistor");
103                 final Function1<Object, Boolean> selectionListener = object.getJSONField("selectionListener");
104
105                 final Function2<Object, Boolean, Boolean> checkStateListener = object.getJSONField("checkStateListener");
106
107                 DragSourceListenerFactory dragSourceListenerFactory = object.getJSONField("dragSourceListenerFactory");
108                 
109                 final IWorkbenchSite site = document.getSite();
110                 final ISelectionProvider selectionProvider = document.getSelectionProvider();
111
112                 Map<String, Object> args = new HashMap<String, Object>();
113                 args.put("displaySelectors", Boolean.FALSE);
114                 args.put("displayFilter", displayFilter != null ? displayFilter : Boolean.FALSE);
115
116 //              GridLayoutFactory.fillDefaults().applyTo(parent);
117
118                 ColumnsBean columns = object.getBeanJSONFieldDefault("columns", ColumnsBean.BINDING, null);
119                 String editingColumn = object.getJSONFieldDefault("editingColumn", null);
120                 
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") 
125 //              };
126                 
127 //              parent.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_RED));
128                 
129                 final ModelBrowser control = new ModelBrowser(Collections.singleton(browseContext), args, site, parent, new WidgetSupportImpl(), style);
130
131                 Tree tree = control.getExplorerControl();
132                 tree.addListener(SWT.SetData, new Listener() {
133                         
134                         @Override
135                         public void handleEvent(Event event) {
136                                 SWTViews.notifyScrolledComposite(control);
137                         }
138                         
139                 });
140                 
141                 if(columns != null)
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);
151                 
152                 control.setStatePersistor(persistor);
153
154                 control.finish();
155                 
156                 control.setInputSource(new InputSourceImpl<Object>() {
157
158                         @Override
159                         public Object get(ISessionContext ctx, Object selection) {
160                                 return selection;
161                         }
162
163                 });
164                 
165                 // TODO: fixme!
166                 Object i = object.getJSONField("explorerInput");
167                 if(i instanceof VariableBean) {
168                         control.setInput(Simantics.getSessionContext(), ((VariableBean)i).getVariable());
169                 } else {
170                         control.setInput(Simantics.getSessionContext(), i);
171                 }
172                 
173                 control.addListenerToControl(SWT.Selection, new Listener() {
174
175                         @Override
176                         public void handleEvent(Event event) {
177                             
178                             switch (event.type) {
179                             case SWT.Selection:
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);
185                                 }
186                                 break;
187                             }
188
189                                 if(selectionListener != null)
190                                         selectionListener.apply(event);
191                                 
192                                 ISelection selection = (ISelection)control.getExplorer().getWidgetSelection();
193                                 
194                                 // TODO: refactor this!
195
196                                 if(selectionProvider != null) {
197                                         selectionProvider.setSelection(selection);
198                                 }
199                                 
200 //                              if (site != null) {
201 //                                      ISelectionProvider sp = site.getSelectionProvider();
202 //                                      if (sp != null) {
203 //                                              sp.setSelection(selection);
204 //                                      }
205 //                              }
206
207                         }
208
209                 });     
210
211                 return control;
212
213         }
214         
215         @Override
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));
220             }
221             return null;
222         }
223
224     public static class ExplorerCommandManager implements CommandManager<SWTDocument, WidgetContainer<ModelBrowser>> {
225
226         @Override
227         public Collection<Object> updateCommandListeners(final SWTDocument document, final JSONObject object,
228                 WidgetContainer<ModelBrowser> container) {
229             
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);
241             }
242             
243             return listeners;
244             
245         }
246
247         @Override
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);
252         }
253
254     }
255
256     @Override
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);
262         }
263         return null;
264     }   
265         
266 }