]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram/src/org/simantics/diagram/query/FlagTextQuery.java
Changed external reference flag texts to obey current name/label mode
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / query / FlagTextQuery.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2017 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  *     Semantum Oy - #7178 Use NameLabelUtil
12  *******************************************************************************/
13 package org.simantics.diagram.query;
14
15 import java.util.ArrayList;
16 import java.util.Collection;
17
18 import org.simantics.NameLabelUtil;
19 import org.simantics.db.ReadGraph;
20 import org.simantics.db.Resource;
21 import org.simantics.db.common.request.ResourceRead;
22 import org.simantics.db.common.utils.NameUtils;
23 import org.simantics.db.exception.DatabaseException;
24 import org.simantics.diagram.stubs.DiagramResource;
25 import org.simantics.layer0.Layer0;
26 import org.simantics.layer0.utils.binaryPredicates.OrderedSetElementsPredicate;
27 import org.simantics.modeling.ModelingResources;
28 import org.simantics.structural.stubs.StructuralResource2;
29 import org.simantics.structural2.utils.StructuralUtils;
30
31 /**
32  * @author Hannu Niemistö
33  * @author Tuukka Lehtonen
34  */
35 public class FlagTextQuery extends ResourceRead<String[]> {
36
37     private static final boolean DEBUG = false;
38
39     static final String[] NO_RESULT = new String[] { "?", "?" };
40
41     public FlagTextQuery(Resource flag) {
42         super(flag);
43     }
44
45     @Override
46     public String[] perform(ReadGraph g) throws DatabaseException {
47         Layer0 l0 = Layer0.getInstance(g);
48         StructuralResource2 sr = StructuralResource2.getInstance(g);
49         DiagramResource dr = DiagramResource.getInstance(g);
50
51         if (DEBUG)
52             System.out.println(getClass().getSimpleName() + "(" + NameUtils.getSafeName(g, resource) + ")");
53
54         Resource connectionJoin = g.getPossibleObject(resource, dr.FlagIsJoinedBy);
55         if (DEBUG)
56             System.out.println(getClass().getSimpleName() + " connectionJoin: " + NameUtils.getSafeName(g, connectionJoin));
57         if (connectionJoin == null)
58             return NO_RESULT;
59
60         Resource diagram = getFirstOwnerDiagram(g, resource);
61         if (diagram == null)
62             return NO_RESULT;
63
64         // Get structural composite of flag's diagram.
65         String DiagramToCompositeURI = ModelingResources.URIs.DiagramToComposite;
66         Resource DiagramToComposite = g.getResource(DiagramToCompositeURI);
67         Resource composite = g.getPossibleObject(diagram, DiagramToComposite);
68         if (DEBUG)
69             System.out.println(getClass().getSimpleName() + " composite: " + NameUtils.getSafeName(g, composite));
70         if (composite == null)
71             return NO_RESULT;
72
73         // Find the connection that is on this diagram's composite and exclude
74         // it from browsing of connections related to the connection join.
75         Collection<Resource> connectionsOnFlagDiagram = findConnectionOnCompositeFromJoin(g, composite, connectionJoin);
76
77         for (Resource connection : StructuralUtils.getRelatedConnectionsOfConnectionJoin(g, connectionJoin, connectionsOnFlagDiagram)) {
78             if (DEBUG)
79                 System.out.println(getClass().getSimpleName() + " connection: " + NameUtils.getSafeName(g, connection));
80             for (Resource component : g.getObjects(connection, sr.Connects)) {
81                 if (DEBUG)
82                     System.out.println(getClass().getSimpleName() + " connects: " + NameUtils.getSafeName(g, component));
83                 // TODO how to correctly resolve flag text in cases where
84                 // the other diagram's connection has many attachments ?
85                 return new String[] {
86                         getSafeLabel(g, g.getSingleObject(component, l0.PartOf)),
87                         getSafeLabel(g, component)
88                 };
89             }
90         }
91
92         return NO_RESULT;
93     }
94
95     private Resource getFirstOwnerDiagram(ReadGraph graph, Resource diagramPart) throws DatabaseException {
96         Resource diagram = null;
97         for (Resource temp : OrderedSetElementsPredicate.INSTANCE.getSubjects(graph, resource)) {
98             diagram = temp;
99             break;
100         }
101         if (DEBUG)
102             System.out.println(getClass().getSimpleName() + " getFirstOwnerDiagram(" + NameUtils.getSafeName(graph, diagramPart) + "): " + NameUtils.getSafeName(graph, diagram));
103         return diagram;
104     }
105
106     /**
107      * Find the connections that are related to the specified connection join
108      * and on the specified composite.
109      * 
110      * @param graph
111      * @param composite
112      * @param connectionJoin
113      * @return
114      * @throws DatabaseException
115      */
116     private Collection<Resource> findConnectionOnCompositeFromJoin(ReadGraph graph, Resource composite, Resource connectionJoin) throws DatabaseException {
117         StructuralResource2 sr = StructuralResource2.getInstance(graph);
118         Collection<Resource> result = new ArrayList<Resource>(1);
119         for (Resource connection : graph.getObjects(connectionJoin, sr.Joins)) {
120             if (DEBUG)
121                 System.out.println(getClass().getSimpleName() + " joins connection: " + NameUtils.getSafeName(graph, connection));
122             if (StructuralUtils.isConnectionInComposite(graph, connection, composite))
123                 result.add(connection);
124         }
125         return result;
126     }
127
128     public static String getSafeLabel(ReadGraph graph, Resource r) throws DatabaseException {
129         return NameLabelUtil.modalName(graph, r);
130     }
131
132 }