]> gerrit.simantics Code Review - simantics/sysdyn.git/commitdiff
Added result listeners to all models that have selected variables. (If selection...
authorlempinen <lempinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Tue, 5 Oct 2010 09:37:23 +0000 (09:37 +0000)
committerlempinen <lempinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Tue, 5 Oct 2010 09:37:23 +0000 (09:37 +0000)
No need to "refresh" selection to get new results to trend view.

git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@18216 ac1ea38d-2e2b-0410-8846-a27921b304fc

org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/viewUtils/SysdynDatasetSelectionListener.java

index 5d70e4e99b3c60581dfcd7863237923b653dce65..4424cedad764adb2ec570e9266d40047339e661f 100644 (file)
@@ -2,6 +2,8 @@ package org.simantics.sysdyn.ui.viewUtils;
 \r
 import java.util.ArrayList;\r
 import java.util.Collection;\r
+import java.util.HashMap;\r
+import java.util.HashSet;\r
 import java.util.List;\r
 import java.util.Set;\r
 \r
@@ -18,7 +20,6 @@ import org.simantics.db.layer0.SelectionHints;
 import org.simantics.db.layer0.variable.Variable;\r
 import org.simantics.db.layer0.variable.Variables;\r
 import org.simantics.diagram.stubs.DiagramResource;\r
-import org.simantics.layer0.Layer0;\r
 import org.simantics.modeling.ModelingUtils;\r
 import org.simantics.simulation.ontology.SimulationResource;\r
 import org.simantics.sysdyn.SysdynResource;\r
@@ -34,28 +35,90 @@ import org.simantics.utils.ui.ISelectionUtils;
 public abstract class SysdynDatasetSelectionListener implements ISelectionListener {\r
        \r
        protected abstract void selectionChanged(Collection<SysdynDataSet> activeDatasets);\r
+       \r
+       HashMap<SysdynModel, Runnable> resultListeners = new HashMap<SysdynModel, Runnable>();\r
 \r
        @Override\r
        public void selectionChanged(IWorkbenchPart part, ISelection selection) {\r
                if(selection.isEmpty() || Boolean.TRUE.equals(PinTrend.getState()))\r
                        return;\r
                if(selection instanceof IStructuredSelection) {\r
+                       if(!resultListeners.isEmpty()) {\r
+                               for(SysdynModel model : resultListeners.keySet())\r
+                                       model.removeResultListener(resultListeners.get(model));\r
+                               resultListeners.clear();\r
+                       }\r
                        Set<BrowserSelection> bs = ISelectionUtils.filterSetSelection(selection, BrowserSelection.class);\r
                        if(!bs.isEmpty()) {\r
-                               updateDatasets(bs.toArray(new BrowserSelection[bs.size()]));\r
+                               BrowserSelection[] browserSelections = bs.toArray(new BrowserSelection[bs.size()]);\r
+                               updateDatasets(browserSelections);\r
+                               addResultListeners(browserSelections);\r
                        } else {\r
                                Set<Resource> ress = ISelectionUtils.filterSetSelection(selection, Resource.class);\r
                                List<Resource> runtimes = ISelectionUtils.getPossibleKeys(selection, SelectionHints.KEY_VARIABLE_RESOURCE, Resource.class);\r
                                if(runtimes.isEmpty())\r
                                        return;\r
                                Resource runtime = runtimes.get(0);\r
-                               updateDatasets(ress.toArray(Resource.NONE), runtime);\r
+                               Resource[] resources = ress.toArray(Resource.NONE);\r
+                               updateDatasets(resources, runtime);\r
+                               addResultListeners(resources, runtime);\r
                        }\r
                }\r
        }\r
 \r
+       private void addResultListeners(final BrowserSelection[] browserSelections) {\r
+               Session session = SimanticsUI.peekSession();\r
+               if (session == null || browserSelections.length < 1)\r
+                       return;\r
 \r
+               session.asyncRequest(new ReadRequest() {\r
+                       @Override\r
+                       public void run(ReadGraph g) throws DatabaseException {\r
+                               HashSet<SysdynModel> models = new HashSet<SysdynModel>(); \r
+                               for(BrowserSelection bs : browserSelections) {\r
+                                       if(bs.getModel() != null) {\r
+                                               models.add(getSysdynModel(g, bs.getModel()));\r
+                                       }\r
+                               }\r
+                               \r
+                               Runnable listener = new Runnable() {\r
+                                       @Override\r
+                                       public void run() {\r
+                                               updateDatasets(browserSelections);\r
+                                       }\r
+                               };\r
+                               \r
+                               for(SysdynModel model : models) {\r
+                                       model.addResultListener(listener);\r
+                                       resultListeners.put(model, listener);\r
+                               }\r
+                       }\r
+               });\r
+       }\r
 \r
+       private void addResultListeners(final Resource[] resources, final Resource runtime) {\r
+               Session session = SimanticsUI.peekSession();\r
+               if (session == null || resources.length < 1)\r
+                       return;\r
+               \r
+               session.asyncRequest(new ReadRequest() {\r
+                       @Override\r
+                       public void run(ReadGraph g) throws DatabaseException {\r
+                               Variable variable = getVariable(g, resources[0], runtime);\r
+                               SysdynModel model = getSysdynModel(g, Variables.getModel(g, variable));\r
+                               Runnable listener = new Runnable() {\r
+                                       @Override\r
+                                       public void run() {\r
+                                               updateDatasets(resources, runtime);\r
+                                       }\r
+                               };\r
+                               model.addResultListener(listener);\r
+                               resultListeners.put(model, listener);\r
+                       }\r
+               });\r
+\r
+       }\r
+       \r
        private void updateDatasets(final Resource[] resources, final Resource runtime) {\r
                Session session = SimanticsUI.peekSession();\r
                if (session == null)\r
@@ -66,7 +129,8 @@ public abstract class SysdynDatasetSelectionListener implements ISelectionListen
                        public void run(ReadGraph g) throws DatabaseException {\r
                                ArrayList<SysdynDataSet> datasets = new ArrayList<SysdynDataSet>();\r
                                for(Resource resource : resources) {\r
-                                       Collection<SysdynDataSet> activeDataSets = loadAllActive(g, resource, runtime);\r
+                                       Variable variable = getVariable(g, resource, runtime);\r
+                                       Collection<SysdynDataSet> activeDataSets = loadAllActive(g, Variables.getRVI(g, variable).substring(1), getSysdynModel(g, Variables.getModel(g, variable)));\r
                                        if(activeDataSets != null && !activeDataSets.isEmpty())\r
                                                datasets.addAll(activeDataSets);\r
                                }\r
@@ -87,7 +151,7 @@ public abstract class SysdynDatasetSelectionListener implements ISelectionListen
                                ArrayList<SysdynDataSet> datasets = new ArrayList<SysdynDataSet>();\r
                                for(BrowserSelection bs : browserSelections) {\r
                                        if(bs.getURI() != null && bs.getModel() != null) {\r
-                                               Collection<SysdynDataSet> activeDataSets = loadAllActive(g, bs.getURI(), bs.getModel());\r
+                                               Collection<SysdynDataSet> activeDataSets = loadAllActive(g, bs.getURI(), getSysdynModel(g, bs.getModel()));\r
                                                if(activeDataSets != null && !activeDataSets.isEmpty()) \r
                                                        datasets.addAll(activeDataSets);\r
                                        }\r
@@ -98,6 +162,7 @@ public abstract class SysdynDatasetSelectionListener implements ISelectionListen
                });\r
        }\r
        \r
+       /*\r
        protected Collection<SysdynDataSet> loadAllActive(ReadGraph g, Resource element, Resource runtime) throws DatabaseException {\r
 \r
                Layer0 l0 = Layer0.getInstance(g);\r
@@ -127,14 +192,14 @@ public abstract class SysdynDatasetSelectionListener implements ISelectionListen
                return loadAllActive(g, rvi.substring(1), model);\r
 \r
        }\r
+       */\r
 \r
-       protected Collection<SysdynDataSet> loadAllActive(ReadGraph g, String rvi, Resource model) throws DatabaseException {\r
+       protected Collection<SysdynDataSet> loadAllActive(ReadGraph g, String rvi, SysdynModel model) throws DatabaseException {\r
                ArrayList<SysdynDataSet> dataSets = new ArrayList<SysdynDataSet>();\r
-\r
-               Resource configuration = g.getPossibleObject(model, SimulationResource.getInstance(g).HasConfiguration);\r
-               SysdynModel sm = SysdynModelManager.getInstance(g.getSession()).getModel(g, configuration);\r
-\r
-               Collection<SysdynResult> activeResults = sm.getActiveResults(g);\r
+               if(model == null || rvi == null)\r
+                       return dataSets;\r
+               \r
+               Collection<SysdynResult> activeResults = model.getActiveResults(g);\r
                for(SysdynResult sysdynResult : activeResults) {\r
                        SysdynDataSet sds = sysdynResult.getDataSet(rvi.replace("/", "."));\r
                        if(sds != null)\r
@@ -143,5 +208,21 @@ public abstract class SysdynDatasetSelectionListener implements ISelectionListen
                return dataSets;\r
 \r
        }\r
+       \r
+       private Variable getVariable(ReadGraph g, Resource element, Resource runtime) throws DatabaseException {\r
+               SysdynResource sr = SysdynResource.getInstance(g);\r
+               DiagramResource dr = DiagramResource.getInstance(g);\r
+               if(runtime == null) return null;\r
+               Resource resource = ModelingUtils.getPossibleElementCorrespondendence(g, element);\r
+               if(resource == null || !g.isInstanceOf(resource, sr.Variable)) return null;\r
+               String variableURI = g.getPossibleRelatedValue(runtime, dr.HasVariable);\r
+               Variable compositeVariable = Variables.getVariable(g, variableURI);\r
+               return compositeVariable.browsePossible(g, resource);\r
+       }\r
+       \r
+       private SysdynModel getSysdynModel(ReadGraph g, Resource model) throws DatabaseException {\r
+               Resource configuration = g.getPossibleObject(model, SimulationResource.getInstance(g).HasConfiguration);\r
+               return SysdynModelManager.getInstance(g.getSession()).getModel(g, configuration);\r
+       }\r
 \r
 }\r