]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.selectionview/src/org/simantics/selectionview/PropertyTable.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.selectionview / src / org / simantics / selectionview / PropertyTable.java
diff --git a/bundles/org.simantics.selectionview/src/org/simantics/selectionview/PropertyTable.java b/bundles/org.simantics.selectionview/src/org/simantics/selectionview/PropertyTable.java
new file mode 100644 (file)
index 0000000..4bb45be
--- /dev/null
@@ -0,0 +1,449 @@
+/*******************************************************************************\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