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.trend.impl;
14 import java.awt.AlphaComposite;
15 import java.awt.BasicStroke;
16 import java.awt.Color;
17 import java.awt.Composite;
18 import java.awt.Graphics2D;
19 import java.awt.RenderingHints;
20 import java.awt.geom.AffineTransform;
21 import java.awt.geom.Path2D;
22 import java.awt.geom.Point2D;
23 import java.awt.geom.Rectangle2D;
24 import java.util.Arrays;
25 import java.util.List;
26 import java.util.TreeSet;
27 import java.util.logging.Level;
28 import java.util.logging.Logger;
30 import org.simantics.databoard.Bindings;
31 import org.simantics.databoard.accessor.StreamAccessor;
32 import org.simantics.databoard.accessor.error.AccessorException;
33 import org.simantics.databoard.binding.Binding;
34 import org.simantics.databoard.binding.BooleanBinding;
35 import org.simantics.databoard.binding.ByteBinding;
36 import org.simantics.databoard.binding.error.BindingException;
37 import org.simantics.databoard.binding.error.RuntimeBindingException;
38 import org.simantics.databoard.type.BooleanType;
39 import org.simantics.databoard.type.Datatype;
40 import org.simantics.databoard.type.NumberType;
41 import org.simantics.databoard.type.RecordType;
42 import org.simantics.databoard.util.Bean;
43 import org.simantics.history.HistoryException;
44 import org.simantics.history.HistoryItem;
45 import org.simantics.history.HistoryManager;
46 import org.simantics.history.ItemManager;
47 import org.simantics.history.util.Stream;
48 import org.simantics.history.util.ValueBand;
49 import org.simantics.history.util.subscription.SamplingFormat;
50 import org.simantics.scenegraph.g2d.G2DNode;
51 import org.simantics.trend.configuration.LineQuality;
52 import org.simantics.trend.configuration.Scale;
53 import org.simantics.trend.configuration.TrendItem;
54 import org.simantics.trend.configuration.TrendItem.DrawMode;
55 import org.simantics.trend.configuration.TrendItem.Renderer;
56 import org.simantics.trend.util.KvikDeviationBuilder;
59 * Data node for a TrendItem
61 * @author toni.kalajainen
63 public class ItemNode extends G2DNode implements TrendLayout {
65 private static final long serialVersionUID = -4741446944761752871L;
67 public static final AlphaComposite composite = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, .61f);
69 public TrendItem item;
76 /** A list of items that is ordered in by interval value, starting from the lowest interval */
77 Bean[] renderableItems = new Bean[0];
79 /** Format for the minmax stream, or null */
82 /** Known item min->max value range, and from->end time range */
83 double from=Double.NaN, end=Double.NaN, min=0, max=1;
89 StreamAccessor openStreamAccessor;
92 Logger log = Logger.getLogger( this.getClass().getName() );
94 boolean disabled = false;
95 Point2D.Double pt = new Point2D.Double();
96 Point2D.Double pt2 = new Point2D.Double();
99 KvikDeviationBuilder dev = new KvikDeviationBuilder();
100 Path2D.Double line = new Path2D.Double();
103 * Set trend item and initialize history
105 * @param items all items in history
107 public void setTrendItem(TrendItem ti, ItemManager items) {
108 if (openStream!=null) {
111 openStreamAccessor = null;
112 openStreamItem = null;
115 this.minmaxItem = null;
116 this.renderableItems = new HistoryItem[0];
118 if (ti==null) return;
121 List<Bean> trendItems = items.search("groupId", ti.groupId, "groupItemId", ti.groupItemId, "variableId", ti.variableId);
122 this.historyItems = trendItems.toArray( new Bean[trendItems.size()] );
123 Arrays.sort( this.historyItems, SamplingFormat.INTERVAL_COMPARATOR );
125 // Read renderable formats, and minmax format
126 TreeSet<Bean> streamFormats = new TreeSet<Bean>( SamplingFormat.INTERVAL_COMPARATOR );
127 for (Bean item : trendItems) {
128 SamplingFormat format = new SamplingFormat();
129 format.readAvailableFields(item);
130 RecordType rt = (RecordType) format.format;
131 Boolean enabled = (Boolean) item.getField("enabled");
132 if (!enabled) continue;
134 boolean isMinMaxFormat = format.interval==Double.MAX_VALUE &&
135 format.deadband==Double.MAX_VALUE &&
136 rt.getComponentIndex2("min")>=0 &&
137 rt.getComponentIndex2("max")>=0;
139 if (isMinMaxFormat) {
140 this.minmaxItem = item;
142 streamFormats.add(item);
145 if (streamFormats.isEmpty()) return;
147 renderableItems = streamFormats.toArray( new Bean[streamFormats.size()] );
149 } catch (BindingException e) {
150 throw new RuntimeException( e );
155 * Draw to graphics context as time,value pairs are.
157 * Phases, 0-Init data, 1-Draw deviation, 2-Draw line, 3-Cleanup
162 public void draw(Graphics2D g, int phase, boolean bold) {
164 item.drawMode==DrawMode.DeviationAndAverage ||
165 item.drawMode==DrawMode.DeviationAndLine ||
166 item.drawMode==DrawMode.DeviationAndSample;
169 Object newQuality = getTrendNode().printing||getTrendNode().quality.lineQuality==LineQuality.Antialias?RenderingHints.VALUE_ANTIALIAS_ON:RenderingHints.VALUE_ANTIALIAS_OFF;
172 if (!dev.isEmpty()) {
173 Object old = g.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
174 g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, newQuality);
176 if (item.renderer == Renderer.Binary) {
177 dev.drawRectangles(g);
180 if (item.renderer == Renderer.Analog) {
183 Composite c = g.getComposite();
184 g.setComposite( composite );
185 dev.drawRectangles(g);
187 } else if (item.drawMode == DrawMode.Deviation) {
189 dev.drawRectangles(g);
192 g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, old);
199 Object old = g.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
200 g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, newQuality);
202 g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, old);
207 private boolean validSample(ValueBand band) throws HistoryException {
208 if(band.getTimeDouble() > 1e50) return false;
209 if(band.getTimeDouble() > band.getEndTimeDouble()) return false;
210 // if(band.getTimeDouble() < 0) return false;
211 // if(band.getEndTimeDouble() < 0) return false;
212 //if(band.getEndTimeDouble()-band.getTimeDouble() < 1e-9) return false;
216 // Draw state needed for clipping
222 void moveTo(double x, double y) {
228 void lineToOrdered(double x, double y) {
231 if(currentX > maxX) return;
233 // We have something to draw
236 if(currentX < minX) {
237 double ny = currentY + (y-currentY) * (minX-currentX) / (x-currentX);
238 line.moveTo(minX, ny);
245 double ny = currentY + (y-currentY) * (maxX-currentX) / (x-currentX);
246 line.lineTo(maxX, ny);
254 void lineTo(double x, double y) {
256 // First assert ordering - if samples are in wrong order draw nothing
270 * This method prepares line and deviation shapes.
273 * @param secondsPerPixel
275 * @throws HistoryException
276 * @throws AccessorException
278 public void prepareLine(double from, double end, double pixelsPerSecond, AffineTransform at) throws HistoryException, AccessorException
280 // boolean devAndLine =
281 // item.drawMode==DrawMode.DeviationAndAverage ||
282 // item.drawMode==DrawMode.DeviationAndLine ||
283 // item.drawMode==DrawMode.DeviationAndSample;
284 // boolean deviationEnabled = devAndLine ||
285 // item.drawMode == DrawMode.Deviation;
287 boolean devAndLine = false;
288 boolean deviationEnabled = false;
293 Stream s = openStream(pixelsPerSecond);
295 ValueBand vb = new ValueBand(s.sampleBinding, s.sampleBinding.createDefaultUnchecked());
296 boolean hasDeviation = vb.hasMin() && vb.hasMax();
297 boolean drawDeviation = hasDeviation & deviationEnabled;
299 Datatype valueType = vb.getValueBinding().type();
301 if ( valueType instanceof BooleanType == false && valueType instanceof NumberType == false ) return;
303 int start = s.binarySearch(Bindings.DOUBLE, from);
304 int count = s.count();
305 if (start<0) start = -2-start;
306 if (start<0) start = 0;
308 // moveTo, on next draw, if true
309 boolean lineNotAttached = true; // =next draw should be move to
310 boolean devNotAttached = true;
312 currentX = Double.NaN;
313 currentY = Double.NaN;
315 pt.setLocation(from, 0);
316 at.transform(pt, pt);
319 pt.setLocation(end, 0);
320 at.transform(pt, pt);
325 boolean wentOver = false;
327 for (int i=start; i<count; i++) {
329 s.accessor.get(i, s.sampleBinding, vb.getSample());
331 if(!validSample(vb)) {
332 System.err.println("-invalid value band: " + i + "/" + count + ":" + vb);
335 // Non-continuation point
336 if (vb.hasQuality()) {
337 Byte b = (Byte) vb.getQuality(Bindings.BYTE);
338 boolean noncontinuation = b.equals( ValueBand.QUALITY_NOVALUE );
339 if ( noncontinuation ) {
340 lineNotAttached = true;
341 devNotAttached = true;
347 double t1 = vb.getTimeDouble();
348 double t2 = vb.hasEndTime() ? vb.getEndTimeDouble() : t1;
351 if (item.renderer == Renderer.Analog) {
353 // Add points to line
354 if (item.drawMode == DrawMode.Deviation && hasDeviation) {
358 double yy = Double.NaN;
359 // boolean showBand = true;
360 boolean flat = false;
362 if( trendNode != null && trendNode.drawSamples ) {
363 yy = vb.getValueDouble();
366 yy = vb.getValueDouble();
367 // Only render the last band
368 // if(i < count-1) showBand = false;
371 // if (item.drawMode == DrawMode.DeviationAndAverage) {
372 // yy = vb.hasMedian() ? vb.getMedianDouble() : ( vb.hasAvg() ? vb.getAvgDouble() : vb.getValueDouble() );
373 // } else if (item.drawMode == DrawMode.Average || item.drawMode == DrawMode.DeviationAndAverage) {
374 // yy = vb.hasAvg() ? vb.getAvgDouble() : vb.getValueDouble();
375 // } else if (item.drawMode == DrawMode.Line || item.drawMode == DrawMode.DeviationAndLine) {
376 // yy = vb.getValueDouble();
377 // // Only render the last band
378 // if(i < count-1) showBand = false;
379 // } else /* if (item.drawMode == DrawMode.Sample || item.drawMode == DrawMode.DeviationAndSample) */ {
380 // yy = vb.getValueDouble();
384 if ( Double.isNaN(yy) ) {
385 lineNotAttached = true;
387 // pt.setLocation(t1, yy);
388 // at.transform(pt, pt);
390 // if (lineNotAttached) {
391 // moveTo(pt.getX(), pt.getY());
396 // lineTo(pt.getX(), pt.getY());
400 // Static variables may have months data that consists of single value
401 // When zoomed in, the single line may be drawn from -1e10, ... 1e10
402 // This is too much for Graphics2D, it refuses to draw.
410 pt.setLocation(t1, yy);
411 at.transform(pt, pt);
412 pt2.setLocation(t2, yy);
413 at.transform(pt2, pt2);
415 double x1 = pt.x, y1 = pt.y, x2 = pt2.x, y2 = pt2.y;
417 if (lineNotAttached) {
421 lineTo(x1, currentY);
435 lineNotAttached = false;
440 double min = vb.getMinDouble();
441 double max = vb.getMaxDouble();
443 pt.setLocation(t1, min);
444 at.transform(pt, pt);
445 pt2.setLocation(t2, max);
446 at.transform(pt2, pt2);
452 double width = x2-x1;
453 boolean tooWide = devAndLine && (width>2.0);
454 if (Double.isNaN(min) || Double.isNaN(max) || tooWide || width<=0.0 || y1==y2) {
455 devNotAttached = true;
457 if (devNotAttached) {
458 dev.addRectangle(x1, x2, y1, y2);
460 dev.extendRectangle(x1);
461 dev.addRectangle(x1, x2, y1, y2);
463 devNotAttached = false;
469 if (item.renderer == Renderer.Binary) {
471 if (vb.getValueBinding() instanceof BooleanBinding) {
472 value = ((Boolean) vb.getValue(Bindings.BOOLEAN)) ? (byte)0 : (byte)1;
473 } else if (vb.getValueBinding() instanceof ByteBinding) {
474 value = ((Byte) vb.getValue(Bindings.BYTE));
475 } else if (vb.hasMax()) {
476 value = (Byte) vb.getMax(Bindings.BYTE);
478 value = (Byte) vb.getValue(Bindings.BYTE);
480 pt.setLocation(t1, value==1 ? BINARY[1] : BINARY[0]);
481 at.transform(pt, pt);
482 pt2.setLocation(t2, BINARY[2]);
483 at.transform(pt2, pt2);
488 dev.extendRectangle(x1);
489 dev.addRectangle(x1, x2, y1, y2);
490 devNotAttached = false;
493 // Already over => break
504 public boolean readMinMaxFromEnd() {
505 if (disabled) return false;
506 HistoryManager history = getTrendNode().historian;
508 boolean hasVariable = !item.variableId.isEmpty() && !item.groupItemId.isEmpty() && !item.groupId.isEmpty();
509 boolean canReadMinMax = minmaxItem != null;
510 boolean manualScale = item.scale instanceof Scale.Manual;
512 if ( !hasVariable ) {
521 if (canReadMinMax && !manualScale) {
522 String id = (String) minmaxItem.getFieldUnchecked("id");
523 StreamAccessor sa = history.openStream(id, "r");
532 if (sa.size()==0) return false;
533 min = Double.MAX_VALUE;
534 max = -Double.MAX_VALUE;
535 from = Double.MAX_VALUE;
536 end = -Double.MAX_VALUE;
537 for (int i=0; i<sa.size(); i++) {
538 Binding binding = Bindings.getBinding( sa.type().componentType() );
539 Object sample = sa.get(i, binding);
540 ValueBand vb = new ValueBand(binding, sample);
541 if (!vb.isNullValue() && !vb.isNanSample()) {
542 min = Math.min(min, vb.getMinDouble());
543 max = Math.max(max, vb.getMaxDouble());
545 if (!vb.isNullValue()) {
546 from = Math.min(from, vb.getTimeDouble());
547 end = Math.max(end, vb.getEndTimeDouble());
550 if ( min==Double.MAX_VALUE || max==-Double.MAX_VALUE) {
553 if ( from==Double.MAX_VALUE || end==-Double.MAX_VALUE) {
554 from = 0; end = 100.0;
558 try { sa.close(); } catch (AccessorException e) {}
563 Scale.Manual ms = (Scale.Manual) item.scale;
568 // Read from, end from any stream
569 if (openStreamAccessor==null) {
572 if (openStreamAccessor!=null){
574 StreamAccessor sa = openStreamAccessor;
576 int count = sa.size();
578 Binding binding = Bindings.getBinding( sa.type().componentType() );
579 Object sample = sa.get(0, binding);
580 ValueBand vb = new ValueBand(binding, sample);
581 from = vb.getTimeDouble();
582 sa.get(count-1, binding, sample);
583 end = vb.hasEndTime() ? vb.getEndTimeDouble() : vb.getTimeDouble();
590 } catch (AccessorException e) {
591 log.log(Level.FINE, e.toString(), e);
593 } catch (HistoryException e) {
594 log.log(Level.FINE, e.toString(), e);
600 public void cleanup() {
602 if (openStreamAccessor != null) {
604 openStreamAccessor.close();
605 } catch (AccessorException e) {
607 openStreamAccessor = null;
608 openStreamItem = null;
613 Bean getFormat(double pixelsPerSecond) {
615 if (renderableItems == null)
618 for (Bean format : renderableItems)
620 double interval = 0.0;
622 interval = format.hasField("interval") ? (Double) format.getFieldUnchecked("interval") : 0.0;
623 } catch (RuntimeBindingException e) {
624 } catch (BindingException e) {
626 if (Double.isNaN( interval ) || interval<=pixelsPerSecond) {
633 if ( renderableItems.length == 0 ) {
636 result = renderableItems[0];
643 public Stream openStream(double pixelsPerSecond) {
644 Bean f = getFormat(pixelsPerSecond);
645 if (f==openStreamItem) return openStream;
647 if (openStream != null) {
649 openStreamAccessor = null;
650 openStreamItem = null;
654 if (disabled) return null;
657 HistoryManager historian = getTrendNode().historian;
659 String id = (String) f.getFieldUnchecked("id");
660 openStreamAccessor = historian.openStream(id, "r");
661 if ( openStreamAccessor!=null ) {
662 openStream = new Stream(openStreamAccessor);
667 } catch (HistoryException e) {
668 log.log(Level.FINE, e.toString(), e);
676 public void render(Graphics2D g2d) {
680 public Rectangle2D getBoundsInLocal() {
684 TrendNode getTrendNode() {
685 return (TrendNode) getParent();