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