]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.annotation.ui/src/org/simantics/annotation/ui/modelBrowser2/model/AnnotationTypeLabelRule.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.annotation.ui / src / org / simantics / annotation / ui / modelBrowser2 / model / AnnotationTypeLabelRule.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.annotation.ui.modelBrowser2.model;
13
14 import java.util.Map;
15
16 import org.simantics.browsing.ui.common.ColumnKeys;
17 import org.simantics.browsing.ui.model.labels.LabelRule;
18 import org.simantics.databoard.Bindings;
19 import org.simantics.db.ReadGraph;
20 import org.simantics.db.Resource;
21 import org.simantics.db.exception.DatabaseException;
22 import org.simantics.layer0.Layer0;
23 import org.simantics.utils.datastructures.ArrayMap;
24
25 /**
26  * @author Antti Villberg
27  */
28 public enum AnnotationTypeLabelRule implements LabelRule {
29
30     INSTANCE;
31
32     public static AnnotationTypeLabelRule get() {
33         return INSTANCE;
34     }
35
36     @Override
37     public boolean isCompatible(Class<?> contentType) {
38         return contentType.equals(Resource.class);
39     }
40
41     @Override
42     public Map<String, String> getLabel(ReadGraph graph, Object content) throws DatabaseException {
43         Resource type = (Resource) content;
44
45         Layer0 L0 = Layer0.getInstance(graph);
46         Resource property = graph.getPossibleObject(type, L0.HasRange_Inverse);
47         String name = "";
48         if (property != null) {
49             String pname = graph.getPossibleRelatedValue(property, L0.HasName, Bindings.STRING);
50             if (pname != null)
51                 name = pname;
52         }
53
54         return ArrayMap.make(ColumnKeys.KEYS_SINGLE, new String[] { name });
55     }
56
57 }