--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in 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.diagram.adapter;\r
+\r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.WriteGraph;\r
+import org.simantics.db.common.utils.OrderedSetUtils;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.diagram.flag.DiagramFlagPreferences;\r
+import org.simantics.diagram.flag.FlagLabelingScheme;\r
+import org.simantics.diagram.flag.FlagUtil;\r
+import org.simantics.diagram.stubs.DiagramResource;\r
+import org.simantics.diagram.synchronization.graph.DiagramGraphUtil;\r
+import org.simantics.diagram.synchronization.graph.ElementWriter;\r
+import org.simantics.g2d.diagram.IDiagram;\r
+import org.simantics.g2d.diagram.handler.DataElementMap;\r
+import org.simantics.g2d.element.ElementUtils;\r
+import org.simantics.g2d.element.IElement;\r
+import org.simantics.g2d.elementclass.FlagClass;\r
+import org.simantics.g2d.elementclass.FlagHandler;\r
+import org.simantics.layer0.Layer0;\r
+\r
+/**\r
+ * @author Tuukka Lehtonen\r
+ */\r
+public class FlagWriter implements ElementWriter {\r
+\r
+ @Override\r
+ public void addToGraph(WriteGraph g, IElement flag, Resource flagResource) throws DatabaseException {\r
+ DiagramResource dia = DiagramResource.getInstance(g);\r
+ Layer0 l0 = Layer0.getInstance(g);\r
+\r
+ FlagHandler fh = flag.getElementClass().getSingleItem(FlagHandler.class);\r
+ FlagClass.Type type = fh.getType(flag);\r
+ String text = ElementUtils.getText(flag);\r
+ if (text != null && text.isEmpty())\r
+ text = null;\r
+\r
+ g.deny(flagResource, dia.HasFlagType);\r
+ g.claim(flagResource, dia.HasFlagType, null, DiagramGraphUtil.toFlagTypeResource(dia, type));\r
+\r
+ Resource counterpart = null;\r
+\r
+ FlagClass.Connection<IElement> conn = fh.getConnection(flag);\r
+ if (conn != null) {\r
+ IElement counterpartE = fh.getCorrespondence(flag);\r
+ IDiagram od = ElementUtils.getDiagram(counterpartE);\r
+ DataElementMap dem = od.getDiagramClass().getSingleItem(DataElementMap.class);\r
+ counterpart = (Resource) dem.getData(od, counterpartE);\r
+ }\r
+\r
+ if (counterpart != null) {\r
+ FlagUtil.join(g, flagResource, counterpart);\r
+\r
+ Resource diagram = OrderedSetUtils.getSingleOwnerList(g, flagResource, dia.Diagram);\r
+ Resource counterpartDiagram = OrderedSetUtils.getSingleOwnerList(g, counterpart, dia.Diagram);\r
+\r
+ // Only give labels if flags are locally connected.\r
+ if (diagram.equals(counterpartDiagram)) {\r
+ // Only set labels if this is a flag pair.\r
+ if (text == null) {\r
+ // Try to generate label if not specfied externally.\r
+ FlagLabelingScheme scheme = DiagramFlagPreferences.getActiveFlagLabelingScheme(g);\r
+ if (scheme != null) {\r
+ text = scheme.generateLabel(g, diagram);\r
+ }\r
+ }\r
+\r
+ if (text != null) {\r
+ g.claimLiteral(flagResource, l0.HasLabel, dia.FlagLabel, text, Bindings.STRING);\r
+ g.claimLiteral(counterpart, l0.HasLabel, dia.FlagLabel, text, Bindings.STRING);\r
+ }\r
+ }\r
+ }\r
+ }\r
+\r
+ @Override\r
+ public void removeFromGraph(WriteGraph graph, Resource flag) throws DatabaseException {\r
+ FlagUtil.disconnectFlag(graph, flag);\r
+ }\r
+\r
+}\r