1 /*******************************************************************************
2 * Copyright (c) 2007, 2018 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.browsing.ui.swt.widgets;
14 import java.util.Collection;
15 import java.util.Collections;
16 import java.util.HashSet;
19 import java.util.function.BiFunction;
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;
140 public class GraphExplorerComposite extends Composite implements Widget, IAdaptable {
142 private static final Logger LOGGER = LoggerFactory.getLogger(GraphExplorerComposite.class);
144 protected UserSelectedComparableFactoryQueryProcessor userSelectedComparableFactoryQueryProcessor;
145 protected UserSelectedViewpointFactoryQueryProcessor userSelectedViewpointFactoryQueryProcessor;
146 protected FilterSelectionRequestQueryProcessor filterSelectionRequestQueryProcessor;
147 protected IFilterArea filterArea;
148 protected EvaluatorData evaluatorData;
150 protected LocalResourceManager resourceManager;
152 protected ISelectionListener workbenchSelectionListener;
154 private final int style;
156 final private IWorkbenchSite site;
158 protected GraphExplorer explorer;
160 protected IMenuManager menuManager;
162 private final Map<String, Object> args;
164 protected ISessionContextProvider contextProvider;
166 private ISessionContext sessionContext;
168 private Object dragSource;
170 private IHintTracker sessionContextTracker = new SessionContextProjectTracker();
172 private InputSource inputSource = new DirectInputSource();
173 private FilterAreaSource filterAreaSource = new SelectionFilterAreaSource();
175 private final TreeColumnLayout ad;
176 private String[] editingColumn = ColumnKeys.KEYS_SINGLE;
178 private StatePersistor persistor = null;
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;
187 static class SelectionElement extends AdaptableHintContext {
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;
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);
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;
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;
232 private Object extractInput(Object content) {
233 if(content instanceof NodeContext) {
234 NodeContext context = (NodeContext)content;
235 return context.getConstant(BuiltinKeys.INPUT);
240 public SelectionElement(GraphExplorer explorer, Key[] keys, Object content) {
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();
250 @SuppressWarnings("unchecked")
252 public <T> T getContent(WorkbenchSelectionContentType<T> contentType) {
254 T result = wse.getContent(contentType);
259 if (contentType instanceof AnyResource) {
260 if (resource != null)
262 if (variable == null)
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);
270 else if (contentType instanceof AnyVariable) {
271 if (variable != null)
273 if (resource == null)
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);
280 } else if (contentType instanceof ExplorerInputContentType) {
282 } else if (contentType instanceof ExplorerColumnContentType) {
283 return (T) explorerState.getActiveColumn();
288 @SuppressWarnings("rawtypes")
290 public Object getAdapter(Class adapter) {
291 if(WorkbenchSelectionElement.class == adapter) {
294 if(NodeContext.class == adapter) {
295 if(content instanceof NodeContext)
296 return (NodeContext)content;
300 return super.getAdapter(adapter);
304 private BiFunction<GraphExplorer, Object[], Object[]> selectionTransformation = new BiFunction<GraphExplorer, Object[], Object[]>() {
306 private Key[] KEYS = new Key[] { SelectionHints.KEY_MAIN };
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]);
321 private Set<String> browseContexts = null;
323 private DisposeState disposeState = DisposeState.Alive;
325 private boolean created = false;
327 protected String contextMenuId = null;
329 protected Set<String> uiContext = null;
331 protected ISessionContextChangedListener contextChangeListener = new ISessionContextChangedListener() {
333 public void sessionContextChanged(SessionContextChangedEvent event) {
334 sessionContext = event.getNewValue();
335 sessionContextTracker.track(sessionContext);
339 public GraphExplorerComposite(Map<String, Object> args, IWorkbenchSite site, Composite parent, WidgetSupport support, int style) {
341 super(parent, SWT.NONE);
344 args = Collections.emptyMap();
349 this.resourceManager = new LocalResourceManager(JFaceResources.getResources(parent.getDisplay()), this);
351 contextProvider = getSessionContextProvider(site);
353 Integer maxChildren = (Integer)args.get("maxChildren");
355 GridLayoutFactory.fillDefaults().equalWidth(false).numColumns(1).margins(0,0).spacing(0,0).applyTo(this);
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);
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);
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);
373 explorer = createExplorerControl(explorerComposite, maxChildren);
376 if (args.containsKey("useNodeBrowseContexts") && Boolean.TRUE.equals(args.get("useNodeBrowseContexts"))) {
377 useNodeBrowseContexts = true;
379 useNodeBrowseContexts = false;
382 if (args.containsKey("useNodeActionContexts") && Boolean.TRUE.equals(args.get("useNodeActionContexts"))) {
383 useNodeActionContexts = true;
385 useNodeActionContexts = false;
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);
394 this.support = support;
397 support.register(this);
401 public GraphExplorerComposite(Map<String, Object> args, IWorkbenchSite site, Composite parent, int style) {
403 this(args, site, parent, null, style);
407 public ISessionContextProvider getSessionContextProvider(IWorkbenchSite site) {
409 return SimanticsUI.getSessionContextProvider(site.getWorkbenchWindow());
411 return Simantics.getSessionContextProvider();
414 public GraphExplorer getExplorer() {
418 public Composite getExplorerComposite() {
419 return explorerComposite;
422 public <T> T getExplorerControl() {
423 return explorer.getControl();
426 public void addListenerToControl(int eventType, Listener listener) {
427 ((Control)explorer.getControl()).addListener(eventType, listener);
430 public void finish() {
432 createControls(site);
436 IWorkbenchSite getSite() {
440 protected void activateUiContexts() {
441 Collection<String> contexts = getUiContexts();
442 if (contexts == null || contexts.isEmpty())
444 IWorkbenchSite site = getSite();
446 IContextService cs = (IContextService) getSite().getService(IContextService.class);
447 for (String context : contexts)
448 cs.activateContext(context);
452 protected void createControls(IWorkbenchSite site) {
454 // Initialize explorer control.
455 // GridDataFactory.fillDefaults().grab(true, true).applyTo(explorer.getControl());
457 Control control = explorer.getControl();
459 // Initialize context menu if an initializer is provided.
460 IContextMenuInitializer cmi = getContextMenuInitializer();
462 ISelectionProvider selectionProvider = (ISelectionProvider)explorer.getAdapter(ISelectionProvider.class);
463 menuManager = cmi.createContextMenu(control, selectionProvider, site);
466 // Initialize UI contexts
467 activateUiContexts();
470 dragSource = setupDND(explorer);
472 // Listeners are only added once per listener, not every time the
473 // session context changes.
474 addListeners(explorer, menuManager);
476 userSelectedComparableFactoryQueryProcessor = new UserSelectedComparableFactoryQueryProcessor();
477 userSelectedViewpointFactoryQueryProcessor = new UserSelectedViewpointFactoryQueryProcessor();
478 filterSelectionRequestQueryProcessor = new FilterSelectionRequestQueryProcessor();
480 explorer.setPrimitiveProcessor(filterSelectionRequestQueryProcessor);
482 boolean hasExtraControls = false;
483 boolean hasExtraControls2 = false;
485 Boolean displaySelectors = (Boolean)args.get("displaySelectors");
486 if(displaySelectors == null || displaySelectors == true) {
488 @SuppressWarnings("unused")
489 ComparatorSelector comparatorSelector = new ComparatorSelector(explorer, userSelectedComparableFactoryQueryProcessor, toolComposite, SWT.READ_ONLY);
490 // comparatorSelector.moveAbove(control);
492 @SuppressWarnings("unused")
493 ViewpointSelector viewpointSelector = new ViewpointSelector(explorer, userSelectedViewpointFactoryQueryProcessor, toolComposite, SWT.READ_ONLY);
494 // viewpointSelector.moveAbove(control);
496 hasExtraControls = true;
500 Boolean displayFilter = (Boolean)args.get("displayFilter");
501 if(displayFilter == null || displayFilter == true) {
504 filterArea = filterAreaSource.getFilterArea(toolComposite, explorer);
505 // filterArea = new FilterArea(explorer, filterSelectionRequestQueryProcessor, toolComposite, SWT.READ_ONLY);
506 //filterArea.moveAbove(control);
508 hasExtraControls = true;
512 Boolean displayFilter2 = (Boolean)args.get("displayFilter2");
513 if(displayFilter2 != null && displayFilter2 == true) {
515 filterArea = filterAreaSource.getFilterArea(toolComposite2, explorer);
516 // filterArea = new FilterArea(explorer, filterSelectionRequestQueryProcessor, toolComposite2, SWT.READ_ONLY);
517 // //filterArea.moveAbove(control);
519 hasExtraControls2 = true;
523 // filterArea = new FilterArea(explorer, filterSelectionRequestQueryProcessor, this, SWT.READ_ONLY);
524 // filterArea.moveAbove(control);
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);
531 GridDataFactory.fillDefaults().grab(true, true).span(2,1).applyTo(control);
533 //tree.getTree().setLayout(new FillLayout()
534 //this.setLayout(LayoutUtils.createNoBorderGridLayout(2, false));
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() {
541 Tree tree = (Tree)explorer.getControl();
544 public void dragEnter(DropTargetEvent event) {
545 event.detail = DND.DROP_COPY;
549 public void dragLeave(DropTargetEvent event) {
553 public void dragOperationChanged(DropTargetEvent event) {
557 public void dragOver(DropTargetEvent event) {
561 public void drop(DropTargetEvent event) {
562 TreeItem item = tree.getItem(tree.toControl(event.x, event.y));
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));
572 handleDrop(event.data, null);
576 public void dropAccept(DropTargetEvent event) {
582 // Add workbench listeners and make sure they are cleaned up
583 setWorkbenchListeners();
584 control.addListener(SWT.Dispose, new Listener() {
586 public void handleEvent(Event event) {
592 @SuppressWarnings({ "rawtypes", "unchecked" })
594 public Object getAdapter(Class adapter) {
595 if (GraphExplorer.class == adapter)
597 if (EvaluatorData.class == adapter)
598 return evaluatorData;
599 if (BrowseContext.class == adapter) {
600 EvaluatorData ed = evaluatorData;
601 return ed != null ? ed.getBrowseContext() : null;
603 if (adapter == IFilterAreaProvider.class)
605 return explorer.getAdapter(adapter);
608 protected void doDispose() {
609 //System.out.println(this + ".GraphExplorerComposite.doDispose()");
610 removeWorkbenchListeners();
611 userSelectedComparableFactoryQueryProcessor = null;
612 userSelectedViewpointFactoryQueryProcessor = null;
613 filterSelectionRequestQueryProcessor = null;
615 disposeState = DisposeState.Disposing;
617 //System.out.println(this + ".GraphExplorerViewBase.dispose()");
618 if (contextProvider != null) {
619 contextProvider.removeContextChangedListener(contextChangeListener);
620 contextProvider = null;
622 sessionContextTracker.untrack();
623 resourceManager = null;
625 sessionContext = null;
629 disposeState = DisposeState.Disposed;
634 public void dispose() {
639 protected StatePersistor getStatePersistor() {
643 public void setStatePersistor(StatePersistor persistor) {
644 this.persistor = persistor;
647 protected void initializeExplorer(GraphExplorer explorer, ISessionContext context) {
649 if(explorer == null || explorer.isDisposed()) return;
650 if(context == null) return;
651 if(browseContexts == null) return;
653 Session session = context != null ? context.getSession() : null;
654 setupDragSource(session);
656 if (session != null) {
657 evaluatorData = createEvaluatorData(session);
658 explorer.setDataSource(new AsyncReadGraphDataSource(session));
659 explorer.setDataSource(new ReadGraphDataSource(session));
662 evaluatorData = new EvaluatorDataImpl();
663 explorer.removeDataSource(AsyncReadGraph.class);
664 explorer.removeDataSource(ReadGraph.class);
667 explorer.setPersistor(getStatePersistor());
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());
681 explorer.setPrimitiveProcessor(userSelectedViewpointFactoryQueryProcessor);
682 explorer.setProcessor(new ComparableSelectorQueryProcessor());
683 explorer.setPrimitiveProcessor(userSelectedComparableFactoryQueryProcessor);
684 explorer.setPrimitiveProcessor(filterSelectionRequestQueryProcessor);
686 initializeExplorerWithEvaluator(explorer, context, evaluatorData);
689 protected void initializeExplorerWithEvaluator(GraphExplorer explorer, ISessionContext context, EvaluatorData data) {
692 protected EvaluatorData createEvaluatorData(Session context) {
694 // Set<String> browseContexts = getArgument("browseContexts");
696 return Evaluators.load(context.getSession(), browseContexts, resourceManager, useNodeBrowseContexts, useNodeActionContexts);
700 protected Transfer[] getAcceptedDataTypes() {
701 return new Transfer[] { LocalObjectTransfer.getTransfer(), FileTransfer.getInstance(), TextTransfer.getInstance() };
704 protected void handleDrop(Object data, NodeContext target) {
707 DragSourceListenerFactory dragSourceListenerFactory = new DragSourceListenerFactory() {
709 final Transfer[] transfers = new Transfer[] {LocalObjectTransfer.getTransfer(), TextTransfer.getInstance() };
712 public DragSourceListener get(ISelectionProvider selectionProvider) {
714 LocalSelectionDragSourceListener ls = new LocalSelectionDragSourceListener(selectionProvider);
716 return new DragSourceListener() {
719 public void dragStart(DragSourceEvent event) {
724 public void dragSetData(DragSourceEvent event) {
725 if(TextTransfer.getInstance().isSupportedType(event.dataType)) {
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);
732 } else if (LocalObjectTransfer.getTransfer().isSupportedType(event.dataType)) {
733 ls.dragSetData(event);
738 public void dragFinished(DragSourceEvent event) {
739 ls.dragFinished(event);
745 public Transfer[] getTransfers() {
751 public void setDragSourceListenerFactory(DragSourceListenerFactory dragSourceListenerFactory) {
752 this.dragSourceListenerFactory = dragSourceListenerFactory;
755 protected DragSourceListener setupDND(GraphExplorer explorer) {
757 ISelectionProvider selectionProvider = (ISelectionProvider)explorer.getAdapter(ISelectionProvider.class);
759 DragSourceListener listener = createDragSourceListener(selectionProvider);
761 Control control = explorer.getControl();
762 DragSource source = createDragSource(control);
763 source.setTransfer(getTransfers());
764 source.addDragListener(listener);
765 source.setDragSourceEffect(new NoImageDragSourceEffect(control));
771 protected DragSourceListener createDragSourceListener(ISelectionProvider selectionProvider) {
772 return dragSourceListenerFactory.get(selectionProvider);
775 private int dragStyle = DND.DROP_LINK | DND.DROP_MOVE | DND.DROP_COPY | DND.DROP_DEFAULT;
777 protected void setDragStyle(int style) {
778 this.dragStyle = style;
781 protected int getDragStyle() {
785 protected DragSource createDragSource(Control control) {
786 return new DragSource(control, getDragStyle());
789 protected Transfer[] getTransfers() {
790 return dragSourceListenerFactory.getTransfers();
793 public EvaluatorData getEvaluatorData() {
794 return evaluatorData;
797 public interface InputSource {
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
805 Object get(ISessionContext ctx, Object selection);
808 public interface FilterSource {
815 * The default hint tracker that will be active if
816 * {@link GraphExplorerComposite#setSessionContextTracker(IHintTracker) is
819 public class SessionContextProjectTracker extends HintTracker {
820 public SessionContextProjectTracker() {
821 IHintListener activeProjectListener = new HintListenerAdapter() {
823 public void hintChanged(IHintObservable sender, Key key, Object oldValue, Object newValue) {
824 applySessionContext(getSessionContext());
827 addKeyHintListener(ProjectKeys.KEY_PROJECT, activeProjectListener);
831 public class DirectInputSource implements InputSource {
833 public Object get(ISessionContext ctx, Object selection) {
838 public static class SelectionFilterAreaSource implements FilterAreaSource {
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);
848 public static class RootFilterAreaSource implements FilterAreaSource {
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);
858 protected void setSessionContextTracker(IHintTracker tracker) {
859 this.sessionContextTracker = tracker;
862 public void setInputSource(InputSource source) {
863 this.inputSource = source;
866 public void setFilterAreaSource(FilterAreaSource provider) {
867 this.filterAreaSource = provider;
870 public void setSelectionTransformation(BiFunction<GraphExplorer, Object[], Object[]> transformation) {
871 this.selectionTransformation = transformation;
872 if(explorer != null) explorer.setSelectionTransformation(transformation);
875 protected Set<String> getBrowseContexts() {
876 return browseContexts;
879 public void setBrowseContexts(Set<String> contexts) {
880 this.browseContexts = contexts;
881 //initializeExplorer(explorer, getSessionContext());
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());
890 public void setContextMenuId(String contextMenuId) {
891 this.contextMenuId = contextMenuId;
894 // protected IContextMenuInitializer getContextMenuInitializer() {
895 // String contextMenuId = getContextMenuId();
896 // if(contextMenuId != null) {
897 // return new ContextMenuInitializer(contextMenuId);
903 protected String getContextMenuId() {
904 return this.contextMenuId;
907 public void setUiContexts(Set<String> uiContext) {
908 this.uiContext = uiContext;
911 public Set<String> getUiContexts() {
915 protected InputSource getInputSource() {
919 protected Map<String, Object> getArguments() {
923 @SuppressWarnings("unchecked")
924 protected <T> T getArgument(String key) {
925 return (T) args.get(key);
928 protected DisposeState getDisposeState() {
932 public ISessionContext getSessionContext() {
933 return sessionContext;
936 public ISessionContextProvider getSessionContextProvider() {
937 return contextProvider;
941 public boolean setFocus() {
942 if (explorer != null && !explorer.isDisposed())
947 public void setWorkbenchListeners() {
948 if (workbenchSelectionListener == null && getSite() != null) {
949 ISelectionProvider selectionProvider = (ISelectionProvider) explorer.getAdapter(ISelectionProvider.class);
950 getSite().setSelectionProvider(selectionProvider);
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);
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;
967 ISelectionProvider selectionProvider = (ISelectionProvider) explorer.getAdapter(ISelectionProvider.class);
968 if(getSite().getSelectionProvider() == selectionProvider) getSite().setSelectionProvider(null);
973 protected final void attachToSession() {
975 // Track active ISessionContext changes
976 //contextProvider = SimanticsUI.getSessionContextProvider(getViewSite().getWorkbenchWindow());
977 contextProvider.addContextChangedListener(contextChangeListener);
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);
989 // /////////////////////////////////////////////////////////////////////////
990 // Override / implement these:
993 // * Returns an ID that is used for persisting a GraphExplorer instance.
995 // * Used for </code>restoreState(IMemento)</code> and
996 // * <code>restoreState(IMemento)</code> in OntologyExplorer. Must be unique
997 // * within a workbench part.
999 // * @return a unique name for this particular graph explorer view used for
1000 // * saving and restoring the state of this view part
1002 // public String getExplorerName() {
1003 // return "GraphExplorerViewBase";
1007 * Override this method to add controls to the view part. This is invoked
1008 * before attaching the view part to a database session.
1012 protected void createControls(Composite parent) {
1017 * Override this method and provide a proper context menu initializer if you
1018 * want to have this base class initialize one for you.
1020 * @return the initializer to be used by {@link #createControls(Composite)}
1022 protected IContextMenuInitializer getContextMenuInitializer() {
1023 String contextMenuId = getContextMenuId();
1024 if(contextMenuId != null) {
1025 return new ContextMenuInitializer(contextMenuId);
1035 protected GraphExplorer createExplorerControl(Composite parent, Integer maxChildren) {
1036 GraphExplorerFactory factory = GraphExplorerFactory.getInstance();
1037 if(maxChildren != null) factory = factory.maxChildrenShown(maxChildren);
1039 GraphExplorer ge = factory
1040 .selectionDataResolver(new DefaultSelectionDataResolver())
1041 .selectionTransformation(selectionTransformation)
1042 .setServiceLocator(site)
1043 .create(parent, style);
1048 protected GraphExplorer createExplorerControl2(Composite parent, Integer maxChildren) {
1049 GraphExplorerFactory factory = GraphExplorerFactory.getInstance();
1050 if(maxChildren != null) factory = factory.maxChildrenShown(maxChildren);
1052 GraphExplorer ge = factory
1053 .selectionDataResolver(new DefaultSelectionDataResolver())
1054 .selectionTransformation(selectionTransformation)
1055 .setServiceLocator(site)
1056 .create2(parent, style);
1061 protected GraphExplorer createExplorerControl3(Composite parent, Integer maxChildren) {
1062 GraphExplorerFactory factory = GraphExplorerFactory.getInstance();
1063 if(maxChildren != null) factory = factory.maxChildrenShown(maxChildren);
1065 GraphExplorer ge = factory
1066 .selectionDataResolver(new DefaultSelectionDataResolver())
1067 .selectionTransformation(selectionTransformation)
1068 .setServiceLocator(site)
1069 .create3(parent, style);
1074 protected void setupDragSource(Session session) {
1075 if (dragSource instanceof SessionContainer) {
1076 ((SessionContainer) dragSource).setSession(session);
1081 * Override to customize the addition of listeners a newly created
1086 protected void addListeners(GraphExplorer explorer, IMenuManager menuManager) {
1087 addSelectionInputListeners(explorer, menuManager);
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[]>() {
1094 public String[] execute(Object... obj) {
1095 return getEditingColumn((NodeContext) obj[0]);
1098 // Default double click handling
1099 explorer.addListener(new DefaultMouseListener(explorer));
1102 protected String[] getEditingColumn(NodeContext context) {
1103 return editingColumn;
1106 public void setEditingColumn(String... columnKeysInOrderOfTrial) {
1107 this.editingColumn = columnKeysInOrderOfTrial;
1110 // Needed for preventing unnecessary re-initialization of the explorer with the same input.
1111 private Object currentInput;
1112 private Object currentRoot;
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.
1122 public final boolean applySessionContext(ISessionContext context) {
1124 // If control is not alive anymore, do nothing.
1125 //System.out.println(this + ": applySessionContext(" + context + "), explorer=" + explorer);
1126 if (disposeState != DisposeState.Alive)
1129 //System.out.println(this + ": initializeExplorer(" + explorer + ", " + context + ")");
1130 initializeExplorer(explorer, context);
1133 // Start tracking the session context.
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
1140 sessionContextTracker.track(sessionContext);
1142 this.sessionContext = context;
1143 Object root = inputSource.get(context, currentInput);
1144 if (ObjectUtils.objectEquals(root, currentRoot))
1149 //System.out.println(this + ": setRoot(" + input + ")");
1150 explorer.setUIContexts(uiContext);
1151 explorer.setRoot(root);
1157 protected boolean isImportantInput(Object previousSelection, Object selection) {
1158 return !ObjectUtils.objectEquals(previousSelection, selection);
1161 public void setInput(Object selection, boolean force) {
1167 if (sessionContext == null)
1170 // Check if this is a duplicate of the previous selection to reduce unnecessary flicker.
1171 if (!force && !isImportantInput(currentInput, selection))
1174 currentInput = selection;
1176 Object root = inputSource.get(sessionContext, selection);
1180 explorer.setUIContexts(uiContext);
1183 explorer.setRoot(GraphExplorer.EMPTY_INPUT);
1185 explorer.setRoot(root);
1190 public void setColumnsVisible(boolean visible) {
1191 explorer.setColumnsVisible(visible);
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());
1201 return column.getWidth();
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);
1214 setColumnData(columns, null);
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)));
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);
1236 explorer.setColumns(columns, this::restoreColumnSizes);
1240 public void setInput(ISessionContext context, Object input) {
1241 setInput(input, false);
1244 public void setMaxChildren(int maxChildren) {
1245 explorer.setMaxChildren(maxChildren);
1248 public <T> void addListener(ExplorerMouseListenerImpl<T> listener) {
1250 support.register(listener);
1251 listener.register(explorer);
1252 explorer.addListener(listener);
1257 // public Point computeSize(int wHint, int hHint) {
1258 // Point p = super.computeSize(wHint, hHint);
1259 // System.err.println("graphExplorerComposite.computeSize " + p);
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);