import org.eclipse.swt.SWT;\r
import org.eclipse.swt.widgets.Composite;\r
import org.eclipse.ui.IWorkbenchSite;\r
+import org.simantics.browsing.ui.swt.widgets.Button;\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.ReadFactoryImpl;\r
+import org.simantics.browsing.ui.swt.widgets.impl.SelectionListenerImpl;\r
import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport;\r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.databoard.util.ObjectUtils;\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.utils.datastructures.Pair;\r
+import org.simantics.utils.datastructures.Triple;\r
\r
public class DependencyTab extends LabelPropertyTabContributor {\r
\r
+ Button none, plus, minus, other;\r
+ TrackedText polarityText;\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).numColumns(6).applyTo(composite);\r
- TrackedText nameText = new TrackedText(composite, support, SWT.BORDER);\r
- nameText.setTextFactory(new StringPropertyFactory(SysdynResource.URIs.Polarity));\r
- nameText.addModifyListener(new StringPropertyModifier(context, SysdynResource.URIs.Polarity));\r
- GridDataFactory.fillDefaults().grab(true, false).applyTo(nameText.getWidget());\r
+ \r
+ none = new Button(composite, support, SWT.RADIO);\r
+ none.setText("None");\r
+ none.setSelectionFactory(new PolarityRadioSelectionFactory(""));\r
+ none.addSelectionListener(new PolaritySelectionListener(context, ""));\r
+ \r
+ plus = new Button(composite, support, SWT.RADIO);\r
+ plus.setText("+");\r
+ plus.setSelectionFactory(new PolarityRadioSelectionFactory("+"));\r
+ plus.addSelectionListener(new PolaritySelectionListener(context, "+"));\r
+ \r
+ minus = new Button(composite, support, SWT.RADIO);\r
+ minus.setText("-");\r
+ minus.setSelectionFactory(new PolarityRadioSelectionFactory("-"));\r
+ minus.addSelectionListener(new PolaritySelectionListener(context, "-"));\r
+ \r
+ other = new Button(composite, support, SWT.RADIO);\r
+ other.setText("other");\r
+ other.setSelectionFactory(new OtherPolaritySelectionFactory(new String[] {null, "+", "-", ""}));\r
+ \r
+ polarityText = new TrackedText(composite, support, SWT.BORDER);\r
+ polarityText.setTextFactory(new StringPropertyFactory(SysdynResource.URIs.Polarity));\r
+ polarityText.addModifyListener(new StringPropertyModifier(context, SysdynResource.URIs.Polarity));\r
+ GridDataFactory.fillDefaults().grab(true, false).applyTo(polarityText.getWidget());\r
}\r
+ \r
+ class PolaritySelectionListener extends SelectionListenerImpl<Resource> {\r
+ private String polarity;\r
\r
+ public PolaritySelectionListener(ISessionContext context, String polarity) {\r
+ super(context);\r
+ this.polarity = polarity;\r
+ }\r
+ \r
+ @Override\r
+ public void apply(WriteGraph graph, Resource connectionElement) throws DatabaseException {\r
+ SysdynResource sr = SysdynResource.getInstance(graph);\r
+ graph.claimLiteral(connectionElement, sr.Polarity, polarity.trim());\r
+ }\r
+ \r
+ }\r
+ \r
+ class PolarityRadioSelectionFactory extends ReadFactoryImpl<Resource, Boolean> {\r
+ private String polarity;\r
+\r
+ public PolarityRadioSelectionFactory(String polarity) {\r
+ this.polarity = polarity;\r
+ }\r
+\r
+ @Override\r
+ public Object getIdentity(Object inputContents) {\r
+ return new Triple<Object, Object, Class<?>>(inputContents, polarity, getClass());\r
+ }\r
+\r
+ @Override\r
+ public Boolean perform(ReadGraph graph, Resource dependencyConnection) throws DatabaseException {\r
+ SysdynResource sr = SysdynResource.getInstance(graph);\r
+ String polarity = graph.getPossibleRelatedValue(dependencyConnection, sr.Polarity, Bindings.STRING); \r
+ if(polarity == null && this.polarity.equals(""))\r
+ return true;\r
+ return ObjectUtils.objectEquals(polarity, this.polarity);\r
+ }\r
+ }\r
+ \r
+ class OtherPolaritySelectionFactory extends ReadFactoryImpl<Resource, Boolean> {\r
+\r
+ String[] limits;\r
+ \r
+ public OtherPolaritySelectionFactory(String[] limits) {\r
+ this.limits = limits;\r
+ }\r
+ \r
+ @Override\r
+ public Object getIdentity(Object inputContents) {\r
+ return new Pair<Object, Class<?>>(inputContents, getClass());\r
+ }\r
+\r
+ @Override\r
+ public Boolean perform(ReadGraph graph, Resource dependencyConnection) throws DatabaseException {\r
+ SysdynResource sr = SysdynResource.getInstance(graph);\r
+ String polarity = graph.getPossibleRelatedValue(dependencyConnection, sr.Polarity, Bindings.STRING);\r
+ for(String s : limits) {\r
+ if(ObjectUtils.objectEquals(polarity, s))\r
+ return false;\r
+ }\r
+ return true;\r
+ }\r
+ }\r
\r
}\r