1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.diagram.adapter;
14 import org.simantics.databoard.Bindings;
15 import org.simantics.db.Resource;
16 import org.simantics.db.WriteGraph;
17 import org.simantics.db.common.utils.OrderedSetUtils;
18 import org.simantics.db.exception.DatabaseException;
19 import org.simantics.diagram.flag.DiagramFlagPreferences;
20 import org.simantics.diagram.flag.FlagLabelingScheme;
21 import org.simantics.diagram.flag.FlagUtil;
22 import org.simantics.diagram.stubs.DiagramResource;
23 import org.simantics.diagram.synchronization.graph.DiagramGraphUtil;
24 import org.simantics.diagram.synchronization.graph.ElementWriter;
25 import org.simantics.g2d.diagram.IDiagram;
26 import org.simantics.g2d.diagram.handler.DataElementMap;
27 import org.simantics.g2d.element.ElementUtils;
28 import org.simantics.g2d.element.IElement;
29 import org.simantics.g2d.elementclass.FlagClass;
30 import org.simantics.g2d.elementclass.FlagHandler;
31 import org.simantics.layer0.Layer0;
34 * @author Tuukka Lehtonen
36 public class FlagWriter implements ElementWriter {
39 public void addToGraph(WriteGraph g, IElement flag, Resource flagResource) throws DatabaseException {
40 DiagramResource dia = DiagramResource.getInstance(g);
41 Layer0 l0 = Layer0.getInstance(g);
43 FlagHandler fh = flag.getElementClass().getSingleItem(FlagHandler.class);
44 FlagClass.Type type = fh.getType(flag);
45 String text = ElementUtils.getText(flag);
46 if (text != null && text.isEmpty())
49 g.deny(flagResource, dia.HasFlagType);
50 g.claim(flagResource, dia.HasFlagType, null, DiagramGraphUtil.toFlagTypeResource(dia, type));
52 Resource counterpart = null;
54 FlagClass.Connection<IElement> conn = fh.getConnection(flag);
56 IElement counterpartE = fh.getCorrespondence(flag);
57 IDiagram od = ElementUtils.getDiagram(counterpartE);
58 DataElementMap dem = od.getDiagramClass().getSingleItem(DataElementMap.class);
59 counterpart = (Resource) dem.getData(od, counterpartE);
62 if (counterpart != null) {
63 FlagUtil.join(g, flagResource, counterpart);
65 Resource diagram = OrderedSetUtils.getSingleOwnerList(g, flagResource, dia.Diagram);
66 Resource counterpartDiagram = OrderedSetUtils.getSingleOwnerList(g, counterpart, dia.Diagram);
68 // Only give labels if flags are locally connected.
69 if (diagram.equals(counterpartDiagram)) {
70 // Only set labels if this is a flag pair.
72 // Try to generate label if not specfied externally.
73 FlagLabelingScheme scheme = DiagramFlagPreferences.getActiveFlagLabelingScheme(g);
75 text = scheme.generateLabel(g, diagram);
80 g.claimLiteral(flagResource, l0.HasLabel, dia.FlagLabel, text, Bindings.STRING);
81 g.claimLiteral(counterpart, l0.HasLabel, dia.FlagLabel, text, Bindings.STRING);
88 public void removeFromGraph(WriteGraph graph, Resource flag) throws DatabaseException {
89 FlagUtil.disconnectFlag(graph, flag);