1 /*******************************************************************************
2 * Copyright (c) 2007, 2011 Association for Decentralized Information Management
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 java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.List;
19 import org.eclipse.core.runtime.preferences.IEclipsePreferences;
20 import org.eclipse.core.runtime.preferences.InstanceScope;
21 import org.simantics.charts.ontology.ChartResource;
22 import org.simantics.charts.preference.ChartPreferences;
23 import org.simantics.databoard.Bindings;
24 import org.simantics.databoard.type.BooleanType;
25 import org.simantics.databoard.type.Datatype;
26 import org.simantics.db.ReadGraph;
27 import org.simantics.db.Resource;
28 import org.simantics.db.WriteGraph;
29 import org.simantics.db.common.CommentMetadata;
30 import org.simantics.db.common.request.Queries;
31 import org.simantics.db.common.request.WriteResultRequest;
32 import org.simantics.db.exception.DatabaseException;
33 import org.simantics.db.request.WriteResult;
34 import org.simantics.diagram.stubs.G2DResource;
35 import org.simantics.layer0.Layer0;
36 import org.simantics.modeling.ModelingResources;
37 import org.simantics.trend.configuration.Scale;
38 import org.simantics.trend.configuration.TrendItem.DrawMode;
39 import org.simantics.trend.configuration.TrendItem.Renderer;
42 * @author Tuukka Lehtonen
44 public class AddChartItem extends WriteResultRequest<Collection<Resource>> {
47 Collection<ChartItemDescriptor> items;
48 public Resource chartItem;
50 public AddChartItem(Resource chart, Collection<ChartItemDescriptor> items) {
56 public Collection<Resource> perform(WriteGraph graph) throws DatabaseException {
57 Layer0 L0 = Layer0.getInstance(graph);
58 ChartResource CHART = ChartResource.getInstance(graph);
60 int nextIndex = graph.sync( new NextChartItemIndexQuery(chart) );
62 List<Resource> result = new ArrayList<Resource>();
65 for (ChartItemDescriptor desc : items) {
66 if (desc.subscriptionItem == null)
69 // Ensure plot is not already in the chart
70 for (Resource chartItem : graph.syncRequest(Queries.objectsWithType(chart, L0.ConsistsOf, CHART.Chart_Item))) {
71 Resource subscriptionItem = graph.getPossibleObject(chartItem, CHART.Chart_Item_HasSubscriptionItem);
72 if (desc.subscriptionItem.equals(subscriptionItem)) {
74 if ( desc.index!=null ) {
75 graph.claimLiteral(chartItem, CHART.Chart_Item_Index, desc.index);
76 if (desc.min!=null) graph.claimLiteral(chartItem, CHART.Chart_Item_ScaleMode_Min, desc.min, Bindings.DOUBLE);
77 if (desc.max!=null) graph.claimLiteral(chartItem, CHART.Chart_Item_ScaleMode_Max, desc.max, Bindings.DOUBLE);
78 if (desc.renderer!=null) graph.claim(chartItem, CHART.Chart_Item_Renderer, getRenderer(graph, desc.renderer));
79 if (desc.subscriptionItem!=null) graph.claim(chartItem, CHART.Chart_Item_HasSubscriptionItem, desc.subscriptionItem);
81 Resource rdm = getDrawmode( graph, desc.drawMode );
82 if ( rdm != null ) graph.claim(chartItem, CHART.Chart_Item_DrawMode, rdm);
84 continue nextReference;
88 // Create & Link new plot
89 Resource chartItem = graph.newResource();
90 graph.claim(chartItem, L0.InstanceOf, null, CHART.Chart_Item);
91 graph.claim(chart, L0.ConsistsOf, chartItem);
92 graph.claimLiteral(chartItem, L0.HasName, desc.name, Bindings.STRING);
93 graph.claim(chartItem, CHART.Chart_Item_HasSubscriptionItem, desc.subscriptionItem);
94 graph.claim(chartItem, CHART.Chart_Item_Renderer, getRenderer(graph, desc.renderer));
95 int index = desc.index!=null ? desc.index : nextIndex++;
96 graph.claimLiteral(chartItem, CHART.Chart_Item_Index, index);
97 if (desc.strokeWidth != null)
98 graph.claimLiteral(chartItem, G2DResource.getInstance(graph).HasStrokeWidth, desc.strokeWidth, Bindings.FLOAT);
99 if (desc.color != null)
100 graph.claimLiteral(chartItem, G2DResource.getInstance(graph).HasColor, desc.color, Bindings.FLOAT_ARRAY);
102 IEclipsePreferences pn = InstanceScope.INSTANCE.getNode( ChartPreferences.P_NODE );
106 String sdm = pn.get(ChartPreferences.P_DRAWMODE, ChartPreferences.DEFAULT_DRAWMODE);
107 DrawMode dm = desc.drawMode != null ? desc.drawMode : DrawMode.valueOf( sdm );
108 Resource rdm = getDrawmode( graph, dm );
110 graph.claim(chartItem, CHART.Chart_Item_DrawMode, rdm);
116 Scale scale = desc.scale;
117 if ( scale == null ) {
118 String sdm = pn.get(ChartPreferences.P_SCALEMODE, ChartPreferences.DEFAULT_SCALEMODE);
119 if ( sdm.equals("Auto") ) {
120 scale = new Scale.Auto();
122 scale = new Scale.Manual(0, 100);
126 if ( scale instanceof Scale.Auto ) {
127 Resource r = CHART.ScaleMode_AutoScale;
128 graph.claim(chartItem, CHART.Chart_Item_ScaleMode, r);
130 graph.claim(chartItem, CHART.Chart_Item_ScaleMode, CHART.ScaleMode_ManualScale);
133 if (desc.min!=null) graph.claimLiteral(chartItem, CHART.Chart_Item_ScaleMode_Min, desc.min, Bindings.DOUBLE);
134 if (desc.max!=null) graph.claimLiteral(chartItem, CHART.Chart_Item_ScaleMode_Max, desc.max, Bindings.DOUBLE);
137 this.chartItem = chartItem;
139 result.add(chartItem);
141 CommentMetadata cm = graph.getMetadata(CommentMetadata.class);
142 graph.addMetadata(cm.add("Added chart item " + chartItem));
149 public static Resource getDrawmode(ReadGraph graph, DrawMode dm)
151 ChartResource CHART = ChartResource.getInstance(graph);
152 if ( dm == DrawMode.Average ) return CHART.DrawMode_Average;
153 if ( dm == DrawMode.Sample ) return CHART.DrawMode_Sample;
154 if ( dm == DrawMode.Line ) return CHART.DrawMode_Line;
155 if ( dm == DrawMode.Deviation ) return CHART.DrawMode_Deviation;
156 if ( dm == DrawMode.DeviationAndAverage ) return CHART.DrawMode_DeviationAndAverage;
157 if ( dm == DrawMode.DeviationAndLine ) return CHART.DrawMode_DeviationAndLine;
158 if ( dm == DrawMode.DeviationAndSample ) return CHART.DrawMode_DeviationAndSample;
162 public static Resource getRenderer(ReadGraph graph, Renderer renderer) {
163 ChartResource CHART = ChartResource.getInstance(graph);
166 return CHART.Renderer_Analog;
168 return CHART.Renderer_Binary;
170 throw new UnsupportedOperationException("invalid renderer: " + renderer);
174 public static ChartItemDescriptor createDescriptor(ReadGraph graph, Resource subscriptionItem) throws DatabaseException {
175 ModelingResources MOD = ModelingResources.getInstance(graph);
177 ChartItemDescriptor desc = new ChartItemDescriptor();
178 desc.subscriptionItem = subscriptionItem;
179 Datatype datatype = graph.getPossibleRelatedValue(subscriptionItem, MOD.Subscription_Item_Datatype, Bindings.getBindingUnchecked(Datatype.class));
180 desc.renderer = datatype instanceof BooleanType ? Renderer.Binary : Renderer.Analog;
185 public static WriteResult<Collection<Resource>> addAndMoveChartItems(final Resource chart, final List<ChartItemDescriptor> references,
186 final Set<Resource> movedPlots) {
187 return new WriteResultRequest<Collection<Resource>>() {
189 public Collection<Resource> perform(WriteGraph graph) throws DatabaseException {
190 List<Resource> result = new ArrayList<Resource>();
191 result.addAll(new AddChartItem(chart, references).perform(graph));
192 Layer0 L0 = Layer0.getInstance(graph);
193 for (Resource plot : movedPlots) {
194 graph.deny(plot, L0.PartOf);
195 graph.claim(chart, L0.ConsistsOf, plot);