From 413bf0a7a965a397d4890d4ff87e2a95726fcc08 Mon Sep 17 00:00:00 2001 From: lempinen Date: Fri, 5 Jul 2013 13:25:49 +0000 Subject: [PATCH] Display chart ranges in withlookup expression tabs (fixes #4379) git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@27697 ac1ea38d-2e2b-0410-8846-a27921b304fc --- .../expressions/WithLookupExpression.java | 134 ++++++++++++------ 1 file changed, 88 insertions(+), 46 deletions(-) diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/WithLookupExpression.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/WithLookupExpression.java index 0ef578b9..32b0bfed 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/WithLookupExpression.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/WithLookupExpression.java @@ -56,6 +56,8 @@ import org.simantics.db.WriteGraph; import org.simantics.db.common.request.WriteRequest; import org.simantics.db.common.utils.ListUtils; import org.simantics.db.exception.DatabaseException; +import org.simantics.db.layer0.variable.Variable; +import org.simantics.db.layer0.variable.Variables; import org.simantics.db.procedure.Listener; import org.simantics.db.request.Read; import org.simantics.layer0.Layer0; @@ -65,6 +67,7 @@ import org.simantics.sysdyn.tableParser.ParseException; import org.simantics.sysdyn.tableParser.TableParser; import org.simantics.sysdyn.tableParser.Token; import org.simantics.sysdyn.ui.utils.SyntaxError; +import org.simantics.sysdyn.utils.SheetUtils; import org.simantics.ui.SimanticsUI; public class WithLookupExpression implements IExpression { @@ -173,10 +176,10 @@ public class WithLookupExpression implements IExpression { @Override public void run() { - lookup.setExpression(result); + lookup.setExpression(result); + updateChart(); } }); - updateChart(); } @Override @@ -260,52 +263,52 @@ public class WithLookupExpression implements IExpression { if(oldExpression == null || oldLookupTable == null || (currentExpression != null && currentLookupTable != null - && (!currentExpression.equals(oldExpression) || - !currentLookupTable.equals(oldLookupTable)))) { + && (!currentExpression.equals(oldExpression) || + !currentLookupTable.equals(oldLookupTable)))) { data.putAll(data); data.put("equation", currentExpression); data.put("lookup", currentLookupTable); SimanticsUI.getSession().asyncRequest(new WriteRequest() { @Override public void perform(WriteGraph g) - throws DatabaseException { + throws DatabaseException { SysdynResource sr = SysdynResource.getInstance(g); Layer0 l0 = Layer0.getInstance(g); - + if(!g.isInstanceOf(expr, sr.WithLookupExpression)) { - - - final Resource newExpression = GraphUtils.create2(g, sr.WithLookupExpression, - sr.WithLookupExpression_minX, 0.0, - sr.WithLookupExpression_maxX, 10.0, - sr.WithLookupExpression_minY, 0.0, - sr.WithLookupExpression_maxY, 10.0); - String arrayRange = g.getPossibleRelatedValue(expression, sr.Expression_arrayRange, Bindings.STRING); - if(arrayRange != null) - g.claimLiteral(newExpression, sr.Expression_arrayRange, arrayRange); - + + + final Resource newExpression = GraphUtils.create2(g, sr.WithLookupExpression, + sr.WithLookupExpression_minX, 0.0, + sr.WithLookupExpression_maxX, 10.0, + sr.WithLookupExpression_minY, 0.0, + sr.WithLookupExpression_maxY, 10.0); + String arrayRange = g.getPossibleRelatedValue(expression, sr.Expression_arrayRange, Bindings.STRING); + if(arrayRange != null) + g.claimLiteral(newExpression, sr.Expression_arrayRange, arrayRange); + final Resource variable = g.getSingleObject(expression, l0.PartOf); - Resource expressions = g.getPossibleObject(variable, sr.Variable_expressionList); - Resource node = ListUtils.getNode(g, expressions, expression); - g.deny(node, l0.List_Element); - g.claim(node, l0.List_Element, newExpression); - + Resource expressions = g.getPossibleObject(variable, sr.Variable_expressionList); + Resource node = ListUtils.getNode(g, expressions, expression); + g.deny(node, l0.List_Element); + g.claim(node, l0.List_Element, newExpression); + g.deny(expression, l0.PartOf); g.claim(newExpression, l0.PartOf, variable); - - VirtualGraph runtime = g.getService(VirtualGraph.class); - g.syncRequest(new WriteRequest(runtime) { - @Override - public void perform(WriteGraph graph) throws DatabaseException { - SysdynResource sr = SysdynResource.getInstance(graph); - if(graph.hasStatement(variable, sr.IndependentVariable_activeExpression)) - graph.deny(variable, sr.IndependentVariable_activeExpression); - graph.claim(variable, sr.IndependentVariable_activeExpression, newExpression); - } - } - ); - expr = newExpression; - + + VirtualGraph runtime = g.getService(VirtualGraph.class); + g.syncRequest(new WriteRequest(runtime) { + @Override + public void perform(WriteGraph graph) throws DatabaseException { + SysdynResource sr = SysdynResource.getInstance(graph); + if(graph.hasStatement(variable, sr.IndependentVariable_activeExpression)) + graph.deny(variable, sr.IndependentVariable_activeExpression); + graph.claim(variable, sr.IndependentVariable_activeExpression, newExpression); + } + } + ); + expr = newExpression; + } g.claimLiteral(expr, sr.WithLookupExpression_expression, currentExpression); g.claimLiteral(expr, sr.WithLookupExpression_lookup, currentLookupTable); @@ -328,12 +331,12 @@ public class WithLookupExpression implements IExpression { this.expression.getSourceViewer().getTextWidget().addKeyListener(listener); this.lookup.getSourceViewer().getTextWidget().addKeyListener(listener); } - - @Override - public void addVerifyKeyListener(VerifyKeyListener listener) { - this.expression.getSourceViewer().getTextWidget().addVerifyKeyListener(listener); - this.lookup.getSourceViewer().getTextWidget().addVerifyKeyListener(listener); - } + + @Override + public void addVerifyKeyListener(VerifyKeyListener listener) { + this.expression.getSourceViewer().getTextWidget().addVerifyKeyListener(listener); + this.lookup.getSourceViewer().getTextWidget().addVerifyKeyListener(listener); + } @Override public void addModifyListener(ModifyListener listener) { @@ -372,7 +375,7 @@ public class WithLookupExpression implements IExpression { true, true, false - ); + ); chart.removeLegend(); chart.getXYPlot().getDomainAxis().setTickLabelsVisible(true); chart.getXYPlot().getDomainAxis().setAxisLineVisible(false); @@ -398,9 +401,48 @@ public class WithLookupExpression implements IExpression { Double.parseDouble(yTokens.get(i).image))); } } catch (ParseException e1) { - this.lookup.setSyntaxError(new SyntaxError(e1.currentToken, "Syntax Error")); - System.out.println("MESSAGE: " + e1.getMessage()); - return; + if(lookup.getExpression().matches("[a-zA-Z0-9]*\\([a-zA-Z0-9:]*\\)")) { + // Might be a sheet reference + try { + final String name = lookup.getExpression().substring(0, lookup.getExpression().indexOf("(")); + final String range = lookup.getExpression().substring(lookup.getExpression().indexOf("(") + 1, lookup.getExpression().indexOf(")")); + String possibleTable = SimanticsUI.getSession().syncRequest(new Read() { + + @Override + public String perform(ReadGraph graph) throws DatabaseException { + Variable variable = input.variable; + Resource model = Variables.getModel(graph, variable); + return SheetUtils.getStringRepresentation(graph, model, name, range); + } + + }); + + + try { + if(possibleTable == null) { + ParseException e = new ParseException(e1.getMessage()); + e.currentToken = e1.currentToken; + throw e; + } + + parser.ReInit(new StringReader(possibleTable)); + parser.table(); + ArrayList xTokens = parser.getXTokens(); + ArrayList yTokens = parser.getYTokens(); + for(int i = 0; i < xTokens.size(); i++) { + dataPoints.add(new Point2D.Double( + Double.parseDouble(xTokens.get(i).image), + Double.parseDouble(yTokens.get(i).image))); + } + } catch (ParseException e2) { + this.lookup.setSyntaxError(new SyntaxError(e2.currentToken, "Syntax Error")); + System.out.println("MESSAGE: " + e2.getMessage()); + return; + } + } catch (DatabaseException e) { + e.printStackTrace(); + } + } } XYSeries series = new XYSeries("Lookup Table"); -- 2.47.1