]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagramEditor/DiagramOutlinePage.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / diagramEditor / DiagramOutlinePage.java
diff --git a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagramEditor/DiagramOutlinePage.java b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagramEditor/DiagramOutlinePage.java
new file mode 100644 (file)
index 0000000..e745ed1
--- /dev/null
@@ -0,0 +1,230 @@
+/*******************************************************************************\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.modeling.ui.diagramEditor;\r
+\r
+import org.eclipse.jface.layout.GridDataFactory;\r
+import org.eclipse.jface.layout.GridLayoutFactory;\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.ISelectionChangedListener;\r
+import org.eclipse.jface.viewers.ISelectionProvider;\r
+import org.eclipse.swt.SWT;\r
+import org.eclipse.swt.widgets.Composite;\r
+import org.eclipse.swt.widgets.Control;\r
+import org.eclipse.ui.part.Page;\r
+import org.eclipse.ui.views.contentoutline.IContentOutlinePage;\r
+import org.simantics.browsing.ui.GraphExplorer;\r
+import org.simantics.browsing.ui.common.ColumnKeys;\r
+import org.simantics.browsing.ui.common.ErrorLogger;\r
+import org.simantics.browsing.ui.common.EvaluatorData;\r
+import org.simantics.browsing.ui.common.EvaluatorDataImpl;\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.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.graph.impl.AsyncReadGraphDataSource;\r
+import org.simantics.browsing.ui.graph.impl.InheritsQueryProcessor;\r
+import org.simantics.browsing.ui.graph.impl.ReadGraphDataSource;\r
+import org.simantics.browsing.ui.graph.impl.RelatedObjectsQueryProcessor;\r
+import org.simantics.browsing.ui.swt.ContextMenuInitializer;\r
+import org.simantics.browsing.ui.swt.DefaultKeyListener;\r
+import org.simantics.browsing.ui.swt.DefaultMouseListener;\r
+import org.simantics.browsing.ui.swt.DefaultSelectionDataResolver;\r
+import org.simantics.browsing.ui.swt.GraphExplorerFactory;\r
+import org.simantics.browsing.ui.swt.TypesQueryProcessor;\r
+import org.simantics.db.AsyncReadGraph;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.Session;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.management.ISessionContext;\r
+import org.simantics.db.management.ISessionContextProvider;\r
+import org.simantics.db.request.Read;\r
+import org.simantics.modeling.ModelingResources;\r
+import org.simantics.modeling.ui.Activator;\r
+import org.simantics.modeling.ui.modelBrowser.ModelEvaluators;\r
+import org.simantics.modeling.ui.modelBrowser.model.INode;\r
+import org.simantics.modeling.ui.modelBrowser.model.INode2;\r
+import org.simantics.structural.ui.menuContributions.LinkWithEditorContribution;\r
+import org.simantics.ui.workbench.IResourceEditorInput2;\r
+import org.simantics.utils.datastructures.Function;\r
+import org.simantics.utils.datastructures.Functions;\r
+\r
+/**\r
+ * TODO: change to use newer contribution mechanisms, get rid of\r
+ * {@link ModelEvaluators} use.\r
+ * \r
+ * @author Tuukka Lehtonen\r
+ */\r
+public class DiagramOutlinePage extends Page implements IContentOutlinePage, IPostSelectionProvider {\r
+\r
+    ISessionContextProvider sessionContextProvider;\r
+    IResourceEditorInput2    input;\r
+    LocalResourceManager    resourceManager;\r
+    Composite               composite;\r
+    GraphExplorer           explorer;\r
+    LinkWithEditor          linkWithEditor;\r
+\r
+    public DiagramOutlinePage(ISessionContextProvider contextProvider, IResourceEditorInput2 input) {\r
+        assert contextProvider != null;\r
+        assert input != null;\r
+\r
+        this.sessionContextProvider = contextProvider;\r
+        this.input = input;\r
+    }\r
+\r
+    @Override\r
+    public void createControl(Composite parent) {\r
+        resourceManager = new LocalResourceManager(JFaceResources.getResources());\r
+        composite = new Composite(parent, SWT.NONE);\r
+        GridLayoutFactory.fillDefaults().applyTo(composite);\r
+        this.explorer = GraphExplorerFactory.getInstance().selectionDataResolver(new DefaultSelectionDataResolver()).create(composite);\r
+               Control control = explorer.getControl();\r
+        ISelectionProvider selectionProvider = (ISelectionProvider)explorer.getAdapter(ISelectionProvider.class);\r
+        new ContextMenuInitializer("#GraphExplorerPopup").createContextMenu(control, selectionProvider, getSite());\r
+        GridDataFactory.fillDefaults().grab(true, true).applyTo(control);\r
+\r
+        // Consider ENTER presses to simulate mouse left button double clicks\r
+        Function<String[]> resolver = Functions.constant(ColumnKeys.SINGLE);\r
+        explorer.addListener(new DefaultKeyListener(sessionContextProvider, explorer, resolver));\r
+\r
+        // Default double click handling\r
+        explorer.addListener(new DefaultMouseListener(explorer));\r
+\r
+        ISessionContext sessionContext = sessionContextProvider.getSessionContext();\r
+        Session session = sessionContext != null ? sessionContext.getSession() : null;\r
+        EvaluatorData data = null;\r
+        if (session != null) {\r
+            data = createEvaluatorData(sessionContext);\r
+            explorer.setDataSource(new AsyncReadGraphDataSource(session));\r
+            explorer.setDataSource(new ReadGraphDataSource(session));\r
+        } else {\r
+            data = new EvaluatorDataImpl();\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
+        explorer.setPrimitiveProcessor(new InheritsQueryProcessor());\r
+        explorer.setPrimitiveProcessor(new RelatedObjectsQueryProcessor());\r
+\r
+        explorer.setProcessor(new ComparableSelectorQueryProcessor());\r
+        explorer.setPrimitiveProcessor(new UserSelectedViewpointFactoryQueryProcessor());\r
+        explorer.setPrimitiveProcessor(new UserSelectedComparableFactoryQueryProcessor());\r
+\r
+        try {\r
+\r
+            // Look for a proper input resource to give to the model browser.\r
+            final Resource diagram = input.getResource();\r
+            if (diagram != null && session != null) {\r
+                Object finalInput = session.syncRequest(new Read<Resource>() {\r
+                    @Override\r
+                    public Resource perform(ReadGraph graph) throws DatabaseException {\r
+                        ModelingResources mr = ModelingResources.getInstance(graph);\r
+                        return graph.getPossibleObject(diagram, mr.DiagramToComposite);\r
+                    }\r
+                });\r
+                if (finalInput != null)\r
+                    explorer.setRoot(finalInput);\r
+            }\r
+\r
+        } catch (DatabaseException e) {\r
+            ErrorLogger.defaultLogError(e);\r
+        }\r
+\r
+//        linkWithEditor = new LinkWithEditor();\r
+//        getSite().getActionBars().getToolBarManager().add(linkWithEditor);\r
+    }\r
+\r
+    @Override\r
+    public void dispose() {\r
+        if (resourceManager != null) {\r
+            resourceManager.dispose();\r
+            resourceManager = null;\r
+        }\r
+        super.dispose();\r
+    }\r
+\r
+    protected EvaluatorData createEvaluatorData(ISessionContext sessionContext) {\r
+        EvaluatorData data = new EvaluatorDataImpl();\r
+        data.addEvaluator(Resource.class, ModelEvaluators.createResourceEvaluator());\r
+        data.addEvaluator(INode.class, ModelEvaluators.createNodeEvaluator());\r
+        data.addEvaluator(INode2.class, ModelEvaluators.createNode2Evaluator());\r
+        return data;\r
+    }\r
+\r
+    @Override\r
+    public Control getControl() {\r
+        return composite;\r
+    }\r
+\r
+    @Override\r
+    public void setFocus() {\r
+        explorer.setFocus();\r
+    }\r
+\r
+    @Override\r
+    public void addSelectionChangedListener(ISelectionChangedListener listener) {\r
+        IPostSelectionProvider selectionProvider = (IPostSelectionProvider)explorer.getAdapter(IPostSelectionProvider.class);\r
+        selectionProvider.addSelectionChangedListener(listener);\r
+    }\r
+\r
+    @Override\r
+    public ISelection getSelection() {\r
+        IPostSelectionProvider selectionProvider = (IPostSelectionProvider)explorer.getAdapter(IPostSelectionProvider.class);\r
+        return selectionProvider.getSelection();\r
+    }\r
+\r
+    @Override\r
+    public void removeSelectionChangedListener(ISelectionChangedListener listener) {\r
+        IPostSelectionProvider selectionProvider = (IPostSelectionProvider)explorer.getAdapter(IPostSelectionProvider.class);\r
+        selectionProvider.removeSelectionChangedListener(listener);\r
+    }\r
+\r
+    @Override\r
+    public void setSelection(ISelection selection) {\r
+        IPostSelectionProvider selectionProvider = (IPostSelectionProvider)explorer.getAdapter(IPostSelectionProvider.class);\r
+        selectionProvider.setSelection(selection);\r
+    }\r
+\r
+    @Override\r
+    public void addPostSelectionChangedListener(ISelectionChangedListener listener) {\r
+        IPostSelectionProvider selectionProvider = (IPostSelectionProvider)explorer.getAdapter(IPostSelectionProvider.class);\r
+        selectionProvider.addPostSelectionChangedListener(listener);\r
+    }\r
+\r
+    @Override\r
+    public void removePostSelectionChangedListener(ISelectionChangedListener listener) {\r
+        IPostSelectionProvider selectionProvider = (IPostSelectionProvider)explorer.getAdapter(IPostSelectionProvider.class);\r
+        selectionProvider.removePostSelectionChangedListener(listener);\r
+    }\r
+\r
+    static class LinkWithEditor extends LinkWithEditorContribution {\r
+        public LinkWithEditor() {\r
+            super("DiagramOutlinePage.linkWithEditor", Activator.getDefault().getPreferenceStore());\r
+        }\r
+    }\r
+\r
+}\r