1 /*******************************************************************************
2 * Copyright (c) 2016 Association for Decentralized Information Management
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
10 * THTH ry - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.debug.browser.utils;
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;
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;
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;
38 public String toString() {
39 StringBuilder sb = new StringBuilder(80);
40 sb.append("<a href=\"")
45 sb.append(" title=\"").append(uri).append("\"");
48 .append(Escapes.html(label))
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);
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";
70 if(graph.hasValue(resource)) {
71 Datatype datatype = graph.getDataType(resource);
72 Binding binding = Bindings.getBinding(datatype);
73 Object value = graph.getValue(resource, binding);
75 String valueString = binding.toString(value, true);
76 if(valueString.length() < 100)
79 return valueString.substring(0, 100) + "...";
80 } catch (BindingException e) {
85 return "$" + resource.getResourceId();
89 public int compareTo(ResourceInfo o) {
90 return AlphanumComparator.CASE_INSENSITIVE_COMPARATOR.compare(label, o.label);