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