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;
57 import org.slf4j.LoggerFactory;
60 * Data node for a TrendItem
62 * @author toni.kalajainen
64 public class ItemNode extends G2DNode implements TrendLayout {
66 private static final long serialVersionUID = -4741446944761752871L;
67 private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(ItemNode.class);
69 public static final AlphaComposite composite = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, .61f);
71 public TrendItem item;
78 /** A list of items that is ordered in by interval value, starting from the lowest interval */
79 Bean[] renderableItems = new Bean[0];
81 /** Format for the minmax stream, or null */
84 /** Known item min->max value range, and from->end time range */
85 double from=Double.NaN, end=Double.NaN, min=0, max=1;
91 StreamAccessor openStreamAccessor;
94 Logger log = Logger.getLogger( this.getClass().getName() );
96 boolean disabled = false;
97 Point2D.Double pt = new Point2D.Double();
98 Point2D.Double pt2 = new Point2D.Double();
101 KvikDeviationBuilder dev = new KvikDeviationBuilder();
102 Path2D.Double line = new Path2D.Double();
105 * Set trend item and initialize history
107 * @param items all items in history
109 public void setTrendItem(TrendItem ti, ItemManager items) {
110 if (openStream!=null) {
113 openStreamAccessor = null;
114 openStreamItem = null;
117 this.minmaxItem = null;
118 this.renderableItems = new HistoryItem[0];
120 if (ti==null) return;
123 List<Bean> trendItems = items.search("groupId", ti.groupId, "groupItemId", ti.groupItemId, "variableId", ti.variableId);
124 this.historyItems = trendItems.toArray( new Bean[trendItems.size()] );
125 Arrays.sort( this.historyItems, SamplingFormat.INTERVAL_COMPARATOR );
127 // Read renderable formats, and minmax format
128 TreeSet<Bean> streamFormats = new TreeSet<Bean>( SamplingFormat.INTERVAL_COMPARATOR );
129 for (Bean item : trendItems) {
130 SamplingFormat format = new SamplingFormat();
131 format.readAvailableFields(item);
132 RecordType rt = (RecordType) format.format;
133 Boolean enabled = (Boolean) item.getField("enabled");
134 if (!enabled) continue;
136 boolean isMinMaxFormat = format.interval==Double.MAX_VALUE &&
137 format.deadband==Double.MAX_VALUE &&
138 rt.getComponentIndex2("min")>=0 &&
139 rt.getComponentIndex2("max")>=0;
141 if (isMinMaxFormat) {
142 this.minmaxItem = item;
144 streamFormats.add(item);
147 if (streamFormats.isEmpty()) return;
149 renderableItems = streamFormats.toArray( new Bean[streamFormats.size()] );
151 } catch (BindingException e) {
152 throw new RuntimeException( e );
157 * Draw to graphics context as time,value pairs are.
159 * Phases, 0-Init data, 1-Draw deviation, 2-Draw line, 3-Cleanup
164 public void draw(Graphics2D g, int phase, boolean bold) {
166 item.drawMode==DrawMode.DeviationAndAverage ||
167 item.drawMode==DrawMode.DeviationAndLine ||
168 item.drawMode==DrawMode.DeviationAndSample;
171 Object newQuality = getTrendNode().printing||getTrendNode().quality.lineQuality==LineQuality.Antialias?RenderingHints.VALUE_ANTIALIAS_ON:RenderingHints.VALUE_ANTIALIAS_OFF;
174 if (!dev.isEmpty()) {
175 Object old = g.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
176 g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, newQuality);
178 if (item.renderer == Renderer.Binary) {
179 dev.drawRectangles(g);
182 if (item.renderer == Renderer.Analog) {
185 Composite c = g.getComposite();
186 g.setComposite( composite );
187 dev.drawRectangles(g);
189 } else if (item.drawMode == DrawMode.Deviation) {
191 dev.drawRectangles(g);
194 g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, old);
201 Object old = g.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
202 g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, newQuality);
204 g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, old);
209 private boolean validSample(ValueBand band) throws HistoryException {
210 if(band.getTimeDouble() > 1e50) return false;
211 if(band.getTimeDouble() > band.getEndTimeDouble()) return false;
212 // if(band.getTimeDouble() < 0) return false;
213 // if(band.getEndTimeDouble() < 0) return false;
214 //if(band.getEndTimeDouble()-band.getTimeDouble() < 1e-9) return false;
218 // Draw state needed for clipping
224 void moveTo(double x, double y) {
230 void lineToOrdered(double x, double y) {
233 if(currentX > maxX) return;
235 // We have something to draw
238 if(currentX < minX) {
239 double ny = currentY + (y-currentY) * (minX-currentX) / (x-currentX);
240 line.moveTo(minX, ny);
247 double ny = currentY + (y-currentY) * (maxX-currentX) / (x-currentX);
248 line.lineTo(maxX, ny);
256 void lineTo(double x, double y) {
258 // First assert ordering - if samples are in wrong order draw nothing
272 * This method prepares line and deviation shapes.
275 * @param secondsPerPixel
277 * @throws HistoryException
278 * @throws AccessorException
280 public void prepareLine(double from, double end, double pixelsPerSecond, AffineTransform at) throws HistoryException, AccessorException
282 // boolean devAndLine =
283 // item.drawMode==DrawMode.DeviationAndAverage ||
284 // item.drawMode==DrawMode.DeviationAndLine ||
285 // item.drawMode==DrawMode.DeviationAndSample;
286 // boolean deviationEnabled = devAndLine ||
287 // item.drawMode == DrawMode.Deviation;
289 boolean devAndLine = false;
290 boolean deviationEnabled = false;
295 Stream s = openStream(pixelsPerSecond);
297 ValueBand vb = new ValueBand(s.sampleBinding, s.sampleBinding.createDefaultUnchecked());
298 boolean hasDeviation = vb.hasMin() && vb.hasMax();
299 boolean drawDeviation = hasDeviation & deviationEnabled;
301 Datatype valueType = vb.getValueBinding().type();
303 if ( valueType instanceof BooleanType == false && valueType instanceof NumberType == false ) return;
305 int start = s.binarySearch(Bindings.DOUBLE, from);
306 int count = s.count();
307 if (start<0) start = -2-start;
308 if (start<0) start = 0;
310 // moveTo, on next draw, if true
311 boolean lineNotAttached = true; // =next draw should be move to
312 boolean devNotAttached = true;
314 currentX = Double.NaN;
315 currentY = Double.NaN;
317 pt.setLocation(from, 0);
318 at.transform(pt, pt);
321 pt.setLocation(end, 0);
322 at.transform(pt, pt);
327 boolean wentOver = false;
329 for (int i=start; i<count; i++) {
331 s.accessor.get(i, s.sampleBinding, vb.getSample());
333 if(!validSample(vb)) {
334 LOGGER.warn("-invalid value band: " + i + "/" + count + ":" + vb);
337 // Non-continuation point
338 if (vb.hasQuality()) {
339 Byte b = (Byte) vb.getQuality(Bindings.BYTE);
340 boolean noncontinuation = b.equals( ValueBand.QUALITY_NOVALUE );
341 if ( noncontinuation ) {
342 lineNotAttached = true;
343 devNotAttached = true;
349 double t1 = vb.getTimeDouble();
350 double t2 = vb.hasEndTime() ? vb.getEndTimeDouble() : t1;
353 if (item.renderer == Renderer.Analog) {
355 // Add points to line
356 if (item.drawMode == DrawMode.Deviation && hasDeviation) {
360 double yy = Double.NaN;
361 // boolean showBand = true;
362 boolean flat = false;
364 if( trendNode != null && trendNode.drawSamples ) {
365 yy = vb.getValueDouble();
368 yy = vb.getValueDouble();
369 // Only render the last band
370 // if(i < count-1) showBand = false;
373 // if (item.drawMode == DrawMode.DeviationAndAverage) {
374 // yy = vb.hasMedian() ? vb.getMedianDouble() : ( vb.hasAvg() ? vb.getAvgDouble() : vb.getValueDouble() );
375 // } else if (item.drawMode == DrawMode.Average || item.drawMode == DrawMode.DeviationAndAverage) {
376 // yy = vb.hasAvg() ? vb.getAvgDouble() : vb.getValueDouble();
377 // } else if (item.drawMode == DrawMode.Line || item.drawMode == DrawMode.DeviationAndLine) {
378 // yy = vb.getValueDouble();
379 // // Only render the last band
380 // if(i < count-1) showBand = false;
381 // } else /* if (item.drawMode == DrawMode.Sample || item.drawMode == DrawMode.DeviationAndSample) */ {
382 // yy = vb.getValueDouble();
386 if ( Double.isNaN(yy) ) {
387 lineNotAttached = true;
389 // pt.setLocation(t1, yy);
390 // at.transform(pt, pt);
392 // if (lineNotAttached) {
393 // moveTo(pt.getX(), pt.getY());
398 // lineTo(pt.getX(), pt.getY());
402 // Static variables may have months data that consists of single value
403 // When zoomed in, the single line may be drawn from -1e10, ... 1e10
404 // This is too much for Graphics2D, it refuses to draw.
412 pt.setLocation(t1, yy);
413 at.transform(pt, pt);
414 pt2.setLocation(t2, yy);
415 at.transform(pt2, pt2);
417 double x1 = pt.x, y1 = pt.y, x2 = pt2.x, y2 = pt2.y;
419 if (lineNotAttached) {
423 lineTo(x1, currentY);
437 lineNotAttached = false;
442 double min = vb.getMinDouble();
443 double max = vb.getMaxDouble();
445 pt.setLocation(t1, min);
446 at.transform(pt, pt);
447 pt2.setLocation(t2, max);
448 at.transform(pt2, pt2);
454 double width = x2-x1;
455 boolean tooWide = devAndLine && (width>2.0);
456 if (Double.isNaN(min) || Double.isNaN(max) || tooWide || width<=0.0 || y1==y2) {
457 devNotAttached = true;
459 if (devNotAttached) {
460 dev.addRectangle(x1, x2, y1, y2);
462 dev.extendRectangle(x1);
463 dev.addRectangle(x1, x2, y1, y2);
465 devNotAttached = false;
471 if (item.renderer == Renderer.Binary) {
473 if (vb.getValueBinding() instanceof BooleanBinding) {
474 value = ((Boolean) vb.getValue(Bindings.BOOLEAN)) ? (byte)0 : (byte)1;
475 } else if (vb.getValueBinding() instanceof ByteBinding) {
476 value = ((Byte) vb.getValue(Bindings.BYTE));
477 } else if (vb.hasMax()) {
478 value = (Byte) vb.getMax(Bindings.BYTE);
480 value = (Byte) vb.getValue(Bindings.BYTE);
482 pt.setLocation(t1, value==1 ? BINARY[1] : BINARY[0]);
483 at.transform(pt, pt);
484 pt2.setLocation(t2, BINARY[2]);
485 at.transform(pt2, pt2);
490 dev.extendRectangle(x1);
491 dev.addRectangle(x1, x2, y1, y2);
492 devNotAttached = false;
495 // Already over => break
506 public boolean readMinMaxFromEnd() {
507 if (disabled) return false;
508 HistoryManager history = getTrendNode().historian;
510 boolean hasVariable = !item.variableId.isEmpty() && !item.groupItemId.isEmpty() && !item.groupId.isEmpty();
511 boolean canReadMinMax = minmaxItem != null;
512 boolean manualScale = item.scale instanceof Scale.Manual;
514 if ( !hasVariable ) {
523 if (canReadMinMax && !manualScale) {
524 String id = (String) minmaxItem.getFieldUnchecked("id");
525 StreamAccessor sa = history.openStream(id, "r");
534 if (sa.size()==0) return false;
535 min = Double.MAX_VALUE;
536 max = -Double.MAX_VALUE;
537 from = Double.MAX_VALUE;
538 end = -Double.MAX_VALUE;
539 for (int i=0; i<sa.size(); i++) {
540 Binding binding = Bindings.getBinding( sa.type().componentType() );
541 Object sample = sa.get(i, binding);
542 ValueBand vb = new ValueBand(binding, sample);
543 if (!vb.isNullValue() && !vb.isNanSample()) {
544 min = Math.min(min, vb.getMinDouble());
545 max = Math.max(max, vb.getMaxDouble());
547 if (!vb.isNullValue()) {
548 from = Math.min(from, vb.getTimeDouble());
549 end = Math.max(end, vb.getEndTimeDouble());
552 if ( min==Double.MAX_VALUE || max==-Double.MAX_VALUE) {
555 if ( from==Double.MAX_VALUE || end==-Double.MAX_VALUE) {
556 from = 0; end = 100.0;
560 try { sa.close(); } catch (AccessorException e) {}
565 Scale.Manual ms = (Scale.Manual) item.scale;
570 // Read from, end from any stream
571 if (openStreamAccessor==null) {
574 if (openStreamAccessor!=null){
576 StreamAccessor sa = openStreamAccessor;
578 int count = sa.size();
580 Binding binding = Bindings.getBinding( sa.type().componentType() );
581 Object sample = sa.get(0, binding);
582 ValueBand vb = new ValueBand(binding, sample);
583 from = vb.getTimeDouble();
584 sa.get(count-1, binding, sample);
585 end = vb.hasEndTime() ? vb.getEndTimeDouble() : vb.getTimeDouble();
592 } catch (AccessorException e) {
593 log.log(Level.FINE, e.toString(), e);
595 } catch (HistoryException e) {
596 log.log(Level.FINE, e.toString(), e);
602 public void cleanup() {
604 if (openStreamAccessor != null) {
606 openStreamAccessor.close();
607 } catch (AccessorException e) {
609 openStreamAccessor = null;
610 openStreamItem = null;
615 Bean getFormat(double pixelsPerSecond) {
617 if (renderableItems == null)
620 for (Bean format : renderableItems)
622 double interval = 0.0;
624 interval = format.hasField("interval") ? (Double) format.getFieldUnchecked("interval") : 0.0;
625 } catch (RuntimeBindingException e) {
626 } catch (BindingException e) {
628 if (Double.isNaN( interval ) || interval<=pixelsPerSecond) {
635 if ( renderableItems.length == 0 ) {
638 result = renderableItems[0];
645 public Stream openStream(double pixelsPerSecond) {
646 Bean f = getFormat(pixelsPerSecond);
647 if (f==openStreamItem) return openStream;
649 if (openStream != null) {
651 openStreamAccessor = null;
652 openStreamItem = null;
656 if (disabled) return null;
659 HistoryManager historian = getTrendNode().historian;
661 String id = (String) f.getFieldUnchecked("id");
662 openStreamAccessor = historian.openStream(id, "r");
663 if ( openStreamAccessor!=null ) {
664 openStream = new Stream(openStreamAccessor);
669 } catch (HistoryException e) {
670 log.log(Level.FINE, e.toString(), e);
678 public void render(Graphics2D g2d) {
682 public Rectangle2D getBoundsInLocal() {
686 TrendNode getTrendNode() {
687 return (TrendNode) getParent();