package org.simantics.sysdyn.ui.properties;\r
\r
+import org.eclipse.jface.dialogs.IInputValidator;\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.FocusEvent;\r
-import org.eclipse.swt.events.FocusListener;\r
-import org.eclipse.swt.events.VerifyEvent;\r
-import org.eclipse.swt.events.VerifyListener;\r
import org.eclipse.swt.widgets.Composite;\r
import org.eclipse.swt.widgets.Label;\r
-import org.eclipse.swt.widgets.Text;\r
import org.eclipse.ui.IWorkbenchSite;\r
import org.simantics.browsing.ui.swt.PropertyTabContributorImpl;\r
import org.simantics.browsing.ui.swt.widgets.StringPropertyFactory;\r
+import org.simantics.browsing.ui.swt.widgets.StringPropertyModifier;\r
+import org.simantics.browsing.ui.swt.widgets.TrackedText;\r
import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport;\r
import org.simantics.db.Builtins;\r
import org.simantics.db.management.ISessionContext;\r
import org.simantics.sysdyn.SysdynResource;\r
-import org.simantics.sysdyn.ui.properties.widgets.TextWidget;\r
import org.simantics.sysdyn.ui.properties.widgets.factories.DoublePropertyFactory;\r
+import org.simantics.sysdyn.ui.properties.widgets.factories.DoublePropertyModifier;\r
\r
public class ConfigurationTab extends PropertyTabContributorImpl {\r
\r
- TextWidget name, startTime, stopTime;\r
\r
\r
@Override\r
Label label = new Label(composite, SWT.NONE);\r
label.setText("Name");\r
\r
- name = new TextWidget(composite, support, SWT.BORDER);\r
+ TrackedText name = new TrackedText(composite, support, SWT.BORDER);\r
name.setTextFactory(new StringPropertyFactory(Builtins.URIs.HasName));\r
- name.addFocusListener(new SaveListener());\r
- name.addVerifyListener(new VerifyListener() {\r
-\r
+ name.addModifyListener(new StringPropertyModifier(context, Builtins.URIs.HasName));\r
+ name.setInputValidator(new IInputValidator() {\r
+ \r
@Override\r
- public void verifyText(VerifyEvent e) {\r
- e.doit = false;\r
- String s = e.text; \r
- if(!s.contains(" ")) {\r
- e.doit = true;\r
- }\r
+ public String isValid(String newText) {\r
+ if (newText.contains(" "))\r
+ return "Sorry but spaces are not allowed for names right now";\r
+ return null;\r
}\r
});\r
- GridDataFactory.fillDefaults().hint(200, SWT.DEFAULT).applyTo(name.getTextWidget());\r
+\r
+ GridDataFactory.fillDefaults().hint(200, SWT.DEFAULT).applyTo(name.getWidget());\r
\r
label = new Label(composite, SWT.NONE);\r
label.setText("Start time");\r
\r
- startTime = new TextWidget(composite, support, SWT.BORDER | SWT.RIGHT);\r
+ TrackedText startTime = new TrackedText(composite, support, SWT.BORDER | SWT.RIGHT);\r
startTime.setTextFactory(new DoublePropertyFactory(SysdynResource.URIs.HasStartTime));\r
- startTime.addVerifyListener(new DoubleVerify());\r
- startTime.addFocusListener(new SaveListener());\r
- GridDataFactory.fillDefaults().hint(200, SWT.DEFAULT).applyTo(startTime.getTextWidget());\r
+ startTime.addModifyListener(new DoublePropertyModifier(context, SysdynResource.URIs.HasStartTime));\r
+ startTime.setInputValidator(new DoubleValidator());\r
+ GridDataFactory.fillDefaults().hint(200, SWT.DEFAULT).applyTo(startTime.getWidget());\r
\r
label = new Label(composite, SWT.NONE);\r
label.setText("Stop time");\r
\r
- stopTime = new TextWidget(composite, support, SWT.BORDER | SWT.RIGHT);\r
+ TrackedText stopTime = new TrackedText(composite, support, SWT.BORDER | SWT.RIGHT);\r
stopTime.setTextFactory(new DoublePropertyFactory(SysdynResource.URIs.HasStopTime));\r
- stopTime.addVerifyListener(new DoubleVerify());\r
- stopTime.addFocusListener(new SaveListener());\r
- GridDataFactory.fillDefaults().hint(200, SWT.DEFAULT).applyTo(stopTime.getTextWidget());\r
+ stopTime.addModifyListener(new DoublePropertyModifier(context, SysdynResource.URIs.HasStopTime));\r
+ stopTime.setInputValidator(new DoubleValidator());\r
+ GridDataFactory.fillDefaults().hint(200, SWT.DEFAULT).applyTo(stopTime.getWidget());\r
+\r
\r
}\r
\r
- private class DoubleVerify implements VerifyListener {\r
+ private class DoubleValidator implements IInputValidator {\r
\r
@Override\r
- public void verifyText(VerifyEvent e) {\r
- e.doit = true;\r
- for(int i = 0; i < e.text.length(); i++){\r
- if(!Character.isDigit(e.text.charAt(i))){\r
- if(e.text.charAt(i) != '.') {\r
- e.doit = false;\r
- break; \r
- } else if(e.text.indexOf('.') != e.text.lastIndexOf('.')) {\r
- e.doit = false;\r
- break; \r
- } else if(((Text)e.widget).getText().substring(0, e.start).contains(".") ||\r
- ((Text)e.widget).getText().substring(e.end, ((Text)e.widget).getText().length()).contains(".")) {\r
- e.doit = false;\r
- break; \r
+ public String isValid(String newText) {\r
+ for(int i = 0; i < newText.length(); i++){\r
+ if(!Character.isDigit(newText.charAt(i))){\r
+ if(newText.charAt(i) != '.') {\r
+ return "Invalid character '" + newText.charAt(i) + "'";\r
+ } else if(newText.indexOf('.') != newText.lastIndexOf('.')) {\r
+ return "There can be only one '.'";\r
}\r
- } \r
+ }\r
}\r
+ return null;\r
}\r
- }\r
- \r
- private class SaveListener implements FocusListener {\r
-\r
- @Override\r
- public void focusGained(FocusEvent e) {\r
- }\r
-\r
- @Override\r
- public void focusLost(FocusEvent e) {\r
- save();\r
- }\r
- \r
- }\r
- \r
- private void save() {\r
- this.name.save();\r
- this.startTime.save();\r
- this.stopTime.save();\r
- }\r
- \r
- \r
+ } \r
}\r
package org.simantics.sysdyn.ui.properties;\r
\r
+import java.util.Collection;\r
+import java.util.HashMap;\r
+import java.util.Map;\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.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.Point;\r
import org.eclipse.swt.widgets.Composite;\r
import org.eclipse.swt.widgets.Label;\r
import org.eclipse.ui.IWorkbenchSite;\r
import org.simantics.browsing.ui.swt.PropertyTabContributorImpl;\r
import org.simantics.browsing.ui.swt.widgets.StringPropertyFactory;\r
+import org.simantics.browsing.ui.swt.widgets.StringPropertyModifier;\r
+import org.simantics.browsing.ui.swt.widgets.TrackedCombo;\r
+import org.simantics.browsing.ui.swt.widgets.TrackedText;\r
+import org.simantics.browsing.ui.swt.widgets.impl.ComboModifyListenerImpl;\r
+import org.simantics.browsing.ui.swt.widgets.impl.ReadFactoryImpl;\r
+import org.simantics.browsing.ui.swt.widgets.impl.TextModifyListener;\r
+import org.simantics.browsing.ui.swt.widgets.impl.TrackedModifyEvent;\r
import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport;\r
import org.simantics.db.Builtins;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.WriteGraph;\r
+import org.simantics.db.exception.DatabaseException;\r
import org.simantics.db.management.ISessionContext;\r
import org.simantics.sysdyn.SysdynResource;\r
-import org.simantics.sysdyn.ui.properties.widgets.ExpressionTypeWidget;\r
+import org.simantics.sysdyn.ui.properties.widgets.ExpressionTypes;\r
import org.simantics.sysdyn.ui.properties.widgets.ExpressionWidget;\r
-import org.simantics.sysdyn.ui.properties.widgets.VariableNameWidget;\r
import org.simantics.sysdyn.ui.properties.widgets.ShortcutTabWidget;\r
-import org.simantics.sysdyn.ui.properties.widgets.UnitWidget;\r
+import org.simantics.sysdyn.ui.properties.widgets.ExpressionTypes.ExpressionType;\r
\r
public class EquationTab extends PropertyTabContributorImpl {\r
\r
- VariableNameWidget nameWidget;\r
- ExpressionTypeWidget expressionTypeWidget;\r
- UnitWidget unitWidget;\r
+ TrackedCombo expressionTypeCombo, unitCombo;\r
ShortcutTabWidget shortcutTabWidget;\r
ExpressionWidget expressionWidget;\r
\r
Composite composite = new Composite(body, SWT.NONE);\r
GridDataFactory.fillDefaults().grab(true, true).applyTo(composite);\r
GridLayoutFactory.fillDefaults().margins(3, 3).numColumns(6).applyTo(composite);\r
- nameWidget = new VariableNameWidget(composite, support, SWT.BORDER);\r
- nameWidget.setTextFactory(new StringPropertyFactory(Builtins.URIs.HasName));\r
+ TrackedText nameText = new TrackedText(composite, support, SWT.BORDER);\r
+ nameText.setTextFactory(new StringPropertyFactory(Builtins.URIs.HasName));\r
+ nameText.addModifyListener(new StringPropertyModifier(context, Builtins.URIs.HasName));\r
+ GridDataFactory.fillDefaults().grab(true, false).applyTo(nameText.getWidget());\r
\r
\r
Label label = new Label(composite, SWT.SINGLE );\r
label.setText("Type:");\r
GridDataFactory.fillDefaults().align(SWT.END, SWT.CENTER).applyTo(label);\r
\r
- expressionTypeWidget = new ExpressionTypeWidget(composite, support, SWT.DROP_DOWN | SWT.BORDER | SWT.READ_ONLY);\r
-\r
+ expressionTypeCombo = new TrackedCombo(composite, support, SWT.DROP_DOWN | SWT.BORDER | SWT.READ_ONLY);\r
\r
+ \r
label = new Label(composite, SWT.SINGLE );\r
label.setText("Unit:");\r
GridDataFactory.fillDefaults().align(SWT.END, SWT.CENTER).applyTo(label);\r
\r
- unitWidget = new UnitWidget(composite, support, SWT.DROP_DOWN | SWT.BORDER);\r
- unitWidget.setTextFactory(new StringPropertyFactory(SysdynResource.URIs.HasUnit));\r
\r
+ unitCombo = new TrackedCombo(composite, support, SWT.DROP_DOWN | SWT.BORDER);\r
+ \r
shortcutTabWidget = new ShortcutTabWidget(composite, support, SWT.NONE);\r
\r
\r
GridDataFactory.fillDefaults().grab(true, true).span(5, 1).applyTo(expressionComposite);\r
expressionWidget = new ExpressionWidget(expressionComposite, support, SWT.NONE);\r
expressionWidget.setVariableTable(shortcutTabWidget.getVariableTable());\r
- \r
+\r
addListeners();\r
}\r
\r
private void addListeners() {\r
+ \r
+ expressionTypeCombo.setItemFactory(new ReadFactoryImpl<Resource, Map<String,Object>>() {\r
+\r
+ @Override\r
+ public Map<String, Object> perform(ReadGraph graph, final Resource input) throws DatabaseException {\r
+\r
+ Map<String, Object> map = new HashMap<String, Object>();\r
+ SysdynResource sr = SysdynResource.getInstance(graph);\r
+\r
+ final ExpressionType[] expressionTypes;\r
+ if(graph.isInstanceOf(input, sr.Auxiliary))\r
+ expressionTypes = ExpressionTypes.auxiliaryExpressions;\r
+ else if(graph.isInstanceOf(input, sr.Stock))\r
+ expressionTypes = ExpressionTypes.stockExpressions;\r
+ else if(graph.isInstanceOf(input, sr.Valve))\r
+ expressionTypes = ExpressionTypes.valveExpressions;\r
+ else\r
+ expressionTypes = new ExpressionType[] {};\r
+ \r
+ for(ExpressionType et : expressionTypes) {\r
+ map.put(et.toString(), et);\r
+ }\r
+ return map;\r
+ }\r
+ });\r
\r
- expressionTypeWidget.addSelectionListener(new SelectionListener() {\r
+ expressionTypeCombo.setSelectionFactory(new ReadFactoryImpl<Resource, String>() {\r
\r
@Override\r
- public void widgetSelected(SelectionEvent e) {\r
- expressionWidget.displayExpression(expressionTypeWidget.getSelecetedType(), false);\r
- expressionWidget.save();\r
+ public String perform(ReadGraph graph, final Resource input) throws DatabaseException {\r
+ return ExpressionTypes.getExpressionTypeForVariable(graph, input).toString();\r
+ }\r
+ });\r
+ \r
+ expressionTypeCombo.addModifyListener(new TextModifyListener() {\r
+ \r
+ @Override\r
+ public void modifyText(TrackedModifyEvent e) {\r
+ System.out.println(e.getText());\r
+ expressionWidget.displayExpression(e.getText(), false);\r
+ expressionWidget.save(); \r
+ }\r
+ });\r
+\r
+ \r
+ unitCombo.setItemFactory(new ReadFactoryImpl<Resource, Map<String,Object>>() {\r
+\r
+ @Override\r
+ public Map<String, Object> perform(ReadGraph graph, final Resource input) throws DatabaseException {\r
+ Map<String, Object> map = new HashMap<String, Object>();\r
+\r
+ SysdynResource sr = SysdynResource.getInstance(graph);\r
+ Builtins b = graph.getBuiltins();\r
+ Resource model = graph.getPossibleObject(input, b.PartOf);\r
+ if (model != null) {\r
+ Collection<Resource> variables = graph.getObjects(model, b.ConsistsOf);\r
+ for(Resource v : variables) {\r
+ Object unit = graph.getPossibleRelatedValue(v, sr.HasUnit);\r
+ if (unit != null && !map.keySet().contains((String) unit)) {\r
+ map.put((String)unit, (String)unit);\r
+\r
+ }\r
+ }\r
+ }\r
+ return map;\r
+ }\r
+ });\r
+ unitCombo.setSelectionFactory(new ReadFactoryImpl<Resource, String>() {\r
+\r
+ @Override\r
+ public String perform(ReadGraph graph, final Resource input) throws DatabaseException {\r
+ String unit = graph.getPossibleRelatedValue(input, SysdynResource.getInstance(graph).HasUnit);\r
+ if(unit == null)\r
+ return "";\r
+ else \r
+ return unit;\r
}\r
+ });\r
+\r
+ unitCombo.addModifyListener(new ComboModifyListenerImpl<Resource>() {\r
\r
@Override\r
- public void widgetDefaultSelected(SelectionEvent e) {\r
+ public void applyText(WriteGraph graph, Resource input, String text) throws DatabaseException {\r
+ graph.claimValue(input, SysdynResource.getInstance(graph).HasUnit, text);\r
}\r
+ \r
});\r
\r
+\r
shortcutTabWidget.addMouseListener(new MouseListener(){\r
\r
@Override\r
expressionWidget.validateFields();\r
}\r
});\r
- \r
+\r
expressionWidget.addFocusListener(new FocusListener() {\r
- \r
+\r
@Override\r
public void focusLost(FocusEvent e) {\r
expressionWidget.save();\r
}\r
- \r
+\r
@Override\r
public void focusGained(FocusEvent e) {\r
expressionWidget.validateFields();\r
package org.simantics.sysdyn.ui.properties;\r
\r
+import org.eclipse.jface.dialogs.IInputValidator;\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.FocusEvent;\r
-import org.eclipse.swt.events.FocusListener;\r
-import org.eclipse.swt.events.VerifyEvent;\r
-import org.eclipse.swt.events.VerifyListener;\r
import org.eclipse.swt.widgets.Composite;\r
import org.eclipse.swt.widgets.Group;\r
import org.eclipse.swt.widgets.Label;\r
-import org.eclipse.swt.widgets.Text;\r
import org.eclipse.ui.IWorkbenchSite;\r
import org.simantics.browsing.ui.swt.PropertyTabContributorImpl;\r
import org.simantics.browsing.ui.swt.widgets.StringPropertyFactory;\r
+import org.simantics.browsing.ui.swt.widgets.StringPropertyModifier;\r
+import org.simantics.browsing.ui.swt.widgets.TrackedText;\r
import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport;\r
import org.simantics.db.Builtins;\r
import org.simantics.db.management.ISessionContext;\r
import org.simantics.sysdyn.SysdynResource;\r
-import org.simantics.sysdyn.ui.properties.widgets.TextWidget;\r
import org.simantics.sysdyn.ui.properties.widgets.factories.DoublePropertyFactory;\r
+import org.simantics.sysdyn.ui.properties.widgets.factories.DoublePropertyModifier;\r
\r
public class VariableInformationTab extends PropertyTabContributorImpl {\r
\r
- TextWidget information, rangeStart, rangeEnd, rangeStep;\r
- \r
@Override\r
public void createControls(Composite body, IWorkbenchSite site, ISessionContext context, WidgetSupport support) {\r
Composite composite = new Composite(body, SWT.NONE);\r
GridDataFactory.fillDefaults().grab(true, true).applyTo(composite);\r
GridLayoutFactory.fillDefaults().margins(3, 3).applyTo(composite);\r
- \r
+\r
Group informationGroup = new Group(composite, SWT.SHADOW_ETCHED_IN);\r
informationGroup.setText("Information");\r
GridDataFactory.fillDefaults().grab(false, true).applyTo(informationGroup);\r
GridLayoutFactory.fillDefaults().margins(3, 3).applyTo(informationGroup);\r
- \r
- information = new TextWidget(informationGroup, support, SWT.MULTI | SWT.BORDER);\r
+\r
+ TrackedText information = new TrackedText(informationGroup, support, SWT.MULTI | SWT.BORDER);\r
information.setTextFactory(new StringPropertyFactory(Builtins.URIs.HasDescription));\r
- information.addFocusListener(new SaveListener());\r
- GridDataFactory.fillDefaults().grab(true, true).applyTo(information.getTextWidget());\r
- \r
- \r
+ information.addModifyListener(new StringPropertyModifier(context, Builtins.URIs.HasDescription));\r
+ GridDataFactory.fillDefaults().grab(true, true).applyTo(information.getWidget());\r
+\r
Group rangeGroup = new Group(composite, SWT.SHADOW_ETCHED_IN);\r
rangeGroup.setText("Range");\r
GridDataFactory.fillDefaults().applyTo(rangeGroup);\r
GridLayoutFactory.fillDefaults().margins(3, 3).numColumns(6).applyTo(rangeGroup);\r
- \r
+\r
Label label = new Label(rangeGroup, SWT.NONE);\r
label.setText("Start");\r
- \r
- rangeStart = new TextWidget(rangeGroup, support, SWT.BORDER | SWT.RIGHT);\r
+\r
+ TrackedText rangeStart = new TrackedText(rangeGroup, support, SWT.RIGHT | SWT.BORDER);\r
rangeStart.setTextFactory(new DoublePropertyFactory(SysdynResource.URIs.HasRangeStart));\r
- rangeStart.addFocusListener(new SaveListener());\r
- rangeStart.addVerifyListener(new DoubleVerify());\r
+ rangeStart.addModifyListener(new DoublePropertyModifier(context, SysdynResource.URIs.HasRangeStart));\r
+ rangeStart.setInputValidator(new DoubleValidator());\r
+ GridDataFactory.fillDefaults().grab(true, false).applyTo(rangeStart.getWidget());\r
+\r
\r
label = new Label(rangeGroup, SWT.NONE);\r
label.setText("End");\r
- \r
- rangeEnd = new TextWidget(rangeGroup, support, SWT.BORDER | SWT.RIGHT);\r
+\r
+\r
+ TrackedText rangeEnd = new TrackedText(rangeGroup, support, SWT.RIGHT | SWT.BORDER);\r
rangeEnd.setTextFactory(new DoublePropertyFactory(SysdynResource.URIs.HasRangeEnd));\r
- rangeEnd.addFocusListener(new SaveListener());\r
- rangeEnd.addVerifyListener(new DoubleVerify());\r
+ rangeEnd.addModifyListener(new DoublePropertyModifier(context, SysdynResource.URIs.HasRangeEnd));\r
+ rangeEnd.setInputValidator(new DoubleValidator());\r
+ GridDataFactory.fillDefaults().grab(true, false).applyTo(rangeEnd.getWidget());\r
\r
label = new Label(rangeGroup, SWT.NONE);\r
label.setText("Step");\r
- \r
- rangeStep = new TextWidget(rangeGroup, support, SWT.BORDER | SWT.RIGHT);\r
+\r
+ TrackedText rangeStep = new TrackedText(rangeGroup, support, SWT.RIGHT | SWT.BORDER);\r
rangeStep.setTextFactory(new DoublePropertyFactory(SysdynResource.URIs.HasRangeStep));\r
- rangeStep.addFocusListener(new SaveListener());\r
- rangeStep.addVerifyListener(new DoubleVerify());\r
- \r
- }\r
- \r
- \r
- private class SaveListener implements FocusListener {\r
+ rangeStep.addModifyListener(new DoublePropertyModifier(context, SysdynResource.URIs.HasRangeStep));\r
+ rangeStep.setInputValidator(new DoubleValidator());\r
+ GridDataFactory.fillDefaults().grab(true, false).applyTo(rangeStep.getWidget());\r
\r
- @Override\r
- public void focusGained(FocusEvent e) {\r
- }\r
\r
- @Override\r
- public void focusLost(FocusEvent e) {\r
- save();\r
- }\r
- \r
- }\r
- \r
- private void save() {\r
- this.information.save();\r
- this.rangeStart.save();\r
- this.rangeEnd.save();\r
- this.rangeStep.save();\r
}\r
\r
- private class DoubleVerify implements VerifyListener {\r
+ private class DoubleValidator implements IInputValidator {\r
\r
@Override\r
- public void verifyText(VerifyEvent e) {\r
- e.doit = true;\r
- for(int i = 0; i < e.text.length(); i++){\r
- if(!Character.isDigit(e.text.charAt(i))){\r
- if(e.text.charAt(i) != '.') {\r
- e.doit = false;\r
- break; \r
- } else if(e.text.indexOf('.') != e.text.lastIndexOf('.')) {\r
- e.doit = false;\r
- break; \r
- } else if(((Text)e.widget).getText().substring(0, e.start).contains(".") ||\r
- ((Text)e.widget).getText().substring(e.end, ((Text)e.widget).getText().length()).contains(".")) {\r
- e.doit = false;\r
- break; \r
+ public String isValid(String newText) {\r
+ for(int i = 0; i < newText.length(); i++){\r
+ if(!Character.isDigit(newText.charAt(i))){\r
+ if(newText.charAt(i) != '.') {\r
+ return "Invalid character '" + newText.charAt(i) + "'";\r
+ } else if(newText.indexOf('.') != newText.lastIndexOf('.')) {\r
+ return "There can be only one '.'";\r
}\r
- } \r
+ }\r
}\r
+ return null;\r
}\r
}\r
+\r
}\r
+++ /dev/null
-package org.simantics.sysdyn.ui.properties.widgets;\r
-\r
-\r
-import org.eclipse.jface.layout.GridDataFactory;\r
-import org.eclipse.jface.viewers.IStructuredSelection;\r
-import org.eclipse.swt.events.SelectionListener;\r
-import org.eclipse.swt.graphics.FontMetrics;\r
-import org.eclipse.swt.graphics.GC;\r
-import org.eclipse.swt.widgets.Combo;\r
-import org.eclipse.swt.widgets.Composite;\r
-import org.simantics.browsing.ui.swt.widgets.impl.Widget;\r
-import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport;\r
-import org.simantics.db.ReadGraph;\r
-import org.simantics.db.Resource;\r
-import org.simantics.db.common.request.ReadRequest;\r
-import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.db.management.ISessionContext;\r
-import org.simantics.sysdyn.SysdynResource;\r
-import org.simantics.sysdyn.ui.properties.widgets.ExpressionTypes.ExpressionType;\r
-import org.simantics.ui.SimanticsUI;\r
-import org.simantics.utils.ui.ISelectionUtils;\r
-\r
-public class ExpressionTypeWidget implements Widget {\r
-\r
- Combo typeCombo;\r
-\r
- public ExpressionTypeWidget(Composite parent, WidgetSupport support, int style) {\r
- support.register(this);\r
-\r
- typeCombo = new Combo(parent, style);\r
-\r
- int columns = 6;\r
- GC gc = new GC (typeCombo);\r
- FontMetrics fm = gc.getFontMetrics ();\r
- int width = columns * fm.getAverageCharWidth ();\r
- int height = fm.getHeight ();\r
- gc.dispose (); \r
- GridDataFactory.fillDefaults().hint(typeCombo.computeSize(width, height)).applyTo(typeCombo);\r
- }\r
-\r
- @Override\r
- public void setInput(ISessionContext context, Object input) {\r
- if(input instanceof IStructuredSelection) {\r
- final Resource variable = ISelectionUtils.filterSingleSelection((IStructuredSelection)input, Resource.class);\r
- if(variable != null) {\r
- final ExpressionType et = ExpressionTypes.getExpressionTypeForVariable(variable);\r
- SimanticsUI.getSession().asyncRequest(new ReadRequest() {\r
- @Override\r
- public void run(ReadGraph graph) throws DatabaseException {\r
- SysdynResource sr = SysdynResource.getInstance(graph);\r
-\r
- final ExpressionType[] expressionTypes;\r
- if(graph.isInstanceOf(variable, sr.Auxiliary))\r
- expressionTypes = ExpressionTypes.auxiliaryExpressions;\r
- else if(graph.isInstanceOf(variable, sr.Stock))\r
- expressionTypes = ExpressionTypes.stockExpressions;\r
- else if(graph.isInstanceOf(variable, sr.Valve))\r
- expressionTypes = ExpressionTypes.valveExpressions;\r
- else\r
- expressionTypes = new ExpressionType[] {};\r
-\r
-\r
- typeCombo.getDisplay().asyncExec(new Runnable() {\r
-\r
- @Override\r
- public void run() {\r
- if(!typeCombo.isDisposed()){\r
- setExpressionTypes(expressionTypes);\r
- setExpressionType(et); \r
- }\r
- }\r
- });\r
-\r
- }\r
-\r
- });\r
-\r
- }\r
- }\r
- }\r
-\r
- private void setExpressionTypes(ExpressionType[] expressionTypes) {\r
- if(expressionTypes != null) {\r
- for(ExpressionType et : expressionTypes) {\r
- typeCombo.add(et.toString());\r
- }\r
- }\r
- }\r
-\r
- public void setExpressionType(ExpressionType expressionType) {\r
- int index = typeCombo.indexOf(expressionType.toString());\r
- if (index > -1) {\r
- typeCombo.select(index);\r
- }\r
- }\r
-\r
-\r
- public void addSelectionListener(SelectionListener listener) {\r
- this.typeCombo.addSelectionListener(listener);\r
- }\r
-\r
- public String getSelecetedType() {\r
- return this.typeCombo.getItem(this.typeCombo.getSelectionIndex());\r
- }\r
-}\r
import org.simantics.ui.SimanticsUI;\r
\r
public class ExpressionTypes {\r
- \r
+\r
public static enum ExpressionType {Auxiliary, Parameter, Constant, Lookup, WithLookup, Stock, Delay, Empty};\r
\r
public static ExpressionType[] auxiliaryExpressions = new ExpressionType[] {\r
- ExpressionType.Parameter, \r
- ExpressionType.Auxiliary, \r
- ExpressionType.Constant, \r
- ExpressionType.Lookup, \r
- ExpressionType.WithLookup};\r
+ ExpressionType.Parameter, \r
+ ExpressionType.Auxiliary, \r
+ ExpressionType.Constant, \r
+ ExpressionType.Lookup, \r
+ ExpressionType.WithLookup};\r
\r
public static ExpressionType[] valveExpressions = new ExpressionType[] {\r
- ExpressionType.Auxiliary, \r
- ExpressionType.Parameter, \r
- ExpressionType.Constant, \r
- ExpressionType.WithLookup};\r
+ ExpressionType.Auxiliary, \r
+ ExpressionType.Parameter, \r
+ ExpressionType.Constant, \r
+ ExpressionType.WithLookup};\r
\r
public static ExpressionType[] stockExpressions = new ExpressionType[] {\r
- ExpressionType.Stock};\r
- \r
+ ExpressionType.Stock};\r
+\r
public static ExpressionType getExpressionTypeForVariable(final Resource variable) {\r
try {\r
return SimanticsUI.getSession().syncRequest(new Read<ExpressionType>() {\r
\r
@Override\r
public ExpressionType perform(ReadGraph graph) throws DatabaseException {\r
- SysdynResource sr = SysdynResource.getInstance(graph);\r
- final ExpressionType et;\r
- Resource expression = graph.getPossibleObject(variable, sr.HasExpression);\r
- if(expression == null) {\r
- if(graph.isInstanceOf(variable, sr.Auxiliary)) {\r
- et = ExpressionType.Auxiliary;\r
- }\r
- else if(graph.isInstanceOf(variable, sr.Valve)) {\r
- et = ExpressionType.Auxiliary;\r
- }\r
- else if(graph.isInstanceOf(variable, sr.Stock)) {\r
- et = ExpressionType.Stock;\r
- } else\r
- et = ExpressionType.Empty;\r
- } else {\r
- if(graph.isInstanceOf(expression, sr.NormalExpression)) {\r
- et = ExpressionType.Auxiliary;\r
- } else if (graph.isInstanceOf(expression, sr.StockExpression)) {\r
- et = ExpressionType.Stock;\r
- } else if (graph.isInstanceOf(expression, sr.ParameterExpression)) {\r
- et = ExpressionType.Parameter;\r
- } else if (graph.isInstanceOf(expression, sr.ConstantExpression)) {\r
- et = ExpressionType.Constant;\r
- } else if (graph.isInstanceOf(expression, sr.DelayExpression)) {\r
- et = ExpressionType.Delay;\r
- } else if (graph.isInstanceOf(expression, sr.LookupExpression)) {\r
- et = ExpressionType.Lookup;\r
- } else if (graph.isInstanceOf(expression, sr.WithLookupExpression)) {\r
- et = ExpressionType.WithLookup;\r
- } else {\r
- et = ExpressionType.Empty;\r
- }\r
- }\r
- return et;\r
+ return getExpressionTypeForVariable(graph, variable);\r
}\r
});\r
} catch (DatabaseException e) {\r
\r
}\r
\r
+ public static ExpressionType getExpressionTypeForVariable(ReadGraph graph, final Resource variable) {\r
+ ExpressionType et = null;\r
+ try {\r
+ SysdynResource sr = SysdynResource.getInstance(graph);\r
+\r
+ Resource expression = graph.getPossibleObject(variable, sr.HasExpression);\r
+ if(expression == null) {\r
+ if(graph.isInstanceOf(variable, sr.Auxiliary)) {\r
+ et = ExpressionType.Auxiliary;\r
+ }\r
+ else if(graph.isInstanceOf(variable, sr.Valve)) {\r
+ et = ExpressionType.Auxiliary;\r
+ }\r
+ else if(graph.isInstanceOf(variable, sr.Stock)) {\r
+ et = ExpressionType.Stock;\r
+ } else\r
+ et = ExpressionType.Empty;\r
+ } else {\r
+ if(graph.isInstanceOf(expression, sr.NormalExpression)) {\r
+ et = ExpressionType.Auxiliary;\r
+ } else if (graph.isInstanceOf(expression, sr.StockExpression)) {\r
+ et = ExpressionType.Stock;\r
+ } else if (graph.isInstanceOf(expression, sr.ParameterExpression)) {\r
+ et = ExpressionType.Parameter;\r
+ } else if (graph.isInstanceOf(expression, sr.ConstantExpression)) {\r
+ et = ExpressionType.Constant;\r
+ } else if (graph.isInstanceOf(expression, sr.DelayExpression)) {\r
+ et = ExpressionType.Delay;\r
+ } else if (graph.isInstanceOf(expression, sr.LookupExpression)) {\r
+ et = ExpressionType.Lookup;\r
+ } else if (graph.isInstanceOf(expression, sr.WithLookupExpression)) {\r
+ et = ExpressionType.WithLookup;\r
+ } else {\r
+ et = ExpressionType.Empty;\r
+ }\r
+ }\r
+ } catch (DatabaseException e) {\r
+ e.printStackTrace();\r
+ }\r
+ return et;\r
+ }\r
+\r
}\r
+++ /dev/null
-package org.simantics.sysdyn.ui.properties.widgets;\r
-\r
-import org.eclipse.jface.viewers.ISelection;\r
-import org.eclipse.swt.SWT;\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.VerifyListener;\r
-import org.eclipse.swt.widgets.Composite;\r
-import org.eclipse.swt.widgets.Text;\r
-import org.simantics.browsing.ui.swt.widgets.StringPropertyFactory;\r
-import org.simantics.browsing.ui.swt.widgets.impl.ReadFactory;\r
-import org.simantics.browsing.ui.swt.widgets.impl.ReadFactoryImpl;\r
-import org.simantics.browsing.ui.swt.widgets.impl.Widget;\r
-import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport;\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.db.management.ISessionContext;\r
-import org.simantics.db.procedure.Listener;\r
-import org.simantics.sysdyn.ui.properties.widgets.factories.DoublePropertyFactory;\r
-import org.simantics.ui.SimanticsUI;\r
-import org.simantics.utils.datastructures.Triple;\r
-import org.simantics.utils.ui.ISelectionUtils;\r
-\r
-public class TextWidget implements Widget {\r
-\r
- private Text textWidget;\r
- private String originalText = "";\r
- private ReadFactory<?, String> readFactory;\r
- private Resource resource;\r
-\r
- public TextWidget(Composite parent, WidgetSupport support, int style) {\r
- support.register(this);\r
- textWidget = new Text(parent, style);\r
- textWidget.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) {\r
- Text text = (Text)e.widget;\r
- text.setText(originalText);\r
- text.setSelection(text.getCharCount());\r
- }\r
- }\r
- });\r
- }\r
-\r
- public void setTextFactory(ReadFactory<?, String> textFactory) {\r
- this.readFactory = textFactory;\r
- }\r
-\r
- @Override\r
- public void setInput(ISessionContext context, Object input) {\r
- if(input instanceof ISelection)\r
- resource = ISelectionUtils.filterSingleSelection((ISelection)input, Resource.class);\r
-\r
- if(readFactory != null) {\r
- readFactory.listen(context, input, new Listener<String>() {\r
-\r
- @Override\r
- public void exception(Throwable t) {\r
- t.printStackTrace();\r
- }\r
-\r
- @Override\r
- public void execute(final String text) {\r
-\r
- if(text == null) return;\r
- if (isDisposed())\r
- return;\r
-\r
- textWidget.getDisplay().asyncExec(new Runnable() {\r
-\r
- @Override\r
- public void run() {\r
-\r
- if(isDisposed()) return;\r
- textWidget.setText(text);\r
- originalText = text;\r
-\r
- }\r
-\r
- });\r
- }\r
-\r
- @Override\r
- public boolean isDisposed() {\r
- return textWidget.isDisposed();\r
- }\r
-\r
- });\r
- }\r
-\r
- }\r
-\r
- @SuppressWarnings("unchecked")\r
- public void save() {\r
- if(originalText.equals(textWidget.getText())) return;\r
-\r
- final String text = textWidget.getText().trim();\r
-\r
- SimanticsUI.getSession().asyncRequest(new WriteRequest() {\r
-\r
- @Override\r
- public void perform(WriteGraph graph) throws DatabaseException {\r
-\r
- Triple triple = null;\r
- Object object = null;\r
- triple = (Triple) ((ReadFactoryImpl)readFactory).getIdentity(null);\r
- if(triple == null) return;\r
-\r
- if(!text.equals("")) {\r
- if(readFactory instanceof DoublePropertyFactory) {\r
- object = Double.parseDouble(text);\r
- } else if (readFactory instanceof StringPropertyFactory) {\r
- object = text;\r
- }\r
- graph.claimValue(resource, graph.getResource((String)triple.second), object);\r
- } else {\r
- Resource relation = graph.getResource((String)triple.second);\r
- if (graph.hasStatement(resource, relation))\r
- graph.deny(resource, relation);\r
- }\r
- }\r
- });\r
-\r
- }\r
-\r
- public void addKeyListener(KeyListener listener) {\r
- this.textWidget.addKeyListener(listener);\r
- }\r
-\r
- public void addVerifyListener(VerifyListener listener) {\r
- this.textWidget.addVerifyListener(listener);\r
- }\r
-\r
- public void addFocusListener(FocusListener listener) {\r
- this.textWidget.addFocusListener(listener);\r
- }\r
-\r
- public String getText() {\r
- return textWidget.getText();\r
- }\r
-\r
- public Text getTextWidget() {\r
- return textWidget;\r
- }\r
-}\r
+++ /dev/null
-package org.simantics.sysdyn.ui.properties.widgets;\r
-\r
-import java.util.ArrayList;\r
-import java.util.Collection;\r
-\r
-import org.eclipse.jface.layout.GridDataFactory;\r
-import org.eclipse.jface.viewers.ISelection;\r
-import org.eclipse.jface.viewers.IStructuredSelection;\r
-import org.eclipse.swt.SWT;\r
-import org.eclipse.swt.events.FocusAdapter;\r
-import org.eclipse.swt.events.FocusEvent;\r
-import org.eclipse.swt.events.KeyEvent;\r
-import org.eclipse.swt.events.KeyListener;\r
-import org.eclipse.swt.graphics.FontMetrics;\r
-import org.eclipse.swt.graphics.GC;\r
-import org.eclipse.swt.widgets.Combo;\r
-import org.eclipse.swt.widgets.Composite;\r
-import org.simantics.browsing.ui.swt.widgets.impl.ReadFactory;\r
-import org.simantics.browsing.ui.swt.widgets.impl.Widget;\r
-import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport;\r
-import org.simantics.db.Builtins;\r
-import org.simantics.db.ReadGraph;\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.db.management.ISessionContext;\r
-import org.simantics.db.procedure.Listener;\r
-import org.simantics.db.request.Read;\r
-import org.simantics.sysdyn.SysdynResource;\r
-import org.simantics.ui.SimanticsUI;\r
-import org.simantics.utils.ui.ExceptionUtils;\r
-import org.simantics.utils.ui.ISelectionUtils;\r
-\r
-public class UnitWidget implements Widget {\r
-\r
- Combo unitCombo;\r
- private ReadFactory<?, String> textFactory;\r
- Resource variable;\r
- String unit;\r
-\r
- public UnitWidget(Composite parent, WidgetSupport support, int style) {\r
- support.register(this);\r
-\r
- unitCombo = new Combo(parent, style);\r
-\r
- int columns = 6;\r
- GC gc = new GC (unitCombo);\r
- FontMetrics fm = gc.getFontMetrics ();\r
- int width = columns * fm.getAverageCharWidth ();\r
- int height = fm.getHeight ();\r
- gc.dispose (); \r
- GridDataFactory.fillDefaults().hint(unitCombo.computeSize(width, height)).applyTo(unitCombo);\r
- \r
- unitCombo.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 && unit != null) {\r
- ((Combo)e.widget).setText(unit);\r
- }\r
- }\r
- });\r
- \r
- unitCombo.addFocusListener(new FocusAdapter() {\r
- public void focusLost(FocusEvent e) {\r
- save();\r
- }\r
- });\r
- }\r
-\r
- public void setTextFactory(ReadFactory<?, String> textFactory) {\r
- this.textFactory = textFactory;\r
- }\r
-\r
- @Override\r
- public void setInput(ISessionContext context, final Object input) {\r
- if(input instanceof ISelection)\r
- variable = ISelectionUtils.filterSingleSelection((ISelection)input, Resource.class);\r
- if(textFactory != null) {\r
- textFactory.listen(context, input, new Listener<String>() {\r
-\r
- @Override\r
- public void exception(Throwable t) {\r
- t.printStackTrace();\r
- }\r
-\r
- @Override\r
- public void execute(final String text) {\r
- \r
- unitCombo.getDisplay().asyncExec(new Runnable() {\r
-\r
- @Override\r
- public void run() {\r
-\r
- if(isDisposed()) return;\r
- \r
- if(input instanceof ISelection) {\r
- ISelection selection = (ISelection)input;\r
- if(unitCombo.getItemCount() > 0) unitCombo.removeAll();\r
- for(String unit : getUnits(selection)) {\r
- unitCombo.add(unit);\r
- }\r
- }\r
- \r
- if(text == null) return;\r
- \r
- if(unitCombo.indexOf(text) >= 0)\r
- unitCombo.select(unitCombo.indexOf(text));\r
- else \r
- unitCombo.setText(text);\r
- unit = text;\r
- \r
- }\r
-\r
- });\r
- }\r
-\r
- @Override\r
- public boolean isDisposed() {\r
- return unitCombo.isDisposed();\r
- }\r
-\r
- });\r
- }\r
- }\r
-\r
- // Returns all the used units in the model\r
- private ArrayList<String> getUnits(ISelection selection) {\r
- ArrayList<String> units = new ArrayList<String>();\r
- if(selection instanceof IStructuredSelection) {\r
- final Resource resource = ISelectionUtils.filterSingleSelection(selection, Resource.class);\r
- if(resource != null) {\r
- try {\r
- units = SimanticsUI.getSession().syncRequest(new Read<ArrayList<String>>() {\r
-\r
- @Override\r
- public ArrayList<String> perform(ReadGraph graph)\r
- throws DatabaseException {\r
- ArrayList<String> units = new ArrayList<String>();\r
-\r
- SysdynResource sr = SysdynResource.getInstance(graph);\r
- Builtins b = graph.getBuiltins();\r
- Resource model = graph.getPossibleObject(resource, b.PartOf);\r
- if (model != null) {\r
- Collection<Resource> variables = graph.getObjects(model, b.ConsistsOf);\r
- for(Resource v : variables) {\r
- Object unit = graph.getPossibleRelatedValue(v, sr.HasUnit);\r
- if (unit != null && !units.contains((String) unit)) {\r
- units.add((String)unit);\r
- }\r
- }\r
- }\r
-\r
- return units;\r
-\r
- }\r
-\r
-\r
- });\r
- } catch (DatabaseException ee) {\r
- ExceptionUtils.logAndShowError(ee);\r
- }\r
- }\r
- }\r
- return units;\r
- }\r
- \r
- public void save() {\r
- final String unitNew = unitCombo.getText();\r
-\r
- if(unitNew != null && !unitNew.equals(unit) && variable != null) {\r
- try {\r
- SimanticsUI.getSession().syncRequest(new WriteRequest() {\r
-\r
- @Override\r
- public void perform(WriteGraph g)\r
- throws DatabaseException {\r
- SysdynResource sr = SysdynResource.getInstance(g);\r
- if (!unitNew.equals("")) {\r
- g.claimValue(variable, sr.HasUnit, unitNew);\r
- }\r
- else if (g.getPossibleRelatedValue(variable, sr.HasUnit) != null)\r
- g.deny(variable, sr.HasUnit);\r
- }\r
- });\r
- } catch (DatabaseException e) {\r
- e.printStackTrace();\r
- }\r
- }\r
- }\r
-}\r
+++ /dev/null
-package org.simantics.sysdyn.ui.properties.widgets;\r
-\r
-import java.util.ArrayList;\r
-\r
-import org.eclipse.jface.layout.GridDataFactory;\r
-import org.eclipse.jface.viewers.ISelection;\r
-import org.eclipse.swt.SWT;\r
-import org.eclipse.swt.events.FocusAdapter;\r
-import org.eclipse.swt.events.FocusEvent;\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.VerifyEvent;\r
-import org.eclipse.swt.events.VerifyListener;\r
-import org.eclipse.swt.widgets.Composite;\r
-import org.eclipse.swt.widgets.Text;\r
-import org.simantics.browsing.ui.swt.widgets.impl.ReadFactory;\r
-import org.simantics.browsing.ui.swt.widgets.impl.Widget;\r
-import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport;\r
-import org.simantics.db.Builtins;\r
-import org.simantics.db.ReadGraph;\r
-import org.simantics.db.Resource;\r
-import org.simantics.db.WriteGraph;\r
-import org.simantics.db.common.request.ReadRequest;\r
-import org.simantics.db.common.request.WriteRequest;\r
-import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.db.management.ISessionContext;\r
-import org.simantics.db.procedure.Listener;\r
-import org.simantics.sysdyn.manager.SysdynModel;\r
-import org.simantics.sysdyn.manager.SysdynModelManager;\r
-import org.simantics.sysdyn.representation.Configuration;\r
-import org.simantics.sysdyn.representation.IElement;\r
-import org.simantics.sysdyn.representation.Variable;\r
-import org.simantics.ui.SimanticsUI;\r
-import org.simantics.utils.ui.ISelectionUtils;\r
-\r
-public class VariableNameWidget implements Widget {\r
-\r
- private org.eclipse.swt.widgets.Text nameText;\r
- private String originalName = "";\r
- private ReadFactory<?, String> textFactory;\r
- private Resource variable;\r
- private Resource configuration;\r
-\r
- public VariableNameWidget(Composite parent, WidgetSupport support, int style) {\r
- support.register(this);\r
- nameText = new org.eclipse.swt.widgets.Text(parent, style);\r
-\r
- GridDataFactory.fillDefaults().grab(true, false).applyTo(nameText);\r
-\r
- nameText.addVerifyListener(new VerifyListener() {\r
-\r
- @Override\r
- public void verifyText(VerifyEvent e) {\r
- e.doit = false;\r
- char c = e.character;\r
- if(c != ' ') {\r
- e.doit = true;\r
- }\r
- }\r
- });\r
-\r
- nameText.addModifyListener(new ModifyListener() {\r
-\r
- @Override\r
- public void modifyText(ModifyEvent e) {\r
- Text t = (Text) e.widget;\r
- String txt = t.getText();\r
- // Don't allow this to produce an eternal loop\r
- if (!txt.equals(originalName) && txt.contains(" ")) {\r
- t.setText(originalName);\r
- }\r
- }\r
- });\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
- nameText.addFocusListener(new FocusAdapter() {\r
- public void focusLost(FocusEvent e) {\r
- save();\r
- }\r
- });\r
- }\r
-\r
- @Override\r
- public void setInput(ISessionContext context, Object input) {\r
- if(input instanceof ISelection) {\r
- variable = ISelectionUtils.filterSingleSelection((ISelection)input, Resource.class);\r
- SimanticsUI.getSession().asyncRequest(new ReadRequest() {\r
- \r
- @Override\r
- public void run(ReadGraph graph) throws DatabaseException {\r
- configuration = graph.getSingleObject(variable, graph.getBuiltins().PartOf); \r
- }\r
- });\r
- \r
- \r
- }\r
- if(textFactory != null) {\r
- textFactory.listen(context, input, new Listener<String>() {\r
-\r
- @Override\r
- public void exception(Throwable t) {\r
- t.printStackTrace();\r
- }\r
-\r
- @Override\r
- public void execute(final String text) {\r
-\r
- if(text == null) return;\r
-\r
- nameText.getDisplay().asyncExec(new Runnable() {\r
-\r
- @Override\r
- public void run() {\r
-\r
- if(isDisposed()) return;\r
- nameText.setText(text);\r
- originalName = text;\r
-\r
- }\r
-\r
- });\r
- }\r
-\r
- @Override\r
- public boolean isDisposed() {\r
- return nameText.isDisposed();\r
- }\r
-\r
- });\r
- }\r
-\r
- }\r
-\r
- public void setTextFactory(ReadFactory<?, String> textFactory) {\r
- this.textFactory = textFactory;\r
- }\r
-\r
-\r
- public void save() {\r
- final String nameNew = this.nameText.getText();\r
- if(nameNew != null && !nameNew.equals(this.originalName) && variable != null) {\r
- SysdynModelManager sdm = SysdynModelManager.getInstance(SimanticsUI.getSession());\r
- SysdynModel model = sdm.getModel(configuration);\r
- Configuration conf = model.getConfiguration();\r
- IElement thisElement = model.getElement(variable);\r
- ArrayList<IElement> elements = conf.elements;\r
- for(IElement e : elements) {\r
- if(e instanceof Variable) {\r
- Variable v = (Variable)e;\r
- if(v != thisElement && v.getName().equals(this.nameText.getText())) {\r
- this.nameText.setText(this.originalName);\r
- this.nameText.setSelection(this.originalName.length());\r
- return;\r
- }\r
- }\r
- }\r
-\r
-\r
-\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
-}\r