]> gerrit.simantics Code Review - simantics/3d.git/blobdiff - dev/org.simantics.proconf.processeditor/src/fi/vtt/simantics/processeditor/dialogs/ConfigureMonitorDialog.java
Release
[simantics/3d.git] / dev / org.simantics.proconf.processeditor / src / fi / vtt / simantics / processeditor / dialogs / ConfigureMonitorDialog.java
diff --git a/dev/org.simantics.proconf.processeditor/src/fi/vtt/simantics/processeditor/dialogs/ConfigureMonitorDialog.java b/dev/org.simantics.proconf.processeditor/src/fi/vtt/simantics/processeditor/dialogs/ConfigureMonitorDialog.java
new file mode 100644 (file)
index 0000000..a771976
--- /dev/null
@@ -0,0 +1,309 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007 VTT Technical Research Centre of Finland and others.\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 fi.vtt.simantics.processeditor.dialogs;\r
+\r
+import java.util.ArrayList;\r
+import java.util.Collection;\r
+import java.util.HashSet;\r
+import java.util.Set;\r
+import java.util.TreeMap;\r
+import java.util.Map.Entry;\r
+\r
+import org.eclipse.jface.dialogs.Dialog;\r
+import org.eclipse.jface.dialogs.IDialogConstants;\r
+import org.eclipse.swt.SWT;\r
+import org.eclipse.swt.events.SelectionAdapter;\r
+import org.eclipse.swt.events.SelectionEvent;\r
+import org.eclipse.swt.events.SelectionListener;\r
+import org.eclipse.swt.layout.FillLayout;\r
+import org.eclipse.swt.layout.GridData;\r
+import org.eclipse.swt.layout.GridLayout;\r
+import org.eclipse.swt.widgets.Button;\r
+import org.eclipse.swt.widgets.Composite;\r
+import org.eclipse.swt.widgets.Control;\r
+import org.eclipse.swt.widgets.Label;\r
+import org.eclipse.swt.widgets.List;\r
+import org.eclipse.swt.widgets.Shell;\r
+import org.simantics.db.Graph;\r
+import org.simantics.db.GraphRequestAdapter;\r
+import org.simantics.db.GraphRequestStatus;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.Session;\r
+import org.simantics.layer0.utils.EntityFactory;\r
+import org.simantics.layer0.utils.IEntity;\r
+import org.simantics.layer0.utils.viewpoints.TraversalPath;\r
+import org.simantics.proconf.browsing.GraphExplorer;\r
+import org.simantics.proconf.g3d.stubs.G3DModel;\r
+import org.simantics.proconf.g3d.tools.OEPathSelectionListener;\r
+\r
+import fi.vtt.simantics.processeditor.ProcessResource;\r
+import fi.vtt.simantics.processeditor.common.PathUtils;\r
+import fi.vtt.simantics.processeditor.monitors.PathContainer;\r
+\r
+public class ConfigureMonitorDialog extends Dialog {\r
+       Session session;\r
+       List typeList;\r
+       java.util.List<Resource> nodes;\r
+\r
+       \r
+       private Composite oeLabelComposite;\r
+       //private Control viewpointControl;\r
+       private Composite oeComposite;\r
+       private GraphExplorer oe;\r
+       \r
+       private Button applyAnimationButton;\r
+       \r
+       Resource selectedType;\r
+       Resource sampleInstance;\r
+       java.util.List<Resource> sampleSource;\r
+       java.util.List<java.util.List<Resource>> samplePath;\r
+\r
+       \r
+       public ConfigureMonitorDialog(Shell parentShell, Session session, java.util.List<Resource> nodes) {\r
+               super(parentShell);\r
+               this.nodes = nodes;\r
+               this.session = session;\r
+               int shellStyle = getShellStyle();\r
+               setShellStyle(shellStyle | SWT.MAX | SWT.RESIZE);\r
+       }\r
+       \r
+       @Override\r
+    protected void configureShell(Shell newShell) {  \r
+        super.configureShell(newShell);\r
+        newShell.setText("Configure monitors");\r
+        \r
+    }\r
+       \r
+       protected Control createDialogArea(Composite parent) {\r
+        Composite composite = (Composite) super.createDialogArea(parent);\r
+        GridLayout layout = new GridLayout(2,true);\r
+               layout.marginHeight = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_MARGIN);\r
+               layout.marginWidth = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN);\r
+               layout.verticalSpacing = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_SPACING);\r
+               layout.horizontalSpacing = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING);\r
+               composite.setLayout(layout);\r
+               \r
+               Label label = new Label(composite, SWT.WRAP);\r
+        label.setText("Types");\r
+        GridData data = new GridData(GridData.CENTER,GridData.FILL,true,false,1,1);\r
+        data.widthHint = convertHorizontalDLUsToPixels(IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH);\r
+        label.setLayoutData(data);\r
+        label.setFont(parent.getFont());\r
+        \r
+        oeLabelComposite = new Composite(composite,SWT.NONE);\r
+        oeLabelComposite.setLayoutData(data);\r
+        oeLabelComposite.setLayout(new FillLayout(SWT.HORIZONTAL));\r
+        //label = new Label(composite, SWT.WRAP);\r
+        label = new Label(oeLabelComposite, SWT.WRAP);\r
+        label.setText("Monitor source");\r
+//        label.setLayoutData(data);\r
+        label.setFont(parent.getFont());\r
+        \r
+        typeList = new List(composite, SWT.BORDER | SWT.SINGLE | SWT.READ_ONLY | SWT.V_SCROLL);\r
+        typeList.setLayoutData(new GridData(GridData.GRAB_VERTICAL | GridData.VERTICAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_FILL));\r
+        \r
+        oeComposite = new Composite(composite,SWT.BORDER);\r
+        oeComposite.setLayoutData(new GridData(GridData.GRAB_VERTICAL | GridData.VERTICAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_FILL));\r
+        layout = new GridLayout();\r
+        layout.marginWidth = 0;\r
+        layout.marginHeight = 0;\r
+        oeComposite.setLayout(layout);\r
+        session.asyncRead(new GraphRequestAdapter() {\r
+               TreeMap<String, Resource> sorter;\r
+               @Override\r
+               public GraphRequestStatus perform(Graph g) throws Exception {\r
+                       Collection<Resource> types = getTypes(g,nodes);\r
+                \r
+                sorter = new TreeMap<String, Resource>();\r
+                for (Resource type : types) {\r
+                       IEntity t = EntityFactory.create(g, type);\r
+                    String key = t.getName();\r
+                    if (key.equals("")) key = "ERROR (" + type.getResourceId() + ")";\r
+                    sorter.put(key, type);\r
+                }\r
+                \r
+               \r
+                       return GraphRequestStatus.transactionComplete();\r
+               }\r
+               \r
+               @Override\r
+               public void requestCompleted(GraphRequestStatus status) {\r
+                       getShell().getDisplay().asyncExec(new Runnable() {\r
+                               @Override\r
+                               public void run() {\r
+                                        for (Entry<String, Resource> e : sorter.entrySet()) {    \r
+                               typeList.add(e.getKey());\r
+                               typeList.setData(e.getKey(), e.getValue());\r
+                                        }\r
+                               }\r
+                       });\r
+               }\r
+        });\r
+        \r
+        typeList.addSelectionListener(new SelectionListener() {\r
+            public void widgetSelected(SelectionEvent e) {\r
+               session.asyncRead(new GraphRequestAdapter() {\r
+                       @Override\r
+                       public GraphRequestStatus perform(Graph g) throws Exception {\r
+                                String key = typeList.getItem(typeList.getSelectionIndex());\r
+                         selectType(EntityFactory.create(g,(Resource) typeList.getData(key)));\r
+                                return GraphRequestStatus.transactionComplete();\r
+                       }\r
+               });\r
+               \r
+            }\r
+            public void widgetDefaultSelected(SelectionEvent e) {\r
+            }\r
+        });\r
+        \r
+     \r
+        \r
+        Composite buttonComposite = new Composite(composite,SWT.NONE);\r
+        buttonComposite.setLayoutData(new GridData(GridData.END,GridData.FILL,true,false,4,1));\r
+        buttonComposite.setLayout(new FillLayout(SWT.HORIZONTAL));\r
+        \r
+        \r
+        applyAnimationButton = new Button(buttonComposite,SWT.PUSH);\r
+        applyAnimationButton.setText("Apply monitor configuration");\r
+        applyAnimationButton.addSelectionListener(new SelectionAdapter() {\r
+               @Override\r
+               public void widgetSelected(SelectionEvent e) {\r
+                       applyAnimation();\r
+               }\r
+        });\r
+        return composite;\r
+       }\r
+       \r
+       /**\r
+        * Updates type selection:\r
+        * Finds all GraphicsModels that are connected to instances of selected type with HasGraphics-relation\r
+        * and lists them in modelList.\r
+        *\r
+        * @param resource\r
+        */\r
+       private void selectType(IEntity resource) {\r
+               selectedType = resource.getResource();\r
+               \r
+               TreeMap<String, G3DModel> sorter = new TreeMap<String, G3DModel>();\r
+               \r
+               Collection<IEntity> models = resource.getRelatedObjects(ProcessResource.plant3Dresource.HasGraphics);\r
+        for (IEntity model : models) {\r
+               \r
+                       String key = model.getName();\r
+               if (key.equals("")) key = "ERROR (" + model.getResource().getResourceId() + ")";\r
+               sorter.put(key, new G3DModel(model));\r
+        }\r
+        \r
+        if (oe != null) {\r
+               oe.dispose();   \r
+        }\r
+        sampleInstance = null;\r
+        sampleSource = null;\r
+        for (Resource n : nodes) {\r
+               IEntity t = EntityFactory.create(resource.getGraph(),n);\r
+               if (t.isInstanceOf(resource.getResource())) {\r
+                       sampleInstance = n;\r
+                       break;\r
+               }\r
+        }\r
+        if (sampleInstance != null) {\r
+//             oe = new OntologyExplorer("Animation Source",this.getShell(), new OntologyEditorInput(sampleInstance.getId()));\r
+//             if(viewpointControl != null && !viewpointControl.isDisposed()) {\r
+//                     viewpointControl.dispose();\r
+//             }\r
+//             viewpointControl = oe.getControl(oeLabelComposite, 1, OntologyExplorer.ViewpointSelector, SWT.NONE);\r
+//             Control c = oe.getControl(oeComposite, 1, OntologyExplorer.OntologyTree, SWT.MULTI);\r
+//             c.setLayoutData(new GridData(GridData.FILL_BOTH));\r
+//             oe.init(null, ViewpointUtils.getModelledHandler(oe.getGraph(), Builtins.DefaultViewpointId), null, ViewLabelProviderDecorationSettings.DEFAULT, new MenuAboutToShowAction(), new NullAdditionAction(), false);\r
+//             oe.setSelectionScheme(new SourceSelectionScheme(oe));\r
+//             oeComposite.getParent().layout(true,true);\r
+               oe = new GraphExplorer(oeComposite,SWT.MULTI);\r
+               oe.getTree().setLayoutData(new GridData(GridData.FILL_BOTH));\r
+               oe.getViewer().addPostSelectionChangedListener(new OEPathSelectionListener(){\r
+                       @Override\r
+                       protected void pathSelectionUpdated(java.util.List<TraversalPath> paths) {\r
+                               if (paths.size() == 0) {\r
+                       //selectSource(null);\r
+                   } else {\r
+                       final java.util.List<TraversalPath> ps = paths;\r
+                       session.asyncRead(new GraphRequestAdapter() {\r
+                               @Override\r
+                               public GraphRequestStatus perform(Graph g) throws Exception {\r
+                                       selectSource(g,ps);\r
+                                       return GraphRequestStatus.transactionComplete();\r
+                               }\r
+                       });\r
+                       \r
+                   }\r
+                               \r
+                       }\r
+               });\r
+        }\r
+               \r
+       }\r
+       \r
+       \r
+       \r
+       private void selectSource(Graph graph, java.util.List<TraversalPath> resources) {\r
+               if (resources == null) {\r
+                       sampleSource = null;\r
+               }\r
+               for (TraversalPath p : resources) {\r
+                       IEntity t = EntityFactory.create(graph, p.getResource());\r
+                       if (!t.isInstanceOf(ProcessResource.builtins.Double)) {\r
+                               sampleSource = null;\r
+                               return;\r
+                       }\r
+               }\r
+               sampleSource = new ArrayList<Resource>();\r
+               for (TraversalPath p : resources)\r
+                       sampleSource.add(p.getResource());\r
+               \r
+               samplePath = new ArrayList<java.util.List<Resource>>();\r
+               for (int i = 0; i < sampleSource.size(); i++) {\r
+                       TraversalPath tpath = resources.get(i);\r
+                       java.util.List<Resource> path = new ArrayList<Resource>();\r
+                       PathUtils.createPath(path, tpath,sampleInstance,sampleSource.get(i),oe);\r
+                       samplePath.add(path);\r
+               }       \r
+               \r
+       }\r
+       \r
+       private void applyAnimation() {\r
+               \r
+               if (sampleInstance == null || sampleSource == null || samplePath == null ) {\r
+                       throw new RuntimeException("Missing required selections");\r
+                       //return;\r
+               }\r
+\r
+               PathContainer.getInstance().clearPaths(selectedType);\r
+               for (java.util.List<Resource> s : samplePath) {\r
+                       PathContainer.getInstance().addPath(selectedType, s);\r
+               }\r
+       }\r
+       \r
+\r
+       \r
+\r
+\r
+       private Collection<Resource> getTypes(Graph g, java.util.List<Resource> instances) {\r
+               Set<Resource> types = new HashSet<Resource>();\r
+               for (Resource r : instances) {\r
+                       IEntity t = EntityFactory.create(g,r);\r
+                       Collection<IEntity> tTypes = t.getTypes();\r
+                       for (IEntity type : tTypes)\r
+                               types.add(type.getResource());\r
+               }\r
+               return types;\r
+       }\r
+       \r
+       \r
+}\r