]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.debug.browser/src/org/simantics/debug/browser/internal/rewriters/BreadcrumbCreator.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.debug.browser / src / org / simantics / debug / browser / internal / rewriters / BreadcrumbCreator.java
1 /*******************************************************************************
2  * Copyright (c) 2016 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  *     THTH ry - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.debug.browser.internal.rewriters;
13
14 import java.util.ArrayList;
15
16 import org.simantics.databoard.Bindings;
17 import org.simantics.db.ReadGraph;
18 import org.simantics.db.Resource;
19 import org.simantics.db.Statement;
20 import org.simantics.db.exception.DatabaseException;
21 import org.simantics.debug.browser.content.ResourceBrowserContent;
22 import org.simantics.debug.browser.content.ResourceBrowserRewriter;
23 import org.simantics.debug.browser.sections.BreadcrumbSection;
24 import org.simantics.debug.browser.sections.RawStatementsSection;
25 import org.simantics.debug.browser.sections.BreadcrumbSection.Node;
26 import org.simantics.layer0.Layer0;
27
28 public enum BreadcrumbCreator implements ResourceBrowserRewriter {
29     INSTANCE;
30
31     @Override
32     public void rewrite(ReadGraph graph, ResourceBrowserContent content)
33             throws DatabaseException {
34         ArrayList<Node> nodes = new ArrayList<Node>();
35         collectBreadcrumbs(graph, nodes, content.resource);
36         if(!nodes.isEmpty()) {
37             content.putSection(BreadcrumbSection.class,
38                     new BreadcrumbSection(nodes.toArray(new Node[nodes.size()])));
39             
40             Layer0 L0 = Layer0.getInstance(graph);
41             RawStatementsSection rawStatementsSection = content.getSection(RawStatementsSection.class);
42             rawStatementsSection.statementsByPredicates.remove(L0.HasName);
43             rawStatementsSection.statementsByPredicates.remove(L0.PartOf);
44         }
45     }
46
47     private static void collectBreadcrumbs(ReadGraph graph, ArrayList<Node> nodes,
48             Resource resource) throws DatabaseException {
49         if(resource.equals(graph.getRootLibrary())) {
50             nodes.add(new Node("", "http:/", resource.getResourceId()));
51             return;
52         }
53         Layer0 L0 = Layer0.getInstance(graph);
54         Resource parent = graph.getPossibleObject(resource, L0.PartOf);
55         if(parent != null) {
56             collectBreadcrumbs(graph, nodes, parent);
57             String name = graph.getPossibleRelatedValue(resource, L0.HasName, Bindings.STRING);
58             if(name != null)
59                 nodes.add(new Node("/", name, resource.getResourceId()));
60             else
61                 nodes.add(new Node("/", String.valueOf(resource.getResourceId()), resource.getResourceId()));
62             return;
63         }
64         Statement propertyInverseStatement = graph.getPossibleStatement(resource, L0.PropertyOf);
65         if(propertyInverseStatement != null) {
66             collectBreadcrumbs(graph, nodes, propertyInverseStatement.getObject());
67             String name = graph.getPossibleRelatedValue(graph.getInverse(propertyInverseStatement.getPredicate()),
68                     L0.HasName, Bindings.STRING);
69             nodes.add(new Node("#", name, resource.getResourceId()));
70             return;
71         }
72         Resource assertion = graph.getPossibleObject(resource, L0.HasObjectInverse);
73         if(assertion != null) {
74             parent = graph.getPossibleObject(assertion, L0.Asserts_Inverse);
75             if(parent != null) {
76                 collectBreadcrumbs(graph, nodes, parent);
77                 Resource predicate = graph.getPossibleObject(assertion, L0.HasPredicate);
78                 String name = predicate == null ? null 
79                         : (String)graph.getPossibleRelatedValue(predicate, L0.HasName, Bindings.STRING);
80                 if(name != null)
81                     nodes.add(new Node("#assertion#", name, resource.getResourceId()));
82                 else
83                     nodes.add(new Node("#assertion#", String.valueOf(resource.getResourceId()), resource.getResourceId()));
84                 return;
85             }
86         }
87     }
88
89     @Override
90     public double getPriority() {
91         return 1;
92     }
93 }