From 3ba690eaa79c5ca19202de8a7dace30228bf1f32 Mon Sep 17 00:00:00 2001 From: lempinen Date: Fri, 14 Jan 2011 13:20:30 +0000 Subject: [PATCH] Simplified selections and trend view selection listener git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@19378 ac1ea38d-2e2b-0410-8846-a27921b304fc --- .../sysdyn/ui/browser/BrowserSelection.java | 14 +- .../sysdyn/ui/browser/SysdynBrowser.java | 3 +- .../editor/participant/PointerInteractor.java | 2 +- .../SysdynDatasetSelectionListener.java | 174 +++++++----------- .../expressions/StockExpression.java | 6 +- 5 files changed, 87 insertions(+), 112 deletions(-) diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/BrowserSelection.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/BrowserSelection.java index 4106a455..60ab9fef 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/BrowserSelection.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/BrowserSelection.java @@ -14,18 +14,22 @@ package org.simantics.sysdyn.ui.browser; import org.eclipse.core.runtime.IAdaptable; import org.simantics.browsing.ui.NodeContext; import org.simantics.db.Resource; +import org.simantics.db.layer0.variable.Variable; +import org.simantics.sysdyn.ui.browser.nodes.VariableNode; public class BrowserSelection implements IAdaptable { private Resource resource; private Resource model; private String URI; private Object originalInput; + private Variable variable; - public BrowserSelection(Object originalInput, Resource resource, String URI, Resource model) { + public BrowserSelection(Object originalInput, VariableNode vn) { this.originalInput = originalInput; - this.resource = resource; - this.URI = URI; - this.model = model; + this.resource = vn.getResource(); + this.URI = vn.getURI(); + this.model = vn.getModel(); + this.variable = vn.getVariable(); } @SuppressWarnings("rawtypes") @@ -33,6 +37,8 @@ public class BrowserSelection implements IAdaptable { public Object getAdapter(Class adapter) { if (adapter == Resource.class) return resource; + if (adapter == Variable.class) + return variable; if (NodeContext.class.equals(adapter)) return originalInput; if (originalInput instanceof IAdaptable) { diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/SysdynBrowser.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/SysdynBrowser.java index dac17e5e..fdd38f71 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/SysdynBrowser.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/SysdynBrowser.java @@ -52,9 +52,8 @@ public class SysdynBrowser extends GraphExplorerView { VariableNode vn = (VariableNode) ctx.getAdapter(VariableNode.class); IHintContext context; if(vn != null) { - Resource resource = vn.getResource() == null ? vn.data : vn.getResource(); context = new AdaptableHintContext(SelectionHints.KEY_MAIN, SelectionHints.KEY_SELECTION_PROPERTY); - context.setHint(SelectionHints.KEY_MAIN, new BrowserSelection(objects[i], resource, vn.getURI(), vn.getModel())); + context.setHint(SelectionHints.KEY_MAIN, new BrowserSelection(objects[i], vn)); context.setHint(SelectionHints.KEY_SELECTION_PROPERTY, vn.getVariable()); } else { context = new AdaptableHintContext(SelectionHints.KEY_MAIN); diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/editor/participant/PointerInteractor.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/editor/participant/PointerInteractor.java index 70a99882..dbaa4014 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/editor/participant/PointerInteractor.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/editor/participant/PointerInteractor.java @@ -122,7 +122,7 @@ public class PointerInteractor extends org.simantics.g2d.diagram.participant.poi diagram.setHint(DiagramHints.KEY_USE_CONNECTION_FLAGS, true); ISysdynElementClassProvider secp = (ISysdynElementClassProvider)elementClassProvider; secp.put(ElementClasses.CONNECTION, elementClassProvider.get(ConnectionClasses.FLOW)); - bsi = new SysdynConnectTool(null, me.mouseId, curCanvasPos); // FIXME + bsi = new SysdynConnectTool(null, me.mouseId, curCanvasPos); } if (bsi != null) { 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 12624c39..3e26d035 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 @@ -38,7 +38,6 @@ import org.simantics.sysdyn.manager.SysdynDataSet; import org.simantics.sysdyn.manager.SysdynModel; import org.simantics.sysdyn.manager.SysdynModelManager; import org.simantics.sysdyn.manager.SysdynResult; -import org.simantics.sysdyn.ui.browser.BrowserSelection; import org.simantics.sysdyn.ui.trend.PinTrend; import org.simantics.ui.SimanticsUI; import org.simantics.utils.ui.ISelectionUtils; @@ -50,7 +49,7 @@ public abstract class SysdynDatasetSelectionListener implements ISelectionListen HashMap resultListeners = new HashMap(); @Override - public void selectionChanged(IWorkbenchPart part, ISelection selection) { + public void selectionChanged(IWorkbenchPart part, final ISelection selection) { if(selection.isEmpty() || Boolean.TRUE.equals(PinTrend.getState())) return; if(selection instanceof IStructuredSelection) { @@ -59,121 +58,88 @@ public abstract class SysdynDatasetSelectionListener implements ISelectionListen model.removeResultListener(resultListeners.get(model)); resultListeners.clear(); } - Set bs = ISelectionUtils.filterSetSelection(selection, BrowserSelection.class); - if(!bs.isEmpty()) { - 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); - 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())); + + Session session = SimanticsUI.peekSession(); + if (session == null) + return; + + session.asyncRequest(new ReadRequest() { + @Override + public void run(ReadGraph graph) throws DatabaseException { + + Collection vars = ISelectionUtils.filterSetSelection(selection, Variable.class); + + if(vars.isEmpty()) { + 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); + + for(Resource resource : ress) { + Variable variable = getVariable(graph, resource, runtime); + if(variable != null) + vars.add(variable); + } } + + updateDatasets(graph, vars); + addResultListeners(graph, vars); } - - 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; + + + private void addResultListeners(ReadGraph graph, final Collection variables) throws DatabaseException { + if(variables.size() < 1) return; - session.asyncRequest(new ReadRequest() { + HashSet models = new HashSet(); + for(Variable variable : variables) { + Resource model = Variables.getModel(graph, variable); + if(model != null) { + models.add(getSysdynModel(graph, model)); + } + } + + Runnable listener = new Runnable() { @Override - public void run(ReadGraph g) throws DatabaseException { - Variable variable = getVariable(g, resources[0], runtime); - if(variable == null) return; - SysdynModel model = getSysdynModel(g, Variables.getModel(g, variable)); - Runnable listener = new Runnable() { + public void run() { + Session session = SimanticsUI.peekSession(); + if (session == null) + return; + + session.asyncRequest(new ReadRequest() { + @Override - public void run() { - updateDatasets(resources, runtime); + public void run(ReadGraph graph) throws DatabaseException { + updateDatasets(graph, variables); } - }; - model.addResultListener(listener); - resultListeners.put(model, listener); + }); } - }); - + }; + + for(SysdynModel model : models) { + model.addResultListener(listener); + resultListeners.put(model, listener); + } } - private void updateDatasets(final Resource[] resources, final Resource runtime) { - Session session = SimanticsUI.peekSession(); - if (session == null) - return; - - session.asyncRequest(new ReadRequest() { - @Override - public void run(ReadGraph g) throws DatabaseException { - ArrayList datasets = new ArrayList(); - for(Resource resource : resources) { - Variable variable = getVariable(g, resource, runtime); - if(variable == null) continue; - Collection activeDataSets = loadAllActive(g, Variables.getRVI(g, variable).substring(1), getSysdynModel(g, Variables.getModel(g, variable))); - if(activeDataSets != null && !activeDataSets.isEmpty()) - datasets.addAll(activeDataSets); - } - selectionChanged(datasets); - } - }); + private void updateDatasets(ReadGraph graph, Collection variables) throws DatabaseException { + + ArrayList datasets = new ArrayList(); + for(Variable variable : variables) { + String rvi = Variables.getRVI(graph, variable); + rvi = rvi.length() > 0 ? rvi.substring(1) : rvi; + Resource model = Variables.getModel(graph, variable); + Collection activeDataSets = loadAllActive(graph, rvi, getSysdynModel(graph, model)); + if(activeDataSets != null && !activeDataSets.isEmpty()) + datasets.addAll(activeDataSets); + } + selectionChanged(datasets); } - private void updateDatasets(final BrowserSelection[] browserSelections) { - Session session = SimanticsUI.peekSession(); - if (session == null) - return; - - session.asyncRequest(new ReadRequest() { - @Override - public void run(ReadGraph g) throws DatabaseException { - ArrayList datasets = new ArrayList(); - for(BrowserSelection bs : browserSelections) { - if(bs.getURI() != null && bs.getModel() != null) { - Collection activeDataSets = loadAllActive(g, bs.getURI(), getSysdynModel(g, bs.getModel())); - if(activeDataSets != null && !activeDataSets.isEmpty()) - datasets.addAll(activeDataSets); - } - - } - selectionChanged(datasets); - } - }); - } protected Collection loadAllActive(ReadGraph g, String rvi, SysdynModel model) throws DatabaseException { ArrayList dataSets = new ArrayList(); diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/StockExpression.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/StockExpression.java index be414d9e..2212b1eb 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/StockExpression.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/StockExpression.java @@ -53,7 +53,11 @@ public class StockExpression extends Expression { range = sb.toString(); } - String each = variable.getArrayIndexes().getEnumerations().isEmpty() ? "" : "each"; + String each = ""; + if( variable.getArrayIndexes() == null ||variable.getArrayIndexes().getEnumerations().isEmpty()) + each = ""; + else + each = "each"; if (value == null) { return " " + variable.getType() + " " + variable.getName() + range + "(" + each + " fixed=false);\n"; } else { -- 2.47.1