]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.charts/src/org/simantics/charts/query/ChartAndSubscriptionItemWriteAction.java
Statements request is missing dec
[simantics/platform.git] / bundles / org.simantics.charts / src / org / simantics / charts / query / ChartAndSubscriptionItemWriteAction.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 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.query;
13
14 import org.simantics.charts.ontology.ChartResource;
15 import org.simantics.databoard.Bindings;
16 import org.simantics.db.Resource;
17 import org.simantics.db.WriteGraph;
18 import org.simantics.db.common.request.WriteRequest;
19 import org.simantics.db.exception.DatabaseException;
20 import org.simantics.db.layer0.request.PossibleModel;
21 import org.simantics.diagram.stubs.G2DResource;
22 import org.simantics.layer0.Layer0;
23 import org.simantics.modeling.ModelingResources;
24 import org.simantics.modeling.subscription.FindSubscription;
25 import org.simantics.modeling.subscription.NewSubscription;
26 import org.simantics.trend.configuration.Scale;
27 import org.simantics.trend.impl.Plot;
28
29 public class ChartAndSubscriptionItemWriteAction extends WriteRequest {
30
31         ChartAndSubscriptionItemData data;
32         Resource chartItem;
33         
34         public ChartAndSubscriptionItemWriteAction(Resource chartItem, ChartAndSubscriptionItemData data)
35         {
36                 this.data = data;
37                 this.chartItem = chartItem;
38         }
39         
40         @Override
41         public void perform(WriteGraph g) throws DatabaseException {
42                 // 0. Initialization
43         Layer0 L0 = Layer0.getInstance(g);
44         G2DResource G2D = G2DResource.getInstance(g);
45         ModelingResources MOD = ModelingResources.getInstance(g);
46         ChartResource CHART = ChartResource.getInstance(g);
47         Resource chart = g.getPossibleObject(chartItem, L0.PartOf);
48         Resource model = g.syncRequest(new PossibleModel( chart ));
49         Resource subscriptionItem = g.getPossibleObject(chartItem, CHART.Chart_Item_HasSubscriptionItem);
50         Resource subscription = subscriptionItem==null ? null : g.getPossibleObject(subscriptionItem, L0.PartOf);
51                 
52                 /// 1. Write Chart data
53         // Renderer
54         g.claim(chartItem, CHART.Chart_Item_Renderer, data.binaryMode ? CHART.Renderer_Binary : CHART.Renderer_Analog);
55
56         // Drawmode
57         Resource rdm = AddChartItem.getDrawmode( g, data.drawmode );
58         if ( rdm != null ) {
59             g.deny(chartItem, CHART.Chart_Item_DrawMode);
60                 g.claim(chartItem, CHART.Chart_Item_DrawMode, rdm);
61         }
62         
63         // Scale
64         if ( data.scale != null ) {
65             Resource r = data.scale instanceof Scale.Auto ? CHART.ScaleMode_AutoScale : CHART.ScaleMode_ManualScale;
66             g.deny(chartItem, CHART.Chart_Item_ScaleMode);
67             g.claim(chartItem, CHART.Chart_Item_ScaleMode, r);
68         }
69         if (data.min!=null) g.claimLiteral(chartItem, CHART.Chart_Item_ScaleMode_Min, data.min, Bindings.DOUBLE);
70         if (data.max!=null) g.claimLiteral(chartItem, CHART.Chart_Item_ScaleMode_Max, data.max, Bindings.DOUBLE);
71
72         // Visual aspects
73         g.denyValue(chartItem, G2D.HasStrokeWidth);
74         if (data.strokeWidth != null && data.strokeWidth.floatValue() != Plot.TREND_LINE_STROKE.getLineWidth())
75             g.claimLiteral(chartItem, G2D.HasStrokeWidth, L0.Float, data.strokeWidth, Bindings.FLOAT);
76         g.denyValue(chartItem, G2D.HasColor);
77         if (data.color != null)
78             g.claimLiteral(chartItem, G2D.HasColor, G2D.Color, data.color, Bindings.FLOAT_ARRAY);
79
80         /// 2. Write Subscription item data
81         if ( data.hasSubscriptionItem && subscriptionItem!=null )
82         {
83             String subscriptionLabel = g.getPossibleRelatedValue(subscription, L0.HasLabel);
84             if ( data.subscription != null && !data.subscription.equals( subscriptionLabel ) ) {
85                 // Wrong subscription group
86                 // Find subscription 
87                 Resource newSubscription = g.sync( new FindSubscription(model, null, data.subscription) );
88                 
89                 // Create subscription if did not exist
90                 if (newSubscription == null) {
91                         NewSubscription ns = new NewSubscription(model, data.subscription);
92                         g.sync( ns );
93                         newSubscription = ns.subscription;
94                 }
95                 
96                 // Move item to new subscription
97                 g.deny(subscription, L0.ConsistsOf, subscriptionItem);
98                 g.claim(newSubscription, L0.ConsistsOf, subscriptionItem);
99             }
100             
101             if (data.label!=null) g.claimLiteral(subscriptionItem, L0.HasLabel, data.label, Bindings.STRING);
102             if (data.mutableCollectionSettings) {
103                 if (data.interval!=null) g.claimLiteral(subscriptionItem, MOD.Subscription_Item_SamplingInterval, data.interval, Bindings.DOUBLE);
104                 if (data.deadband!=null) g.claimLiteral(subscriptionItem, MOD.Subscription_Item_Deadband, data.deadband, Bindings.DOUBLE);
105             }
106             if (data.unit!=null) g.claimLiteral(subscriptionItem, MOD.Subscription_Item_Unit, data.unit, Bindings.STRING);
107                 if (data.gain!=null) g.claimLiteral(subscriptionItem, MOD.Subscription_Item_Gain, data.gain, Bindings.DOUBLE);
108                 if (data.bias!=null) g.claimLiteral(subscriptionItem, MOD.Subscription_Item_Bias, data.bias, Bindings.DOUBLE);
109                 
110         }
111                 
112         }
113
114 }