--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2010 Association for Decentralized Information Management in\r
+ * Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.sysdyn.ui.properties;\r
+\r
+import java.util.ArrayList;\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.widgets.Composite;\r
+import org.eclipse.swt.widgets.Group;\r
+import org.eclipse.ui.IWorkbenchSite;\r
+import org.simantics.browsing.ui.swt.widgets.Button;\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.TextModifyListenerImpl;\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.sysdyn.ui.elements.connections.DependencyNode;\r
+import org.simantics.utils.datastructures.Pair;\r
+import org.simantics.utils.datastructures.Triple;\r
+\r
+/**\r
+ * Tab for multiple dependencies.\r
+ * @author Tuomas Miettinen\r
+ *\r
+ */\r
+public class ArrayDependencyTab extends LabelPropertyTabContributor {\r
+\r
+ Button none, plus, minus, other, inside, outside;\r
+ TrackedText polarityText, polarityLocationText;\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(3).applyTo(composite);\r
+ \r
+ /* Add a dummy button at the begin to suck in the nonsense selection events that\r
+ * the first radio button in the group of ALL the radio buttons receives when no\r
+ * radio is selected and then one is selected in other group than where the first \r
+ * radio button lies. \r
+ */\r
+ Group hiddenDefaultbuttonGroup = new Group(composite, SWT.NONE);\r
+ GridDataFactory.fillDefaults().exclude(true).applyTo(hiddenDefaultbuttonGroup);\r
+ GridLayoutFactory.fillDefaults().applyTo(hiddenDefaultbuttonGroup);\r
+ new Button(hiddenDefaultbuttonGroup, support, SWT.RADIO);\r
+ \r
+ Group polarityGroup = new Group(composite, SWT.NONE);\r
+ polarityGroup.setText("Polarity");\r
+ GridDataFactory.fillDefaults().grab(true, false).applyTo(polarityGroup);\r
+ GridLayoutFactory.fillDefaults().numColumns(5).applyTo(polarityGroup);\r
+ \r
+ none = new Button(polarityGroup, support, SWT.RADIO);\r
+ none.setText("None");\r
+ none.setSelectionFactory(new PolarityRadioSelectionFactory(""));\r
+ none.addSelectionListener(new PolaritySelectionListener(context, ""));\r
+ \r
+ plus = new Button(polarityGroup, support, SWT.RADIO);\r
+ plus.setText("+");\r
+ plus.setSelectionFactory(new PolarityRadioSelectionFactory("+"));\r
+ plus.addSelectionListener(new PolaritySelectionListener(context, "+"));\r
+ \r
+ minus = new Button(polarityGroup, support, SWT.RADIO);\r
+ minus.setText("-");\r
+ minus.setSelectionFactory(new PolarityRadioSelectionFactory("-"));\r
+ minus.addSelectionListener(new PolaritySelectionListener(context, "-"));\r
+ \r
+ other = new Button(polarityGroup, support, SWT.RADIO);\r
+ other.setText("other");\r
+ other.setSelectionFactory(new OtherPolaritySelectionFactory(new String[] {null, "+", "-", ""}));\r
+ \r
+ polarityText = new TrackedText(polarityGroup, support, SWT.BORDER);\r
+ polarityText.setTextFactory(new OtherPolarityStringPropertyFactory());\r
+ polarityText.addModifyListener(new OtherPolarityStringPropertyModifier());\r
+ GridDataFactory.fillDefaults().grab(true, false).applyTo(polarityText.getWidget());\r
+ \r
+ Group locationGroup = new Group(composite, SWT.NONE);\r
+ GridDataFactory.fillDefaults().applyTo(locationGroup);\r
+ GridLayoutFactory.fillDefaults().applyTo(locationGroup);\r
+ locationGroup.setText("Location");\r
+ \r
+ inside = new Button(locationGroup, support, SWT.RADIO);\r
+ inside.setText("Inside");\r
+ inside.setSelectionFactory(new PolarityLocationRadioSelectionFactory(DependencyNode.INSIDE));\r
+ inside.addSelectionListener(new PolarityLocationSelectionListener(context, DependencyNode.INSIDE));\r
+ \r
+ outside = new Button(locationGroup, support, SWT.RADIO);\r
+ outside.setText("Outside");\r
+ outside.setSelectionFactory(new PolarityLocationRadioSelectionFactory(DependencyNode.OUTSIDE));\r
+ outside.addSelectionListener(new PolarityLocationSelectionListener(context, DependencyNode.OUTSIDE));\r
+ }\r
+ \r
+ class OtherPolarityStringPropertyFactory extends ReadFactoryImpl<ArrayList<Resource>, String> {\r
+\r
+ private final String propertyURI;\r
+\r
+ public OtherPolarityStringPropertyFactory() {\r
+ this.propertyURI = SysdynResource.URIs.DependencyConnection_polarity;\r
+ }\r
+\r
+ @SuppressWarnings("unchecked")\r
+ @Override\r
+ public Object getIdentity(Object inputContents) {\r
+ return new Triple<ArrayList<Resource>, String, Object>((ArrayList<Resource>) inputContents, propertyURI, getClass());\r
+ }\r
+\r
+ @Override\r
+ public String perform(ReadGraph graph, ArrayList<Resource> resources) throws DatabaseException {\r
+ String value = graph.getPossibleRelatedValue(resources.get(0), graph.getResource(propertyURI));;\r
+ if (value == null)\r
+ return "";\r
+ for (Resource r : resources) {\r
+ String s = graph.getPossibleRelatedValue(r, graph.getResource(propertyURI));\r
+ if (!value.equals(s))\r
+ return "";\r
+ }\r
+ return value;\r
+ } \r
+ }\r
+ \r
+ class OtherPolarityStringPropertyModifier extends TextModifyListenerImpl<ArrayList<Resource>> {\r
+\r
+ final private String propertyURI;\r
+ \r
+ public OtherPolarityStringPropertyModifier() {\r
+ this.propertyURI = SysdynResource.URIs.DependencyConnection_polarity;\r
+ }\r
+\r
+ @Override\r
+ public void applyText(WriteGraph graph, ArrayList<Resource> resources, String text) throws DatabaseException {\r
+ for (Resource r : resources)\r
+ graph.claimLiteral(r, graph.getResource(propertyURI), text, Bindings.STRING);\r
+ }\r
+ \r
+ }\r
+\r
+ class PolarityLocationSelectionListener extends SelectionListenerImpl<ArrayList<Resource>> {\r
+ private String location;\r
+\r
+ public PolarityLocationSelectionListener(ISessionContext context, String location) {\r
+ super(context);\r
+ this.location = location;\r
+ }\r
+ \r
+ @Override\r
+ public void apply(WriteGraph graph, ArrayList<Resource> connectionElements) throws DatabaseException {\r
+ SysdynResource sr = SysdynResource.getInstance(graph);\r
+ for (Resource r : connectionElements)\r
+ graph.claimLiteral(r, sr.DependencyConnection_polarityLocation, location);\r
+ }\r
+ \r
+ }\r
+ \r
+ class PolarityLocationRadioSelectionFactory extends ReadFactoryImpl<ArrayList<Resource>, Boolean> {\r
+ private String location;\r
+\r
+ public PolarityLocationRadioSelectionFactory(String location) {\r
+ this.location = location;\r
+ }\r
+\r
+ @Override\r
+ public Object getIdentity(Object inputContents) {\r
+ return new Triple<Object, Object, Class<?>>(inputContents, location, getClass());\r
+ }\r
+\r
+ @Override\r
+ public Boolean perform(ReadGraph graph, ArrayList<Resource> dependencyConnections) throws DatabaseException {\r
+ SysdynResource sr = SysdynResource.getInstance(graph);\r
+ for (Resource r : dependencyConnections) {\r
+ String location = graph.getPossibleRelatedValue(r, sr.DependencyConnection_polarityLocation, Bindings.STRING); \r
+ if (DependencyNode.OUTSIDE.equals(this.location)) {\r
+ if (ObjectUtils.objectEquals(this.location, location))\r
+ continue;\r
+ } else {\r
+ if (location == null) {\r
+ continue;\r
+ } else {\r
+ if (ObjectUtils.objectEquals(this.location, location))\r
+ continue;\r
+ }\r
+ }\r
+ return false;\r
+ }\r
+ return true;\r
+ }\r
+ }\r
+ \r
+ class PolaritySelectionListener extends SelectionListenerImpl<ArrayList<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, ArrayList<Resource> connectionElements) throws DatabaseException {\r
+ SysdynResource sr = SysdynResource.getInstance(graph);\r
+ for (Resource r : connectionElements)\r
+ graph.claimLiteral(r, sr.DependencyConnection_polarity, polarity.trim());\r
+ }\r
+ \r
+ }\r
+ \r
+ class PolarityRadioSelectionFactory extends ReadFactoryImpl<ArrayList<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, ArrayList<Resource> dependencyConnections) throws DatabaseException {\r
+ SysdynResource sr = SysdynResource.getInstance(graph);\r
+ for (Resource r : dependencyConnections) {\r
+ String polarity = graph.getPossibleRelatedValue(r, sr.DependencyConnection_polarity, Bindings.STRING); \r
+ if (polarity == null && this.polarity.equals(""))\r
+ continue;\r
+ if (ObjectUtils.objectEquals(polarity, this.polarity))\r
+ continue;\r
+ return false;\r
+ }\r
+ return true;\r
+ }\r
+ }\r
+ \r
+ class OtherPolaritySelectionFactory extends ReadFactoryImpl<ArrayList<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, ArrayList<Resource> dependencyConnections) throws DatabaseException {\r
+ SysdynResource sr = SysdynResource.getInstance(graph);\r
+ for (Resource r : dependencyConnections) {\r
+ String polarity = graph.getPossibleRelatedValue(r, sr.DependencyConnection_polarity, Bindings.STRING);\r
+ for(String s : limits) {\r
+ if(ObjectUtils.objectEquals(polarity, s))\r
+ return false;\r
+ }\r
+ }\r
+ return true;\r
+ }\r
+ }\r
+\r
+}\r