]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagram/monitor/ResolveMonitorVariable.java
Externalize strings
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / diagram / monitor / ResolveMonitorVariable.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.diagram.monitor;
13
14 import org.simantics.common.format.Formatter;
15 import org.simantics.databoard.Bindings;
16 import org.simantics.datatypes.literal.DecimalFormatFormatter;
17 import org.simantics.db.ReadGraph;
18 import org.simantics.db.Resource;
19 import org.simantics.db.common.request.BinaryRead;
20 import org.simantics.db.exception.DatabaseException;
21 import org.simantics.db.exception.VariableException;
22 import org.simantics.db.layer0.variable.RVI;
23 import org.simantics.db.layer0.variable.Variable;
24 import org.simantics.db.layer0.variable.Variables;
25 import org.simantics.diagram.stubs.DiagramResource;
26 import org.simantics.operation.Layer0X;
27
28 /**
29  * @author Tuukka Lehtonen
30  */
31 public class ResolveMonitorVariable extends BinaryRead<Resource, Resource, MonitorVariable> {
32
33     public ResolveMonitorVariable(Resource runtimeDiagram, Resource monitorElement) {
34         super(runtimeDiagram, monitorElement);
35     }
36
37     @Override
38     public MonitorVariable perform(ReadGraph graph) throws DatabaseException {
39         Boolean external = null;
40         DiagramResource DIA = DiagramResource.getInstance(graph);
41
42         Resource monitorComponent = graph.getPossibleObject(parameter2, DIA.HasMonitorComponent);
43         if (monitorComponent == null)
44             return null;
45
46         String suffix = graph.getPossibleRelatedValue(parameter2, DIA.HasMonitorSuffix, Bindings.STRING);
47         if (suffix == null)
48             return null;
49
50         String diagramVariable = graph.getPossibleRelatedValue(parameter, DIA.RuntimeDiagram_HasVariable);
51         if (diagramVariable == null)
52             return null;
53
54         Variable var = Variables.getPossibleVariable(graph, diagramVariable);
55         if (var == null)
56             return null;
57
58         Variable component = var.browsePossible(graph, monitorComponent);
59         if (component == null) {
60             // The monitored component is not in this diagram.
61             component = Variables.getPossibleVariable(graph, monitorComponent);
62             if (component == null)
63                 return null;
64
65             String componentRVI = Variables.getPossibleRVI(graph, component);
66             if (componentRVI == null)
67                 return null;
68
69             Resource realization = Variables.getRealization(graph, var);
70             String diagramRealizationURI = graph.getPossibleURI(realization);
71             if (diagramRealizationURI == null)
72                 return null;
73
74             String componentURI = diagramRealizationURI + componentRVI;
75             var = Variables.getPossibleVariable(graph, componentURI);
76             if (var == null)
77                 return null;
78
79             external = Boolean.TRUE;
80         } else {
81             var = component;
82         }
83
84         var = var.browsePossible(graph, suffix);
85         if (var == null)
86             return null;
87
88         Formatter formatter = graph.getPossibleRelatedAdapter(parameter2, DIA.HasFormatter, Formatter.class);
89         if (formatter == null)
90             formatter = new DecimalFormatFormatter();
91
92         Layer0X L0X = Layer0X.getInstance(graph);
93         String _expression = graph.getPossibleRelatedAdapter(parameter2, L0X.HasExpression, String.class);
94         if(_expression == null) _expression  = "value"; //$NON-NLS-1$
95
96         RVI rvi = null;
97         try {
98             rvi = Variables.getRVI2(graph, var);
99         } catch (VariableException e) {
100             // No RVI, which is fine for contents that do not originate from the model configuration.
101         }
102
103         return new MonitorVariable(monitorComponent, formatter, _expression, var, rvi, external);
104     }
105
106 }