From: lempinen Date: Mon, 25 Jan 2010 17:00:45 +0000 (+0000) Subject: NameComposite renames variables (the new name doesn't yet go to the visual representa... X-Git-Tag: simantics-1.0~98 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=5ebc6761e265fd8222e1456d1cbd5b3525742235;p=simantics%2Fsysdyn.git NameComposite renames variables (the new name doesn't yet go to the visual representation) git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@13637 ac1ea38d-2e2b-0410-8846-a27921b304fc --- diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/EquationView.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/EquationView.java index b39f0044..6d1fd01f 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/EquationView.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/EquationView.java @@ -12,6 +12,8 @@ import org.eclipse.jface.viewers.IStructuredSelection; import org.eclipse.swt.SWT; import org.eclipse.swt.events.FocusEvent; import org.eclipse.swt.events.FocusListener; +import org.eclipse.swt.events.KeyEvent; +import org.eclipse.swt.events.KeyListener; import org.eclipse.swt.events.ModifyEvent; import org.eclipse.swt.events.ModifyListener; import org.eclipse.swt.events.MouseEvent; @@ -52,6 +54,7 @@ public class EquationView extends ViewPart implements ISelectionListener { boolean disposed = false; org.eclipse.ui.IPartListener2 focusLostListener; + NameComposite nameComposite; ShortcutTabs shortcutTabs; ExpressionTypeSelector expressionController; Composite unitComposite; @@ -59,7 +62,6 @@ public class EquationView extends ViewPart implements ISelectionListener { ExpressionComposite expressionComposite; // Dummies - Text nameText; Composite unitsAndRange; Composite emptyComposite; @@ -86,6 +88,10 @@ public class EquationView extends ViewPart implements ISelectionListener { if(unitSelector != null) { unitSelector.save(); } + + if(nameComposite != null) { + nameComposite.save(variable); + } } } @@ -100,9 +106,8 @@ public class EquationView extends ViewPart implements ISelectionListener { // Upper part of the view: Name (and renaming) - nameText = new Text(parent, SWT.SINGLE | SWT.BORDER); - nameText.setFont(FONT); - GridDataFactory.fillDefaults().grab(true, false).applyTo(nameText); + nameComposite = new NameComposite(parent, SWT.NONE); + GridDataFactory.fillDefaults().grab(true, false).applyTo(nameComposite); // Upper part of the view: Expression type @@ -218,7 +223,7 @@ public class EquationView extends ViewPart implements ISelectionListener { expressionController.setExpressionTypes(expressionComposite.getExpressionTypes()); expressionController.select(expressionType); - nameText.setText(result.name); + nameComposite.setName(result.name); } }); @@ -313,6 +318,7 @@ public class EquationView extends ViewPart implements ISelectionListener { private void addExpressionFieldListeners() { if(expressionComposite.getExpressionViewFactor() != null) for(Text text : expressionComposite.getExpressionViewFactor().getExpressionFields()) { + final String expression = text.getText(); text.addModifyListener(new ModifyListener() { @Override @@ -320,6 +326,20 @@ public class EquationView extends ViewPart implements ISelectionListener { validateExpressionFields(); } }); + text.addKeyListener(new KeyListener() { + + @Override + public void keyReleased(KeyEvent e) { + } + + @Override + public void keyPressed(KeyEvent e) { + if(e.keyCode == SWT.ESC && expression != null) { + ((Text)e.widget).setText(expression); + ((Text)e.widget).setSelection(expression.length()); + } + } + }); } } @@ -357,7 +377,6 @@ public class EquationView extends ViewPart implements ISelectionListener { for(String s : variables) { no_connections = no_connections + s + " "; } - System.out.println(no_connections); } } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/NameComposite.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/NameComposite.java new file mode 100644 index 00000000..48e5d76d --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/NameComposite.java @@ -0,0 +1,73 @@ +package org.simantics.sysdyn.ui.equation; + +import org.eclipse.jface.layout.GridDataFactory; +import org.eclipse.jface.layout.GridLayoutFactory; +import org.eclipse.swt.SWT; +import org.eclipse.swt.events.KeyEvent; +import org.eclipse.swt.events.KeyListener; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Text; +import org.simantics.db.Builtins; +import org.simantics.db.Resource; +import org.simantics.db.WriteGraph; +import org.simantics.db.common.request.WriteRequest; +import org.simantics.db.exception.DatabaseException; +import org.simantics.ui.SimanticsUI; + +public class NameComposite extends Composite { + + Text nameText; + String originalName; + + + public NameComposite(Composite parent, int style) { + super(parent, style); + GridLayoutFactory.fillDefaults().spacing(3, 3).applyTo(this); + + nameText = new Text(this, SWT.SINGLE | SWT.BORDER); + nameText.setFont(EquationView.FONT); + GridDataFactory.fillDefaults().grab(true, false).applyTo(nameText); + + nameText.addKeyListener(new KeyListener() { + + @Override + public void keyReleased(KeyEvent e) { + + } + + @Override + public void keyPressed(KeyEvent e) { + if(e.keyCode == SWT.ESC && originalName != null) { + ((Text)e.widget).setText(originalName); + ((Text)e.widget).setSelection(originalName.length()); + } + } + }); + } + + public void setName(String name) { + this.originalName = name; + this.nameText.setText(name); + } + + public void save(final Resource variable) { + final String nameNew = this.nameText.getText(); + + if(nameNew != null && !nameNew.equals(this.originalName) && variable != null) { + try { + SimanticsUI.getSession().syncRequest(new WriteRequest() { + + @Override + public void perform(WriteGraph g) + throws DatabaseException { + Builtins b = g.getBuiltins(); + g.claimValue(variable, b.HasName, nameNew); + } + }); + } catch (DatabaseException e) { + e.printStackTrace(); + } + } + } + +} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/ExpressionFieldKeyListener.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/ExpressionFieldKeyListener.java deleted file mode 100644 index e9452789..00000000 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/ExpressionFieldKeyListener.java +++ /dev/null @@ -1,50 +0,0 @@ -package org.simantics.sysdyn.ui.equation.expressions; - -import java.io.StringReader; - -import org.eclipse.swt.SWT; -import org.eclipse.swt.events.KeyEvent; -import org.eclipse.swt.events.KeyListener; -import org.eclipse.swt.graphics.Color; -import org.eclipse.swt.widgets.Text; -import org.simantics.sysdyn.expressionParser.ExpressionParser; -import org.simantics.sysdyn.expressionParser.ParseException; - -public class ExpressionFieldKeyListener implements KeyListener { - - private String originalText; - private ExpressionParser parser; - - public ExpressionFieldKeyListener(ExpressionParser parser, Text text) { - this.originalText = text.getText(); - this.parser = parser; - this.handleKeyReleased(text); - } - @Override - public void keyPressed(KeyEvent e) { - if(e.keyCode == SWT.ESC && e.widget instanceof Text) { - ((Text)e.widget).setText(originalText); - ((Text)e.widget).setSelection(originalText.length()); - } - } - - @Override - public void keyReleased(KeyEvent e) { - if(e.widget instanceof Text) { - Text text = (Text)e.widget; - this.handleKeyReleased(text); - } - } - - private void handleKeyReleased(Text text) { - String textString = text.getText(); - this.parser.ReInit(new StringReader(textString)); - - try { - parser.expr(); - text.setBackground(new Color(text.getDisplay(), 255, 255, 255)); - } catch (ParseException e1) { - text.setBackground(new Color(text.getDisplay(), 255, 230, 230)); - } - } -}