]> gerrit.simantics Code Review - simantics/platform.git/blob
2f52153a1192b45e2a6c33e42afd2da58a86e1a1
[simantics/platform.git] /
1 /*******************************************************************************
2  * Copyright (c) 2007, 2018 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.browsing.ui.swt.widgets;
13
14 import java.util.Collection;
15 import java.util.Collections;
16 import java.util.HashSet;
17 import java.util.Map;
18 import java.util.Set;
19 import java.util.function.BiFunction;
20
21 import org.eclipse.core.runtime.IAdaptable;
22 import org.eclipse.jface.action.IMenuManager;
23 import org.eclipse.jface.layout.GridDataFactory;
24 import org.eclipse.jface.layout.GridLayoutFactory;
25 import org.eclipse.jface.layout.TreeColumnLayout;
26 import org.eclipse.jface.resource.JFaceResources;
27 import org.eclipse.jface.resource.LocalResourceManager;
28 import org.eclipse.jface.viewers.ColumnWeightData;
29 import org.eclipse.jface.viewers.ISelectionProvider;
30 import org.eclipse.swt.SWT;
31 import org.eclipse.swt.dnd.DND;
32 import org.eclipse.swt.dnd.DragSource;
33 import org.eclipse.swt.dnd.DragSourceEvent;
34 import org.eclipse.swt.dnd.DragSourceListener;
35 import org.eclipse.swt.dnd.DropTarget;
36 import org.eclipse.swt.dnd.DropTargetEvent;
37 import org.eclipse.swt.dnd.DropTargetListener;
38 import org.eclipse.swt.dnd.FileTransfer;
39 import org.eclipse.swt.dnd.TextTransfer;
40 import org.eclipse.swt.dnd.Transfer;
41 import org.eclipse.swt.widgets.Composite;
42 import org.eclipse.swt.widgets.Control;
43 import org.eclipse.swt.widgets.Event;
44 import org.eclipse.swt.widgets.Listener;
45 import org.eclipse.swt.widgets.Tree;
46 import org.eclipse.swt.widgets.TreeItem;
47 import org.eclipse.ui.ISelectionListener;
48 import org.eclipse.ui.IWorkbenchSite;
49 import org.eclipse.ui.contexts.IContextService;
50 import org.simantics.Simantics;
51 import org.simantics.browsing.ui.BuiltinKeys;
52 import org.simantics.browsing.ui.Column;
53 import org.simantics.browsing.ui.ExplorerState;
54 import org.simantics.browsing.ui.GraphExplorer;
55 import org.simantics.browsing.ui.GraphExplorer.TransientExplorerState;
56 import org.simantics.browsing.ui.NodeContext;
57 import org.simantics.browsing.ui.StatePersistor;
58 import org.simantics.browsing.ui.common.AdaptableHintContext;
59 import org.simantics.browsing.ui.common.ColumnKeys;
60 import org.simantics.browsing.ui.common.EvaluatorData;
61 import org.simantics.browsing.ui.common.EvaluatorDataImpl;
62 import org.simantics.browsing.ui.common.processors.ComparableFactoryResolver;
63 import org.simantics.browsing.ui.common.processors.ComparableSelectorQueryProcessor;
64 import org.simantics.browsing.ui.common.processors.FilterSelectionRequestQueryProcessor;
65 import org.simantics.browsing.ui.common.processors.ImageDecoratorFactoryResolver;
66 import org.simantics.browsing.ui.common.processors.ImagerFactoryResolver;
67 import org.simantics.browsing.ui.common.processors.LabelDecoratorFactoryResolver;
68 import org.simantics.browsing.ui.common.processors.LabelerFactoryResolver;
69 import org.simantics.browsing.ui.common.processors.UserSelectedComparableFactoryQueryProcessor;
70 import org.simantics.browsing.ui.common.processors.UserSelectedViewpointFactoryQueryProcessor;
71 import org.simantics.browsing.ui.common.processors.ViewpointFactoryResolver;
72 import org.simantics.browsing.ui.common.state.ExplorerStates;
73 import org.simantics.browsing.ui.common.views.FilterAreaSource;
74 import org.simantics.browsing.ui.common.views.IFilterArea;
75 import org.simantics.browsing.ui.common.views.IFilterAreaProvider;
76 import org.simantics.browsing.ui.graph.impl.AsyncReadGraphDataSource;
77 import org.simantics.browsing.ui.graph.impl.Evaluators;
78 import org.simantics.browsing.ui.graph.impl.InheritsQueryProcessor;
79 import org.simantics.browsing.ui.graph.impl.ReadGraphDataSource;
80 import org.simantics.browsing.ui.graph.impl.RelatedObjectsQueryProcessor;
81 import org.simantics.browsing.ui.graph.impl.SessionContextInputSource;
82 import org.simantics.browsing.ui.model.browsecontexts.BrowseContext;
83 import org.simantics.browsing.ui.model.nodetypes.NodeType;
84 import org.simantics.browsing.ui.swt.ComparatorSelector;
85 import org.simantics.browsing.ui.swt.ContextMenuInitializer;
86 import org.simantics.browsing.ui.swt.DefaultExplorerSelectionListener;
87 import org.simantics.browsing.ui.swt.DefaultIsCheckedProcessor2;
88 import org.simantics.browsing.ui.swt.DefaultKeyListener;
89 import org.simantics.browsing.ui.swt.DefaultMouseListener;
90 import org.simantics.browsing.ui.swt.DefaultSelectionDataResolver;
91 import org.simantics.browsing.ui.swt.FilterArea;
92 import org.simantics.browsing.ui.swt.GraphExplorerFactory;
93 import org.simantics.browsing.ui.swt.IContextMenuInitializer;
94 import org.simantics.browsing.ui.swt.RootFilterArea;
95 import org.simantics.browsing.ui.swt.StandardContextTypesQueryProcessor;
96 import org.simantics.browsing.ui.swt.TypesQueryProcessor;
97 import org.simantics.browsing.ui.swt.ViewpointSelector;
98 import org.simantics.browsing.ui.swt.widgets.impl.Widget;
99 import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport;
100 import org.simantics.db.AsyncReadGraph;
101 import org.simantics.db.ReadGraph;
102 import org.simantics.db.Resource;
103 import org.simantics.db.Session;
104 import org.simantics.db.exception.DatabaseException;
105 import org.simantics.db.layer0.SelectionHints;
106 import org.simantics.db.layer0.request.PossibleVariable;
107 import org.simantics.db.layer0.request.PossibleVariableRepresents;
108 import org.simantics.db.layer0.variable.Variable;
109 import org.simantics.db.management.ISessionContext;
110 import org.simantics.db.management.ISessionContextChangedListener;
111 import org.simantics.db.management.ISessionContextProvider;
112 import org.simantics.db.management.SessionContextChangedEvent;
113 import org.simantics.project.ProjectKeys;
114 import org.simantics.ui.SimanticsUI;
115 import org.simantics.ui.dnd.LocalObjectTransfer;
116 import org.simantics.ui.dnd.LocalSelectionDragSourceListener;
117 import org.simantics.ui.dnd.NoImageDragSourceEffect;
118 import org.simantics.ui.dnd.SessionContainer;
119 import org.simantics.ui.selection.AnyResource;
120 import org.simantics.ui.selection.AnyVariable;
121 import org.simantics.ui.selection.ExplorerColumnContentType;
122 import org.simantics.ui.selection.ExplorerInputContentType;
123 import org.simantics.ui.selection.WorkbenchSelectionContentType;
124 import org.simantics.ui.selection.WorkbenchSelectionElement;
125 import org.simantics.ui.selection.WorkbenchSelectionUtils;
126 import org.simantics.utils.ObjectUtils;
127 import org.simantics.utils.datastructures.Function;
128 import org.simantics.utils.datastructures.disposable.DisposeState;
129 import org.simantics.utils.datastructures.hints.HintListenerAdapter;
130 import org.simantics.utils.datastructures.hints.HintTracker;
131 import org.simantics.utils.datastructures.hints.IHintContext.Key;
132 import org.simantics.utils.datastructures.hints.IHintListener;
133 import org.simantics.utils.datastructures.hints.IHintObservable;
134 import org.simantics.utils.datastructures.hints.IHintTracker;
135 import org.simantics.utils.ui.SWTUtils;
136 import org.slf4j.Logger;
137 import org.slf4j.LoggerFactory;
138
139
140 public class GraphExplorerComposite extends Composite implements Widget, IAdaptable {
141
142     private static final Logger LOGGER = LoggerFactory.getLogger(GraphExplorerComposite.class);
143
144     protected UserSelectedComparableFactoryQueryProcessor userSelectedComparableFactoryQueryProcessor;
145     protected UserSelectedViewpointFactoryQueryProcessor  userSelectedViewpointFactoryQueryProcessor;
146     protected FilterSelectionRequestQueryProcessor        filterSelectionRequestQueryProcessor;
147     protected IFilterArea                                 filterArea;
148     protected EvaluatorData                               evaluatorData;
149
150     protected LocalResourceManager                        resourceManager;
151
152     protected ISelectionListener                          workbenchSelectionListener;
153
154     private final int                                     style;
155
156     final private IWorkbenchSite                          site;
157
158     protected GraphExplorer                               explorer;
159
160     protected IMenuManager                                menuManager;
161
162     private final Map<String, Object>                     args;
163
164     protected ISessionContextProvider                       contextProvider;
165
166     private ISessionContext                               sessionContext;
167
168     private Object                                        dragSource;
169
170     private IHintTracker                                  sessionContextTracker = new SessionContextProjectTracker();
171
172     private InputSource                                   inputSource           = new DirectInputSource();
173     private FilterAreaSource                              filterAreaSource    = new SelectionFilterAreaSource();
174
175     private final TreeColumnLayout                                                        ad;
176     private String[]                                        editingColumn = ColumnKeys.KEYS_SINGLE;
177     
178     private StatePersistor                                                                      persistor = null;
179
180     private final Composite                                                                       toolComposite;
181     private final Composite                                                                       toolComposite2;
182     private final Composite                                                                       explorerComposite;
183     final private WidgetSupport                                 support;
184     private final boolean                                                                       useNodeBrowseContexts;
185     private final boolean                                                                       useNodeActionContexts;
186
187     static class SelectionElement extends AdaptableHintContext {
188
189         final public WorkbenchSelectionElement wse;
190         final public Object content;
191         final public Resource resource;
192         final public Variable variable;
193         final public Object input;
194         final public TransientExplorerState explorerState;
195         
196         private WorkbenchSelectionElement extractWse(Object content) {
197             if(content instanceof NodeContext) {
198                 NodeContext context = (NodeContext)content;
199                 Object input = context.getConstant(NodeType.TYPE);
200                 if(input instanceof NodeType) 
201                     return ((NodeType)input).getWorkbenchSelectionElement(context);
202             }
203             return null;
204         }
205         
206         private Resource extractResource(Object content) {
207                 if(content instanceof NodeContext) {
208                         NodeContext context = (NodeContext)content;
209                         Object input = context.getConstant(BuiltinKeys.INPUT);
210                         if(input instanceof Resource) return (Resource)input;
211                         if(input instanceof IAdaptable) {
212                                 Resource var = (Resource)((IAdaptable)input).getAdapter(Resource.class);
213                                 if(var != null) return var;
214                         }
215                 }
216                 return null;
217         }
218         
219         private Variable extractVariable(Object content) {
220                 if(content instanceof NodeContext) {
221                         NodeContext context = (NodeContext)content;
222                         Object input = context.getConstant(BuiltinKeys.INPUT);
223                         if(input instanceof Variable) return (Variable)input;
224                         if(input instanceof IAdaptable) {
225                                 Variable var = (Variable)((IAdaptable)input).getAdapter(Variable.class);
226                                 if(var != null) return var;
227                         }
228                 }
229                 return null;
230         }
231
232         private Object extractInput(Object content) {
233                 if(content instanceof NodeContext) {
234                         NodeContext context = (NodeContext)content;
235                         return context.getConstant(BuiltinKeys.INPUT);
236                 }
237                 return null;
238         }
239
240         public SelectionElement(GraphExplorer explorer, Key[] keys, Object content) {
241             super(keys);
242             this.content = content;
243             this.wse = extractWse(content);
244             this.resource = extractResource(content);
245             this.variable = extractVariable(content);
246             this.input = extractInput(content);
247             this.explorerState = explorer.getTransientState();
248         }
249
250         @SuppressWarnings("unchecked")
251         @Override
252         public <T> T getContent(WorkbenchSelectionContentType<T> contentType) {
253             if (wse != null) {
254                 T result = wse.getContent(contentType);
255                 if (result != null)
256                     return result;
257             }
258
259             if (contentType instanceof AnyResource) {
260                 if (resource != null)
261                     return (T) resource;
262                 if (variable == null)
263                     return null;
264                 try {
265                     return (T) ((AnyResource) contentType).processor.syncRequest(new PossibleVariableRepresents(variable));
266                 } catch (DatabaseException e) {
267                     LOGGER.error("Unexpected error occurred while resolving Resource from Variable " + variable, e);
268                 }
269             }
270             else if (contentType instanceof AnyVariable) {
271                 if (variable != null)
272                     return (T) variable;
273                 if (resource == null)
274                     return null;
275                 try {
276                     return (T) ((AnyVariable) contentType).processor.syncRequest(new PossibleVariable(resource));
277                 } catch (DatabaseException e) {
278                     LOGGER.error("Unexpected error occurred while resolving Variable from Resource " + resource, e);
279                 }
280             } else if (contentType instanceof ExplorerInputContentType) {
281                 return (T) input;
282             } else if (contentType instanceof ExplorerColumnContentType) {
283                 return (T) explorerState.getActiveColumn();
284             }
285             return null;
286         }
287
288         @SuppressWarnings("rawtypes")
289         @Override
290         public Object getAdapter(Class adapter) {
291                 if(WorkbenchSelectionElement.class == adapter) {
292                         return wse;
293                 }
294                 if(NodeContext.class == adapter) {
295                     if(content instanceof NodeContext)
296                         return (NodeContext)content;
297                     else
298                         return null;
299                 }
300                 return super.getAdapter(adapter);
301         }
302
303     }
304     private BiFunction<GraphExplorer, Object[], Object[]> selectionTransformation = new BiFunction<GraphExplorer, Object[], Object[]>() {
305
306         private Key[] KEYS = new Key[] { SelectionHints.KEY_MAIN };
307         
308         @Override
309         public Object[] apply(GraphExplorer explorer, Object[] objects) {
310             Object[] result = new Object[objects.length];
311             for (int i = 0; i < objects.length; i++) {
312                 SelectionElement context = new SelectionElement(explorer, KEYS, objects[i]);
313                 context.setHint(SelectionHints.KEY_MAIN, objects[i]);
314                 result[i] = context;
315             }
316             return result;
317         }
318
319     };
320
321     private Set<String>                                   browseContexts        = null;
322
323     private DisposeState                                  disposeState          = DisposeState.Alive;
324
325     private boolean                                       created               = false;
326
327     protected String                                      contextMenuId         = null;
328
329     protected Set<String>                                 uiContext             = null;
330
331     protected ISessionContextChangedListener              contextChangeListener = new ISessionContextChangedListener() {
332         @Override
333         public void sessionContextChanged(SessionContextChangedEvent event) {
334             sessionContext = event.getNewValue();
335             sessionContextTracker.track(sessionContext);
336         }
337     };
338
339     public GraphExplorerComposite(Map<String, Object> args, IWorkbenchSite site, Composite parent, WidgetSupport support, int style) {
340
341         super(parent, SWT.NONE);
342
343         if (args == null)
344             args = Collections.emptyMap();
345
346         this.args = args;
347         this.site = site;
348         this.style = style;
349         this.resourceManager = new LocalResourceManager(JFaceResources.getResources(parent.getDisplay()), this);
350
351         contextProvider = getSessionContextProvider(site);
352
353         Integer maxChildren = (Integer)args.get("maxChildren");
354
355         GridLayoutFactory.fillDefaults().equalWidth(false).numColumns(1).margins(0,0).spacing(0,0).applyTo(this);
356
357         toolComposite = new Composite(this, SWT.NONE);
358 //        toolComposite.setBackground(toolComposite.getDisplay().getSystemColor(SWT.COLOR_DARK_YELLOW));
359 //        GridDataFactory.fillDefaults().grab(true, false).minSize(1, 1).applyTo(toolComposite);
360         GridDataFactory.fillDefaults().grab(true, false).applyTo(toolComposite);
361         GridLayoutFactory.fillDefaults().applyTo(toolComposite);
362
363         ad = new TreeColumnLayout();
364         explorerComposite = new Composite(this, SWT.NONE);
365         explorerComposite.setLayout(ad);
366         GridDataFactory.fillDefaults().grab(true, true).minSize(1, 50).applyTo(explorerComposite);
367
368         if (args.containsKey("treeView") && Boolean.TRUE.equals(args.get("treeView"))) {
369                 explorer = createExplorerControl2(explorerComposite, maxChildren);
370         } else if (args.containsKey("natTable") && Boolean.TRUE.equals(args.get("natTable"))) {
371                 explorer = createExplorerControl3(explorerComposite, maxChildren);
372         } else {
373                 explorer = createExplorerControl(explorerComposite, maxChildren);
374         }
375         
376         if (args.containsKey("useNodeBrowseContexts") && Boolean.TRUE.equals(args.get("useNodeBrowseContexts"))) {
377                 useNodeBrowseContexts = true;
378         } else {
379                 useNodeBrowseContexts = false;
380         }
381         
382         if (args.containsKey("useNodeActionContexts") && Boolean.TRUE.equals(args.get("useNodeActionContexts"))) {
383                 useNodeActionContexts = true;
384         } else {
385                 useNodeActionContexts = false;
386         }
387         
388         toolComposite2 = new Composite(this, SWT.NONE);
389 //        toolComposite2.setBackground(toolComposite2.getDisplay().getSystemColor(SWT.COLOR_DARK_YELLOW));
390 //        GridDataFactory.fillDefaults().grab(true, false).minSize(1, 1).applyTo(toolComposite);
391         GridDataFactory.fillDefaults().grab(true, false).applyTo(toolComposite2);
392         GridLayoutFactory.fillDefaults().applyTo(toolComposite2);
393
394         this.support = support;
395
396         if (support != null)
397             support.register(this);
398
399     }
400
401     public GraphExplorerComposite(Map<String, Object> args, IWorkbenchSite site, Composite parent, int style) {
402
403         this(args, site, parent, null, style);
404
405     }
406
407     public ISessionContextProvider getSessionContextProvider(IWorkbenchSite site) {
408         if(site != null)
409             return SimanticsUI.getSessionContextProvider(site.getWorkbenchWindow());
410         else
411             return Simantics.getSessionContextProvider();
412     }
413
414     public GraphExplorer getExplorer() {
415         return explorer;
416     }
417
418     public Composite getExplorerComposite() {
419         return explorerComposite;
420     }
421
422     public <T> T getExplorerControl() {
423         return explorer.getControl();
424     }
425
426     public void addListenerToControl(int eventType, Listener listener) {
427         ((Control)explorer.getControl()).addListener(eventType, listener);
428     }
429
430     public void finish() {
431         created = true;
432         createControls(site);
433         attachToSession();
434     }
435
436     IWorkbenchSite getSite() {
437         return site;
438     }
439
440     protected void activateUiContexts() {
441         Collection<String> contexts = getUiContexts();
442         if (contexts == null || contexts.isEmpty())
443             return;
444         IWorkbenchSite site = getSite();
445         if (site != null) {
446             IContextService cs = (IContextService) getSite().getService(IContextService.class);
447             for (String context : contexts)
448                 cs.activateContext(context);
449         }
450     }
451
452     protected void createControls(IWorkbenchSite site) {
453
454         // Initialize explorer control.
455 //        GridDataFactory.fillDefaults().grab(true, true).applyTo(explorer.getControl());
456
457         Control control = explorer.getControl();
458
459         // Initialize context menu if an initializer is provided.
460         IContextMenuInitializer cmi = getContextMenuInitializer();
461         if (cmi != null) {
462             ISelectionProvider selectionProvider = (ISelectionProvider)explorer.getAdapter(ISelectionProvider.class);
463             menuManager = cmi.createContextMenu(control, selectionProvider, site);
464         }
465
466         // Initialize UI contexts
467         activateUiContexts();
468
469         // Initialize DND.
470         dragSource = setupDND(explorer);
471
472         // Listeners are only added once per listener, not every time the
473         // session context changes.
474         addListeners(explorer, menuManager);
475
476         userSelectedComparableFactoryQueryProcessor = new UserSelectedComparableFactoryQueryProcessor();
477         userSelectedViewpointFactoryQueryProcessor = new UserSelectedViewpointFactoryQueryProcessor();
478         filterSelectionRequestQueryProcessor = new FilterSelectionRequestQueryProcessor();
479         
480         explorer.setPrimitiveProcessor(filterSelectionRequestQueryProcessor);
481
482         boolean hasExtraControls = false;
483         boolean hasExtraControls2 = false;
484
485         Boolean displaySelectors = (Boolean)args.get("displaySelectors");
486         if(displaySelectors == null || displaySelectors == true) {
487
488             @SuppressWarnings("unused")
489             ComparatorSelector comparatorSelector = new ComparatorSelector(explorer, userSelectedComparableFactoryQueryProcessor, toolComposite, SWT.READ_ONLY);
490 //            comparatorSelector.moveAbove(control);
491
492             @SuppressWarnings("unused")
493             ViewpointSelector viewpointSelector = new ViewpointSelector(explorer, userSelectedViewpointFactoryQueryProcessor, toolComposite, SWT.READ_ONLY);
494 //            viewpointSelector.moveAbove(control);
495
496             hasExtraControls = true;
497
498         }
499
500         Boolean displayFilter = (Boolean)args.get("displayFilter");
501         if(displayFilter == null || displayFilter == true) {
502
503                 
504                 filterArea = filterAreaSource.getFilterArea(toolComposite, explorer);
505 //            filterArea = new FilterArea(explorer, filterSelectionRequestQueryProcessor, toolComposite, SWT.READ_ONLY);
506             //filterArea.moveAbove(control);
507
508             hasExtraControls = true;
509
510         }
511
512         Boolean displayFilter2 = (Boolean)args.get("displayFilter2");
513         if(displayFilter2 != null && displayFilter2 == true) {
514
515                 filterArea = filterAreaSource.getFilterArea(toolComposite2, explorer);
516 //            filterArea = new FilterArea(explorer, filterSelectionRequestQueryProcessor, toolComposite2, SWT.READ_ONLY);
517 //            //filterArea.moveAbove(control);
518
519             hasExtraControls2 = true;
520
521         }
522
523 //        filterArea = new FilterArea(explorer, filterSelectionRequestQueryProcessor, this, SWT.READ_ONLY);
524 //        filterArea.moveAbove(control);
525
526         if(!hasExtraControls)
527             GridDataFactory.fillDefaults().grab(true, false).minSize(0, 0).hint(0, 0).applyTo(toolComposite);
528         if(!hasExtraControls2)
529             GridDataFactory.fillDefaults().grab(true, false).minSize(0, 0).hint(0, 0).applyTo(toolComposite2);
530
531         GridDataFactory.fillDefaults().grab(true, true).span(2,1).applyTo(control);
532
533         //tree.getTree().setLayout(new FillLayout()
534         //this.setLayout(LayoutUtils.createNoBorderGridLayout(2, false));
535
536         DropTarget target = new DropTarget(control, DND.DROP_COPY | DND.DROP_MOVE | DND.DROP_LINK | DND.DROP_DEFAULT);
537         target.setTransfer(getAcceptedDataTypes());
538         if (control instanceof Tree)  {
539         target.addDropListener(new DropTargetListener() {
540
541             Tree tree = (Tree)explorer.getControl();
542
543             @Override
544             public void dragEnter(DropTargetEvent event) {
545                 event.detail = DND.DROP_COPY;
546             }
547
548             @Override
549             public void dragLeave(DropTargetEvent event) {
550             }
551
552             @Override
553             public void dragOperationChanged(DropTargetEvent event) {
554             }
555
556             @Override
557             public void dragOver(DropTargetEvent event) {
558             }
559
560             @Override
561             public void drop(DropTargetEvent event) {
562                 TreeItem item = tree.getItem(tree.toControl(event.x, event.y));
563                 if(item != null) {
564                         Object data = item.getData();
565                         if (data instanceof NodeContext)
566                                 handleDrop(event.data, (NodeContext) data);
567                         else if (data instanceof IAdaptable) {
568                                 IAdaptable a = (IAdaptable) data;
569                                 handleDrop(event.data, (NodeContext) a.getAdapter(NodeContext.class));
570                         }
571                 } else
572                     handleDrop(event.data, null);
573             }
574
575             @Override
576             public void dropAccept(DropTargetEvent event) {
577             }
578
579         });
580         }
581
582         // Add workbench listeners and make sure they are cleaned up
583         setWorkbenchListeners();
584         control.addListener(SWT.Dispose, new Listener() {
585             @Override
586             public void handleEvent(Event event) {
587                 doDispose();
588             }
589         });
590     }
591
592     @SuppressWarnings({ "rawtypes", "unchecked" })
593     @Override
594     public Object getAdapter(Class adapter) {
595         if (GraphExplorer.class == adapter)
596             return explorer;
597         if (EvaluatorData.class == adapter)
598             return evaluatorData;
599         if (BrowseContext.class == adapter) {
600             EvaluatorData ed = evaluatorData;
601             return ed != null ? ed.getBrowseContext() : null;
602         }
603         if (adapter == IFilterAreaProvider.class)
604             return filterArea;
605         return explorer.getAdapter(adapter);
606     }
607
608     protected void doDispose() {
609         //System.out.println(this + ".GraphExplorerComposite.doDispose()");
610         removeWorkbenchListeners();
611         userSelectedComparableFactoryQueryProcessor = null;
612         userSelectedViewpointFactoryQueryProcessor = null;
613         filterSelectionRequestQueryProcessor = null;
614
615         disposeState = DisposeState.Disposing;
616         try {
617             //System.out.println(this + ".GraphExplorerViewBase.dispose()");
618             if (contextProvider != null) {
619                 contextProvider.removeContextChangedListener(contextChangeListener);
620                 contextProvider = null;
621             }
622             sessionContextTracker.untrack();
623             resourceManager = null;
624             explorer = null;
625             sessionContext = null;
626             dragSource = null;
627 //            parent = null;
628         } finally {
629             disposeState = DisposeState.Disposed;
630         }
631     }
632
633     @Override
634     public void dispose() {
635         doDispose();
636         super.dispose();
637     }
638
639     protected StatePersistor getStatePersistor() {
640         return persistor;
641     }
642     
643     public void setStatePersistor(StatePersistor persistor) {
644         this.persistor = persistor;
645     }
646     
647     protected void initializeExplorer(GraphExplorer explorer, ISessionContext context) {
648
649         if(explorer == null || explorer.isDisposed()) return;
650         if(context == null) return;
651         if(browseContexts == null) return;
652
653         Session session = context != null ? context.getSession() : null;
654         setupDragSource(session);
655
656         if (session != null) {
657             evaluatorData = createEvaluatorData(session);
658             explorer.setDataSource(new AsyncReadGraphDataSource(session));
659             explorer.setDataSource(new ReadGraphDataSource(session));
660         }
661         else {
662             evaluatorData = new EvaluatorDataImpl();
663             explorer.removeDataSource(AsyncReadGraph.class);
664             explorer.removeDataSource(ReadGraph.class);
665         }
666
667         explorer.setPersistor(getStatePersistor());
668         
669         explorer.setProcessor(new ComparableFactoryResolver(evaluatorData));
670         explorer.setProcessor(new ViewpointFactoryResolver(evaluatorData));
671         explorer.setProcessor(new LabelerFactoryResolver(evaluatorData));
672         explorer.setProcessor(new ImagerFactoryResolver(evaluatorData));
673         explorer.setProcessor(new LabelDecoratorFactoryResolver(evaluatorData));
674         explorer.setProcessor(new ImageDecoratorFactoryResolver(evaluatorData));
675         explorer.setProcessor(new DefaultIsCheckedProcessor2(evaluatorData));
676         explorer.setPrimitiveProcessor(new TypesQueryProcessor());
677         explorer.setPrimitiveProcessor(new StandardContextTypesQueryProcessor());
678         explorer.setPrimitiveProcessor(new InheritsQueryProcessor());
679         explorer.setPrimitiveProcessor(new RelatedObjectsQueryProcessor());
680
681         explorer.setPrimitiveProcessor(userSelectedViewpointFactoryQueryProcessor);
682         explorer.setProcessor(new ComparableSelectorQueryProcessor());
683         explorer.setPrimitiveProcessor(userSelectedComparableFactoryQueryProcessor);
684         explorer.setPrimitiveProcessor(filterSelectionRequestQueryProcessor);
685
686         initializeExplorerWithEvaluator(explorer, context, evaluatorData);
687     }
688
689     protected void initializeExplorerWithEvaluator(GraphExplorer explorer, ISessionContext context, EvaluatorData data) {
690     }
691
692     protected EvaluatorData createEvaluatorData(Session context) {
693
694 //        Set<String> browseContexts = getArgument("browseContexts");
695
696         return Evaluators.load(context.getSession(), browseContexts, resourceManager, useNodeBrowseContexts, useNodeActionContexts);
697
698     }
699
700     protected Transfer[] getAcceptedDataTypes() {
701         return new Transfer[] { LocalObjectTransfer.getTransfer(), FileTransfer.getInstance(), TextTransfer.getInstance() };
702     }
703
704     protected void handleDrop(Object data, NodeContext target) {
705     }
706
707     DragSourceListenerFactory dragSourceListenerFactory = new DragSourceListenerFactory() {
708
709         final Transfer[] transfers = new Transfer[] {LocalObjectTransfer.getTransfer(), TextTransfer.getInstance() };
710
711         @Override
712         public DragSourceListener get(ISelectionProvider selectionProvider) {
713
714                 LocalSelectionDragSourceListener ls = new LocalSelectionDragSourceListener(selectionProvider);
715
716                 return new DragSourceListener() {
717                                 
718                                 @Override
719                                 public void dragStart(DragSourceEvent event) {
720                                         ls.dragStart(event);
721                                 }
722                                 
723                                 @Override
724                                 public void dragSetData(DragSourceEvent event) {
725                                         if(TextTransfer.getInstance().isSupportedType(event.dataType)) {
726                                         try {
727                                                         event.data = WorkbenchSelectionUtils.getPossibleJSON(selectionProvider.getSelection());
728                                                 } catch (DatabaseException e) {
729                                                         event.data = "{ type:\"Exception\" }";
730                                                         LOGGER.error("Failed to get current selection as JSON.", e);
731                                                 }
732                                         } else if (LocalObjectTransfer.getTransfer().isSupportedType(event.dataType)) {
733                                                 ls.dragSetData(event);
734                                         }
735                                 }
736                                 
737                                 @Override
738                                 public void dragFinished(DragSourceEvent event) {
739                                         ls.dragFinished(event);
740                                 }
741                         };
742         }
743
744         @Override
745         public Transfer[] getTransfers() {
746             return transfers;
747         }
748
749     };
750
751     public void setDragSourceListenerFactory(DragSourceListenerFactory dragSourceListenerFactory) {
752         this.dragSourceListenerFactory = dragSourceListenerFactory;
753     }
754
755     protected DragSourceListener setupDND(GraphExplorer explorer) {
756
757         ISelectionProvider selectionProvider = (ISelectionProvider)explorer.getAdapter(ISelectionProvider.class);
758
759         DragSourceListener listener = createDragSourceListener(selectionProvider);
760
761         Control control = explorer.getControl();
762         DragSource source = createDragSource(control);
763         source.setTransfer(getTransfers());
764         source.addDragListener(listener);
765         source.setDragSourceEffect(new NoImageDragSourceEffect(control));
766
767         return listener;
768
769     }
770
771     protected DragSourceListener createDragSourceListener(ISelectionProvider selectionProvider) {
772         return dragSourceListenerFactory.get(selectionProvider);
773     }
774
775     private int dragStyle = DND.DROP_LINK | DND.DROP_MOVE | DND.DROP_COPY | DND.DROP_DEFAULT;
776
777     protected void setDragStyle(int style) {
778         this.dragStyle = style;
779     }
780
781     protected int getDragStyle() {
782         return dragStyle;
783     }
784
785     protected DragSource createDragSource(Control control) {
786         return new DragSource(control, getDragStyle());
787     }
788
789     protected Transfer[] getTransfers() {
790         return dragSourceListenerFactory.getTransfers();
791     }
792
793     public EvaluatorData getEvaluatorData() {
794         return evaluatorData;
795     }
796
797     public interface InputSource {
798         /**
799          * @param ctx the session context to read the input from. May be
800          *        <code>null</code> if there is no session.
801          * @return the input object of a graph explorer. To indicate no input,
802          *         use {@link GraphExplorerConstants#EMPTY_INPUT}. Never return
803          *         <code>null</code>.
804          */
805         Object get(ISessionContext ctx, Object selection);
806     }
807     
808     public interface FilterSource {
809         
810         
811         
812     }
813
814     /**
815      * The default hint tracker that will be active if
816      * {@link GraphExplorerComposite#setSessionContextTracker(IHintTracker) is
817      * not called.
818      */
819     public class SessionContextProjectTracker extends HintTracker {
820         public SessionContextProjectTracker() {
821             IHintListener activeProjectListener = new HintListenerAdapter() {
822                 @Override
823                 public void hintChanged(IHintObservable sender, Key key, Object oldValue, Object newValue) {
824                     applySessionContext(getSessionContext());
825                 }
826             };
827             addKeyHintListener(ProjectKeys.KEY_PROJECT, activeProjectListener);
828         }
829     }
830
831     public class DirectInputSource implements InputSource {
832         @Override
833         public Object get(ISessionContext ctx, Object selection) {
834             return selection;
835         }
836     }
837     
838     public static class SelectionFilterAreaSource implements FilterAreaSource {
839
840                 @Override
841                 public IFilterArea getFilterArea(Composite parent, GraphExplorer explorer) {
842                         FilterSelectionRequestQueryProcessor processor = (FilterSelectionRequestQueryProcessor)explorer.getPrimitiveProcessor(BuiltinKeys.SELECTION_REQUESTS);
843                         return new FilterArea(explorer, processor, parent, SWT.READ_ONLY);
844                 }
845         
846     }
847
848     public static class RootFilterAreaSource implements FilterAreaSource {
849
850                 @Override
851                 public IFilterArea getFilterArea(Composite parent, GraphExplorer explorer) {
852                         FilterSelectionRequestQueryProcessor processor = (FilterSelectionRequestQueryProcessor)explorer.getPrimitiveProcessor(BuiltinKeys.SELECTION_REQUESTS);
853                         return new RootFilterArea(explorer, processor, parent, SWT.READ_ONLY);
854                 }
855         
856     }
857
858     protected void setSessionContextTracker(IHintTracker tracker) {
859         this.sessionContextTracker = tracker;
860     }
861
862     public void setInputSource(InputSource source) {
863         this.inputSource = source;
864     }
865     
866     public void setFilterAreaSource(FilterAreaSource provider) {
867         this.filterAreaSource = provider;
868     }
869
870     public void setSelectionTransformation(BiFunction<GraphExplorer, Object[], Object[]> transformation) {
871         this.selectionTransformation = transformation;
872         if(explorer != null) explorer.setSelectionTransformation(transformation);
873     }
874
875     protected Set<String> getBrowseContexts() {
876         return browseContexts;
877     }
878     
879     public void setBrowseContexts(Set<String> contexts) {
880         this.browseContexts = contexts;
881         //initializeExplorer(explorer, getSessionContext());
882     }
883
884     public void setBrowseContexts(String ... contexts) {
885         this.browseContexts = new HashSet<String>();
886         for(String s : contexts) this.browseContexts.add(s);
887         initializeExplorer(explorer, getSessionContext());
888     }
889
890     public void setContextMenuId(String contextMenuId) {
891         this.contextMenuId = contextMenuId;
892     }
893
894 //    protected IContextMenuInitializer getContextMenuInitializer() {
895 //        String contextMenuId = getContextMenuId();
896 //        if(contextMenuId != null) {
897 //            return new ContextMenuInitializer(contextMenuId);
898 //        } else {
899 //            return null;
900 //        }
901 //    }
902
903     protected String getContextMenuId() {
904         return this.contextMenuId;
905     }
906
907     public void setUiContexts(Set<String> uiContext) {
908         this.uiContext = uiContext;
909     }
910
911     public Set<String> getUiContexts() {
912         return uiContext;
913     }
914
915     protected InputSource getInputSource() {
916         return inputSource;
917     }
918
919     protected Map<String, Object> getArguments() {
920         return args;
921     }
922
923     @SuppressWarnings("unchecked")
924     protected <T> T getArgument(String key) {
925         return (T) args.get(key);
926     }
927
928     protected DisposeState getDisposeState() {
929         return disposeState;
930     }
931
932     public ISessionContext getSessionContext() {
933         return sessionContext;
934     }
935
936     public ISessionContextProvider getSessionContextProvider() {
937         return contextProvider;
938     }
939
940     @Override
941     public boolean setFocus() {
942         if (explorer != null && !explorer.isDisposed())
943             explorer.setFocus();
944         return true;
945     }
946
947     public void setWorkbenchListeners() {
948         if (workbenchSelectionListener == null && getSite() != null) {
949             ISelectionProvider selectionProvider = (ISelectionProvider) explorer.getAdapter(ISelectionProvider.class);
950             getSite().setSelectionProvider(selectionProvider);
951
952             // Listen to the workbench selection also to propagate it to
953             // the explorer also.
954             workbenchSelectionListener = new DefaultExplorerSelectionListener(site.getPage().getActivePart(), explorer);
955             //System.out.println("ADD WORKBENCH SELECTION LISTENER: " + workbenchSelectionListener);
956             getSite().getWorkbenchWindow().getSelectionService().addPostSelectionListener(workbenchSelectionListener);
957         }
958     }
959
960     protected void removeWorkbenchListeners() {
961         //System.out.println("REMOVE WORKBENCH SELECTION LISTENER: " + workbenchSelectionListener);
962         // Remember to remove the installed workbench selection listener
963         if (workbenchSelectionListener != null) {
964             getSite().getWorkbenchWindow().getSelectionService().removePostSelectionListener(workbenchSelectionListener);
965             workbenchSelectionListener = null;
966
967             ISelectionProvider selectionProvider = (ISelectionProvider) explorer.getAdapter(ISelectionProvider.class);
968             if(getSite().getSelectionProvider() == selectionProvider) getSite().setSelectionProvider(null);
969             
970         }
971     }
972
973     protected final void attachToSession() {
974
975         // Track active ISessionContext changes
976         //contextProvider = SimanticsUI.getSessionContextProvider(getViewSite().getWorkbenchWindow());
977         contextProvider.addContextChangedListener(contextChangeListener);
978
979         // Start tracking the current session context for input changes.
980         // This will/must cause applySessionContext to get called.
981         // Doing the applySessionContext initialization this way
982         // instead of directly calling it will also make sure that
983         // applySessionContext is only called once when first initialized,
984         // and not twice like with the direct invocation.
985         this.sessionContext = contextProvider.getSessionContext();
986         sessionContextTracker.track(sessionContext);
987     }
988
989     // /////////////////////////////////////////////////////////////////////////
990     // Override / implement these:
991
992 //    /**
993 //     * Returns an ID that is used for persisting a GraphExplorer instance.
994 //     *
995 //     * Used for </code>restoreState(IMemento)</code> and
996 //     * <code>restoreState(IMemento)</code> in OntologyExplorer. Must be unique
997 //     * within a workbench part.
998 //     *
999 //     * @return a unique name for this particular graph explorer view used for
1000 //     *         saving and restoring the state of this view part
1001 //     */
1002 //    public String getExplorerName() {
1003 //        return "GraphExplorerViewBase";
1004 //    }
1005
1006     /**
1007      * Override this method to add controls to the view part. This is invoked
1008      * before attaching the view part to a database session.
1009      * 
1010      * @param parent
1011      */
1012     protected void createControls(Composite parent) {
1013
1014     }
1015
1016     /**
1017      * Override this method and provide a proper context menu initializer if you
1018      * want to have this base class initialize one for you.
1019      * 
1020      * @return the initializer to be used by {@link #createControls(Composite)}
1021      */
1022     protected IContextMenuInitializer getContextMenuInitializer() {
1023         String contextMenuId = getContextMenuId();
1024         if(contextMenuId != null) {
1025             return new ContextMenuInitializer(contextMenuId);
1026         } else {
1027             return null;
1028         }
1029     }
1030
1031     /**
1032      * @param parent
1033      * @return
1034      */
1035     protected GraphExplorer createExplorerControl(Composite parent, Integer maxChildren) {
1036         GraphExplorerFactory factory = GraphExplorerFactory.getInstance();
1037         if(maxChildren != null) factory = factory.maxChildrenShown(maxChildren);
1038
1039         GraphExplorer ge = factory
1040         .selectionDataResolver(new DefaultSelectionDataResolver())
1041         .selectionTransformation(selectionTransformation)
1042         .setServiceLocator(site)
1043         .create(parent, style);
1044
1045         return ge;
1046     }
1047     
1048     protected GraphExplorer createExplorerControl2(Composite parent, Integer maxChildren) {
1049         GraphExplorerFactory factory = GraphExplorerFactory.getInstance();
1050         if(maxChildren != null) factory = factory.maxChildrenShown(maxChildren);
1051
1052         GraphExplorer ge = factory
1053         .selectionDataResolver(new DefaultSelectionDataResolver())
1054         .selectionTransformation(selectionTransformation)
1055         .setServiceLocator(site)
1056         .create2(parent, style);
1057
1058         return ge;
1059     }
1060     
1061     protected GraphExplorer createExplorerControl3(Composite parent, Integer maxChildren) {
1062         GraphExplorerFactory factory = GraphExplorerFactory.getInstance();
1063         if(maxChildren != null) factory = factory.maxChildrenShown(maxChildren);
1064
1065         GraphExplorer ge = factory
1066         .selectionDataResolver(new DefaultSelectionDataResolver())
1067         .selectionTransformation(selectionTransformation)
1068         .setServiceLocator(site)
1069         .create3(parent, style);
1070
1071         return ge;
1072     }
1073
1074     protected void setupDragSource(Session session) {
1075         if (dragSource instanceof SessionContainer) {
1076             ((SessionContainer) dragSource).setSession(session);
1077         }
1078     }
1079
1080     /**
1081      * Override to customize the addition of listeners a newly created
1082      * GraphExplorer.
1083      * 
1084      * @param explorer
1085      */
1086     protected void addListeners(GraphExplorer explorer, IMenuManager menuManager) {
1087         addSelectionInputListeners(explorer, menuManager);
1088     }
1089
1090     protected void addSelectionInputListeners(GraphExplorer explorer, IMenuManager menuManager) {
1091         // Consider ENTER presses to simulate mouse left button double clicks
1092         explorer.addListener(new DefaultKeyListener(contextProvider, explorer, new Function<String[]>() {
1093             @Override
1094             public String[] execute(Object... obj) {
1095                 return getEditingColumn((NodeContext) obj[0]);
1096             }
1097         }));
1098         // Default double click handling
1099         explorer.addListener(new DefaultMouseListener(explorer));
1100     }
1101
1102     protected String[] getEditingColumn(NodeContext context) {
1103         return editingColumn;
1104     }
1105     
1106     public void setEditingColumn(String... columnKeysInOrderOfTrial) {
1107         this.editingColumn = columnKeysInOrderOfTrial;
1108     }
1109
1110     // Needed for preventing unnecessary re-initialization of the explorer with the same input.
1111     private Object currentInput;
1112     private Object currentRoot;
1113
1114     /**
1115      * Invoke this to reinitialize the explorer and reset its input. The input
1116      * will be resolved from the specified ISessionContext based on the
1117      * {@link SessionContextInputSource} that is currently in use. If the input
1118      * is identical to the previous input, nothing will be done.
1119      * 
1120      * @param context
1121      */
1122     public final boolean applySessionContext(ISessionContext context) {
1123
1124         // If control is not alive anymore, do nothing.
1125         //System.out.println(this + ": applySessionContext(" + context + "), explorer="  + explorer);
1126         if (disposeState != DisposeState.Alive)
1127             return false;
1128
1129         //System.out.println(this + ": initializeExplorer(" + explorer + ", " + context + ")");
1130         initializeExplorer(explorer, context);
1131
1132
1133         // Start tracking the session context.
1134         //
1135         // If this is not the same session that is currently tracked, it will
1136         // cause IHintListeners of the sessionContextTracker to fire.
1137         // For this we need the above input equality (identity) checking.
1138         // This is here just to make sure that we are tracking the correct
1139         // session context.
1140         sessionContextTracker.track(sessionContext);
1141
1142         this.sessionContext = context;
1143         Object root = inputSource.get(context, currentInput);
1144         if (ObjectUtils.objectEquals(root, currentRoot))
1145             return false;
1146
1147         currentRoot = root;
1148
1149         //System.out.println(this + ": setRoot(" + input + ")");
1150         explorer.setUIContexts(uiContext);
1151         explorer.setRoot(root);
1152
1153         return true;
1154
1155     }
1156
1157     protected boolean isImportantInput(Object previousSelection, Object selection) {
1158         return !ObjectUtils.objectEquals(previousSelection, selection);
1159     }
1160
1161     public void setInput(Object selection, boolean force) {
1162
1163         assert(created);
1164
1165         if (isDisposed())
1166             return;
1167         if (sessionContext == null)
1168             return;
1169
1170         // Check if this is a duplicate of the previous selection to reduce unnecessary flicker.
1171         if (!force && !isImportantInput(currentInput, selection))
1172             return;
1173
1174         currentInput = selection;
1175
1176         Object root = inputSource.get(sessionContext, selection);
1177
1178         currentRoot = root;
1179
1180         explorer.setUIContexts(uiContext);
1181
1182         if (root == null) {
1183             explorer.setRoot(GraphExplorer.EMPTY_INPUT);
1184         } else {
1185             explorer.setRoot(root);
1186         }
1187
1188     }
1189
1190     public void setColumnsVisible(boolean visible) {
1191         explorer.setColumnsVisible(visible);
1192     }
1193
1194     private int getColumnWidth(Column column, ExplorerState state) {
1195         // Get saved width from the persistor if there is one.
1196         if (state != null && state.columnWidths != null) {
1197             Integer width = state.columnWidths.get(column.getLabel());
1198             if (width != null)
1199                 return width;
1200         }
1201         return column.getWidth();
1202     }
1203
1204     protected void restoreColumnSizes(Map<Column, Object> columns) {
1205         if (persistor != null) {
1206             setColumnData(columns, null);
1207             ExplorerStates.scheduleRead(explorer.getRoot(), persistor).thenAccept(state -> {
1208                 SWTUtils.asyncExec(GraphExplorerComposite.this, () -> {
1209                     if (explorerComposite.isDisposed())
1210                         setColumnData(columns, state);
1211                 });
1212             });
1213         } else {
1214             setColumnData(columns, null);
1215         }
1216     }
1217
1218     protected void setColumnData(Map<Column, Object> columns, ExplorerState state) {
1219         columns.forEach((column, widget) -> {
1220             org.eclipse.swt.widgets.Widget columnWidget = (org.eclipse.swt.widgets.Widget) widget;
1221             ad.setColumnData(columnWidget,
1222                     new ColumnWeightData(
1223                             column.hasGrab() ? column.getWeight() : 0,
1224                             getColumnWidth(column, state)));
1225         });
1226     }
1227
1228     public void setColumns(Column[] columns) {
1229         // ColumnWeightData does not support column weight/width < 0
1230         for (Column column : columns) {
1231             if (column.getWeight() < 0)
1232                 throw new IllegalArgumentException("Column weight must be >= 0, got " + column.getWeight() + " for " + column);
1233             if (column.getWidth() < 0)
1234                 throw new IllegalArgumentException("Column minimum width must be >= 0, got " + column.getWidth() + " for " + column);
1235         }
1236         explorer.setColumns(columns, this::restoreColumnSizes);
1237     }
1238
1239     @Override
1240     public void setInput(ISessionContext context, Object input) {
1241         setInput(input, false);
1242     }
1243
1244     public void setMaxChildren(int maxChildren) {
1245         explorer.setMaxChildren(maxChildren);
1246     }
1247     
1248     public <T> void addListener(ExplorerMouseListenerImpl<T> listener) {
1249         
1250         support.register(listener);
1251         listener.register(explorer);
1252         explorer.addListener(listener);
1253         
1254     }
1255
1256 //    @Override
1257 //    public Point computeSize(int wHint, int hHint) {
1258 //      Point p = super.computeSize(wHint, hHint);
1259 //      System.err.println("graphExplorerComposite.computeSize " + p);
1260 //      return p;
1261 //    }
1262 //    
1263 //    @Override
1264 //    public Point computeSize(int wHint, int hHint, boolean changed) {
1265 //      Point p = super.computeSize(wHint, hHint, changed);
1266 //      System.err.println("graphExplorerComposite.computeSize " + p);
1267 //      return p;
1268 //    }
1269     
1270 }