From: lempinen Date: Thu, 17 Jun 2010 13:24:31 +0000 (+0000) Subject: git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@16189 ac1ea38d-2e2b... X-Git-Tag: simantics-1.2.0~210 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=92f09e87ccf8e0748215b32f076eda132c4dd8bf;p=simantics%2Fsysdyn.git git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@16189 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 3c2ff783..f5d6359d 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,18 +1,116 @@ package org.simantics.sysdyn.ui.properties; +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.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; public class ConfigurationTab extends PropertyTabContributorImpl { + TextWidget name, startTime, stopTime; + + @Override public void createControls(Composite body, IWorkbenchSite site, ISessionContext context, WidgetSupport support) { - // TODO Auto-generated method stub + Composite composite = new Composite(body, SWT.NONE); + GridDataFactory.fillDefaults().grab(true, true).applyTo(composite); + GridLayoutFactory.fillDefaults().margins(3, 3).numColumns(2).applyTo(composite); + + Label label = new Label(composite, SWT.NONE); + label.setText("Name"); + + name = new TextWidget(composite, support, SWT.BORDER); + name.setTextFactory(new StringPropertyFactory(context, Builtins.URIs.HasName)); + name.addFocusListener(new SaveListener()); + name.addVerifyListener(new VerifyListener() { + + @Override + public void verifyText(VerifyEvent e) { + e.doit = false; + String s = e.text; + if(!s.contains(" ")) { + e.doit = true; + } + } + }); + GridDataFactory.fillDefaults().hint(200, SWT.DEFAULT).applyTo(name.getTextWidget()); + + label = new Label(composite, SWT.NONE); + label.setText("Start time"); + + startTime = new TextWidget(composite, support, SWT.BORDER | SWT.RIGHT); + startTime.setTextFactory(new DoublePropertyFactory(context, SysdynResource.URIs.HasStartTime)); + startTime.addVerifyListener(new DoubleVerify()); + startTime.addFocusListener(new SaveListener()); + GridDataFactory.fillDefaults().hint(200, SWT.DEFAULT).applyTo(startTime.getTextWidget()); + + label = new Label(composite, SWT.NONE); + label.setText("Stop time"); + + stopTime = new TextWidget(composite, support, SWT.BORDER | SWT.RIGHT); + stopTime.setTextFactory(new DoublePropertyFactory(context, SysdynResource.URIs.HasStopTime)); + stopTime.addVerifyListener(new DoubleVerify()); + stopTime.addFocusListener(new SaveListener()); + GridDataFactory.fillDefaults().hint(200, SWT.DEFAULT).applyTo(stopTime.getTextWidget()); } + private class DoubleVerify implements VerifyListener { + @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; + } + } + } + } + } + + 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 e398245a..b581a06a 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 @@ -25,13 +25,13 @@ 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.ExpressionWidget; -import org.simantics.sysdyn.ui.properties.widgets.NameWidget; +import org.simantics.sysdyn.ui.properties.widgets.VariableNameWidget; import org.simantics.sysdyn.ui.properties.widgets.ShortcutTabWidget; import org.simantics.sysdyn.ui.properties.widgets.UnitWidget; public class EquationTab extends PropertyTabContributorImpl { - NameWidget nameWidget; + VariableNameWidget nameWidget; ExpressionTypeWidget expressionTypeWidget; UnitWidget unitWidget; ShortcutTabWidget shortcutTabWidget; @@ -42,7 +42,7 @@ 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 NameWidget(composite, support, SWT.BORDER); + nameWidget = new VariableNameWidget(composite, support, SWT.BORDER); nameWidget.setTextFactory(new StringPropertyFactory(context, Builtins.URIs.HasName)); diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/ResourceSelectionProcessor.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/ResourceSelectionProcessor.java index 83d0eca1..57f162b2 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/ResourceSelectionProcessor.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/ResourceSelectionProcessor.java @@ -1,6 +1,5 @@ package org.simantics.sysdyn.ui.properties; -import java.util.ArrayList; import java.util.Collection; import java.util.Collections; @@ -21,7 +20,7 @@ public class ResourceSelectionProcessor implements SelectionProcessor tabs = new ArrayList(); +// ArrayList tabs = new ArrayList(); if (backend.isInstanceOf(r, sr.IndependentVariable)) return Collections.singleton( new ComparableTabContributor( @@ -39,21 +38,21 @@ public class ResourceSelectionProcessor implements SelectionProcessor 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(Object input) { + if(input instanceof ISelection) + resource = ISelectionUtils.filterSingleSelection((ISelection)input, Resource.class); + + if(readFactory != null) { + readFactory.listen(input, new Listener() { + + @Override + public void exception(Throwable t) { + t.printStackTrace(); + } + + @Override + public void execute(final String text) { + + if(text == null) 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(); + + SimanticsUI.getSession().asyncRequest(new WriteRequest() { + + @Override + public void perform(WriteGraph graph) throws DatabaseException { + + Triple triple = null; + Object object = null; + + if(readFactory instanceof DoublePropertyFactory) { + DoublePropertyFactory factory = (DoublePropertyFactory)readFactory; + triple = (Triple) factory.getIdentity(null); + object = Double.parseDouble(text); + } else if (readFactory instanceof StringPropertyFactory) { + StringPropertyFactory factory = (StringPropertyFactory)readFactory; + triple = (Triple) factory.getIdentity(null); + object = text; + } + + if(triple == null || object == null) return; + graph.claimValue(resource, graph.getResource((String)triple.second), object); + } + }); + + } + + 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/NameWidget.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/VariableNameWidget.java similarity index 95% rename from org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/NameWidget.java rename to org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/VariableNameWidget.java index ed83f234..ab10229e 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/NameWidget.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/VariableNameWidget.java @@ -34,7 +34,7 @@ import org.simantics.sysdyn.representation.Variable; import org.simantics.ui.SimanticsUI; import org.simantics.utils.ui.ISelectionUtils; -public class NameWidget implements Widget { +public class VariableNameWidget implements Widget { private org.eclipse.swt.widgets.Text nameText; private String originalName = ""; @@ -42,7 +42,7 @@ public class NameWidget implements Widget { private Resource variable; private Resource configuration; - public NameWidget(Composite parent, WidgetSupport support, int style) { + public VariableNameWidget(Composite parent, WidgetSupport support, int style) { support.register(this); nameText = new org.eclipse.swt.widgets.Text(parent, style); diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/factories/DoublePropertyFactory.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/factories/DoublePropertyFactory.java new file mode 100644 index 00000000..f7a5d666 --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/factories/DoublePropertyFactory.java @@ -0,0 +1,32 @@ +package org.simantics.sysdyn.ui.properties.widgets.factories; + +import org.simantics.browsing.ui.swt.widgets.impl.ReadFactoryImpl; +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.management.ISessionContext; +import org.simantics.utils.datastructures.Triple; + +public class DoublePropertyFactory extends ReadFactoryImpl { + + final private String propertyURI; + + public DoublePropertyFactory(ISessionContext context, String propertyURI) { + super(context); + this.propertyURI = propertyURI; + } + + @Override + public Object getIdentity(Object inputContents) { + return new Triple((Resource)inputContents, propertyURI, getClass()); + } + + @Override + public String perform(ReadGraph graph, Resource issue) throws DatabaseException { + + Double value = graph.getPossibleRelatedValue(issue, graph.getResource(propertyURI)); + return value.toString(); + + } + +}