1 /*******************************************************************************
2 * Copyright (c) 2007, 2011 Association for Decentralized Information Management in
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.charts.query;
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;
29 public class ChartAndSubscriptionItemWriteAction extends WriteRequest {
31 ChartAndSubscriptionItemData data;
34 public ChartAndSubscriptionItemWriteAction(Resource chartItem, ChartAndSubscriptionItemData data)
37 this.chartItem = chartItem;
41 public void perform(WriteGraph g) throws DatabaseException {
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);
52 /// 1. Write Chart data
54 g.claim(chartItem, CHART.Chart_Item_Renderer, data.binaryMode ? CHART.Renderer_Binary : CHART.Renderer_Analog);
57 Resource rdm = AddChartItem.getDrawmode( g, data.drawmode );
59 g.deny(chartItem, CHART.Chart_Item_DrawMode);
60 g.claim(chartItem, CHART.Chart_Item_DrawMode, rdm);
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);
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);
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);
80 /// 2. Write Subscription item data
81 if ( data.hasSubscriptionItem && subscriptionItem!=null )
83 String subscriptionLabel = g.getPossibleRelatedValue(subscription, L0.HasLabel);
84 if ( data.subscription != null && !data.subscription.equals( subscriptionLabel ) ) {
85 // Wrong subscription group
87 Resource newSubscription = g.sync( new FindSubscription(model, null, data.subscription) );
89 // Create subscription if did not exist
90 if (newSubscription == null) {
91 NewSubscription ns = new NewSubscription(model, data.subscription);
93 newSubscription = ns.subscription;
96 // Move item to new subscription
97 g.deny(subscription, L0.ConsistsOf, subscriptionItem);
98 g.claim(newSubscription, L0.ConsistsOf, subscriptionItem);
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);
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);