1 /*******************************************************************************
\r
2 * Copyright (c) 2016 Association for Decentralized Information Management
\r
3 * in Industry THTH ry.
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * THTH ry - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.debug.browser.utils;
\r
14 import org.simantics.databoard.Bindings;
\r
15 import org.simantics.databoard.binding.Binding;
\r
16 import org.simantics.databoard.binding.error.BindingException;
\r
17 import org.simantics.databoard.type.Datatype;
\r
18 import org.simantics.db.ReadGraph;
\r
19 import org.simantics.db.Resource;
\r
20 import org.simantics.db.exception.DatabaseException;
\r
21 import org.simantics.layer0.Layer0;
\r
22 import org.simantics.utils.strings.AlphanumComparator;
\r
24 public class ResourceInfo implements Comparable<ResourceInfo> {
\r
25 public final String label;
\r
26 public final String uri;
\r
27 public final long resourceId;
\r
28 public final Resource resource;
\r
30 public ResourceInfo(ReadGraph graph, Resource resource) throws DatabaseException {
\r
31 this.label = getLabel(graph, resource);
\r
32 this.uri = graph.getPossibleURI(resource);
\r
33 this.resourceId = resource.getResourceId();
\r
34 this.resource = resource;
\r
38 public String toString() {
\r
39 StringBuilder sb = new StringBuilder(80);
\r
40 sb.append("<a href=\"")
\r
45 sb.append(" title=\"").append(uri).append("\"");
\r
48 .append(Escapes.html(label))
\r
51 return sb.toString();
\r
54 public static String getLabel(ReadGraph graph, Resource resource) throws DatabaseException {
\r
55 Layer0 L0 = Layer0.getInstance(graph);
\r
56 String name = graph.getPossibleRelatedValue(resource, L0.HasName);
\r
58 if(name.equals("Inverse")) {
\r
59 Resource inverse = graph.getPossibleInverse(resource);
\r
60 Resource parent = graph.getPossibleObject(resource, L0.PartOf);
\r
61 if(inverse != null && inverse.equals(parent)) {
\r
62 String parentName = graph.getPossibleRelatedValue(parent, L0.HasName);
\r
63 if(parentName != null)
\r
64 return parentName + "/Inverse";
\r
70 if(graph.hasValue(resource)) {
\r
71 Datatype datatype = graph.getDataType(resource);
\r
72 Binding binding = Bindings.getBinding(datatype);
\r
73 Object value = graph.getValue(resource, binding);
\r
75 String valueString = binding.toString(value, true);
\r
76 if(valueString.length() < 100)
\r
79 return valueString.substring(0, 100) + "...";
\r
80 } catch (BindingException e) {
\r
81 e.printStackTrace();
\r
85 return "$" + resource.getResourceId();
\r
89 public int compareTo(ResourceInfo o) {
\r
90 return AlphanumComparator.CASE_INSENSITIVE_COMPARATOR.compare(label, o.label);
\r