]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.charts/src/org/simantics/charts/function/Labels.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.charts / src / org / simantics / charts / function / Labels.java
1 /*******************************************************************************
2  * Copyright (c) 2011 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.charts.function;
13
14 import org.simantics.charts.ontology.ChartResource;
15 import org.simantics.charts.ui.ChartItemLabelRule;
16 import org.simantics.databoard.Bindings;
17 import org.simantics.db.ReadGraph;
18 import org.simantics.db.Resource;
19 import org.simantics.db.exception.DatabaseException;
20 import org.simantics.db.layer0.variable.Variable;
21 import org.simantics.layer0.Layer0;
22 import org.simantics.modeling.subscription.SubscriptionItemLabel;
23 import org.simantics.scl.reflection.annotations.SCLValue;
24
25 /**
26  * @author Tuukka Lehtonen
27  */
28 public class Labels {
29
30     @SCLValue(type = "ReadGraph -> Resource -> a -> String")
31     public static String chartItemLabel(ReadGraph graph, Resource resource, Object context) throws DatabaseException {
32         if (context instanceof Resource) {
33             return ChartItemLabelRule.INSTANCE.getLabel_(graph, (Resource) context);
34         } else if (context instanceof Variable) {
35                 Variable var = (Variable)context;
36                 Resource r = var.getParent(graph).getPossibleRepresents(graph);
37                 if(r == null) return "";
38             return ChartItemLabelRule.INSTANCE.getLabel_(graph, r);
39         } else {
40             throw new DatabaseException("Unknown context " + context);
41         }
42     }
43
44     @SCLValue(type = "ReadGraph -> Resource -> a -> String")
45     public static String chartItemSubscriptionInfo(ReadGraph graph, Resource resource, Object context) throws DatabaseException {
46         Resource chartItem = null;
47         if (context instanceof Resource) {
48             chartItem = (Resource) context;
49         } else if (context instanceof Variable) {
50             Variable var = (Variable) context;
51             chartItem = var.getParent(graph).getPossibleRepresents(graph);
52         } else {
53             throw new DatabaseException("Unknown context " + context);
54         }
55         if (chartItem == null)
56             return "";
57
58         ChartResource CHART = ChartResource.getInstance(graph);
59         Resource subscriptionItem = graph.getPossibleObject(chartItem, CHART.Chart_Item_HasSubscriptionItem);
60         if (subscriptionItem == null)
61             return "Not bound to a subscription item";
62
63         String itemLabel = SubscriptionItemLabel.resolveLabel(graph, subscriptionItem, null, false, false, false);
64
65         Layer0 L0 = Layer0.getInstance(graph);
66         Resource subscription = graph.getPossibleObject(subscriptionItem, L0.PartOf);
67         if (subscription != null) {
68             String subscriptionLabel = graph.getPossibleRelatedValue2(subscription, L0.HasLabel, Bindings.STRING);
69             if (subscriptionLabel != null) {
70                 return itemLabel + " in subscription " + subscriptionLabel;
71             }
72         }
73
74         return itemLabel;
75     }
76
77 }