]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram/src/org/simantics/diagram/adapter/FlagWriter.java
Add stack trace to document server event handler error messages
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / adapter / FlagWriter.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.diagram.adapter;
13
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;
32
33 /**
34  * @author Tuukka Lehtonen
35  */
36 public class FlagWriter implements ElementWriter {
37
38     @Override
39     public void addToGraph(WriteGraph g, IElement flag, Resource flagResource) throws DatabaseException {
40         DiagramResource dia = DiagramResource.getInstance(g);
41         Layer0 l0 = Layer0.getInstance(g);
42
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())
47             text = null;
48
49         g.deny(flagResource, dia.HasFlagType);
50         g.claim(flagResource, dia.HasFlagType, null, DiagramGraphUtil.toFlagTypeResource(dia, type));
51
52         Resource counterpart = null;
53
54         FlagClass.Connection<IElement> conn = fh.getConnection(flag);
55         if (conn != null) {
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);
60         }
61
62         if (counterpart != null) {
63             FlagUtil.join(g, flagResource, counterpart);
64
65             Resource diagram = OrderedSetUtils.getSingleOwnerList(g, flagResource, dia.Diagram);
66             Resource counterpartDiagram = OrderedSetUtils.getSingleOwnerList(g, counterpart, dia.Diagram);
67
68             // Only give labels if flags are locally connected.
69             if (diagram.equals(counterpartDiagram)) {
70                 // Only set labels if this is a flag pair.
71                 if (text == null) {
72                     // Try to generate label if not specfied externally.
73                     FlagLabelingScheme scheme = DiagramFlagPreferences.getActiveFlagLabelingScheme(g);
74                     if (scheme != null) {
75                         text = scheme.generateLabel(g, diagram);
76                     }
77                 }
78
79                 if (text != null) {
80                     g.claimLiteral(flagResource, l0.HasLabel, dia.FlagLabel, text, Bindings.STRING);
81                     g.claimLiteral(counterpart, l0.HasLabel, dia.FlagLabel, text, Bindings.STRING);
82                 }
83             }
84         }
85     }
86
87     @Override
88     public void removeFromGraph(WriteGraph graph, Resource flag) throws DatabaseException {
89         FlagUtil.disconnectFlag(graph, flag);
90     }
91
92 }