]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.charts/src/org/simantics/charts/query/AddChartItem.java
Statements request is missing dec
[simantics/platform.git] / bundles / org.simantics.charts / src / org / simantics / charts / query / AddChartItem.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2011 Association for Decentralized Information Management
3  * in 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 java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.List;
17 import java.util.Set;
18
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;
40
41 /**
42  * @author Tuukka Lehtonen
43  */
44 public class AddChartItem extends WriteResultRequest<Collection<Resource>> {
45
46     Resource chart;
47     Collection<ChartItemDescriptor> items;
48     public Resource chartItem;
49
50     public AddChartItem(Resource chart, Collection<ChartItemDescriptor> items) {
51         this.chart = chart;
52         this.items = items;
53     }
54
55     @Override
56     public Collection<Resource> perform(WriteGraph graph) throws DatabaseException {
57         Layer0 L0 = Layer0.getInstance(graph);
58         ChartResource CHART = ChartResource.getInstance(graph);
59
60         int nextIndex = graph.sync( new NextChartItemIndexQuery(chart) );
61         
62         List<Resource> result = new ArrayList<Resource>();
63         
64         nextReference:
65             for (ChartItemDescriptor desc : items) {
66                 if (desc.subscriptionItem == null)
67                     continue;
68
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)) {
73                         // Update fields
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);
80                             
81                             Resource rdm = getDrawmode( graph, desc.drawMode );
82                             if ( rdm != null ) graph.claim(chartItem, CHART.Chart_Item_DrawMode, rdm);
83                         }
84                         continue nextReference;
85                     }
86                 }
87
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);
101
102                 IEclipsePreferences pn = InstanceScope.INSTANCE.getNode( ChartPreferences.P_NODE );
103
104                 // Draw Mode
105                 {
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 );
109                     if ( rdm != null ) {
110                         graph.claim(chartItem, CHART.Chart_Item_DrawMode, rdm);
111                     }
112                 }
113
114                 // Scale Mode
115                 {
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();
121                         } else {
122                                 scale = new Scale.Manual(0, 100);
123                         }                               
124                         }
125                         
126                     if ( scale instanceof Scale.Auto ) {
127                         Resource r = CHART.ScaleMode_AutoScale;
128                         graph.claim(chartItem, CHART.Chart_Item_ScaleMode, r);
129                     } else {
130                         graph.claim(chartItem, CHART.Chart_Item_ScaleMode, CHART.ScaleMode_ManualScale);
131                     }
132
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);
135                 }
136                 
137                 this.chartItem = chartItem;
138                 
139                 result.add(chartItem);
140                 
141                 CommentMetadata cm = graph.getMetadata(CommentMetadata.class);
142                 graph.addMetadata(cm.add("Added chart item " + chartItem));
143             }
144         
145         return result;
146         
147     }
148
149     public static Resource getDrawmode(ReadGraph graph, DrawMode dm)
150     {
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;
159         return null;
160     }
161     
162     public static Resource getRenderer(ReadGraph graph, Renderer renderer) {
163         ChartResource CHART = ChartResource.getInstance(graph);
164         switch (renderer) {
165             case Analog:
166                 return CHART.Renderer_Analog;
167             case Binary:
168                 return CHART.Renderer_Binary;
169             default:
170                 throw new UnsupportedOperationException("invalid renderer: " + renderer);
171         }
172     }
173
174     public static ChartItemDescriptor createDescriptor(ReadGraph graph, Resource subscriptionItem) throws DatabaseException {
175         ModelingResources MOD = ModelingResources.getInstance(graph);
176
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;
181
182         return desc;
183     }
184
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>>() {
188             @Override
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);
196                     result.add(plot);
197                 }
198                 return result;
199             }
200         };
201     }
202
203 }