1 /*******************************************************************************
\r
2 * Copyright (c) 2011 Association for Decentralized Information Management in
\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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.charts.function;
\r
14 import org.simantics.charts.ontology.ChartResource;
\r
15 import org.simantics.charts.ui.ChartItemLabelRule;
\r
16 import org.simantics.databoard.Bindings;
\r
17 import org.simantics.db.ReadGraph;
\r
18 import org.simantics.db.Resource;
\r
19 import org.simantics.db.exception.DatabaseException;
\r
20 import org.simantics.db.layer0.variable.Variable;
\r
21 import org.simantics.layer0.Layer0;
\r
22 import org.simantics.modeling.subscription.SubscriptionItemLabel;
\r
23 import org.simantics.scl.reflection.annotations.SCLValue;
\r
26 * @author Tuukka Lehtonen
\r
28 public class Labels {
\r
30 @SCLValue(type = "ReadGraph -> Resource -> a -> String")
\r
31 public static String chartItemLabel(ReadGraph graph, Resource resource, Object context) throws DatabaseException {
\r
32 if (context instanceof Resource) {
\r
33 return ChartItemLabelRule.INSTANCE.getLabel_(graph, (Resource) context);
\r
34 } else if (context instanceof Variable) {
\r
35 Variable var = (Variable)context;
\r
36 Resource r = var.getParent(graph).getPossibleRepresents(graph);
\r
37 if(r == null) return "";
\r
38 return ChartItemLabelRule.INSTANCE.getLabel_(graph, r);
\r
40 throw new DatabaseException("Unknown context " + context);
\r
44 @SCLValue(type = "ReadGraph -> Resource -> a -> String")
\r
45 public static String chartItemSubscriptionInfo(ReadGraph graph, Resource resource, Object context) throws DatabaseException {
\r
46 Resource chartItem = null;
\r
47 if (context instanceof Resource) {
\r
48 chartItem = (Resource) context;
\r
49 } else if (context instanceof Variable) {
\r
50 Variable var = (Variable) context;
\r
51 chartItem = var.getParent(graph).getPossibleRepresents(graph);
\r
53 throw new DatabaseException("Unknown context " + context);
\r
55 if (chartItem == null)
\r
58 ChartResource CHART = ChartResource.getInstance(graph);
\r
59 Resource subscriptionItem = graph.getPossibleObject(chartItem, CHART.Chart_Item_HasSubscriptionItem);
\r
60 if (subscriptionItem == null)
\r
61 return "Not bound to a subscription item";
\r
63 String itemLabel = SubscriptionItemLabel.resolveLabel(graph, subscriptionItem, null, false, false, false);
\r
65 Layer0 L0 = Layer0.getInstance(graph);
\r
66 Resource subscription = graph.getPossibleObject(subscriptionItem, L0.PartOf);
\r
67 if (subscription != null) {
\r
68 String subscriptionLabel = graph.getPossibleRelatedValue2(subscription, L0.HasLabel, Bindings.STRING);
\r
69 if (subscriptionLabel != null) {
\r
70 return itemLabel + " in subscription " + subscriptionLabel;
\r