]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/modelBrowser2/label/SubscriptionItemLabelRule.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / modelBrowser2 / label / SubscriptionItemLabelRule.java
1 /*******************************************************************************\r
2  * Copyright (c) 2012 Association for Decentralized Information Management in\r
3  * 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.modeling.ui.modelBrowser2.label;\r
13 \r
14 import java.util.Collections;\r
15 import java.util.Map;\r
16 \r
17 import org.eclipse.core.runtime.preferences.IEclipsePreferences;\r
18 import org.eclipse.core.runtime.preferences.InstanceScope;\r
19 import org.simantics.browsing.ui.common.ColumnKeys;\r
20 import org.simantics.browsing.ui.model.labels.LabelRule;\r
21 import org.simantics.db.ReadGraph;\r
22 import org.simantics.db.Resource;\r
23 import org.simantics.db.exception.DatabaseException;\r
24 import org.simantics.modeling.subscription.SubscriptionItemLabel;\r
25 import org.simantics.utils.format.ValueFormat;\r
26 \r
27 /**\r
28  * @author Tuukka Lehtonen\r
29  */\r
30 public class SubscriptionItemLabelRule implements LabelRule {\r
31 \r
32     public static final SubscriptionItemLabelRule INSTANCE = new SubscriptionItemLabelRule();\r
33 \r
34     ValueFormat valueFormat;\r
35 \r
36     public SubscriptionItemLabelRule() {\r
37         IEclipsePreferences chartPreferenceNode = InstanceScope.INSTANCE.getNode( "org.simantics.charts" );\r
38         String s = chartPreferenceNode.get("chart.valueformat", ValueFormat.Default.name());\r
39         valueFormat = ValueFormat.valueOf( s );\r
40     }\r
41 \r
42     @Override\r
43     public boolean isCompatible(Class<?> contentType) {\r
44         return contentType.equals(Resource.class);\r
45     }\r
46 \r
47     @Override\r
48     public Map<String, String> getLabel(ReadGraph graph, Object content) throws DatabaseException {\r
49         boolean oldSync = graph.setSynchronous(true);\r
50         try {\r
51             Resource item = (Resource) content;\r
52             String label = SubscriptionItemLabel.resolveLabel(graph, item, valueFormat, true);\r
53             return Collections.singletonMap(ColumnKeys.SINGLE, label);\r
54         } finally {\r
55             graph.setSynchronous(oldSync);\r
56         }\r
57     }\r
58 }\r