]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/ModelledGraphExplorerComposite.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.browsing.ui.swt / src / org / simantics / browsing / ui / swt / ModelledGraphExplorerComposite.java
diff --git a/bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/ModelledGraphExplorerComposite.java b/bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/ModelledGraphExplorerComposite.java
new file mode 100644 (file)
index 0000000..27bb074
--- /dev/null
@@ -0,0 +1,183 @@
+/*******************************************************************************\r
+ * Copyright (c) 2012 Association for Decentralized Information Management in\r
+ * 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.browsing.ui.swt;\r
+\r
+import java.util.Collections;\r
+import java.util.HashMap;\r
+import java.util.Map;\r
+\r
+import org.eclipse.jface.action.IMenuManager;\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.SelectionChangedEvent;\r
+import org.eclipse.swt.SWT;\r
+import org.eclipse.swt.layout.GridData;\r
+import org.eclipse.swt.widgets.Composite;\r
+import org.eclipse.swt.widgets.Listener;\r
+import org.eclipse.ui.IWorkbenchSite;\r
+import org.simantics.Simantics;\r
+import org.simantics.browsing.ui.GraphExplorer;\r
+import org.simantics.browsing.ui.swt.stubs.BrowsingResource;\r
+import org.simantics.browsing.ui.swt.widgets.GraphExplorerComposite;\r
+import org.simantics.browsing.ui.swt.widgets.ModelBrowser;\r
+import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport;\r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.management.ISessionContext;\r
+import org.simantics.db.request.Read;\r
+\r
+public class ModelledGraphExplorerComposite implements ModelledControl {\r
+\r
+       final private Resource configuration;\r
+       \r
+       public ModelledGraphExplorerComposite(Resource configuration) {\r
+               this.configuration = configuration;\r
+       }\r
+       \r
+       @Override\r
+       public Composite create(Composite parent, IWorkbenchSite site, ISessionContext context, final WidgetSupport support) throws DatabaseException {\r
+\r
+               int style = Simantics.getSession().syncRequest(new Read<Integer>() {\r
+\r
+                       @Override\r
+                       public Integer perform(ReadGraph graph) throws DatabaseException {\r
+                               int style = SWT.BORDER | SWT.FULL_SELECTION | SWT.MULTI;\r
+                               BrowsingResource br = BrowsingResource.getInstance(graph);\r
+                               Boolean check = graph.getPossibleRelatedValue(configuration, br.GraphExplorerComposite_Check, Bindings.BOOLEAN);\r
+                               if(check != null && check) style |= SWT.CHECK;\r
+                               return style;\r
+                               \r
+                       }\r
+                       \r
+               });\r
+               \r
+               Map<String, Object> args = Simantics.getSession().syncRequest(new Read<Map<String, Object>>() {\r
+\r
+                       @Override\r
+                       public Map<String, Object> perform(ReadGraph graph) throws DatabaseException {\r
+                               \r
+                       Map<String, Object> args = new HashMap<String, Object>();\r
+                       args.put("displaySelectors", Boolean.FALSE);\r
+                       args.put("displayFilter", Boolean.FALSE);\r
+                       \r
+                               BrowsingResource br = BrowsingResource.getInstance(graph);\r
+                               Boolean displayFilter = graph.getPossibleRelatedValue(configuration, br.GraphExplorerComposite_DisplayFilter, Bindings.BOOLEAN);\r
+                               if(displayFilter != null) args.put("displayFilter", displayFilter);\r
+                       \r
+                       return args;\r
+                       \r
+                       }\r
+                       \r
+               });\r
+\r
+               String browseContextURI = Simantics.getSession().syncRequest(new Read<String>() {\r
+\r
+                       @Override\r
+                       public String perform(ReadGraph graph) throws DatabaseException {\r
+                               BrowsingResource br = BrowsingResource.getInstance(graph);\r
+                               Resource browseContext = graph.getSingleObject(configuration, br.GraphExplorerComposite_BrowseContext);\r
+                               return graph.getURI(browseContext);\r
+                       }\r
+                       \r
+               });\r
+               \r
+        final GraphExplorerComposite explorer = new ModelBrowser(Collections.singleton(browseContextURI), args, site, parent, support, style) {\r
+            @Override\r
+            protected void addListeners(GraphExplorer explorer, IMenuManager menuManager) {\r
+                // No mouse/key listeners in this explorer.\r
+                // Prevents unwanted contributions from interfering\r
+                // with how the control works.\r
+            }\r
+        };\r
+        \r
+        GridData gridData = Simantics.getSession().syncRequest(new Read<GridData>() {\r
+\r
+            @Override\r
+            public GridData perform(ReadGraph graph) throws DatabaseException {\r
+                BrowsingResource br = BrowsingResource.getInstance(graph);\r
+                GridData data = new GridData(SWT.FILL, SWT.FILL, true, true);                \r
+                Integer span = graph.getPossibleRelatedValue(configuration, br.Control_HorizontalSpan, Bindings.INTEGER);\r
+                if(span != null) {\r
+                       data.horizontalSpan = span;\r
+                }\r
+                Integer preferredWidth = graph.getPossibleRelatedValue(configuration, br.Control_PreferredWidth, Bindings.INTEGER);\r
+                data.widthHint = preferredWidth != null ? preferredWidth : SWT.DEFAULT;\r
+                Integer preferredHeight = graph.getPossibleRelatedValue(configuration, br.Control_PreferredHeight, Bindings.INTEGER);\r
+                data.heightHint = preferredHeight != null ? preferredHeight : SWT.DEFAULT;\r
+                return data;\r
+            }\r
+\r
+        });\r
+        \r
+        explorer.setLayoutData(gridData);\r
+        \r
+//        explorer.setBrowseContexts(Collections.singleton(browseContextURI));\r
+        \r
+        explorer.setInputSource(new InputSourceImpl<Object>() {\r
+\r
+                       @Override\r
+                       public Object get(ISessionContext ctx, Object selection) {\r
+                               new Exception(""+selection).printStackTrace();\r
+                               return selection;\r
+                       }\r
+               });\r
+        \r
+               Listener selectionListener = Simantics.getSession().syncRequest(new Read<Listener>() {\r
+\r
+                       @Override\r
+                       public Listener perform(ReadGraph graph) throws DatabaseException {\r
+                               BrowsingResource br = BrowsingResource.getInstance(graph);\r
+                               Resource listener = graph.getPossibleObject(configuration, br.GraphExplorerComposite_SelectionListener);\r
+                               if(listener == null) return null;\r
+                               return graph.adapt(listener, Listener.class);\r
+                       }\r
+                       \r
+               });\r
+               \r
+               if(selectionListener != null) explorer.addListenerToControl(SWT.Selection, selectionListener);\r
+               \r
+               final String selectionParameter = Simantics.getSession().syncRequest(new Read<String>() {\r
+\r
+                       @Override\r
+                       public String perform(ReadGraph graph) throws DatabaseException {\r
+                               BrowsingResource br = BrowsingResource.getInstance(graph);\r
+                               Resource parameter = graph.getPossibleObject(configuration, br.GraphExplorerComposite_SelectionParameter);\r
+                               if(parameter == null) return null;\r
+                               return graph.getPossibleURI(parameter);\r
+                       }\r
+                       \r
+               });\r
+               \r
+               \r
+               if(selectionParameter != null) {\r
+                       IPostSelectionProvider selectionProvider = (IPostSelectionProvider)explorer.getExplorer().getAdapter(IPostSelectionProvider.class);\r
+                       selectionProvider.addPostSelectionChangedListener(new ISelectionChangedListener() {\r
+                               \r
+                               @Override\r
+                               public void selectionChanged(SelectionChangedEvent event) {\r
+                                       ISelection selection = event.getSelection();\r
+                                       support.setParameter(selectionParameter, selection);\r
+                               }\r
+                               \r
+                       });\r
+               }\r
+               \r
+        explorer.finish();\r
+        \r
+        return explorer;\r
+               \r
+       }\r
+       \r
+}\r