package org.simantics.sysdyn.ui.equation;\r
\r
\r
+import java.io.StringReader;\r
+import java.util.HashSet;\r
+import java.util.Set;\r
+\r
import org.eclipse.jface.layout.GridDataFactory;\r
import org.eclipse.jface.layout.GridLayoutFactory;\r
import org.eclipse.jface.viewers.ISelection;\r
import org.eclipse.swt.SWT;\r
import org.eclipse.swt.events.FocusEvent;\r
import org.eclipse.swt.events.FocusListener;\r
+import org.eclipse.swt.events.ModifyEvent;\r
+import org.eclipse.swt.events.ModifyListener;\r
import org.eclipse.swt.events.MouseEvent;\r
import org.eclipse.swt.events.MouseListener;\r
import org.eclipse.swt.events.SelectionEvent;\r
import org.eclipse.swt.events.SelectionListener;\r
+import org.eclipse.swt.graphics.Color;\r
import org.eclipse.swt.graphics.Font;\r
import org.eclipse.swt.graphics.Point;\r
import org.eclipse.swt.layout.RowLayout;\r
import org.simantics.db.exception.DatabaseException;\r
import org.simantics.db.procedure.Listener;\r
import org.simantics.db.request.Read;\r
+import org.simantics.sysdyn.expressionParser.ExpressionParser;\r
+import org.simantics.sysdyn.expressionParser.ParseException;\r
import org.simantics.ui.SimanticsUI;\r
\r
public class EquationView extends ViewPart implements ISelectionListener {\r
Composite unitsAndRange; \r
Composite emptyComposite;\r
\r
- \r
@Override \r
public void createPartControl(Composite parent) {\r
// Listeners\r
unitSelector.updateUnits(variable);\r
}\r
\r
+ if(shortcutTabs != null) shortcutTabs.updateTables(variable);\r
+ \r
String expressionType = expressionComposite.resetExpressionView(variable);\r
+ addExpressionFieldListeners();\r
+ validateExpressionFields();\r
expressionController.setExpressionTypes(expressionComposite.getExpressionTypes()); \r
expressionController.select(expressionType);\r
\r
nameText.setText(result.name);\r
- \r
- if(shortcutTabs != null) {\r
- shortcutTabs.updateTables(variable);\r
- }\r
}\r
}); \r
\r
@Override\r
public void widgetSelected(SelectionEvent e) {\r
expressionComposite.displayExpressionView(expressionController.getSelecetedType());\r
+ addExpressionFieldListeners();\r
+ validateExpressionFields();\r
}\r
\r
@Override\r
});\r
}\r
\r
-\r
-\r
+ private void addExpressionFieldListeners() {\r
+ if(expressionComposite.getExpressionViewFactor() != null)\r
+ for(Text text : expressionComposite.getExpressionViewFactor().getExpressionFields()) {\r
+ text.addModifyListener(new ModifyListener() {\r
+\r
+ @Override\r
+ public void modifyText(ModifyEvent e) {\r
+ validateExpressionFields();\r
+ }\r
+ });\r
+ }\r
+ }\r
+ \r
+ private void validateExpressionFields() {\r
+ ExpressionParser parser = new ExpressionParser(new StringReader(""));\r
+ Set<String> variables = new HashSet<String>();\r
+ \r
+ if(expressionComposite.getExpressionViewFactor() != null)\r
+ for(Text text : expressionComposite.getExpressionViewFactor().getExpressionFields()) {\r
+ String textString = text.getText();\r
+ parser.ReInit(new StringReader(textString));\r
+ try {\r
+ parser.expr();\r
+ variables.addAll(parser.getReferences());\r
+ text.setBackground(new Color(text.getDisplay(), 255, 255, 255));\r
+ } catch (ParseException e1) {\r
+ text.setBackground(new Color(text.getDisplay(), 255, 230, 230));\r
+ return;\r
+ }\r
+ }\r
+ \r
+ for(TableItem ti : this.shortcutTabs.getVariableTable().getItems()) {\r
+ if(!variables.contains(ti.getText())) {\r
+ for(Text text : expressionComposite.getExpressionViewFactor().getExpressionFields())\r
+ text.setBackground(new Color(text.getDisplay(), 255, 230, 230)); \r
+ ti.setBackground(new Color(ti.getDisplay(), 255, 230, 230));\r
+ } else {\r
+ ti.setBackground(new Color(ti.getDisplay(), 255, 255, 255));\r
+ }\r
+ }\r
+ }\r
\r
}
\ No newline at end of file
}\r
\r
public String resetExpressionView(Resource variable) {\r
- data.clear();\r
- origData.clear();\r
+ this.data.clear();\r
+ this.origData.clear();\r
this.variable = variable;\r
this.expressionViewFactor = null;\r
String origExpressionType = getOriginalExpressionType().toString();\r
this.expressionViewFactor.replaceSelection(var);\r
}\r
\r
+ public IExpressionViewFactor getExpressionViewFactor() {\r
+ return this.expressionViewFactor;\r
+ }\r
+ \r
private ExpressionType getOriginalExpressionType() {\r
\r
ExpressionType et = null;\r
package org.simantics.sysdyn.ui.equation.expressions;\r
\r
+import java.util.Arrays;\r
+import java.util.List;\r
import java.util.Map;\r
\r
import org.eclipse.jface.layout.GridDataFactory;\r
\r
expression.addFocusListener(new FocusAdapter() {\r
\r
+ @Override\r
+ public void focusGained(FocusEvent e){\r
+ \r
+ }\r
+\r
@Override\r
public void focusLost(FocusEvent e) {\r
lastSelection = expression.getSelection(); \r
}\r
});\r
\r
- expression.addKeyListener(new ExpressionFieldKeyListener(equation));\r
}\r
\r
@Override\r
if(this.expression != null && this.expression.getText() != null)\r
data.put("equation", this.expression.getText());\r
}\r
+\r
+ @Override\r
+ public List<Text> getExpressionFields() {\r
+ return Arrays.asList(this.expression);\r
+ }\r
}\r
package org.simantics.sysdyn.ui.equation.expressions;\r
\r
+import java.util.Arrays;\r
+import java.util.List;\r
import java.util.Map;\r
\r
import org.eclipse.jface.layout.GridDataFactory;\r
\r
final String equation = data.get("equation") != null ? (String)data.get("equation") : "";\r
\r
-\r
GridDataFactory.fillDefaults().grab(true, true).applyTo(parent);\r
GridLayoutFactory.fillDefaults().numColumns(2).spacing(3, 3).applyTo(parent);\r
equationLabel = new Label(parent, SWT.NONE);\r
}\r
});\r
\r
- expression.addKeyListener(new ExpressionFieldKeyListener(equation));\r
-\r
-\r
}\r
\r
@Override\r
data.put("equation", this.expression.getText()); \r
}\r
\r
+ @Override\r
+ public List<Text> getExpressionFields() {\r
+ return Arrays.asList(this.expression);\r
+ }\r
+\r
}\r
package org.simantics.sysdyn.ui.equation.expressions;\r
\r
+import java.util.List;\r
import java.util.Map;\r
\r
import org.eclipse.swt.widgets.Composite;\r
+import org.eclipse.swt.widgets.Text;\r
import org.simantics.db.Resource;\r
\r
public class DelayExpressionViewFactor implements IExpressionViewFactor {\r
\r
}\r
\r
+ @Override\r
+ public List<Text> getExpressionFields() {\r
+ // TODO Auto-generated method stub\r
+ return null;\r
+ }\r
+\r
}\r
\r
public class ExpressionFieldKeyListener implements KeyListener {\r
\r
- String originalText; \r
+ private String originalText; \r
+ private ExpressionParser parser;\r
\r
- public ExpressionFieldKeyListener(String originalText) {\r
- this.originalText = originalText; \r
+ public ExpressionFieldKeyListener(ExpressionParser parser, Text text) {\r
+ this.originalText = text.getText(); \r
+ this.parser = parser;\r
+ this.handleKeyReleased(text); \r
}\r
@Override\r
public void keyPressed(KeyEvent e) { \r
public void keyReleased(KeyEvent e) { \r
if(e.widget instanceof Text) {\r
Text text = (Text)e.widget;\r
- \r
- ExpressionParser parser = new ExpressionParser(\r
- new StringReader(text.getText())\r
- );\r
- try {\r
- parser.expr();\r
- text.setBackground(new Color(text.getDisplay(), 255, 255, 255));\r
- } catch (ParseException e1) {\r
- text.setBackground(new Color(text.getDisplay(), 255, 230, 230));\r
- }\r
+ this.handleKeyReleased(text);\r
+ }\r
+ }\r
+ \r
+ private void handleKeyReleased(Text text) {\r
+ String textString = text.getText();\r
+ this.parser.ReInit(new StringReader(textString));\r
+ \r
+ try {\r
+ parser.expr();\r
+ text.setBackground(new Color(text.getDisplay(), 255, 255, 255));\r
+ } catch (ParseException e1) {\r
+ text.setBackground(new Color(text.getDisplay(), 255, 230, 230));\r
}\r
}\r
-\r
}\r
package org.simantics.sysdyn.ui.equation.expressions;\r
\r
+import java.util.List;\r
import java.util.Map;\r
\r
import org.eclipse.swt.SWT;\r
import org.eclipse.swt.graphics.Font;\r
import org.eclipse.swt.widgets.Composite;\r
+import org.eclipse.swt.widgets.Text;\r
import org.simantics.db.Resource;\r
\r
public interface IExpressionViewFactor {\r
* @param data\r
*/\r
void updateData(Map<String, Object> data);\r
+ \r
+ /**\r
+ * @return all individual expression fields for modelica validation\r
+ */\r
+ List<Text> getExpressionFields();\r
+ \r
}\r
package org.simantics.sysdyn.ui.equation.expressions;\r
\r
+import java.util.List;\r
import java.util.Map;\r
\r
import org.eclipse.swt.widgets.Composite;\r
+import org.eclipse.swt.widgets.Text;\r
import org.simantics.db.Resource;\r
\r
public class LookupExpressionViewFactor implements IExpressionViewFactor {\r
\r
}\r
\r
+ @Override\r
+ public List<Text> getExpressionFields() {\r
+ // TODO Auto-generated method stub\r
+ return null;\r
+ }\r
+\r
}\r
package org.simantics.sysdyn.ui.equation.expressions;\r
\r
+import java.util.Arrays;\r
+import java.util.List;\r
import java.util.Map;\r
\r
import org.eclipse.jface.layout.GridDataFactory;\r
}\r
});\r
\r
- expression.addKeyListener(new ExpressionFieldKeyListener(equation));\r
-\r
- \r
}\r
\r
@Override\r
data.put("equation", this.expression.getText()); \r
}\r
\r
+ @Override\r
+ public List<Text> getExpressionFields() {\r
+ return Arrays.asList(this.expression);\r
+ }\r
+\r
}\r
\r
package org.simantics.sysdyn.ui.equation.expressions;\r
\r
+import java.util.Arrays;\r
import java.util.Collection;\r
+import java.util.List;\r
import java.util.Map;\r
\r
import org.eclipse.jface.layout.GridDataFactory;\r
\r
public class StockExpressionViewFactor implements IExpressionViewFactor {\r
\r
- Label integralLabel;\r
- Text integral;\r
- Label equationLabel;\r
- Text expression;\r
- Point lastSelection = new Point(0,0);\r
+ private Label integralLabel;\r
+ private Text integral;\r
+ private Label equationLabel;\r
+ private Text expression;\r
+ private Point lastSelection = new Point(0,0);\r
\r
@Override\r
public void createView(Composite parent, Map<String, Object> data) {\r
\r
final String initialEquation = data.get("initialEquation") != null ? (String)data.get("initialEquation") : "";\r
final String integralEquation = data.get("integral") != null ? (String)data.get("integral") : "";\r
-\r
+ \r
GridDataFactory.fillDefaults().grab(true, true).applyTo(parent);\r
GridLayoutFactory.fillDefaults().numColumns(2).spacing(3, 3).applyTo(parent);\r
\r
}\r
});\r
\r
- expression.addKeyListener(new ExpressionFieldKeyListener(initialEquation));\r
-\r
}\r
\r
@Override\r
if(this.integral != null && this.integral.getText() != null)\r
data.put("integral", this.integral.getText());\r
}\r
+\r
+ @Override\r
+ public List<Text> getExpressionFields() {\r
+ return Arrays.asList(this.expression);\r
+ }\r
}\r
package org.simantics.sysdyn.ui.equation.expressions;\r
\r
+import java.util.Arrays;\r
+import java.util.List;\r
import java.util.Map;\r
\r
import org.eclipse.jface.layout.GridDataFactory;\r
public class WithLookupExpressionViewFactor implements IExpressionViewFactor {\r
\r
\r
- Label expressionLabel;\r
- Text expression;\r
- Label lookupLabel;\r
- Text lookup;\r
- \r
+ private Label expressionLabel;\r
+ private Text expression;\r
+ private Label lookupLabel;\r
+ private Text lookup;\r
private Point lastSelection = new Point(0,0);\r
private Text lastSelectedText = expression;\r
\r
}\r
});\r
\r
- expression.addKeyListener(new ExpressionFieldKeyListener(equation));\r
-\r
lookupLabel = new Label(parent, SWT.NONE);\r
lookupLabel.setFont(FONT);\r
lookupLabel.setText("Lookup\ntable");\r
}\r
});\r
\r
- lookup.addKeyListener(new ExpressionFieldKeyListener(lookupTable));\r
- \r
}\r
\r
@Override\r
if(this.lookup != null && this.lookup.getText() != null)\r
data.put("lookup", this.lookup.getText()); \r
}\r
+\r
+ @Override\r
+ public List<Text> getExpressionFields() {\r
+ return Arrays.asList(this.expression, this.lookup);\r
+ }\r
}
\ No newline at end of file
PARSER_BEGIN(ExpressionParser)\r
package org.simantics.sysdyn.expressionParser;\r
\r
-import java.util.List;\r
-import java.util.ArrayList;\r
+import java.util.Set;\r
+import java.util.HashSet;\r
\r
public class ExpressionParser {\r
- List<List<String>> references = new ArrayList<List<String>>();\r
+ Set<String> references = new HashSet<String>();\r
\r
- public List<List<String>> getReferences() {\r
+ public Set<String> getReferences() {\r
return references;\r
}\r
}\r
// { add_op term } -> ( add_op() term() )*\r
\r
void expr() : {\r
+ references = new HashSet<String>();\r
} {\r
simple_expression() <EOF> \r
| "if" expression() "then" expression() ( "elseif" expression() "then" expression() )*\r
| "false"\r
| "true"\r
| LOOKAHEAD( name() "(" ) name() function_call_args()\r
- | { List<String> reference = new ArrayList<String>(); } \r
- component_reference(reference)\r
- { references.add(reference); }\r
+ | component_reference()\r
// | "(" output_expression_list() ")"\r
// | "[" expression_list() { ";" expression_list() } "]"\r
//| "{" function_arguments() "}"\r
<IDENT> ( "." name() )?\r
}\r
\r
-void component_reference(List<String> reference) : {\r
+void component_reference() : {\r
} {\r
//IDENT [ array_subscripts ] [ "." component_reference ]\r
- <IDENT> { reference.add(token.image); } \r
- ( "." component_reference(reference) )?\r
+ <IDENT> { references.add(token.image); } \r
+ ( "." component_reference() )?\r
}\r
\r
\r
package org.simantics.sysdyn.expressionParser;\r
\r
import java.io.StringReader;\r
-import java.util.List;\r
\r
public class TestExpressionParser {\r
\r
+ static private ExpressionParser parser;\r
+ \r
public static void parse(String string) {\r
- ExpressionParser parser = new ExpressionParser(\r
- new StringReader(string)\r
- );\r
+ parser.ReInit(new StringReader(string));\r
try {\r
parser.expr();\r
- for(List<String> ref : parser.getReferences()) {\r
+ for(String ref : parser.getReferences()) {\r
System.out.println(ref);\r
}\r
} catch (ParseException e) {\r
}\r
\r
public static void main(String[] args) {\r
- parse("1 + m2ma +");\r
+ parser = new ExpressionParser(\r
+ new StringReader("")\r
+ );\r
+ parse("1 + m2ma");\r
+ System.out.println("##");\r
parse("ter2e + moro");\r
- parse("moro * sqr(4.0) + min(3, 2)");\r
+ System.out.println("##");\r
+ parse("moro * sqr(4.0) + min(m2ma, moi)");\r
}\r
}\r