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.KeyEvent;\r
+import org.eclipse.swt.events.KeyListener;\r
import org.eclipse.swt.events.ModifyEvent;\r
import org.eclipse.swt.events.ModifyListener;\r
import org.eclipse.swt.events.MouseEvent;\r
boolean disposed = false;\r
org.eclipse.ui.IPartListener2 focusLostListener; \r
\r
+ NameComposite nameComposite;\r
ShortcutTabs shortcutTabs;\r
ExpressionTypeSelector expressionController; \r
Composite unitComposite;\r
ExpressionComposite expressionComposite; \r
\r
// Dummies\r
- Text nameText;\r
Composite unitsAndRange; \r
Composite emptyComposite;\r
\r
if(unitSelector != null) {\r
unitSelector.save(); \r
}\r
+ \r
+ if(nameComposite != null) {\r
+ nameComposite.save(variable);\r
+ }\r
}\r
\r
}\r
\r
\r
// Upper part of the view: Name (and renaming)\r
- nameText = new Text(parent, SWT.SINGLE | SWT.BORDER);\r
- nameText.setFont(FONT);\r
- GridDataFactory.fillDefaults().grab(true, false).applyTo(nameText);\r
+ nameComposite = new NameComposite(parent, SWT.NONE);\r
+ GridDataFactory.fillDefaults().grab(true, false).applyTo(nameComposite);\r
\r
\r
// Upper part of the view: Expression type\r
expressionController.setExpressionTypes(expressionComposite.getExpressionTypes()); \r
expressionController.select(expressionType);\r
\r
- nameText.setText(result.name);\r
+ nameComposite.setName(result.name);\r
}\r
}); \r
\r
private void addExpressionFieldListeners() {\r
if(expressionComposite.getExpressionViewFactor() != null)\r
for(Text text : expressionComposite.getExpressionViewFactor().getExpressionFields()) {\r
+ final String expression = text.getText();\r
text.addModifyListener(new ModifyListener() {\r
\r
@Override\r
validateExpressionFields();\r
}\r
});\r
+ text.addKeyListener(new KeyListener() {\r
+ \r
+ @Override\r
+ public void keyReleased(KeyEvent e) {\r
+ }\r
+ \r
+ @Override\r
+ public void keyPressed(KeyEvent e) {\r
+ if(e.keyCode == SWT.ESC && expression != null) {\r
+ ((Text)e.widget).setText(expression);\r
+ ((Text)e.widget).setSelection(expression.length());\r
+ } \r
+ }\r
+ });\r
}\r
}\r
\r
for(String s : variables) {\r
no_connections = no_connections + s + " ";\r
}\r
- System.out.println(no_connections);\r
}\r
}\r
\r
--- /dev/null
+package org.simantics.sysdyn.ui.equation;\r
+\r
+import org.eclipse.jface.layout.GridDataFactory;\r
+import org.eclipse.jface.layout.GridLayoutFactory;\r
+import org.eclipse.swt.SWT;\r
+import org.eclipse.swt.events.KeyEvent;\r
+import org.eclipse.swt.events.KeyListener;\r
+import org.eclipse.swt.widgets.Composite;\r
+import org.eclipse.swt.widgets.Text;\r
+import org.simantics.db.Builtins;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.WriteGraph;\r
+import org.simantics.db.common.request.WriteRequest;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.ui.SimanticsUI;\r
+\r
+public class NameComposite extends Composite {\r
+\r
+ Text nameText;\r
+ String originalName;\r
+\r
+\r
+ public NameComposite(Composite parent, int style) {\r
+ super(parent, style);\r
+ GridLayoutFactory.fillDefaults().spacing(3, 3).applyTo(this);\r
+\r
+ nameText = new Text(this, SWT.SINGLE | SWT.BORDER);\r
+ nameText.setFont(EquationView.FONT);\r
+ GridDataFactory.fillDefaults().grab(true, false).applyTo(nameText);\r
+\r
+ nameText.addKeyListener(new KeyListener() {\r
+\r
+ @Override\r
+ public void keyReleased(KeyEvent e) {\r
+\r
+ }\r
+\r
+ @Override\r
+ public void keyPressed(KeyEvent e) {\r
+ if(e.keyCode == SWT.ESC && originalName != null) {\r
+ ((Text)e.widget).setText(originalName);\r
+ ((Text)e.widget).setSelection(originalName.length());\r
+ } \r
+ }\r
+ });\r
+ }\r
+\r
+ public void setName(String name) {\r
+ this.originalName = name;\r
+ this.nameText.setText(name);\r
+ }\r
+\r
+ public void save(final Resource variable) {\r
+ final String nameNew = this.nameText.getText();\r
+\r
+ if(nameNew != null && !nameNew.equals(this.originalName) && variable != null) {\r
+ try {\r
+ SimanticsUI.getSession().syncRequest(new WriteRequest() {\r
+\r
+ @Override\r
+ public void perform(WriteGraph g)\r
+ throws DatabaseException {\r
+ Builtins b = g.getBuiltins();\r
+ g.claimValue(variable, b.HasName, nameNew);\r
+ }\r
+ });\r
+ } catch (DatabaseException e) {\r
+ e.printStackTrace();\r
+ }\r
+ }\r
+ }\r
+\r
+}\r
+++ /dev/null
-package org.simantics.sysdyn.ui.equation.expressions;\r
-\r
-import java.io.StringReader;\r
-\r
-import org.eclipse.swt.SWT;\r
-import org.eclipse.swt.events.KeyEvent;\r
-import org.eclipse.swt.events.KeyListener;\r
-import org.eclipse.swt.graphics.Color;\r
-import org.eclipse.swt.widgets.Text;\r
-import org.simantics.sysdyn.expressionParser.ExpressionParser;\r
-import org.simantics.sysdyn.expressionParser.ParseException;\r
-\r
-public class ExpressionFieldKeyListener implements KeyListener {\r
-\r
- private String originalText; \r
- private ExpressionParser parser;\r
- \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
- if(e.keyCode == SWT.ESC && e.widget instanceof Text) {\r
- ((Text)e.widget).setText(originalText);\r
- ((Text)e.widget).setSelection(originalText.length());\r
- }\r
- }\r
-\r
- @Override\r
- public void keyReleased(KeyEvent e) { \r
- if(e.widget instanceof Text) {\r
- Text text = (Text)e.widget;\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