From: lempinen Date: Tue, 22 Jun 2010 07:39:06 +0000 (+0000) Subject: Switched basic components to browsing.ui.swt -components. Created a tracked combo... X-Git-Tag: simantics-1.2.0~199 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=b85e0bc39553316308d3f9cd7a38288a6f8eaed2;p=simantics%2Fsysdyn.git Switched basic components to browsing.ui.swt -components. Created a tracked combo -component, which works like a tracked text -component when combo is not read only git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@16267 ac1ea38d-2e2b-0410-8846-a27921b304fc --- diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/ConfigurationTab.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/ConfigurationTab.java index 0c41c928..cc061e97 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/ConfigurationTab.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/ConfigurationTab.java @@ -1,28 +1,25 @@ package org.simantics.sysdyn.ui.properties; +import org.eclipse.jface.dialogs.IInputValidator; import org.eclipse.jface.layout.GridDataFactory; import org.eclipse.jface.layout.GridLayoutFactory; import org.eclipse.swt.SWT; -import org.eclipse.swt.events.FocusEvent; -import org.eclipse.swt.events.FocusListener; -import org.eclipse.swt.events.VerifyEvent; -import org.eclipse.swt.events.VerifyListener; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Label; -import org.eclipse.swt.widgets.Text; import org.eclipse.ui.IWorkbenchSite; import org.simantics.browsing.ui.swt.PropertyTabContributorImpl; import org.simantics.browsing.ui.swt.widgets.StringPropertyFactory; +import org.simantics.browsing.ui.swt.widgets.StringPropertyModifier; +import org.simantics.browsing.ui.swt.widgets.TrackedText; import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport; import org.simantics.db.Builtins; import org.simantics.db.management.ISessionContext; import org.simantics.sysdyn.SysdynResource; -import org.simantics.sysdyn.ui.properties.widgets.TextWidget; import org.simantics.sysdyn.ui.properties.widgets.factories.DoublePropertyFactory; +import org.simantics.sysdyn.ui.properties.widgets.factories.DoublePropertyModifier; public class ConfigurationTab extends PropertyTabContributorImpl { - TextWidget name, startTime, stopTime; @Override @@ -34,83 +31,56 @@ public class ConfigurationTab extends PropertyTabContributorImpl { Label label = new Label(composite, SWT.NONE); label.setText("Name"); - name = new TextWidget(composite, support, SWT.BORDER); + TrackedText name = new TrackedText(composite, support, SWT.BORDER); name.setTextFactory(new StringPropertyFactory(Builtins.URIs.HasName)); - name.addFocusListener(new SaveListener()); - name.addVerifyListener(new VerifyListener() { - + name.addModifyListener(new StringPropertyModifier(context, Builtins.URIs.HasName)); + name.setInputValidator(new IInputValidator() { + @Override - public void verifyText(VerifyEvent e) { - e.doit = false; - String s = e.text; - if(!s.contains(" ")) { - e.doit = true; - } + public String isValid(String newText) { + if (newText.contains(" ")) + return "Sorry but spaces are not allowed for names right now"; + return null; } }); - GridDataFactory.fillDefaults().hint(200, SWT.DEFAULT).applyTo(name.getTextWidget()); + + GridDataFactory.fillDefaults().hint(200, SWT.DEFAULT).applyTo(name.getWidget()); label = new Label(composite, SWT.NONE); label.setText("Start time"); - startTime = new TextWidget(composite, support, SWT.BORDER | SWT.RIGHT); + TrackedText startTime = new TrackedText(composite, support, SWT.BORDER | SWT.RIGHT); startTime.setTextFactory(new DoublePropertyFactory(SysdynResource.URIs.HasStartTime)); - startTime.addVerifyListener(new DoubleVerify()); - startTime.addFocusListener(new SaveListener()); - GridDataFactory.fillDefaults().hint(200, SWT.DEFAULT).applyTo(startTime.getTextWidget()); + startTime.addModifyListener(new DoublePropertyModifier(context, SysdynResource.URIs.HasStartTime)); + startTime.setInputValidator(new DoubleValidator()); + GridDataFactory.fillDefaults().hint(200, SWT.DEFAULT).applyTo(startTime.getWidget()); label = new Label(composite, SWT.NONE); label.setText("Stop time"); - stopTime = new TextWidget(composite, support, SWT.BORDER | SWT.RIGHT); + TrackedText stopTime = new TrackedText(composite, support, SWT.BORDER | SWT.RIGHT); stopTime.setTextFactory(new DoublePropertyFactory(SysdynResource.URIs.HasStopTime)); - stopTime.addVerifyListener(new DoubleVerify()); - stopTime.addFocusListener(new SaveListener()); - GridDataFactory.fillDefaults().hint(200, SWT.DEFAULT).applyTo(stopTime.getTextWidget()); + stopTime.addModifyListener(new DoublePropertyModifier(context, SysdynResource.URIs.HasStopTime)); + stopTime.setInputValidator(new DoubleValidator()); + GridDataFactory.fillDefaults().hint(200, SWT.DEFAULT).applyTo(stopTime.getWidget()); + } - private class DoubleVerify implements VerifyListener { + private class DoubleValidator implements IInputValidator { @Override - public void verifyText(VerifyEvent e) { - e.doit = true; - for(int i = 0; i < e.text.length(); i++){ - if(!Character.isDigit(e.text.charAt(i))){ - if(e.text.charAt(i) != '.') { - e.doit = false; - break; - } else if(e.text.indexOf('.') != e.text.lastIndexOf('.')) { - e.doit = false; - break; - } else if(((Text)e.widget).getText().substring(0, e.start).contains(".") || - ((Text)e.widget).getText().substring(e.end, ((Text)e.widget).getText().length()).contains(".")) { - e.doit = false; - break; + public String isValid(String newText) { + for(int i = 0; i < newText.length(); i++){ + if(!Character.isDigit(newText.charAt(i))){ + if(newText.charAt(i) != '.') { + return "Invalid character '" + newText.charAt(i) + "'"; + } else if(newText.indexOf('.') != newText.lastIndexOf('.')) { + return "There can be only one '.'"; } - } + } } + return null; } - } - - private class SaveListener implements FocusListener { - - @Override - public void focusGained(FocusEvent e) { - } - - @Override - public void focusLost(FocusEvent e) { - save(); - } - - } - - private void save() { - this.name.save(); - this.startTime.save(); - this.stopTime.save(); - } - - + } } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/EquationTab.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/EquationTab.java index ac34c633..8e32c593 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/EquationTab.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/EquationTab.java @@ -1,5 +1,9 @@ package org.simantics.sysdyn.ui.properties; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + import org.eclipse.jface.layout.GridDataFactory; import org.eclipse.jface.layout.GridLayoutFactory; import org.eclipse.swt.SWT; @@ -9,8 +13,6 @@ import org.eclipse.swt.events.ModifyEvent; import org.eclipse.swt.events.ModifyListener; import org.eclipse.swt.events.MouseEvent; import org.eclipse.swt.events.MouseListener; -import org.eclipse.swt.events.SelectionEvent; -import org.eclipse.swt.events.SelectionListener; import org.eclipse.swt.graphics.Point; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Label; @@ -19,21 +21,29 @@ import org.eclipse.swt.widgets.TableItem; import org.eclipse.ui.IWorkbenchSite; import org.simantics.browsing.ui.swt.PropertyTabContributorImpl; import org.simantics.browsing.ui.swt.widgets.StringPropertyFactory; +import org.simantics.browsing.ui.swt.widgets.StringPropertyModifier; +import org.simantics.browsing.ui.swt.widgets.TrackedCombo; +import org.simantics.browsing.ui.swt.widgets.TrackedText; +import org.simantics.browsing.ui.swt.widgets.impl.ComboModifyListenerImpl; +import org.simantics.browsing.ui.swt.widgets.impl.ReadFactoryImpl; +import org.simantics.browsing.ui.swt.widgets.impl.TextModifyListener; +import org.simantics.browsing.ui.swt.widgets.impl.TrackedModifyEvent; import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport; import org.simantics.db.Builtins; +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.WriteGraph; +import org.simantics.db.exception.DatabaseException; import org.simantics.db.management.ISessionContext; import org.simantics.sysdyn.SysdynResource; -import org.simantics.sysdyn.ui.properties.widgets.ExpressionTypeWidget; +import org.simantics.sysdyn.ui.properties.widgets.ExpressionTypes; import org.simantics.sysdyn.ui.properties.widgets.ExpressionWidget; -import org.simantics.sysdyn.ui.properties.widgets.VariableNameWidget; import org.simantics.sysdyn.ui.properties.widgets.ShortcutTabWidget; -import org.simantics.sysdyn.ui.properties.widgets.UnitWidget; +import org.simantics.sysdyn.ui.properties.widgets.ExpressionTypes.ExpressionType; public class EquationTab extends PropertyTabContributorImpl { - VariableNameWidget nameWidget; - ExpressionTypeWidget expressionTypeWidget; - UnitWidget unitWidget; + TrackedCombo expressionTypeCombo, unitCombo; ShortcutTabWidget shortcutTabWidget; ExpressionWidget expressionWidget; @@ -42,24 +52,26 @@ public class EquationTab extends PropertyTabContributorImpl { Composite composite = new Composite(body, SWT.NONE); GridDataFactory.fillDefaults().grab(true, true).applyTo(composite); GridLayoutFactory.fillDefaults().margins(3, 3).numColumns(6).applyTo(composite); - nameWidget = new VariableNameWidget(composite, support, SWT.BORDER); - nameWidget.setTextFactory(new StringPropertyFactory(Builtins.URIs.HasName)); + TrackedText nameText = new TrackedText(composite, support, SWT.BORDER); + nameText.setTextFactory(new StringPropertyFactory(Builtins.URIs.HasName)); + nameText.addModifyListener(new StringPropertyModifier(context, Builtins.URIs.HasName)); + GridDataFactory.fillDefaults().grab(true, false).applyTo(nameText.getWidget()); Label label = new Label(composite, SWT.SINGLE ); label.setText("Type:"); GridDataFactory.fillDefaults().align(SWT.END, SWT.CENTER).applyTo(label); - expressionTypeWidget = new ExpressionTypeWidget(composite, support, SWT.DROP_DOWN | SWT.BORDER | SWT.READ_ONLY); - + expressionTypeCombo = new TrackedCombo(composite, support, SWT.DROP_DOWN | SWT.BORDER | SWT.READ_ONLY); + label = new Label(composite, SWT.SINGLE ); label.setText("Unit:"); GridDataFactory.fillDefaults().align(SWT.END, SWT.CENTER).applyTo(label); - unitWidget = new UnitWidget(composite, support, SWT.DROP_DOWN | SWT.BORDER); - unitWidget.setTextFactory(new StringPropertyFactory(SysdynResource.URIs.HasUnit)); + unitCombo = new TrackedCombo(composite, support, SWT.DROP_DOWN | SWT.BORDER); + shortcutTabWidget = new ShortcutTabWidget(composite, support, SWT.NONE); @@ -67,25 +79,100 @@ public class EquationTab extends PropertyTabContributorImpl { GridDataFactory.fillDefaults().grab(true, true).span(5, 1).applyTo(expressionComposite); expressionWidget = new ExpressionWidget(expressionComposite, support, SWT.NONE); expressionWidget.setVariableTable(shortcutTabWidget.getVariableTable()); - + addListeners(); } private void addListeners() { + + expressionTypeCombo.setItemFactory(new ReadFactoryImpl>() { + + @Override + public Map perform(ReadGraph graph, final Resource input) throws DatabaseException { + + Map map = new HashMap(); + SysdynResource sr = SysdynResource.getInstance(graph); + + final ExpressionType[] expressionTypes; + if(graph.isInstanceOf(input, sr.Auxiliary)) + expressionTypes = ExpressionTypes.auxiliaryExpressions; + else if(graph.isInstanceOf(input, sr.Stock)) + expressionTypes = ExpressionTypes.stockExpressions; + else if(graph.isInstanceOf(input, sr.Valve)) + expressionTypes = ExpressionTypes.valveExpressions; + else + expressionTypes = new ExpressionType[] {}; + + for(ExpressionType et : expressionTypes) { + map.put(et.toString(), et); + } + return map; + } + }); - expressionTypeWidget.addSelectionListener(new SelectionListener() { + expressionTypeCombo.setSelectionFactory(new ReadFactoryImpl() { @Override - public void widgetSelected(SelectionEvent e) { - expressionWidget.displayExpression(expressionTypeWidget.getSelecetedType(), false); - expressionWidget.save(); + public String perform(ReadGraph graph, final Resource input) throws DatabaseException { + return ExpressionTypes.getExpressionTypeForVariable(graph, input).toString(); + } + }); + + expressionTypeCombo.addModifyListener(new TextModifyListener() { + + @Override + public void modifyText(TrackedModifyEvent e) { + System.out.println(e.getText()); + expressionWidget.displayExpression(e.getText(), false); + expressionWidget.save(); + } + }); + + + unitCombo.setItemFactory(new ReadFactoryImpl>() { + + @Override + public Map perform(ReadGraph graph, final Resource input) throws DatabaseException { + Map map = new HashMap(); + + SysdynResource sr = SysdynResource.getInstance(graph); + Builtins b = graph.getBuiltins(); + Resource model = graph.getPossibleObject(input, b.PartOf); + if (model != null) { + Collection variables = graph.getObjects(model, b.ConsistsOf); + for(Resource v : variables) { + Object unit = graph.getPossibleRelatedValue(v, sr.HasUnit); + if (unit != null && !map.keySet().contains((String) unit)) { + map.put((String)unit, (String)unit); + + } + } + } + return map; + } + }); + unitCombo.setSelectionFactory(new ReadFactoryImpl() { + + @Override + public String perform(ReadGraph graph, final Resource input) throws DatabaseException { + String unit = graph.getPossibleRelatedValue(input, SysdynResource.getInstance(graph).HasUnit); + if(unit == null) + return ""; + else + return unit; } + }); + + unitCombo.addModifyListener(new ComboModifyListenerImpl() { @Override - public void widgetDefaultSelected(SelectionEvent e) { + public void applyText(WriteGraph graph, Resource input, String text) throws DatabaseException { + graph.claimValue(input, SysdynResource.getInstance(graph).HasUnit, text); } + }); + shortcutTabWidget.addMouseListener(new MouseListener(){ @Override @@ -136,14 +223,14 @@ public class EquationTab extends PropertyTabContributorImpl { expressionWidget.validateFields(); } }); - + expressionWidget.addFocusListener(new FocusListener() { - + @Override public void focusLost(FocusEvent e) { expressionWidget.save(); } - + @Override public void focusGained(FocusEvent e) { expressionWidget.validateFields(); diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/VariableInformationTab.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/VariableInformationTab.java index 3175a7d9..e179e9d7 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/VariableInformationTab.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/VariableInformationTab.java @@ -1,119 +1,94 @@ package org.simantics.sysdyn.ui.properties; +import org.eclipse.jface.dialogs.IInputValidator; import org.eclipse.jface.layout.GridDataFactory; import org.eclipse.jface.layout.GridLayoutFactory; import org.eclipse.swt.SWT; -import org.eclipse.swt.events.FocusEvent; -import org.eclipse.swt.events.FocusListener; -import org.eclipse.swt.events.VerifyEvent; -import org.eclipse.swt.events.VerifyListener; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Group; import org.eclipse.swt.widgets.Label; -import org.eclipse.swt.widgets.Text; import org.eclipse.ui.IWorkbenchSite; import org.simantics.browsing.ui.swt.PropertyTabContributorImpl; import org.simantics.browsing.ui.swt.widgets.StringPropertyFactory; +import org.simantics.browsing.ui.swt.widgets.StringPropertyModifier; +import org.simantics.browsing.ui.swt.widgets.TrackedText; import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport; import org.simantics.db.Builtins; import org.simantics.db.management.ISessionContext; import org.simantics.sysdyn.SysdynResource; -import org.simantics.sysdyn.ui.properties.widgets.TextWidget; import org.simantics.sysdyn.ui.properties.widgets.factories.DoublePropertyFactory; +import org.simantics.sysdyn.ui.properties.widgets.factories.DoublePropertyModifier; public class VariableInformationTab extends PropertyTabContributorImpl { - TextWidget information, rangeStart, rangeEnd, rangeStep; - @Override public void createControls(Composite body, IWorkbenchSite site, ISessionContext context, WidgetSupport support) { Composite composite = new Composite(body, SWT.NONE); GridDataFactory.fillDefaults().grab(true, true).applyTo(composite); GridLayoutFactory.fillDefaults().margins(3, 3).applyTo(composite); - + Group informationGroup = new Group(composite, SWT.SHADOW_ETCHED_IN); informationGroup.setText("Information"); GridDataFactory.fillDefaults().grab(false, true).applyTo(informationGroup); GridLayoutFactory.fillDefaults().margins(3, 3).applyTo(informationGroup); - - information = new TextWidget(informationGroup, support, SWT.MULTI | SWT.BORDER); + + TrackedText information = new TrackedText(informationGroup, support, SWT.MULTI | SWT.BORDER); information.setTextFactory(new StringPropertyFactory(Builtins.URIs.HasDescription)); - information.addFocusListener(new SaveListener()); - GridDataFactory.fillDefaults().grab(true, true).applyTo(information.getTextWidget()); - - + information.addModifyListener(new StringPropertyModifier(context, Builtins.URIs.HasDescription)); + GridDataFactory.fillDefaults().grab(true, true).applyTo(information.getWidget()); + Group rangeGroup = new Group(composite, SWT.SHADOW_ETCHED_IN); rangeGroup.setText("Range"); GridDataFactory.fillDefaults().applyTo(rangeGroup); GridLayoutFactory.fillDefaults().margins(3, 3).numColumns(6).applyTo(rangeGroup); - + Label label = new Label(rangeGroup, SWT.NONE); label.setText("Start"); - - rangeStart = new TextWidget(rangeGroup, support, SWT.BORDER | SWT.RIGHT); + + TrackedText rangeStart = new TrackedText(rangeGroup, support, SWT.RIGHT | SWT.BORDER); rangeStart.setTextFactory(new DoublePropertyFactory(SysdynResource.URIs.HasRangeStart)); - rangeStart.addFocusListener(new SaveListener()); - rangeStart.addVerifyListener(new DoubleVerify()); + rangeStart.addModifyListener(new DoublePropertyModifier(context, SysdynResource.URIs.HasRangeStart)); + rangeStart.setInputValidator(new DoubleValidator()); + GridDataFactory.fillDefaults().grab(true, false).applyTo(rangeStart.getWidget()); + label = new Label(rangeGroup, SWT.NONE); label.setText("End"); - - rangeEnd = new TextWidget(rangeGroup, support, SWT.BORDER | SWT.RIGHT); + + + TrackedText rangeEnd = new TrackedText(rangeGroup, support, SWT.RIGHT | SWT.BORDER); rangeEnd.setTextFactory(new DoublePropertyFactory(SysdynResource.URIs.HasRangeEnd)); - rangeEnd.addFocusListener(new SaveListener()); - rangeEnd.addVerifyListener(new DoubleVerify()); + rangeEnd.addModifyListener(new DoublePropertyModifier(context, SysdynResource.URIs.HasRangeEnd)); + rangeEnd.setInputValidator(new DoubleValidator()); + GridDataFactory.fillDefaults().grab(true, false).applyTo(rangeEnd.getWidget()); label = new Label(rangeGroup, SWT.NONE); label.setText("Step"); - - rangeStep = new TextWidget(rangeGroup, support, SWT.BORDER | SWT.RIGHT); + + TrackedText rangeStep = new TrackedText(rangeGroup, support, SWT.RIGHT | SWT.BORDER); rangeStep.setTextFactory(new DoublePropertyFactory(SysdynResource.URIs.HasRangeStep)); - rangeStep.addFocusListener(new SaveListener()); - rangeStep.addVerifyListener(new DoubleVerify()); - - } - - - private class SaveListener implements FocusListener { + rangeStep.addModifyListener(new DoublePropertyModifier(context, SysdynResource.URIs.HasRangeStep)); + rangeStep.setInputValidator(new DoubleValidator()); + GridDataFactory.fillDefaults().grab(true, false).applyTo(rangeStep.getWidget()); - @Override - public void focusGained(FocusEvent e) { - } - @Override - public void focusLost(FocusEvent e) { - save(); - } - - } - - private void save() { - this.information.save(); - this.rangeStart.save(); - this.rangeEnd.save(); - this.rangeStep.save(); } - private class DoubleVerify implements VerifyListener { + private class DoubleValidator implements IInputValidator { @Override - public void verifyText(VerifyEvent e) { - e.doit = true; - for(int i = 0; i < e.text.length(); i++){ - if(!Character.isDigit(e.text.charAt(i))){ - if(e.text.charAt(i) != '.') { - e.doit = false; - break; - } else if(e.text.indexOf('.') != e.text.lastIndexOf('.')) { - e.doit = false; - break; - } else if(((Text)e.widget).getText().substring(0, e.start).contains(".") || - ((Text)e.widget).getText().substring(e.end, ((Text)e.widget).getText().length()).contains(".")) { - e.doit = false; - break; + public String isValid(String newText) { + for(int i = 0; i < newText.length(); i++){ + if(!Character.isDigit(newText.charAt(i))){ + if(newText.charAt(i) != '.') { + return "Invalid character '" + newText.charAt(i) + "'"; + } else if(newText.indexOf('.') != newText.lastIndexOf('.')) { + return "There can be only one '.'"; } - } + } } + return null; } } + } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/ExpressionTypeWidget.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/ExpressionTypeWidget.java deleted file mode 100644 index 0b4d9808..00000000 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/ExpressionTypeWidget.java +++ /dev/null @@ -1,105 +0,0 @@ -package org.simantics.sysdyn.ui.properties.widgets; - - -import org.eclipse.jface.layout.GridDataFactory; -import org.eclipse.jface.viewers.IStructuredSelection; -import org.eclipse.swt.events.SelectionListener; -import org.eclipse.swt.graphics.FontMetrics; -import org.eclipse.swt.graphics.GC; -import org.eclipse.swt.widgets.Combo; -import org.eclipse.swt.widgets.Composite; -import org.simantics.browsing.ui.swt.widgets.impl.Widget; -import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport; -import org.simantics.db.ReadGraph; -import org.simantics.db.Resource; -import org.simantics.db.common.request.ReadRequest; -import org.simantics.db.exception.DatabaseException; -import org.simantics.db.management.ISessionContext; -import org.simantics.sysdyn.SysdynResource; -import org.simantics.sysdyn.ui.properties.widgets.ExpressionTypes.ExpressionType; -import org.simantics.ui.SimanticsUI; -import org.simantics.utils.ui.ISelectionUtils; - -public class ExpressionTypeWidget implements Widget { - - Combo typeCombo; - - public ExpressionTypeWidget(Composite parent, WidgetSupport support, int style) { - support.register(this); - - typeCombo = new Combo(parent, style); - - int columns = 6; - GC gc = new GC (typeCombo); - FontMetrics fm = gc.getFontMetrics (); - int width = columns * fm.getAverageCharWidth (); - int height = fm.getHeight (); - gc.dispose (); - GridDataFactory.fillDefaults().hint(typeCombo.computeSize(width, height)).applyTo(typeCombo); - } - - @Override - public void setInput(ISessionContext context, Object input) { - if(input instanceof IStructuredSelection) { - final Resource variable = ISelectionUtils.filterSingleSelection((IStructuredSelection)input, Resource.class); - if(variable != null) { - final ExpressionType et = ExpressionTypes.getExpressionTypeForVariable(variable); - SimanticsUI.getSession().asyncRequest(new ReadRequest() { - @Override - public void run(ReadGraph graph) throws DatabaseException { - SysdynResource sr = SysdynResource.getInstance(graph); - - final ExpressionType[] expressionTypes; - if(graph.isInstanceOf(variable, sr.Auxiliary)) - expressionTypes = ExpressionTypes.auxiliaryExpressions; - else if(graph.isInstanceOf(variable, sr.Stock)) - expressionTypes = ExpressionTypes.stockExpressions; - else if(graph.isInstanceOf(variable, sr.Valve)) - expressionTypes = ExpressionTypes.valveExpressions; - else - expressionTypes = new ExpressionType[] {}; - - - typeCombo.getDisplay().asyncExec(new Runnable() { - - @Override - public void run() { - if(!typeCombo.isDisposed()){ - setExpressionTypes(expressionTypes); - setExpressionType(et); - } - } - }); - - } - - }); - - } - } - } - - private void setExpressionTypes(ExpressionType[] expressionTypes) { - if(expressionTypes != null) { - for(ExpressionType et : expressionTypes) { - typeCombo.add(et.toString()); - } - } - } - - public void setExpressionType(ExpressionType expressionType) { - int index = typeCombo.indexOf(expressionType.toString()); - if (index > -1) { - typeCombo.select(index); - } - } - - - public void addSelectionListener(SelectionListener listener) { - this.typeCombo.addSelectionListener(listener); - } - - public String getSelecetedType() { - return this.typeCombo.getItem(this.typeCombo.getSelectionIndex()); - } -} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/ExpressionTypes.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/ExpressionTypes.java index fdc1f986..3741a34e 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/ExpressionTypes.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/ExpressionTypes.java @@ -8,65 +8,32 @@ import org.simantics.sysdyn.SysdynResource; import org.simantics.ui.SimanticsUI; public class ExpressionTypes { - + public static enum ExpressionType {Auxiliary, Parameter, Constant, Lookup, WithLookup, Stock, Delay, Empty}; public static ExpressionType[] auxiliaryExpressions = new ExpressionType[] { - ExpressionType.Parameter, - ExpressionType.Auxiliary, - ExpressionType.Constant, - ExpressionType.Lookup, - ExpressionType.WithLookup}; + ExpressionType.Parameter, + ExpressionType.Auxiliary, + ExpressionType.Constant, + ExpressionType.Lookup, + ExpressionType.WithLookup}; public static ExpressionType[] valveExpressions = new ExpressionType[] { - ExpressionType.Auxiliary, - ExpressionType.Parameter, - ExpressionType.Constant, - ExpressionType.WithLookup}; + ExpressionType.Auxiliary, + ExpressionType.Parameter, + ExpressionType.Constant, + ExpressionType.WithLookup}; public static ExpressionType[] stockExpressions = new ExpressionType[] { - ExpressionType.Stock}; - + ExpressionType.Stock}; + public static ExpressionType getExpressionTypeForVariable(final Resource variable) { try { return SimanticsUI.getSession().syncRequest(new Read() { @Override public ExpressionType perform(ReadGraph graph) throws DatabaseException { - SysdynResource sr = SysdynResource.getInstance(graph); - final ExpressionType et; - Resource expression = graph.getPossibleObject(variable, sr.HasExpression); - if(expression == null) { - if(graph.isInstanceOf(variable, sr.Auxiliary)) { - et = ExpressionType.Auxiliary; - } - else if(graph.isInstanceOf(variable, sr.Valve)) { - et = ExpressionType.Auxiliary; - } - else if(graph.isInstanceOf(variable, sr.Stock)) { - et = ExpressionType.Stock; - } else - et = ExpressionType.Empty; - } else { - if(graph.isInstanceOf(expression, sr.NormalExpression)) { - et = ExpressionType.Auxiliary; - } else if (graph.isInstanceOf(expression, sr.StockExpression)) { - et = ExpressionType.Stock; - } else if (graph.isInstanceOf(expression, sr.ParameterExpression)) { - et = ExpressionType.Parameter; - } else if (graph.isInstanceOf(expression, sr.ConstantExpression)) { - et = ExpressionType.Constant; - } else if (graph.isInstanceOf(expression, sr.DelayExpression)) { - et = ExpressionType.Delay; - } else if (graph.isInstanceOf(expression, sr.LookupExpression)) { - et = ExpressionType.Lookup; - } else if (graph.isInstanceOf(expression, sr.WithLookupExpression)) { - et = ExpressionType.WithLookup; - } else { - et = ExpressionType.Empty; - } - } - return et; + return getExpressionTypeForVariable(graph, variable); } }); } catch (DatabaseException e) { @@ -76,4 +43,46 @@ public class ExpressionTypes { } + public static ExpressionType getExpressionTypeForVariable(ReadGraph graph, final Resource variable) { + ExpressionType et = null; + try { + SysdynResource sr = SysdynResource.getInstance(graph); + + Resource expression = graph.getPossibleObject(variable, sr.HasExpression); + if(expression == null) { + if(graph.isInstanceOf(variable, sr.Auxiliary)) { + et = ExpressionType.Auxiliary; + } + else if(graph.isInstanceOf(variable, sr.Valve)) { + et = ExpressionType.Auxiliary; + } + else if(graph.isInstanceOf(variable, sr.Stock)) { + et = ExpressionType.Stock; + } else + et = ExpressionType.Empty; + } else { + if(graph.isInstanceOf(expression, sr.NormalExpression)) { + et = ExpressionType.Auxiliary; + } else if (graph.isInstanceOf(expression, sr.StockExpression)) { + et = ExpressionType.Stock; + } else if (graph.isInstanceOf(expression, sr.ParameterExpression)) { + et = ExpressionType.Parameter; + } else if (graph.isInstanceOf(expression, sr.ConstantExpression)) { + et = ExpressionType.Constant; + } else if (graph.isInstanceOf(expression, sr.DelayExpression)) { + et = ExpressionType.Delay; + } else if (graph.isInstanceOf(expression, sr.LookupExpression)) { + et = ExpressionType.Lookup; + } else if (graph.isInstanceOf(expression, sr.WithLookupExpression)) { + et = ExpressionType.WithLookup; + } else { + et = ExpressionType.Empty; + } + } + } catch (DatabaseException e) { + e.printStackTrace(); + } + return et; + } + } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/TextWidget.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/TextWidget.java deleted file mode 100644 index af99f2f3..00000000 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/TextWidget.java +++ /dev/null @@ -1,154 +0,0 @@ -package org.simantics.sysdyn.ui.properties.widgets; - -import org.eclipse.jface.viewers.ISelection; -import org.eclipse.swt.SWT; -import org.eclipse.swt.events.FocusListener; -import org.eclipse.swt.events.KeyEvent; -import org.eclipse.swt.events.KeyListener; -import org.eclipse.swt.events.VerifyListener; -import org.eclipse.swt.widgets.Composite; -import org.eclipse.swt.widgets.Text; -import org.simantics.browsing.ui.swt.widgets.StringPropertyFactory; -import org.simantics.browsing.ui.swt.widgets.impl.ReadFactory; -import org.simantics.browsing.ui.swt.widgets.impl.ReadFactoryImpl; -import org.simantics.browsing.ui.swt.widgets.impl.Widget; -import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport; -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.db.management.ISessionContext; -import org.simantics.db.procedure.Listener; -import org.simantics.sysdyn.ui.properties.widgets.factories.DoublePropertyFactory; -import org.simantics.ui.SimanticsUI; -import org.simantics.utils.datastructures.Triple; -import org.simantics.utils.ui.ISelectionUtils; - -public class TextWidget implements Widget { - - private Text textWidget; - private String originalText = ""; - private ReadFactory readFactory; - private Resource resource; - - public TextWidget(Composite parent, WidgetSupport support, int style) { - support.register(this); - textWidget = new Text(parent, style); - textWidget.addKeyListener(new KeyListener() { - - @Override - public void keyReleased(KeyEvent e) { - } - - @Override - public void keyPressed(KeyEvent e) { - if(e.keyCode == SWT.ESC) { - Text text = (Text)e.widget; - text.setText(originalText); - text.setSelection(text.getCharCount()); - } - } - }); - } - - public void setTextFactory(ReadFactory textFactory) { - this.readFactory = textFactory; - } - - @Override - public void setInput(ISessionContext context, Object input) { - if(input instanceof ISelection) - resource = ISelectionUtils.filterSingleSelection((ISelection)input, Resource.class); - - if(readFactory != null) { - readFactory.listen(context, input, new Listener() { - - @Override - public void exception(Throwable t) { - t.printStackTrace(); - } - - @Override - public void execute(final String text) { - - if(text == null) return; - if (isDisposed()) - return; - - textWidget.getDisplay().asyncExec(new Runnable() { - - @Override - public void run() { - - if(isDisposed()) return; - textWidget.setText(text); - originalText = text; - - } - - }); - } - - @Override - public boolean isDisposed() { - return textWidget.isDisposed(); - } - - }); - } - - } - - @SuppressWarnings("unchecked") - public void save() { - if(originalText.equals(textWidget.getText())) return; - - final String text = textWidget.getText().trim(); - - SimanticsUI.getSession().asyncRequest(new WriteRequest() { - - @Override - public void perform(WriteGraph graph) throws DatabaseException { - - Triple triple = null; - Object object = null; - triple = (Triple) ((ReadFactoryImpl)readFactory).getIdentity(null); - if(triple == null) return; - - if(!text.equals("")) { - if(readFactory instanceof DoublePropertyFactory) { - object = Double.parseDouble(text); - } else if (readFactory instanceof StringPropertyFactory) { - object = text; - } - graph.claimValue(resource, graph.getResource((String)triple.second), object); - } else { - Resource relation = graph.getResource((String)triple.second); - if (graph.hasStatement(resource, relation)) - graph.deny(resource, relation); - } - } - }); - - } - - public void addKeyListener(KeyListener listener) { - this.textWidget.addKeyListener(listener); - } - - public void addVerifyListener(VerifyListener listener) { - this.textWidget.addVerifyListener(listener); - } - - public void addFocusListener(FocusListener listener) { - this.textWidget.addFocusListener(listener); - } - - public String getText() { - return textWidget.getText(); - } - - public Text getTextWidget() { - return textWidget; - } -} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/UnitWidget.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/UnitWidget.java deleted file mode 100644 index fae7df99..00000000 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/UnitWidget.java +++ /dev/null @@ -1,197 +0,0 @@ -package org.simantics.sysdyn.ui.properties.widgets; - -import java.util.ArrayList; -import java.util.Collection; - -import org.eclipse.jface.layout.GridDataFactory; -import org.eclipse.jface.viewers.ISelection; -import org.eclipse.jface.viewers.IStructuredSelection; -import org.eclipse.swt.SWT; -import org.eclipse.swt.events.FocusAdapter; -import org.eclipse.swt.events.FocusEvent; -import org.eclipse.swt.events.KeyEvent; -import org.eclipse.swt.events.KeyListener; -import org.eclipse.swt.graphics.FontMetrics; -import org.eclipse.swt.graphics.GC; -import org.eclipse.swt.widgets.Combo; -import org.eclipse.swt.widgets.Composite; -import org.simantics.browsing.ui.swt.widgets.impl.ReadFactory; -import org.simantics.browsing.ui.swt.widgets.impl.Widget; -import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport; -import org.simantics.db.Builtins; -import org.simantics.db.ReadGraph; -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.db.management.ISessionContext; -import org.simantics.db.procedure.Listener; -import org.simantics.db.request.Read; -import org.simantics.sysdyn.SysdynResource; -import org.simantics.ui.SimanticsUI; -import org.simantics.utils.ui.ExceptionUtils; -import org.simantics.utils.ui.ISelectionUtils; - -public class UnitWidget implements Widget { - - Combo unitCombo; - private ReadFactory textFactory; - Resource variable; - String unit; - - public UnitWidget(Composite parent, WidgetSupport support, int style) { - support.register(this); - - unitCombo = new Combo(parent, style); - - int columns = 6; - GC gc = new GC (unitCombo); - FontMetrics fm = gc.getFontMetrics (); - int width = columns * fm.getAverageCharWidth (); - int height = fm.getHeight (); - gc.dispose (); - GridDataFactory.fillDefaults().hint(unitCombo.computeSize(width, height)).applyTo(unitCombo); - - unitCombo.addKeyListener(new KeyListener() { - - @Override - public void keyReleased(KeyEvent e) { - - } - - @Override - public void keyPressed(KeyEvent e) { - if(e.keyCode == SWT.ESC && unit != null) { - ((Combo)e.widget).setText(unit); - } - } - }); - - unitCombo.addFocusListener(new FocusAdapter() { - public void focusLost(FocusEvent e) { - save(); - } - }); - } - - public void setTextFactory(ReadFactory textFactory) { - this.textFactory = textFactory; - } - - @Override - public void setInput(ISessionContext context, final Object input) { - if(input instanceof ISelection) - variable = ISelectionUtils.filterSingleSelection((ISelection)input, Resource.class); - if(textFactory != null) { - textFactory.listen(context, input, new Listener() { - - @Override - public void exception(Throwable t) { - t.printStackTrace(); - } - - @Override - public void execute(final String text) { - - unitCombo.getDisplay().asyncExec(new Runnable() { - - @Override - public void run() { - - if(isDisposed()) return; - - if(input instanceof ISelection) { - ISelection selection = (ISelection)input; - if(unitCombo.getItemCount() > 0) unitCombo.removeAll(); - for(String unit : getUnits(selection)) { - unitCombo.add(unit); - } - } - - if(text == null) return; - - if(unitCombo.indexOf(text) >= 0) - unitCombo.select(unitCombo.indexOf(text)); - else - unitCombo.setText(text); - unit = text; - - } - - }); - } - - @Override - public boolean isDisposed() { - return unitCombo.isDisposed(); - } - - }); - } - } - - // Returns all the used units in the model - private ArrayList getUnits(ISelection selection) { - ArrayList units = new ArrayList(); - if(selection instanceof IStructuredSelection) { - final Resource resource = ISelectionUtils.filterSingleSelection(selection, Resource.class); - if(resource != null) { - try { - units = SimanticsUI.getSession().syncRequest(new Read>() { - - @Override - public ArrayList perform(ReadGraph graph) - throws DatabaseException { - ArrayList units = new ArrayList(); - - SysdynResource sr = SysdynResource.getInstance(graph); - Builtins b = graph.getBuiltins(); - Resource model = graph.getPossibleObject(resource, b.PartOf); - if (model != null) { - Collection variables = graph.getObjects(model, b.ConsistsOf); - for(Resource v : variables) { - Object unit = graph.getPossibleRelatedValue(v, sr.HasUnit); - if (unit != null && !units.contains((String) unit)) { - units.add((String)unit); - } - } - } - - return units; - - } - - - }); - } catch (DatabaseException ee) { - ExceptionUtils.logAndShowError(ee); - } - } - } - return units; - } - - public void save() { - final String unitNew = unitCombo.getText(); - - if(unitNew != null && !unitNew.equals(unit) && variable != null) { - try { - SimanticsUI.getSession().syncRequest(new WriteRequest() { - - @Override - public void perform(WriteGraph g) - throws DatabaseException { - SysdynResource sr = SysdynResource.getInstance(g); - if (!unitNew.equals("")) { - g.claimValue(variable, sr.HasUnit, unitNew); - } - else if (g.getPossibleRelatedValue(variable, sr.HasUnit) != null) - g.deny(variable, sr.HasUnit); - } - }); - } catch (DatabaseException e) { - e.printStackTrace(); - } - } - } -} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/VariableNameWidget.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/VariableNameWidget.java deleted file mode 100644 index 46897945..00000000 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/VariableNameWidget.java +++ /dev/null @@ -1,194 +0,0 @@ -package org.simantics.sysdyn.ui.properties.widgets; - -import java.util.ArrayList; - -import org.eclipse.jface.layout.GridDataFactory; -import org.eclipse.jface.viewers.ISelection; -import org.eclipse.swt.SWT; -import org.eclipse.swt.events.FocusAdapter; -import org.eclipse.swt.events.FocusEvent; -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.VerifyEvent; -import org.eclipse.swt.events.VerifyListener; -import org.eclipse.swt.widgets.Composite; -import org.eclipse.swt.widgets.Text; -import org.simantics.browsing.ui.swt.widgets.impl.ReadFactory; -import org.simantics.browsing.ui.swt.widgets.impl.Widget; -import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport; -import org.simantics.db.Builtins; -import org.simantics.db.ReadGraph; -import org.simantics.db.Resource; -import org.simantics.db.WriteGraph; -import org.simantics.db.common.request.ReadRequest; -import org.simantics.db.common.request.WriteRequest; -import org.simantics.db.exception.DatabaseException; -import org.simantics.db.management.ISessionContext; -import org.simantics.db.procedure.Listener; -import org.simantics.sysdyn.manager.SysdynModel; -import org.simantics.sysdyn.manager.SysdynModelManager; -import org.simantics.sysdyn.representation.Configuration; -import org.simantics.sysdyn.representation.IElement; -import org.simantics.sysdyn.representation.Variable; -import org.simantics.ui.SimanticsUI; -import org.simantics.utils.ui.ISelectionUtils; - -public class VariableNameWidget implements Widget { - - private org.eclipse.swt.widgets.Text nameText; - private String originalName = ""; - private ReadFactory textFactory; - private Resource variable; - private Resource configuration; - - public VariableNameWidget(Composite parent, WidgetSupport support, int style) { - support.register(this); - nameText = new org.eclipse.swt.widgets.Text(parent, style); - - GridDataFactory.fillDefaults().grab(true, false).applyTo(nameText); - - nameText.addVerifyListener(new VerifyListener() { - - @Override - public void verifyText(VerifyEvent e) { - e.doit = false; - char c = e.character; - if(c != ' ') { - e.doit = true; - } - } - }); - - nameText.addModifyListener(new ModifyListener() { - - @Override - public void modifyText(ModifyEvent e) { - Text t = (Text) e.widget; - String txt = t.getText(); - // Don't allow this to produce an eternal loop - if (!txt.equals(originalName) && txt.contains(" ")) { - t.setText(originalName); - } - } - }); - - 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()); - } - } - }); - - nameText.addFocusListener(new FocusAdapter() { - public void focusLost(FocusEvent e) { - save(); - } - }); - } - - @Override - public void setInput(ISessionContext context, Object input) { - if(input instanceof ISelection) { - variable = ISelectionUtils.filterSingleSelection((ISelection)input, Resource.class); - SimanticsUI.getSession().asyncRequest(new ReadRequest() { - - @Override - public void run(ReadGraph graph) throws DatabaseException { - configuration = graph.getSingleObject(variable, graph.getBuiltins().PartOf); - } - }); - - - } - if(textFactory != null) { - textFactory.listen(context, input, new Listener() { - - @Override - public void exception(Throwable t) { - t.printStackTrace(); - } - - @Override - public void execute(final String text) { - - if(text == null) return; - - nameText.getDisplay().asyncExec(new Runnable() { - - @Override - public void run() { - - if(isDisposed()) return; - nameText.setText(text); - originalName = text; - - } - - }); - } - - @Override - public boolean isDisposed() { - return nameText.isDisposed(); - } - - }); - } - - } - - public void setTextFactory(ReadFactory textFactory) { - this.textFactory = textFactory; - } - - - public void save() { - final String nameNew = this.nameText.getText(); - if(nameNew != null && !nameNew.equals(this.originalName) && variable != null) { - SysdynModelManager sdm = SysdynModelManager.getInstance(SimanticsUI.getSession()); - SysdynModel model = sdm.getModel(configuration); - Configuration conf = model.getConfiguration(); - IElement thisElement = model.getElement(variable); - ArrayList elements = conf.elements; - for(IElement e : elements) { - if(e instanceof Variable) { - Variable v = (Variable)e; - if(v != thisElement && v.getName().equals(this.nameText.getText())) { - this.nameText.setText(this.originalName); - this.nameText.setSelection(this.originalName.length()); - return; - } - } - } - - - - 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(); - } - } - } - - -}