]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.diagram/src/org/simantics/diagram/query/FlagTextQuery.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / query / FlagTextQuery.java
index f7936207d1ad856c929a42311f8f2ed1db5baa4b..0c097c53d49ab75f4f01b40b0ff150317f56d013 100644 (file)
-/*******************************************************************************\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.query;\r
-\r
-import java.util.ArrayList;\r
-import java.util.Collection;\r
-\r
-import org.simantics.db.ReadGraph;\r
-import org.simantics.db.Resource;\r
-import org.simantics.db.Statement;\r
-import org.simantics.db.common.request.ResourceRead;\r
-import org.simantics.db.common.utils.NameUtils;\r
-import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.diagram.stubs.DiagramResource;\r
-import org.simantics.layer0.Layer0;\r
-import org.simantics.layer0.utils.binaryPredicates.OrderedSetElementsPredicate;\r
-import org.simantics.modeling.ModelingResources;\r
-import org.simantics.structural.stubs.StructuralResource2;\r
-import org.simantics.structural2.utils.StructuralUtils;\r
-\r
-/**\r
- * @author Hannu Niemistö\r
- * @author Tuukka Lehtonen\r
- */\r
-public class FlagTextQuery extends ResourceRead<String[]> {\r
-\r
-    private static final boolean DEBUG = false;\r
-\r
-    static final String[] NO_RESULT = new String[] { "?", "?" };\r
-\r
-    public FlagTextQuery(Resource flag) {\r
-        super(flag);\r
-    }\r
-\r
-    @Override\r
-    public String[] perform(ReadGraph g) throws DatabaseException {\r
-        Layer0 l0 = Layer0.getInstance(g);\r
-        StructuralResource2 sr = StructuralResource2.getInstance(g);\r
-        DiagramResource dr = DiagramResource.getInstance(g);\r
-\r
-        if (DEBUG)\r
-            System.out.println(getClass().getSimpleName() + "(" + NameUtils.getSafeName(g, resource) + ")");\r
-\r
-        Resource connectionJoin = g.getPossibleObject(resource, dr.FlagIsJoinedBy);\r
-        if (DEBUG)\r
-            System.out.println(getClass().getSimpleName() + " connectionJoin: " + NameUtils.getSafeName(g, connectionJoin));\r
-        if (connectionJoin == null)\r
-            return NO_RESULT;\r
-\r
-        Resource diagram = getFirstOwnerDiagram(g, resource);\r
-        if (diagram == null)\r
-            return NO_RESULT;\r
-\r
-        // Get structural composite of flag's diagram.\r
-        String DiagramToCompositeURI = ModelingResources.URIs.DiagramToComposite;\r
-        Resource DiagramToComposite = g.getResource(DiagramToCompositeURI);\r
-        Resource composite = g.getPossibleObject(diagram, DiagramToComposite);\r
-        if (DEBUG)\r
-            System.out.println(getClass().getSimpleName() + " composite: " + NameUtils.getSafeName(g, composite));\r
-        if (composite == null)\r
-            return NO_RESULT;\r
-\r
-        // Find the connection that is on this diagram's composite and exclude\r
-        // it from browsing of connections related to the connection join.\r
-        Collection<Resource> connectionsOnFlagDiagram = findConnectionOnCompositeFromJoin(g, composite, connectionJoin);\r
-\r
-        for (Resource connection : StructuralUtils.getRelatedConnectionsOfConnectionJoin(g, connectionJoin, connectionsOnFlagDiagram)) {\r
-            if (DEBUG)\r
-                System.out.println(getClass().getSimpleName() + " connection: " + NameUtils.getSafeName(g, connection));\r
-            for (Resource component : g.getObjects(connection, sr.Connects)) {\r
-                if (DEBUG)\r
-                    System.out.println(getClass().getSimpleName() + " connects: " + NameUtils.getSafeName(g, component));\r
-                // TODO how to correctly resolve flag text in cases where\r
-                // the other diagram's connection has many attachments ?\r
-                return new String[] {\r
-                        getSafeLabel(g, g.getSingleObject(component, l0.PartOf)),\r
-                        getSafeLabel(g, component)\r
-                };\r
-            }\r
-        }\r
-\r
-        return NO_RESULT;\r
-    }\r
-\r
-    private Resource getFirstOwnerDiagram(ReadGraph graph, Resource diagramPart) throws DatabaseException {\r
-        Resource diagram = null;\r
-        for (Resource temp : OrderedSetElementsPredicate.INSTANCE.getSubjects(graph, resource)) {\r
-            diagram = temp;\r
-            break;\r
-        }\r
-        if (DEBUG)\r
-            System.out.println(getClass().getSimpleName() + " getFirstOwnerDiagram(" + NameUtils.getSafeName(graph, diagramPart) + "): " + NameUtils.getSafeName(graph, diagram));\r
-        return diagram;\r
-    }\r
-\r
-    /**\r
-     * Find the connections that are related to the specified connection join\r
-     * and on the specified composite.\r
-     * \r
-     * @param graph\r
-     * @param composite\r
-     * @param connectionJoin\r
-     * @return\r
-     * @throws DatabaseException\r
-     */\r
-    private Collection<Resource> findConnectionOnCompositeFromJoin(ReadGraph graph, Resource composite, Resource connectionJoin) throws DatabaseException {\r
-        StructuralResource2 sr = StructuralResource2.getInstance(graph);\r
-        Collection<Resource> result = new ArrayList<Resource>(1);\r
-        for (Resource connection : graph.getObjects(connectionJoin, sr.Joins)) {\r
-            if (DEBUG)\r
-                System.out.println(getClass().getSimpleName() + " joins connection: " + NameUtils.getSafeName(graph, connection));\r
-            if (StructuralUtils.isConnectionInComposite(graph, connection, composite))\r
-                result.add(connection);\r
-        }\r
-        return result;\r
-    }\r
-\r
-    public static String getSafeLabel(ReadGraph graph, Resource r) throws DatabaseException {\r
-        Layer0 l0 = Layer0.getInstance(graph);\r
-        Statement stm = graph.getPossibleStatement(r, l0.HasLabel);\r
-        if (stm != null) {\r
-            String label = NameUtils.getSafeLabel(graph, r);\r
-            if (!label.isEmpty())\r
-                return label;\r
-            //if (!stm.isAsserted(r))\r
-            //    return label;\r
-        }\r
-        return NameUtils.getSafeName(graph, r);\r
-    }\r
-\r
-}\r
+/*******************************************************************************
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.diagram.query;
+
+import java.util.ArrayList;
+import java.util.Collection;
+
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.Statement;
+import org.simantics.db.common.request.ResourceRead;
+import org.simantics.db.common.utils.NameUtils;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.diagram.stubs.DiagramResource;
+import org.simantics.layer0.Layer0;
+import org.simantics.layer0.utils.binaryPredicates.OrderedSetElementsPredicate;
+import org.simantics.modeling.ModelingResources;
+import org.simantics.structural.stubs.StructuralResource2;
+import org.simantics.structural2.utils.StructuralUtils;
+
+/**
+ * @author Hannu Niemist&ouml;
+ * @author Tuukka Lehtonen
+ */
+public class FlagTextQuery extends ResourceRead<String[]> {
+
+    private static final boolean DEBUG = false;
+
+    static final String[] NO_RESULT = new String[] { "?", "?" };
+
+    public FlagTextQuery(Resource flag) {
+        super(flag);
+    }
+
+    @Override
+    public String[] perform(ReadGraph g) throws DatabaseException {
+        Layer0 l0 = Layer0.getInstance(g);
+        StructuralResource2 sr = StructuralResource2.getInstance(g);
+        DiagramResource dr = DiagramResource.getInstance(g);
+
+        if (DEBUG)
+            System.out.println(getClass().getSimpleName() + "(" + NameUtils.getSafeName(g, resource) + ")");
+
+        Resource connectionJoin = g.getPossibleObject(resource, dr.FlagIsJoinedBy);
+        if (DEBUG)
+            System.out.println(getClass().getSimpleName() + " connectionJoin: " + NameUtils.getSafeName(g, connectionJoin));
+        if (connectionJoin == null)
+            return NO_RESULT;
+
+        Resource diagram = getFirstOwnerDiagram(g, resource);
+        if (diagram == null)
+            return NO_RESULT;
+
+        // Get structural composite of flag's diagram.
+        String DiagramToCompositeURI = ModelingResources.URIs.DiagramToComposite;
+        Resource DiagramToComposite = g.getResource(DiagramToCompositeURI);
+        Resource composite = g.getPossibleObject(diagram, DiagramToComposite);
+        if (DEBUG)
+            System.out.println(getClass().getSimpleName() + " composite: " + NameUtils.getSafeName(g, composite));
+        if (composite == null)
+            return NO_RESULT;
+
+        // Find the connection that is on this diagram's composite and exclude
+        // it from browsing of connections related to the connection join.
+        Collection<Resource> connectionsOnFlagDiagram = findConnectionOnCompositeFromJoin(g, composite, connectionJoin);
+
+        for (Resource connection : StructuralUtils.getRelatedConnectionsOfConnectionJoin(g, connectionJoin, connectionsOnFlagDiagram)) {
+            if (DEBUG)
+                System.out.println(getClass().getSimpleName() + " connection: " + NameUtils.getSafeName(g, connection));
+            for (Resource component : g.getObjects(connection, sr.Connects)) {
+                if (DEBUG)
+                    System.out.println(getClass().getSimpleName() + " connects: " + NameUtils.getSafeName(g, component));
+                // TODO how to correctly resolve flag text in cases where
+                // the other diagram's connection has many attachments ?
+                return new String[] {
+                        getSafeLabel(g, g.getSingleObject(component, l0.PartOf)),
+                        getSafeLabel(g, component)
+                };
+            }
+        }
+
+        return NO_RESULT;
+    }
+
+    private Resource getFirstOwnerDiagram(ReadGraph graph, Resource diagramPart) throws DatabaseException {
+        Resource diagram = null;
+        for (Resource temp : OrderedSetElementsPredicate.INSTANCE.getSubjects(graph, resource)) {
+            diagram = temp;
+            break;
+        }
+        if (DEBUG)
+            System.out.println(getClass().getSimpleName() + " getFirstOwnerDiagram(" + NameUtils.getSafeName(graph, diagramPart) + "): " + NameUtils.getSafeName(graph, diagram));
+        return diagram;
+    }
+
+    /**
+     * Find the connections that are related to the specified connection join
+     * and on the specified composite.
+     * 
+     * @param graph
+     * @param composite
+     * @param connectionJoin
+     * @return
+     * @throws DatabaseException
+     */
+    private Collection<Resource> findConnectionOnCompositeFromJoin(ReadGraph graph, Resource composite, Resource connectionJoin) throws DatabaseException {
+        StructuralResource2 sr = StructuralResource2.getInstance(graph);
+        Collection<Resource> result = new ArrayList<Resource>(1);
+        for (Resource connection : graph.getObjects(connectionJoin, sr.Joins)) {
+            if (DEBUG)
+                System.out.println(getClass().getSimpleName() + " joins connection: " + NameUtils.getSafeName(graph, connection));
+            if (StructuralUtils.isConnectionInComposite(graph, connection, composite))
+                result.add(connection);
+        }
+        return result;
+    }
+
+    public static String getSafeLabel(ReadGraph graph, Resource r) throws DatabaseException {
+        Layer0 l0 = Layer0.getInstance(graph);
+        Statement stm = graph.getPossibleStatement(r, l0.HasLabel);
+        if (stm != null) {
+            String label = NameUtils.getSafeLabel(graph, r);
+            if (!label.isEmpty())
+                return label;
+            //if (!stm.isAsserted(r))
+            //    return label;
+        }
+        return NameUtils.getSafeName(graph, r);
+    }
+
+}