From 649eb7851e264b454911b4cd9486a16bc8fb0aef Mon Sep 17 00:00:00 2001 From: lempinen Date: Thu, 6 Oct 2011 13:04:29 +0000 Subject: [PATCH] Controls for polarities git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@22557 ac1ea38d-2e2b-0410-8846-a27921b304fc --- .../sysdyn/ui/properties/DependencyTab.java | 104 +++++++++++++++++- 1 file changed, 100 insertions(+), 4 deletions(-) diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/DependencyTab.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/DependencyTab.java index 6b72b35c..125fe3ac 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/DependencyTab.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/DependencyTab.java @@ -16,25 +16,121 @@ import org.eclipse.jface.layout.GridLayoutFactory; import org.eclipse.swt.SWT; import org.eclipse.swt.widgets.Composite; import org.eclipse.ui.IWorkbenchSite; +import org.simantics.browsing.ui.swt.widgets.Button; 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.ReadFactoryImpl; +import org.simantics.browsing.ui.swt.widgets.impl.SelectionListenerImpl; import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport; +import org.simantics.databoard.Bindings; +import org.simantics.databoard.util.ObjectUtils; +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.utils.datastructures.Pair; +import org.simantics.utils.datastructures.Triple; public class DependencyTab extends LabelPropertyTabContributor { + Button none, plus, minus, other; + TrackedText polarityText; + @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).numColumns(6).applyTo(composite); - TrackedText nameText = new TrackedText(composite, support, SWT.BORDER); - nameText.setTextFactory(new StringPropertyFactory(SysdynResource.URIs.Polarity)); - nameText.addModifyListener(new StringPropertyModifier(context, SysdynResource.URIs.Polarity)); - GridDataFactory.fillDefaults().grab(true, false).applyTo(nameText.getWidget()); + + none = new Button(composite, support, SWT.RADIO); + none.setText("None"); + none.setSelectionFactory(new PolarityRadioSelectionFactory("")); + none.addSelectionListener(new PolaritySelectionListener(context, "")); + + plus = new Button(composite, support, SWT.RADIO); + plus.setText("+"); + plus.setSelectionFactory(new PolarityRadioSelectionFactory("+")); + plus.addSelectionListener(new PolaritySelectionListener(context, "+")); + + minus = new Button(composite, support, SWT.RADIO); + minus.setText("-"); + minus.setSelectionFactory(new PolarityRadioSelectionFactory("-")); + minus.addSelectionListener(new PolaritySelectionListener(context, "-")); + + other = new Button(composite, support, SWT.RADIO); + other.setText("other"); + other.setSelectionFactory(new OtherPolaritySelectionFactory(new String[] {null, "+", "-", ""})); + + polarityText = new TrackedText(composite, support, SWT.BORDER); + polarityText.setTextFactory(new StringPropertyFactory(SysdynResource.URIs.Polarity)); + polarityText.addModifyListener(new StringPropertyModifier(context, SysdynResource.URIs.Polarity)); + GridDataFactory.fillDefaults().grab(true, false).applyTo(polarityText.getWidget()); } + + class PolaritySelectionListener extends SelectionListenerImpl { + private String polarity; + public PolaritySelectionListener(ISessionContext context, String polarity) { + super(context); + this.polarity = polarity; + } + + @Override + public void apply(WriteGraph graph, Resource connectionElement) throws DatabaseException { + SysdynResource sr = SysdynResource.getInstance(graph); + graph.claimLiteral(connectionElement, sr.Polarity, polarity.trim()); + } + + } + + class PolarityRadioSelectionFactory extends ReadFactoryImpl { + private String polarity; + + public PolarityRadioSelectionFactory(String polarity) { + this.polarity = polarity; + } + + @Override + public Object getIdentity(Object inputContents) { + return new Triple>(inputContents, polarity, getClass()); + } + + @Override + public Boolean perform(ReadGraph graph, Resource dependencyConnection) throws DatabaseException { + SysdynResource sr = SysdynResource.getInstance(graph); + String polarity = graph.getPossibleRelatedValue(dependencyConnection, sr.Polarity, Bindings.STRING); + if(polarity == null && this.polarity.equals("")) + return true; + return ObjectUtils.objectEquals(polarity, this.polarity); + } + } + + class OtherPolaritySelectionFactory extends ReadFactoryImpl { + + String[] limits; + + public OtherPolaritySelectionFactory(String[] limits) { + this.limits = limits; + } + + @Override + public Object getIdentity(Object inputContents) { + return new Pair>(inputContents, getClass()); + } + + @Override + public Boolean perform(ReadGraph graph, Resource dependencyConnection) throws DatabaseException { + SysdynResource sr = SysdynResource.getInstance(graph); + String polarity = graph.getPossibleRelatedValue(dependencyConnection, sr.Polarity, Bindings.STRING); + for(String s : limits) { + if(ObjectUtils.objectEquals(polarity, s)) + return false; + } + return true; + } + } } -- 2.47.1