]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.graph.impl/src/org/simantics/browsing/ui/graph/impl/LabelerUtil.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.browsing.ui.graph.impl / src / org / simantics / browsing / ui / graph / impl / LabelerUtil.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 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
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.browsing.ui.graph.impl;\r
13 \r
14 import org.simantics.db.ReadGraph;\r
15 import org.simantics.db.Resource;\r
16 import org.simantics.db.common.utils.NameUtils;\r
17 import org.simantics.db.exception.AdaptionException;\r
18 import org.simantics.db.exception.DatabaseException;\r
19 import org.simantics.layer0.utils.representation.StringRepresentation2;\r
20 import org.simantics.utils.datastructures.slice.ValueRange;\r
21 \r
22 /**\r
23  * @author Tuukka Lehtonen\r
24  */\r
25 public final class LabelerUtil {\r
26 \r
27     /**\r
28      * @param g\r
29      * @param r\r
30      * @return\r
31      */\r
32     public static String safeStringRepresentation(ReadGraph g, Resource r) throws DatabaseException {\r
33         try {\r
34             String rep = g.adapt(r, String.class);\r
35             return rep;\r
36         } catch (AdaptionException e) {\r
37             return "<no string representation: " + e.getMessage() + ">";\r
38         } catch (DatabaseException e) {\r
39             return NameUtils.getSafeName(g, r);\r
40         }\r
41     }\r
42 \r
43     /**\r
44      * @param g\r
45      * @param r\r
46      * @param range\r
47      * @return\r
48      */\r
49     public static String safeStringRepresentation(ReadGraph g, Resource r, int index) throws DatabaseException {\r
50         try {\r
51             StringRepresentation2 rep = g.adapt(r, StringRepresentation2.class);\r
52             return rep.get(g, index);\r
53         } catch (AdaptionException e) {\r
54             return "<no string representation: " + e.getMessage() + ">";\r
55         }\r
56     }\r
57 \r
58     /**\r
59      * @param g\r
60      * @param r\r
61      * @param range\r
62      * @return\r
63      */\r
64     public static String safeStringRepresentation(ReadGraph g, Resource r, ValueRange range) throws DatabaseException {\r
65         try {\r
66             StringRepresentation2 rep = g.adapt(r, StringRepresentation2.class);\r
67             return rep.get(g, range.start(), range.size());\r
68         } catch (AdaptionException e) {\r
69             return "<no string representation: " + e.getMessage() + ">";\r
70         }\r
71     }\r
72 \r
73 }\r