From ced6f2290c0c02729fe87c6fb604cd549d4c4614 Mon Sep 17 00:00:00 2001 From: lempinen Date: Tue, 5 Oct 2010 09:37:23 +0000 Subject: [PATCH] Added result listeners to all models that have selected variables. (If selection is in model browser, variables from multiple models can be selected) 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 --- .../SysdynDatasetSelectionListener.java | 103 ++++++++++++++++-- 1 file changed, 92 insertions(+), 11 deletions(-) diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/viewUtils/SysdynDatasetSelectionListener.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/viewUtils/SysdynDatasetSelectionListener.java index 5d70e4e9..4424ceda 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/viewUtils/SysdynDatasetSelectionListener.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/viewUtils/SysdynDatasetSelectionListener.java @@ -2,6 +2,8 @@ package org.simantics.sysdyn.ui.viewUtils; import java.util.ArrayList; import java.util.Collection; +import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.Set; @@ -18,7 +20,6 @@ import org.simantics.db.layer0.SelectionHints; import org.simantics.db.layer0.variable.Variable; import org.simantics.db.layer0.variable.Variables; import org.simantics.diagram.stubs.DiagramResource; -import org.simantics.layer0.Layer0; import org.simantics.modeling.ModelingUtils; import org.simantics.simulation.ontology.SimulationResource; import org.simantics.sysdyn.SysdynResource; @@ -34,28 +35,90 @@ import org.simantics.utils.ui.ISelectionUtils; public abstract class SysdynDatasetSelectionListener implements ISelectionListener { protected abstract void selectionChanged(Collection activeDatasets); + + HashMap resultListeners = new HashMap(); @Override public void selectionChanged(IWorkbenchPart part, ISelection selection) { if(selection.isEmpty() || Boolean.TRUE.equals(PinTrend.getState())) return; if(selection instanceof IStructuredSelection) { + if(!resultListeners.isEmpty()) { + for(SysdynModel model : resultListeners.keySet()) + model.removeResultListener(resultListeners.get(model)); + resultListeners.clear(); + } Set bs = ISelectionUtils.filterSetSelection(selection, BrowserSelection.class); if(!bs.isEmpty()) { - updateDatasets(bs.toArray(new BrowserSelection[bs.size()])); + BrowserSelection[] browserSelections = bs.toArray(new BrowserSelection[bs.size()]); + updateDatasets(browserSelections); + addResultListeners(browserSelections); } else { Set ress = ISelectionUtils.filterSetSelection(selection, Resource.class); List runtimes = ISelectionUtils.getPossibleKeys(selection, SelectionHints.KEY_VARIABLE_RESOURCE, Resource.class); if(runtimes.isEmpty()) return; Resource runtime = runtimes.get(0); - updateDatasets(ress.toArray(Resource.NONE), runtime); + Resource[] resources = ress.toArray(Resource.NONE); + updateDatasets(resources, runtime); + addResultListeners(resources, runtime); } } } + private void addResultListeners(final BrowserSelection[] browserSelections) { + Session session = SimanticsUI.peekSession(); + if (session == null || browserSelections.length < 1) + return; + session.asyncRequest(new ReadRequest() { + @Override + public void run(ReadGraph g) throws DatabaseException { + HashSet models = new HashSet(); + for(BrowserSelection bs : browserSelections) { + if(bs.getModel() != null) { + models.add(getSysdynModel(g, bs.getModel())); + } + } + + Runnable listener = new Runnable() { + @Override + public void run() { + updateDatasets(browserSelections); + } + }; + + for(SysdynModel model : models) { + model.addResultListener(listener); + resultListeners.put(model, listener); + } + } + }); + } + private void addResultListeners(final Resource[] resources, final Resource runtime) { + Session session = SimanticsUI.peekSession(); + if (session == null || resources.length < 1) + return; + + session.asyncRequest(new ReadRequest() { + @Override + public void run(ReadGraph g) throws DatabaseException { + Variable variable = getVariable(g, resources[0], runtime); + SysdynModel model = getSysdynModel(g, Variables.getModel(g, variable)); + Runnable listener = new Runnable() { + @Override + public void run() { + updateDatasets(resources, runtime); + } + }; + model.addResultListener(listener); + resultListeners.put(model, listener); + } + }); + + } + private void updateDatasets(final Resource[] resources, final Resource runtime) { Session session = SimanticsUI.peekSession(); if (session == null) @@ -66,7 +129,8 @@ public abstract class SysdynDatasetSelectionListener implements ISelectionListen public void run(ReadGraph g) throws DatabaseException { ArrayList datasets = new ArrayList(); for(Resource resource : resources) { - Collection activeDataSets = loadAllActive(g, resource, runtime); + Variable variable = getVariable(g, resource, runtime); + Collection activeDataSets = loadAllActive(g, Variables.getRVI(g, variable).substring(1), getSysdynModel(g, Variables.getModel(g, variable))); if(activeDataSets != null && !activeDataSets.isEmpty()) datasets.addAll(activeDataSets); } @@ -87,7 +151,7 @@ public abstract class SysdynDatasetSelectionListener implements ISelectionListen ArrayList datasets = new ArrayList(); for(BrowserSelection bs : browserSelections) { if(bs.getURI() != null && bs.getModel() != null) { - Collection activeDataSets = loadAllActive(g, bs.getURI(), bs.getModel()); + Collection activeDataSets = loadAllActive(g, bs.getURI(), getSysdynModel(g, bs.getModel())); if(activeDataSets != null && !activeDataSets.isEmpty()) datasets.addAll(activeDataSets); } @@ -98,6 +162,7 @@ public abstract class SysdynDatasetSelectionListener implements ISelectionListen }); } + /* protected Collection loadAllActive(ReadGraph g, Resource element, Resource runtime) throws DatabaseException { Layer0 l0 = Layer0.getInstance(g); @@ -127,14 +192,14 @@ public abstract class SysdynDatasetSelectionListener implements ISelectionListen return loadAllActive(g, rvi.substring(1), model); } + */ - protected Collection loadAllActive(ReadGraph g, String rvi, Resource model) throws DatabaseException { + protected Collection loadAllActive(ReadGraph g, String rvi, SysdynModel model) throws DatabaseException { ArrayList dataSets = new ArrayList(); - - Resource configuration = g.getPossibleObject(model, SimulationResource.getInstance(g).HasConfiguration); - SysdynModel sm = SysdynModelManager.getInstance(g.getSession()).getModel(g, configuration); - - Collection activeResults = sm.getActiveResults(g); + if(model == null || rvi == null) + return dataSets; + + Collection activeResults = model.getActiveResults(g); for(SysdynResult sysdynResult : activeResults) { SysdynDataSet sds = sysdynResult.getDataSet(rvi.replace("/", ".")); if(sds != null) @@ -143,5 +208,21 @@ public abstract class SysdynDatasetSelectionListener implements ISelectionListen return dataSets; } + + private Variable getVariable(ReadGraph g, Resource element, Resource runtime) throws DatabaseException { + SysdynResource sr = SysdynResource.getInstance(g); + DiagramResource dr = DiagramResource.getInstance(g); + if(runtime == null) return null; + Resource resource = ModelingUtils.getPossibleElementCorrespondendence(g, element); + if(resource == null || !g.isInstanceOf(resource, sr.Variable)) return null; + String variableURI = g.getPossibleRelatedValue(runtime, dr.HasVariable); + Variable compositeVariable = Variables.getVariable(g, variableURI); + return compositeVariable.browsePossible(g, resource); + } + + private SysdynModel getSysdynModel(ReadGraph g, Resource model) throws DatabaseException { + Resource configuration = g.getPossibleObject(model, SimulationResource.getInstance(g).HasConfiguration); + return SysdynModelManager.getInstance(g.getSession()).getModel(g, configuration); + } } -- 2.47.1