-/*******************************************************************************\r
- * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
- * in Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- * VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-package org.simantics.selectionview;\r
-\r
-import java.util.ArrayList;\r
-import java.util.Iterator;\r
-import java.util.List;\r
-import java.util.function.Consumer;\r
-\r
-import org.eclipse.jface.action.ContributionItem;\r
-import org.eclipse.jface.action.IContributionItem;\r
-import org.eclipse.jface.layout.GridDataFactory;\r
-import org.eclipse.jface.resource.JFaceResources;\r
-import org.eclipse.jface.resource.LocalResourceManager;\r
-import org.eclipse.jface.viewers.IPostSelectionProvider;\r
-import org.eclipse.jface.viewers.ISelection;\r
-import org.eclipse.jface.viewers.ISelectionProvider;\r
-import org.eclipse.jface.viewers.IStructuredSelection;\r
-import org.eclipse.swt.SWT;\r
-import org.eclipse.swt.dnd.DND;\r
-import org.eclipse.swt.dnd.DragSource;\r
-import org.eclipse.swt.widgets.Composite;\r
-import org.eclipse.swt.widgets.Control;\r
-import org.eclipse.swt.widgets.Event;\r
-import org.eclipse.swt.widgets.Listener;\r
-import org.eclipse.swt.widgets.ToolBar;\r
-import org.eclipse.swt.widgets.ToolItem;\r
-import org.eclipse.swt.widgets.Tree;\r
-import org.eclipse.ui.IActionBars;\r
-import org.eclipse.ui.IWorkbenchSite;\r
-import org.simantics.browsing.ui.GraphExplorer;\r
-import org.simantics.browsing.ui.common.ColumnKeys;\r
-import org.simantics.browsing.ui.common.EvaluatorData;\r
-import org.simantics.browsing.ui.common.EvaluatorData.Evaluator;\r
-import org.simantics.browsing.ui.common.EvaluatorDataImpl;\r
-import org.simantics.browsing.ui.common.comparators.AlphanumericComparatorFactory;\r
-import org.simantics.browsing.ui.common.processors.ComparableFactoryResolver;\r
-import org.simantics.browsing.ui.common.processors.ComparableSelectorQueryProcessor;\r
-import org.simantics.browsing.ui.common.processors.FilterSelectionRequestQueryProcessor;\r
-import org.simantics.browsing.ui.common.processors.ImageDecoratorFactoryResolver;\r
-import org.simantics.browsing.ui.common.processors.ImagerFactoryResolver;\r
-import org.simantics.browsing.ui.common.processors.LabelDecoratorFactoryResolver;\r
-import org.simantics.browsing.ui.common.processors.LabelerFactoryResolver;\r
-import org.simantics.browsing.ui.common.processors.UserSelectedComparableFactoryQueryProcessor;\r
-import org.simantics.browsing.ui.common.processors.UserSelectedViewpointFactoryQueryProcessor;\r
-import org.simantics.browsing.ui.common.processors.ViewpointFactoryResolver;\r
-import org.simantics.browsing.ui.common.views.IFilterArea;\r
-import org.simantics.browsing.ui.common.views.IFilterAreaProvider;\r
-import org.simantics.browsing.ui.common.views.PropertyTableConstants;\r
-import org.simantics.browsing.ui.graph.impl.ArrayPropertyLabelerFactory;\r
-import org.simantics.browsing.ui.graph.impl.ArrayPropertyValueViewpointFactory;\r
-import org.simantics.browsing.ui.graph.impl.AsyncReadGraphDataSource;\r
-import org.simantics.browsing.ui.graph.impl.PropertyViewpointFactory;\r
-import org.simantics.browsing.ui.graph.impl.ReadGraphDataSource;\r
-import org.simantics.browsing.ui.graph.impl.RelationContextLabelerFactory;\r
-import org.simantics.browsing.ui.graph.impl.ResourceProperty;\r
-import org.simantics.browsing.ui.graph.impl.StringRepresentationLabelerFactory;\r
-import org.simantics.browsing.ui.swt.ArrayPropertyImagerFactory;\r
-import org.simantics.browsing.ui.swt.ComparatorSelector;\r
-import org.simantics.browsing.ui.swt.ContextMenuInitializer;\r
-import org.simantics.browsing.ui.swt.GraphExplorerFactory;\r
-import org.simantics.browsing.ui.swt.PropertyTableUtil;\r
-import org.simantics.browsing.ui.swt.RootFilterArea;\r
-import org.simantics.browsing.ui.swt.TypesQueryProcessor;\r
-import org.simantics.browsing.ui.swt.ViewpointSelector;\r
-import org.simantics.db.AsyncReadGraph;\r
-import org.simantics.db.ReadGraph;\r
-import org.simantics.db.RelationContext;\r
-import org.simantics.db.Resource;\r
-import org.simantics.db.Session;\r
-import org.simantics.db.Statement;\r
-import org.simantics.db.common.ResourceArray;\r
-import org.simantics.db.management.ISessionContext;\r
-import org.simantics.ui.dnd.BasicDragSource;\r
-import org.simantics.ui.dnd.SessionContainer;\r
-import org.simantics.ui.utils.ResourceAdaptionUtils;\r
-import org.simantics.utils.ObjectUtils;\r
-import org.simantics.utils.ui.LayoutUtils;\r
-\r
-\r
-/**\r
- * <p>\r
- * Subclasses may extend or reimplement the following methods as required:\r
- * <ul>\r
- * <li><code>createExplorerControl</code> - reimplement to customize the way the IGraphExplorer control is created</li>\r
- * <li><code>createControl</code> - reimplement to customize the way other supplementary controls get created</li>\r
- * <li><code>createDragSource</code> - reimplement to customize the kind of DragSource this control provides</li>\r
- * </ul>\r
- * </p>\r
- * \r
- * @author Tuukka Lehtonen\r
- */\r
-public class PropertyTable extends Composite implements IFilterAreaProvider, IPropertyTab2 {\r
-\r
- protected IWorkbenchSite site;\r
- protected GraphExplorer explorer;\r
- protected Object dragSource;\r
- protected ISessionContext sessionContext;\r
- protected ISelection currentInput;\r
-\r
- protected UserSelectedComparableFactoryQueryProcessor userSelectedComparableFactoryQueryProcessor;\r
- protected UserSelectedViewpointFactoryQueryProcessor userSelectedViewpointFactoryQueryProcessor;\r
- protected FilterSelectionRequestQueryProcessor filterSelectionRequestQueryProcessor;\r
-\r
- protected LocalResourceManager resourceManager;\r
-\r
- private RootFilterArea filterArea;\r
- private IContributionItem filterAreaContribution;\r
- private IActionBars actionBars;\r
-\r
- public PropertyTable(IWorkbenchSite site, Composite parent, int style) {\r
- super(parent, style);\r
- this.site = site;\r
- //System.out.println("created " + this + " " + System.identityHashCode(this));\r
- resourceManager = new LocalResourceManager(JFaceResources.getResources(parent.getDisplay()));\r
-\r
- addListener(SWT.Dispose, new Listener() {\r
- @Override\r
- public void handleEvent(Event event) {\r
- //System.out.println("DISPOSING " + this + " " + System.identityHashCode(PropertyTable.this));\r
- tableDisposed();\r
-\r
- PropertyTableUtil.setupDragSource(dragSource, null);\r
-\r
- PropertyTable.this.site = null;\r
- explorer = null;\r
- dragSource = null;\r
- sessionContext = null;\r
- currentInput = null;\r
-\r
- userSelectedComparableFactoryQueryProcessor = null;\r
- userSelectedViewpointFactoryQueryProcessor = null;\r
- filterSelectionRequestQueryProcessor = null;\r
-\r
- resourceManager.dispose();\r
- }\r
- });\r
- }\r
-\r
- /**\r
- * Override to perform actions when this property table is disposed.\r
- * Remember to call super.\r
- */\r
- protected void tableDisposed() {\r
- getDisplay().asyncExec(new Runnable() {\r
- @Override\r
- public void run() {\r
- if (filterAreaContribution != null) {\r
- actionBars.getToolBarManager().remove(filterAreaContribution);\r
- filterAreaContribution.dispose();\r
- filterAreaContribution = null;\r
- actionBars.updateActionBars();\r
- }\r
- }\r
- });\r
- }\r
-\r
- @Override\r
- public Control getControl() {\r
- return this;\r
- }\r
-\r
- @Override\r
- public void createControl(final Composite parent, ISessionContext context) {\r
- explorer = createExplorerControl(parent);\r
- dragSource = createDragSource(explorer);\r
-\r
- Control control = explorer.getControl();\r
-\r
- userSelectedComparableFactoryQueryProcessor = new UserSelectedComparableFactoryQueryProcessor();\r
- userSelectedViewpointFactoryQueryProcessor = new UserSelectedViewpointFactoryQueryProcessor();\r
- filterSelectionRequestQueryProcessor = new FilterSelectionRequestQueryProcessor();\r
-\r
- parent.setLayout(LayoutUtils.createNoBorderGridLayout(2, false));\r
- GridDataFactory.fillDefaults().grab(true, true).span(2,1).applyTo(control);\r
-\r
- addControls(parent);\r
- setSessionContext(context);\r
- }\r
-\r
- protected void addControls(Composite parent) {\r
- //createSelectors(parent);\r
- //createFilterArea(parent);\r
- }\r
-\r
- protected void createSelectors(Composite parent) {\r
- Control control = explorer.getControl();\r
- ComparatorSelector comparatorSelector = new ComparatorSelector(explorer, userSelectedComparableFactoryQueryProcessor, parent, SWT.READ_ONLY);\r
- comparatorSelector.moveAbove(control);\r
- ViewpointSelector viewpointSelector = new ViewpointSelector(explorer, userSelectedViewpointFactoryQueryProcessor, parent, SWT.READ_ONLY);\r
- viewpointSelector.moveAbove(control);\r
- }\r
-\r
- protected void createFilterArea(IActionBars actionBars) {\r
- this.filterAreaContribution = new ContributionItem("filterArea") {\r
- ToolItem item;\r
- @Override\r
- public boolean isDynamic() {\r
- return true;\r
- }\r
- @Override\r
- public final void fill(ToolBar parent, int index) {\r
- Control control = createControl(parent);\r
- item = new ToolItem(parent, SWT.SEPARATOR, index);\r
- item.setControl(control);\r
- item.setWidth(computeWidth(control));\r
- }\r
- protected Control createControl(Composite parent) {\r
- filterArea = new RootFilterArea(explorer, filterSelectionRequestQueryProcessor, parent, SWT.NONE);\r
- return filterArea;\r
- }\r
- protected int computeWidth(Control control) {\r
- // FIXME: better size control?\r
- return control.computeSize(150, SWT.DEFAULT, true).x;\r
- }\r
- @Override\r
- public void dispose() {\r
- if (filterArea != null) {\r
- filterArea.dispose();\r
- filterArea = null;\r
- }\r
- if (item != null) {\r
- item.dispose();\r
- item = null;\r
- }\r
- }\r
- };\r
- this.actionBars = actionBars;\r
- actionBars.getToolBarManager().add(filterAreaContribution);\r
- }\r
-\r
- protected void createFilterArea(Composite parent) {\r
- Control control = explorer.getControl();\r
- filterArea = new RootFilterArea(explorer, filterSelectionRequestQueryProcessor, parent, SWT.NONE);\r
- filterArea.moveAbove(control);\r
- }\r
-\r
- protected GraphExplorer createExplorerControl(Composite parent) {\r
- GraphExplorer e = GraphExplorerFactory.getInstance().create(parent, SWT.FULL_SELECTION);\r
- Control c = e.getControl();\r
- ISelectionProvider selectionProvider = (ISelectionProvider)e.getAdapter(ISelectionProvider.class);\r
- new ContextMenuInitializer("#PropertiesPopup").createContextMenu(c, selectionProvider, site);\r
- if (c instanceof Tree)\r
- ((Tree) c).setLinesVisible(true);\r
- e.setColumns(PropertyTableConstants.NORMAL_COLUMNS);\r
- return e;\r
- }\r
-\r
- /**\r
- * Override to customize drag source initialization. This default\r
- * implementation creates a {@link BasicDragSource}. The drag source is\r
- * initialized when the active database session is set.\r
- * \r
- * @param explorer\r
- * @return the object representing the drag source. If the object implements\r
- * {@link SessionContainer}, its\r
- * {@link SessionContainer#setSession(Session)} will be invoked\r
- * every time the active database session changes.\r
- */\r
- protected Object createDragSource(GraphExplorer explorer) {\r
- Control c = explorer.getControl();\r
- DragSource existingSource = (DragSource) c.getData(DND.DRAG_SOURCE_KEY);\r
- if (existingSource != null)\r
- return existingSource;\r
- return PropertyTableUtil.createResourceDragSource(explorer);\r
- }\r
-\r
- protected void setupDragSource(ISessionContext sessionContext) {\r
- PropertyTableUtil.setupDragSource(dragSource, sessionContext);\r
- }\r
-\r
- @Override\r
- public ISelectionProvider getSelectionProvider() {\r
- GraphExplorer e = explorer;\r
- if (e != null) {\r
- IPostSelectionProvider selectionProvider = (IPostSelectionProvider)e.getAdapter(IPostSelectionProvider.class);\r
- return selectionProvider;\r
- }\r
- return null;\r
- }\r
-\r
- @Override\r
- public void requestFocus() {\r
- GraphExplorer e = explorer;\r
- if (e != null)\r
- e.setFocus();\r
- }\r
-\r
- @Override\r
- public IFilterArea getFilterArea() {\r
- return filterArea;\r
- }\r
-\r
- protected ISessionContext getSessionContext() {\r
- return sessionContext;\r
- }\r
-\r
- protected Session getSession() {\r
- return sessionContext != null ? sessionContext.getSession() : null;\r
- }\r
-\r
- @Override\r
- public void setInput(ISessionContext context, ISelection selection, boolean force) {\r
- if (isDisposed())\r
- return;\r
- if (sessionContext == null)\r
- return;\r
-\r
- // Check if this is a duplicate of the previous selection to reduce unnecessary flicker.\r
- if (!force && ObjectUtils.objectEquals(currentInput, selection))\r
- return;\r
-\r
- //Resource[] rs = ResourceAdaptionUtils.toResources(selection);\r
- Object[] rs = toObjects(selection);\r
-// System.out.println("resources: " + Arrays.toString(rs));\r
-\r
- if (rs.length == 0) {\r
- explorer.setRoot(GraphExplorer.EMPTY_INPUT);\r
- } else {\r
- if (rs.length == 1) {\r
- explorer.setRoot(rs[0]);\r
- } else {\r
- explorer.setRoot(rs);\r
- }\r
- }\r
-\r
- currentInput = selection;\r
- }\r
-\r
- protected Object[] toObjects(ISelection s) {\r
- ResourceArray[] ras = ResourceAdaptionUtils.toResourceArrays(s);\r
- if (ras.length > 0)\r
- return ras;\r
- Resource[] rs = ResourceAdaptionUtils.toResources(s);\r
- if (rs.length > 0)\r
- return rs;\r
-\r
- if (!(s instanceof IStructuredSelection))\r
- return new Object[0];\r
- IStructuredSelection ss = (IStructuredSelection) s;\r
- List<Object> result = new ArrayList<Object>();\r
- for (Iterator<?> iterator = ss.iterator(); iterator.hasNext();)\r
- result.add(iterator.next());\r
- return result.toArray();\r
- }\r
-\r
- public void setSessionContext(ISessionContext sessionContext) {\r
- if (isDisposed())\r
- return;\r
-\r
-// System.out.println("setSessionContext: " + sessionContext + " (" + this + ")");\r
- if (this.sessionContext == sessionContext)\r
- return;\r
- this.sessionContext = sessionContext;\r
- Session session = sessionContext != null ? sessionContext.getSession() : null;\r
- reinitializeExplorer(explorer, sessionContext, session);\r
- }\r
-\r
- protected void reinitializeExplorer(GraphExplorer explorer, ISessionContext sessionContext, Session session) {\r
- if (isDisposed())\r
- return;\r
-\r
- setupDragSource(sessionContext);\r
-\r
- // TODO: synchronize the explorer somehow to make sure nothing running in its background is trying to modify the explorer during this process\r
-\r
- EvaluatorData data = new EvaluatorDataImpl();\r
-\r
- if (session != null) {\r
- explorer.setDataSource(new AsyncReadGraphDataSource(session));\r
- explorer.setDataSource(new ReadGraphDataSource(session));\r
-\r
- Evaluator resourceEvaluator = data.newEvaluator()\r
- .addViewpoint(new PropertyViewpointFactory(), 1.0)\r
- .addComparator(new AlphanumericComparatorFactory(ColumnKeys.PROPERTY), 2.0)\r
- .addComparator(new AlphanumericComparatorFactory(ColumnKeys.PROPERTY, true), 1.0)\r
- .addLabeler(new StringRepresentationLabelerFactory(ColumnKeys.PROPERTY), 1.0);\r
-\r
- Evaluator statementEvaluator = data.newEvaluator()\r
- .addViewpoint(new PropertyViewpointFactory(), 1.0)\r
- .addComparator(new AlphanumericComparatorFactory(ColumnKeys.PROPERTY), 2.0)\r
- .addComparator(new AlphanumericComparatorFactory(ColumnKeys.PROPERTY, true), 1.0);\r
-\r
- Evaluator relationContextEvaluator = data.newEvaluator()\r
- .addLabeler(new RelationContextLabelerFactory(), 1.0);\r
-\r
- Evaluator propertyEvaluator = data.newEvaluator()\r
- .addViewpoint(new ArrayPropertyValueViewpointFactory(), 1.0)\r
- .addLabeler(new ArrayPropertyLabelerFactory(), 1.0)\r
- .addImager(new ArrayPropertyImagerFactory(), 1.0);\r
-\r
- data.addEvaluator(Resource.class, resourceEvaluator);\r
- data.addEvaluator(ResourceArray.class, resourceEvaluator);\r
- data.addEvaluator(Statement.class, statementEvaluator);\r
- data.addEvaluator(RelationContext.class, relationContextEvaluator);\r
- data.addEvaluator(ResourceProperty.class, propertyEvaluator);\r
-\r
- } else {\r
- explorer.removeDataSource(AsyncReadGraph.class);\r
- explorer.removeDataSource(ReadGraph.class);\r
- }\r
-\r
- explorer.setProcessor(new ComparableFactoryResolver(data));\r
- explorer.setProcessor(new ViewpointFactoryResolver(data));\r
- explorer.setProcessor(new LabelerFactoryResolver(data));\r
- explorer.setProcessor(new ImagerFactoryResolver(data));\r
- explorer.setProcessor(new LabelDecoratorFactoryResolver(data));\r
- explorer.setProcessor(new ImageDecoratorFactoryResolver(data));\r
- explorer.setPrimitiveProcessor(new TypesQueryProcessor());\r
-\r
- explorer.setPrimitiveProcessor(userSelectedViewpointFactoryQueryProcessor);\r
- explorer.setProcessor(new ComparableSelectorQueryProcessor());\r
- explorer.setPrimitiveProcessor(userSelectedComparableFactoryQueryProcessor);\r
- explorer.setPrimitiveProcessor(filterSelectionRequestQueryProcessor);\r
-\r
- // Make sure the explorer gets reset.\r
- explorer.setRoot(GraphExplorer.EMPTY_INPUT);\r
- }\r
-\r
- @Override\r
- public void updatePartName(Consumer<String> updateCallback) {\r
- ISelection forSelection = currentInput;\r
- if (forSelection instanceof IStructuredSelection) {\r
- IStructuredSelection s = (IStructuredSelection) forSelection;\r
- int size = s.size();\r
- if (size == 1) {\r
- Object obj = s.getFirstElement();\r
- if (obj != null)\r
- updateCallback.accept(obj.toString());\r
- else\r
- updateCallback.accept("Properties");\r
- } else {\r
- updateCallback.accept("Properties [" + size + "]");\r
- }\r
- } else {\r
- updateCallback.accept("Properties");\r
- }\r
- }\r
-\r
-}\r
+/*******************************************************************************
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.selectionview;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import java.util.function.Consumer;
+
+import org.eclipse.jface.action.ContributionItem;
+import org.eclipse.jface.action.IContributionItem;
+import org.eclipse.jface.layout.GridDataFactory;
+import org.eclipse.jface.resource.JFaceResources;
+import org.eclipse.jface.resource.LocalResourceManager;
+import org.eclipse.jface.viewers.IPostSelectionProvider;
+import org.eclipse.jface.viewers.ISelection;
+import org.eclipse.jface.viewers.ISelectionProvider;
+import org.eclipse.jface.viewers.IStructuredSelection;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.dnd.DND;
+import org.eclipse.swt.dnd.DragSource;
+import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.Control;
+import org.eclipse.swt.widgets.Event;
+import org.eclipse.swt.widgets.Listener;
+import org.eclipse.swt.widgets.ToolBar;
+import org.eclipse.swt.widgets.ToolItem;
+import org.eclipse.swt.widgets.Tree;
+import org.eclipse.ui.IActionBars;
+import org.eclipse.ui.IWorkbenchSite;
+import org.simantics.browsing.ui.GraphExplorer;
+import org.simantics.browsing.ui.common.ColumnKeys;
+import org.simantics.browsing.ui.common.EvaluatorData;
+import org.simantics.browsing.ui.common.EvaluatorData.Evaluator;
+import org.simantics.browsing.ui.common.EvaluatorDataImpl;
+import org.simantics.browsing.ui.common.comparators.AlphanumericComparatorFactory;
+import org.simantics.browsing.ui.common.processors.ComparableFactoryResolver;
+import org.simantics.browsing.ui.common.processors.ComparableSelectorQueryProcessor;
+import org.simantics.browsing.ui.common.processors.FilterSelectionRequestQueryProcessor;
+import org.simantics.browsing.ui.common.processors.ImageDecoratorFactoryResolver;
+import org.simantics.browsing.ui.common.processors.ImagerFactoryResolver;
+import org.simantics.browsing.ui.common.processors.LabelDecoratorFactoryResolver;
+import org.simantics.browsing.ui.common.processors.LabelerFactoryResolver;
+import org.simantics.browsing.ui.common.processors.UserSelectedComparableFactoryQueryProcessor;
+import org.simantics.browsing.ui.common.processors.UserSelectedViewpointFactoryQueryProcessor;
+import org.simantics.browsing.ui.common.processors.ViewpointFactoryResolver;
+import org.simantics.browsing.ui.common.views.IFilterArea;
+import org.simantics.browsing.ui.common.views.IFilterAreaProvider;
+import org.simantics.browsing.ui.common.views.PropertyTableConstants;
+import org.simantics.browsing.ui.graph.impl.ArrayPropertyLabelerFactory;
+import org.simantics.browsing.ui.graph.impl.ArrayPropertyValueViewpointFactory;
+import org.simantics.browsing.ui.graph.impl.AsyncReadGraphDataSource;
+import org.simantics.browsing.ui.graph.impl.PropertyViewpointFactory;
+import org.simantics.browsing.ui.graph.impl.ReadGraphDataSource;
+import org.simantics.browsing.ui.graph.impl.RelationContextLabelerFactory;
+import org.simantics.browsing.ui.graph.impl.ResourceProperty;
+import org.simantics.browsing.ui.graph.impl.StringRepresentationLabelerFactory;
+import org.simantics.browsing.ui.swt.ArrayPropertyImagerFactory;
+import org.simantics.browsing.ui.swt.ComparatorSelector;
+import org.simantics.browsing.ui.swt.ContextMenuInitializer;
+import org.simantics.browsing.ui.swt.GraphExplorerFactory;
+import org.simantics.browsing.ui.swt.PropertyTableUtil;
+import org.simantics.browsing.ui.swt.RootFilterArea;
+import org.simantics.browsing.ui.swt.TypesQueryProcessor;
+import org.simantics.browsing.ui.swt.ViewpointSelector;
+import org.simantics.db.AsyncReadGraph;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.RelationContext;
+import org.simantics.db.Resource;
+import org.simantics.db.Session;
+import org.simantics.db.Statement;
+import org.simantics.db.common.ResourceArray;
+import org.simantics.db.management.ISessionContext;
+import org.simantics.ui.dnd.BasicDragSource;
+import org.simantics.ui.dnd.SessionContainer;
+import org.simantics.ui.utils.ResourceAdaptionUtils;
+import org.simantics.utils.ObjectUtils;
+import org.simantics.utils.ui.LayoutUtils;
+
+
+/**
+ * <p>
+ * Subclasses may extend or reimplement the following methods as required:
+ * <ul>
+ * <li><code>createExplorerControl</code> - reimplement to customize the way the IGraphExplorer control is created</li>
+ * <li><code>createControl</code> - reimplement to customize the way other supplementary controls get created</li>
+ * <li><code>createDragSource</code> - reimplement to customize the kind of DragSource this control provides</li>
+ * </ul>
+ * </p>
+ *
+ * @author Tuukka Lehtonen
+ */
+public class PropertyTable extends Composite implements IFilterAreaProvider, IPropertyTab2 {
+
+ protected IWorkbenchSite site;
+ protected GraphExplorer explorer;
+ protected Object dragSource;
+ protected ISessionContext sessionContext;
+ protected ISelection currentInput;
+
+ protected UserSelectedComparableFactoryQueryProcessor userSelectedComparableFactoryQueryProcessor;
+ protected UserSelectedViewpointFactoryQueryProcessor userSelectedViewpointFactoryQueryProcessor;
+ protected FilterSelectionRequestQueryProcessor filterSelectionRequestQueryProcessor;
+
+ protected LocalResourceManager resourceManager;
+
+ private RootFilterArea filterArea;
+ private IContributionItem filterAreaContribution;
+ private IActionBars actionBars;
+
+ public PropertyTable(IWorkbenchSite site, Composite parent, int style) {
+ super(parent, style);
+ this.site = site;
+ //System.out.println("created " + this + " " + System.identityHashCode(this));
+ resourceManager = new LocalResourceManager(JFaceResources.getResources(parent.getDisplay()));
+
+ addListener(SWT.Dispose, new Listener() {
+ @Override
+ public void handleEvent(Event event) {
+ //System.out.println("DISPOSING " + this + " " + System.identityHashCode(PropertyTable.this));
+ tableDisposed();
+
+ PropertyTableUtil.setupDragSource(dragSource, null);
+
+ PropertyTable.this.site = null;
+ explorer = null;
+ dragSource = null;
+ sessionContext = null;
+ currentInput = null;
+
+ userSelectedComparableFactoryQueryProcessor = null;
+ userSelectedViewpointFactoryQueryProcessor = null;
+ filterSelectionRequestQueryProcessor = null;
+
+ resourceManager.dispose();
+ }
+ });
+ }
+
+ /**
+ * Override to perform actions when this property table is disposed.
+ * Remember to call super.
+ */
+ protected void tableDisposed() {
+ getDisplay().asyncExec(new Runnable() {
+ @Override
+ public void run() {
+ if (filterAreaContribution != null) {
+ actionBars.getToolBarManager().remove(filterAreaContribution);
+ filterAreaContribution.dispose();
+ filterAreaContribution = null;
+ actionBars.updateActionBars();
+ }
+ }
+ });
+ }
+
+ @Override
+ public Control getControl() {
+ return this;
+ }
+
+ @Override
+ public void createControl(final Composite parent, ISessionContext context) {
+ explorer = createExplorerControl(parent);
+ dragSource = createDragSource(explorer);
+
+ Control control = explorer.getControl();
+
+ userSelectedComparableFactoryQueryProcessor = new UserSelectedComparableFactoryQueryProcessor();
+ userSelectedViewpointFactoryQueryProcessor = new UserSelectedViewpointFactoryQueryProcessor();
+ filterSelectionRequestQueryProcessor = new FilterSelectionRequestQueryProcessor();
+
+ parent.setLayout(LayoutUtils.createNoBorderGridLayout(2, false));
+ GridDataFactory.fillDefaults().grab(true, true).span(2,1).applyTo(control);
+
+ addControls(parent);
+ setSessionContext(context);
+ }
+
+ protected void addControls(Composite parent) {
+ //createSelectors(parent);
+ //createFilterArea(parent);
+ }
+
+ protected void createSelectors(Composite parent) {
+ Control control = explorer.getControl();
+ ComparatorSelector comparatorSelector = new ComparatorSelector(explorer, userSelectedComparableFactoryQueryProcessor, parent, SWT.READ_ONLY);
+ comparatorSelector.moveAbove(control);
+ ViewpointSelector viewpointSelector = new ViewpointSelector(explorer, userSelectedViewpointFactoryQueryProcessor, parent, SWT.READ_ONLY);
+ viewpointSelector.moveAbove(control);
+ }
+
+ protected void createFilterArea(IActionBars actionBars) {
+ this.filterAreaContribution = new ContributionItem("filterArea") {
+ ToolItem item;
+ @Override
+ public boolean isDynamic() {
+ return true;
+ }
+ @Override
+ public final void fill(ToolBar parent, int index) {
+ Control control = createControl(parent);
+ item = new ToolItem(parent, SWT.SEPARATOR, index);
+ item.setControl(control);
+ item.setWidth(computeWidth(control));
+ }
+ protected Control createControl(Composite parent) {
+ filterArea = new RootFilterArea(explorer, filterSelectionRequestQueryProcessor, parent, SWT.NONE);
+ return filterArea;
+ }
+ protected int computeWidth(Control control) {
+ // FIXME: better size control?
+ return control.computeSize(150, SWT.DEFAULT, true).x;
+ }
+ @Override
+ public void dispose() {
+ if (filterArea != null) {
+ filterArea.dispose();
+ filterArea = null;
+ }
+ if (item != null) {
+ item.dispose();
+ item = null;
+ }
+ }
+ };
+ this.actionBars = actionBars;
+ actionBars.getToolBarManager().add(filterAreaContribution);
+ }
+
+ protected void createFilterArea(Composite parent) {
+ Control control = explorer.getControl();
+ filterArea = new RootFilterArea(explorer, filterSelectionRequestQueryProcessor, parent, SWT.NONE);
+ filterArea.moveAbove(control);
+ }
+
+ protected GraphExplorer createExplorerControl(Composite parent) {
+ GraphExplorer e = GraphExplorerFactory.getInstance().create(parent, SWT.FULL_SELECTION);
+ Control c = e.getControl();
+ ISelectionProvider selectionProvider = (ISelectionProvider)e.getAdapter(ISelectionProvider.class);
+ new ContextMenuInitializer("#PropertiesPopup").createContextMenu(c, selectionProvider, site);
+ if (c instanceof Tree)
+ ((Tree) c).setLinesVisible(true);
+ e.setColumns(PropertyTableConstants.NORMAL_COLUMNS);
+ return e;
+ }
+
+ /**
+ * Override to customize drag source initialization. This default
+ * implementation creates a {@link BasicDragSource}. The drag source is
+ * initialized when the active database session is set.
+ *
+ * @param explorer
+ * @return the object representing the drag source. If the object implements
+ * {@link SessionContainer}, its
+ * {@link SessionContainer#setSession(Session)} will be invoked
+ * every time the active database session changes.
+ */
+ protected Object createDragSource(GraphExplorer explorer) {
+ Control c = explorer.getControl();
+ DragSource existingSource = (DragSource) c.getData(DND.DRAG_SOURCE_KEY);
+ if (existingSource != null)
+ return existingSource;
+ return PropertyTableUtil.createResourceDragSource(explorer);
+ }
+
+ protected void setupDragSource(ISessionContext sessionContext) {
+ PropertyTableUtil.setupDragSource(dragSource, sessionContext);
+ }
+
+ @Override
+ public ISelectionProvider getSelectionProvider() {
+ GraphExplorer e = explorer;
+ if (e != null) {
+ IPostSelectionProvider selectionProvider = (IPostSelectionProvider)e.getAdapter(IPostSelectionProvider.class);
+ return selectionProvider;
+ }
+ return null;
+ }
+
+ @Override
+ public void requestFocus() {
+ GraphExplorer e = explorer;
+ if (e != null)
+ e.setFocus();
+ }
+
+ @Override
+ public IFilterArea getFilterArea() {
+ return filterArea;
+ }
+
+ protected ISessionContext getSessionContext() {
+ return sessionContext;
+ }
+
+ protected Session getSession() {
+ return sessionContext != null ? sessionContext.getSession() : null;
+ }
+
+ @Override
+ public void setInput(ISessionContext context, ISelection selection, boolean force) {
+ if (isDisposed())
+ return;
+ if (sessionContext == null)
+ return;
+
+ // Check if this is a duplicate of the previous selection to reduce unnecessary flicker.
+ if (!force && ObjectUtils.objectEquals(currentInput, selection))
+ return;
+
+ //Resource[] rs = ResourceAdaptionUtils.toResources(selection);
+ Object[] rs = toObjects(selection);
+// System.out.println("resources: " + Arrays.toString(rs));
+
+ if (rs.length == 0) {
+ explorer.setRoot(GraphExplorer.EMPTY_INPUT);
+ } else {
+ if (rs.length == 1) {
+ explorer.setRoot(rs[0]);
+ } else {
+ explorer.setRoot(rs);
+ }
+ }
+
+ currentInput = selection;
+ }
+
+ protected Object[] toObjects(ISelection s) {
+ ResourceArray[] ras = ResourceAdaptionUtils.toResourceArrays(s);
+ if (ras.length > 0)
+ return ras;
+ Resource[] rs = ResourceAdaptionUtils.toResources(s);
+ if (rs.length > 0)
+ return rs;
+
+ if (!(s instanceof IStructuredSelection))
+ return new Object[0];
+ IStructuredSelection ss = (IStructuredSelection) s;
+ List<Object> result = new ArrayList<Object>();
+ for (Iterator<?> iterator = ss.iterator(); iterator.hasNext();)
+ result.add(iterator.next());
+ return result.toArray();
+ }
+
+ public void setSessionContext(ISessionContext sessionContext) {
+ if (isDisposed())
+ return;
+
+// System.out.println("setSessionContext: " + sessionContext + " (" + this + ")");
+ if (this.sessionContext == sessionContext)
+ return;
+ this.sessionContext = sessionContext;
+ Session session = sessionContext != null ? sessionContext.getSession() : null;
+ reinitializeExplorer(explorer, sessionContext, session);
+ }
+
+ protected void reinitializeExplorer(GraphExplorer explorer, ISessionContext sessionContext, Session session) {
+ if (isDisposed())
+ return;
+
+ setupDragSource(sessionContext);
+
+ // TODO: synchronize the explorer somehow to make sure nothing running in its background is trying to modify the explorer during this process
+
+ EvaluatorData data = new EvaluatorDataImpl();
+
+ if (session != null) {
+ explorer.setDataSource(new AsyncReadGraphDataSource(session));
+ explorer.setDataSource(new ReadGraphDataSource(session));
+
+ Evaluator resourceEvaluator = data.newEvaluator()
+ .addViewpoint(new PropertyViewpointFactory(), 1.0)
+ .addComparator(new AlphanumericComparatorFactory(ColumnKeys.PROPERTY), 2.0)
+ .addComparator(new AlphanumericComparatorFactory(ColumnKeys.PROPERTY, true), 1.0)
+ .addLabeler(new StringRepresentationLabelerFactory(ColumnKeys.PROPERTY), 1.0);
+
+ Evaluator statementEvaluator = data.newEvaluator()
+ .addViewpoint(new PropertyViewpointFactory(), 1.0)
+ .addComparator(new AlphanumericComparatorFactory(ColumnKeys.PROPERTY), 2.0)
+ .addComparator(new AlphanumericComparatorFactory(ColumnKeys.PROPERTY, true), 1.0);
+
+ Evaluator relationContextEvaluator = data.newEvaluator()
+ .addLabeler(new RelationContextLabelerFactory(), 1.0);
+
+ Evaluator propertyEvaluator = data.newEvaluator()
+ .addViewpoint(new ArrayPropertyValueViewpointFactory(), 1.0)
+ .addLabeler(new ArrayPropertyLabelerFactory(), 1.0)
+ .addImager(new ArrayPropertyImagerFactory(), 1.0);
+
+ data.addEvaluator(Resource.class, resourceEvaluator);
+ data.addEvaluator(ResourceArray.class, resourceEvaluator);
+ data.addEvaluator(Statement.class, statementEvaluator);
+ data.addEvaluator(RelationContext.class, relationContextEvaluator);
+ data.addEvaluator(ResourceProperty.class, propertyEvaluator);
+
+ } else {
+ explorer.removeDataSource(AsyncReadGraph.class);
+ explorer.removeDataSource(ReadGraph.class);
+ }
+
+ explorer.setProcessor(new ComparableFactoryResolver(data));
+ explorer.setProcessor(new ViewpointFactoryResolver(data));
+ explorer.setProcessor(new LabelerFactoryResolver(data));
+ explorer.setProcessor(new ImagerFactoryResolver(data));
+ explorer.setProcessor(new LabelDecoratorFactoryResolver(data));
+ explorer.setProcessor(new ImageDecoratorFactoryResolver(data));
+ explorer.setPrimitiveProcessor(new TypesQueryProcessor());
+
+ explorer.setPrimitiveProcessor(userSelectedViewpointFactoryQueryProcessor);
+ explorer.setProcessor(new ComparableSelectorQueryProcessor());
+ explorer.setPrimitiveProcessor(userSelectedComparableFactoryQueryProcessor);
+ explorer.setPrimitiveProcessor(filterSelectionRequestQueryProcessor);
+
+ // Make sure the explorer gets reset.
+ explorer.setRoot(GraphExplorer.EMPTY_INPUT);
+ }
+
+ @Override
+ public void updatePartName(Consumer<String> updateCallback) {
+ ISelection forSelection = currentInput;
+ if (forSelection instanceof IStructuredSelection) {
+ IStructuredSelection s = (IStructuredSelection) forSelection;
+ int size = s.size();
+ if (size == 1) {
+ Object obj = s.getFirstElement();
+ if (obj != null)
+ updateCallback.accept(obj.toString());
+ else
+ updateCallback.accept("Properties");
+ } else {
+ updateCallback.accept("Properties [" + size + "]");
+ }
+ } else {
+ updateCallback.accept("Properties");
+ }
+ }
+
+}