--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2016 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
+ * THTH ry - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.debug.browser.utils;\r
+\r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.databoard.binding.Binding;\r
+import org.simantics.databoard.binding.error.BindingException;\r
+import org.simantics.databoard.type.Datatype;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.layer0.Layer0;\r
+import org.simantics.utils.strings.AlphanumComparator;\r
+\r
+public class ResourceInfo implements Comparable<ResourceInfo> {\r
+ public final String label;\r
+ public final String uri;\r
+ public final long resourceId;\r
+ public final Resource resource;\r
+ \r
+ public ResourceInfo(ReadGraph graph, Resource resource) throws DatabaseException {\r
+ this.label = getLabel(graph, resource);\r
+ this.uri = graph.getPossibleURI(resource);\r
+ this.resourceId = resource.getResourceId();\r
+ this.resource = resource;\r
+ }\r
+ \r
+ @Override\r
+ public String toString() {\r
+ StringBuilder sb = new StringBuilder(80);\r
+ sb.append("<a href=\"")\r
+ .append(resourceId)\r
+ .append("\"");\r
+ \r
+ if (uri != null)\r
+ sb.append(" title=\"").append(uri).append("\"");\r
+ \r
+ sb.append(">")\r
+ .append(Escapes.html(label))\r
+ .append("</a>");\r
+ \r
+ return sb.toString();\r
+ }\r
+ \r
+ public static String getLabel(ReadGraph graph, Resource resource) throws DatabaseException {\r
+ Layer0 L0 = Layer0.getInstance(graph);\r
+ String name = graph.getPossibleRelatedValue(resource, L0.HasName);\r
+ if(name != null) {\r
+ if(name.equals("Inverse")) {\r
+ Resource inverse = graph.getPossibleInverse(resource);\r
+ Resource parent = graph.getPossibleObject(resource, L0.PartOf);\r
+ if(inverse != null && inverse.equals(parent)) {\r
+ String parentName = graph.getPossibleRelatedValue(parent, L0.HasName);\r
+ if(parentName != null)\r
+ return parentName + "/Inverse";\r
+ }\r
+ }\r
+ return name;\r
+ }\r
+ \r
+ if(graph.hasValue(resource)) {\r
+ Datatype datatype = graph.getDataType(resource);\r
+ Binding binding = Bindings.getBinding(datatype);\r
+ Object value = graph.getValue(resource, binding);\r
+ try {\r
+ String valueString = binding.toString(value, true);\r
+ if(valueString.length() < 100)\r
+ return valueString;\r
+ else\r
+ return valueString.substring(0, 100) + "...";\r
+ } catch (BindingException e) {\r
+ e.printStackTrace();\r
+ }\r
+ }\r
+ \r
+ return "$" + resource.getResourceId();\r
+ }\r
+\r
+ @Override\r
+ public int compareTo(ResourceInfo o) {\r
+ return AlphanumComparator.CASE_INSENSITIVE_COMPARATOR.compare(label, o.label);\r
+ }\r
+}\r