]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.debug.browser/src/org/simantics/debug/browser/utils/ResourceInfo.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.debug.browser / src / org / simantics / debug / browser / utils / ResourceInfo.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.utils;
13
14 import org.simantics.databoard.Bindings;
15 import org.simantics.databoard.binding.Binding;
16 import org.simantics.databoard.binding.error.BindingException;
17 import org.simantics.databoard.type.Datatype;
18 import org.simantics.db.ReadGraph;
19 import org.simantics.db.Resource;
20 import org.simantics.db.exception.DatabaseException;
21 import org.simantics.layer0.Layer0;
22 import org.simantics.utils.strings.AlphanumComparator;
23
24 public class ResourceInfo implements Comparable<ResourceInfo> {
25     public final String label;
26     public final String uri;
27     public final long resourceId;
28     public final Resource resource;
29     
30     public ResourceInfo(ReadGraph graph, Resource resource) throws DatabaseException {
31         this.label = getLabel(graph, resource);
32         this.uri = graph.getPossibleURI(resource);
33         this.resourceId = resource.getResourceId();
34         this.resource = resource;
35     }
36     
37     @Override
38     public String toString() {
39         StringBuilder sb = new StringBuilder(80);
40         sb.append("<a href=\"")
41           .append(resourceId)
42           .append("\"");
43         
44         if (uri != null)
45             sb.append(" title=\"").append(uri).append("\"");
46         
47         sb.append(">")
48           .append(Escapes.html(label))
49           .append("</a>");
50         
51         return sb.toString();
52     }
53     
54     public static String getLabel(ReadGraph graph, Resource resource) throws DatabaseException {
55         Layer0 L0 = Layer0.getInstance(graph);
56         String name = graph.getPossibleRelatedValue(resource, L0.HasName);
57         if(name != null) {
58             if(name.equals("Inverse")) {
59                 Resource inverse = graph.getPossibleInverse(resource);
60                 Resource parent = graph.getPossibleObject(resource, L0.PartOf);
61                 if(inverse != null && inverse.equals(parent)) {
62                     String parentName = graph.getPossibleRelatedValue(parent, L0.HasName);
63                     if(parentName != null)
64                         return parentName + "/Inverse";
65                 }
66             }
67             return name;
68         }
69         
70         if(graph.hasValue(resource)) {
71             Datatype datatype = graph.getDataType(resource);
72             Binding binding = Bindings.getBinding(datatype);
73             Object value = graph.getValue(resource, binding);
74             try {
75                 String valueString = binding.toString(value, true);
76                 if(valueString.length() < 100)
77                     return valueString;
78                 else
79                     return valueString.substring(0, 100) + "...";
80             } catch (BindingException e) {
81                 e.printStackTrace();
82             }
83         }
84         
85         return "$" + resource.getResourceId();
86     }
87
88     @Override
89     public int compareTo(ResourceInfo o) {
90         return AlphanumComparator.CASE_INSENSITIVE_COMPARATOR.compare(label, o.label);
91     }
92 }