]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.charts/src/org/simantics/charts/query/ChartDataWrite.java
Statements request is missing dec
[simantics/platform.git] / bundles / org.simantics.charts / src / org / simantics / charts / query / ChartDataWrite.java
1 package org.simantics.charts.query;
2
3 import java.util.Arrays;
4
5 import org.simantics.charts.ontology.ChartResource;
6 import org.simantics.charts.ui.ChartData;
7 import org.simantics.charts.ui.ChartData.ItemKey;
8 import org.simantics.databoard.Bindings;
9 import org.simantics.db.Resource;
10 import org.simantics.db.WriteGraph;
11 import org.simantics.db.common.request.WriteRequest;
12 import org.simantics.db.exception.DatabaseException;
13 import org.simantics.layer0.Layer0;
14 import org.simantics.trend.configuration.YAxisMode;
15
16 public class ChartDataWrite extends WriteRequest {
17
18     ChartData data;
19     Resource chart;
20
21     public ChartDataWrite(Resource chart, ChartData data)
22     {
23         this.data = data;
24         this.chart = chart;
25     }
26
27     @Override
28     public void perform(WriteGraph g) throws DatabaseException {
29         // 0. Initialization
30         Layer0 L0 = Layer0.getInstance(g);
31         ChartResource CHART = ChartResource.getInstance(g);
32
33         // Label
34         g.claimLiteral(chart, L0.HasName, data.name, Bindings.STRING);
35
36         // Axis mode
37         if ( data.axisMode == YAxisMode.SingleAxis ) {
38             g.deny(chart, CHART.Chart_YAxisMode, CHART.YAxisMode_MultiAxis);
39             g.claim(chart, CHART.Chart_YAxisMode, CHART.YAxisMode_SingleAxis);
40         } else {
41             g.deny(chart, CHART.Chart_YAxisMode, CHART.YAxisMode_SingleAxis);
42             g.claim(chart, CHART.Chart_YAxisMode, CHART.YAxisMode_MultiAxis);
43         }
44
45         // Boolean flags
46         g.claimLiteral(chart, CHART.Chart_ShowMilestones, data.showMilestones, Bindings.BOOLEAN);
47         g.claimLiteral(chart, CHART.Chart_showGrid, data.showGrid, Bindings.BOOLEAN);
48         g.claimLiteral(chart, CHART.Chart_trackExperimentTime, data.trackExperimentTime, Bindings.BOOLEAN);
49
50         if ( data.timeIncrement!=null ) {
51             g.claimLiteral(chart, CHART.Chart_TimeWindowIncrement, data.timeIncrement, Bindings.DOUBLE);
52         } else {
53             g.deny(chart, CHART.Chart_TimeWindowIncrement);
54         }
55
56         if ( data.timeLength==null ) {
57             g.deny(chart, CHART.Chart_TimeWindowLength);
58         } else {
59             g.claimLiteral(chart, CHART.Chart_TimeWindowLength, data.timeLength, Bindings.DOUBLE);
60         }
61         if ( data.timeStart==null ) {
62             g.deny(chart, CHART.Chart_TimeWindowStart);
63         } else {
64             g.claimLiteral(chart, CHART.Chart_TimeWindowStart, data.timeStart, Bindings.DOUBLE);
65         }
66
67         if ( data.allItems != null && data.hiddenItems != null ) {
68             for (ItemKey item : data.allItems.keySet()) {
69                 boolean shouldBeHidden = data.hiddenItems.contains(item);
70                 Boolean isHidden = g.getRelatedValue(item.resource.getResource(), CHART.Chart_Item_hidden, Bindings.BOOLEAN);
71                 if (shouldBeHidden != isHidden)
72                     g.claimLiteral(item.resource.getResource(), CHART.Chart_Item_hidden, shouldBeHidden, Bindings.BOOLEAN);
73             }
74         }
75
76         if (data.backgroundColor1 == null) {
77             g.denyValue(chart, CHART.Chart_backgroundColor);
78         } else {
79             float[] oldValue = g.getPossibleRelatedValue(chart, CHART.Chart_backgroundColor, Bindings.FLOAT_ARRAY);
80             float[] newValue = data.backgroundGradient ? joinArrays(data.backgroundColor1, data.backgroundColor2) : data.backgroundColor1;
81             if (newValue != null && (oldValue == null || !Arrays.equals(oldValue, newValue))) {
82                 g.claimLiteral(chart, CHART.Chart_backgroundColor, newValue, Bindings.FLOAT_ARRAY);
83             }
84         }
85
86         if (data.gridColor == null) {
87             g.denyValue(chart, CHART.Chart_gridColor);
88         } else {
89             float[] oldValue = g.getPossibleRelatedValue(chart, CHART.Chart_gridColor, Bindings.FLOAT_ARRAY);
90             if (data.gridColor != null && (oldValue == null || !Arrays.equals(oldValue, data.gridColor))) {
91                 g.claimLiteral(chart, CHART.Chart_gridColor, data.gridColor, Bindings.FLOAT_ARRAY);
92             }
93         }
94     }
95
96     private static float[] joinArrays(float[] c1, float[] c2) {
97         int l1 = c1 != null ? c1.length : 0;
98         int l2 = c2 != null ? c2.length : 0;
99         int l = l1 + l2;
100         float[] result = new float[l];
101         int i = 0;
102         if (c1 != null) {
103             System.arraycopy(c1, 0, result, 0, l1);
104             i += l1;
105         }
106         if (c2 != null) {
107             System.arraycopy(c2, 0, result, i, l2);
108             i += l1;
109         }
110         return result;
111     }
112
113 }