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.BasicStroke;
15 import java.awt.Color;
17 import java.awt.Graphics2D;
18 import java.awt.Rectangle;
19 import java.awt.RenderingHints;
20 import java.awt.font.GlyphVector;
21 import java.awt.geom.Rectangle2D;
22 import java.util.ArrayList;
23 import java.util.Collections;
24 import java.util.Comparator;
25 import java.util.HashMap;
26 import java.util.HashSet;
27 import java.util.List;
30 import java.util.TreeSet;
32 import org.simantics.databoard.Bindings;
33 import org.simantics.databoard.accessor.error.AccessorException;
34 import org.simantics.databoard.binding.error.BindingException;
35 import org.simantics.databoard.util.Bean;
36 import org.simantics.g2d.utils.GridUtil;
37 import org.simantics.history.Collector;
38 import org.simantics.history.History;
39 import org.simantics.history.HistoryException;
40 import org.simantics.history.HistoryManager;
41 import org.simantics.history.ItemManager;
42 import org.simantics.history.impl.CollectorImpl;
43 import org.simantics.history.util.Stream;
44 import org.simantics.history.util.ValueBand;
45 import org.simantics.scenegraph.g2d.G2DParentNode;
46 import org.simantics.scenegraph.utils.QualityHints;
47 import org.simantics.trend.configuration.ItemPlacement;
48 import org.simantics.trend.configuration.LineQuality;
49 import org.simantics.trend.configuration.Scale;
50 import org.simantics.trend.configuration.TimeFormat;
51 import org.simantics.trend.configuration.TimeWindow;
52 import org.simantics.trend.configuration.TrendItem;
53 import org.simantics.trend.configuration.TrendItem.Renderer;
54 import org.simantics.trend.configuration.TrendQualitySpec;
55 import org.simantics.trend.configuration.TrendSpec;
56 import org.simantics.trend.configuration.Viewport;
57 import org.simantics.trend.configuration.Viewport.AxisViewport;
58 import org.simantics.trend.configuration.YAxisMode;
59 import org.simantics.utils.format.ValueFormat;
61 import gnu.trove.map.TObjectIntMap;
62 import gnu.trove.map.hash.TObjectIntHashMap;
63 import gnu.trove.procedure.TObjectProcedure;
65 public class TrendNode extends G2DParentNode implements TrendLayout {
67 private static final long serialVersionUID = -8339696405893626168L;
70 public TextNode titleNode;
74 public HorizRuler horizRuler;
75 VertRuler vertRuler; // Selected vertical ruler, null if there are no analog items
76 int vertRulerIndex; // Index of the selected vertical ruler
77 List<VertRuler> vertRulers = new ArrayList<VertRuler>();
78 SelectionNode selection;
79 public MilestoneSpec milestones;
82 Rectangle2D bounds = new Rectangle2D.Double();
85 public TrendSpec spec;
86 public ViewRenderingProfile renderingProfile = new ViewRenderingProfile();
87 public TrendQualitySpec quality = TrendQualitySpec.DEFAULT;
88 public boolean printing = false;
92 List<ItemNode> analogItems = new ArrayList<ItemNode>();
93 List<ItemNode> binaryItems = new ArrayList<ItemNode>();
94 List<ItemNode> allItems = new ArrayList<ItemNode>();
97 static HistoryManager DUMMY_HISTORY = History.createMemoryHistory();
98 public HistoryManager historian = DUMMY_HISTORY;
100 // Collector - set for flushing the stream, right before drawing
101 public Collector collector = null;
102 Set<String> itemIds = new HashSet<String>();
104 // Signal to indicate history has changed.
105 public boolean datadirty = false;
106 // Signal to indicate the cached shapes are dirty. This will cause reloading of data.
107 public boolean shapedirty = false;
109 public boolean autoscaletime = true;
111 public TimeFormat timeFormat = TimeFormat.Time;
112 public ValueFormat valueFormat = ValueFormat.Default;
113 public boolean drawSamples = false;
115 /** Time at mouse, when mouse hovers over trend. This value is set by TrendParticipant. NaN is mouse is not hovering */
116 public double mouseHoverTime = Double.NaN, lastMouseHoverTime = 0.0;
118 /** If set, valueTip is drawn at the time */
119 public Double valueTipTime = null, prevValueTipTime = null;
120 public boolean valueTipHover = false;
122 public ItemPlacement itemPlacement = ItemPlacement.Overlapping;
126 spec = new TrendSpec();
129 milestones = new MilestoneSpec();
132 //// Create title node
133 titleNode = addNode( "Title", TextNode.class );
134 titleNode.setFont( new Font("Arial", 0, 30) );
135 titleNode.setColor( Color.BLACK );
136 titleNode.setText( "<title here>");
137 titleNode.setSize(300, 40);
139 plot = addNode( "Plot", Plot.class );
141 horizRuler = addNode("HorizRuler", HorizRuler.class);
142 vertRuler = addNode("VertRuler", VertRuler.class);
143 vertRulers.add( vertRuler );
146 horizRuler.setFromEnd(0, 100);
147 vertRuler.setMinMax(0, 100);
154 * @param collector (Optional) Used for flushing collectors
156 public void setHistorian(HistoryManager historian, Collector collector) {
157 this.historian = historian==null?DUMMY_HISTORY:historian;
158 this.collector = collector;
160 ItemManager allFiles = ItemManager.createUnchecked( getHistoryItems() );
161 for (ItemNode item : allItems) {
162 item.setTrendItem(item.item, allFiles);
163 for (Bean historyItem : item.historyItems ) {
165 itemIds.add( (String) historyItem.getField("id") );
166 } catch (BindingException e) {
173 * Get info of all history items.
174 * This util is created for polling strategy.
178 Bean[] getHistoryItems() {
179 Bean[] result = null;
180 HistoryException e = null;
181 for (int attempt=0; attempt<10; attempt++) {
183 result = historian.getItems();
185 } catch (HistoryException e2) {
189 } catch (InterruptedException e1) {
193 if (result!=null) return result;
194 //throw new RuntimeException(e);
198 public void setMilestones(Bean milestones) {
199 if (this.milestones.equals(milestones)) return;
200 this.milestones.readFrom( milestones );
201 boolean hasBaseline = this.milestones.baseline>=0;
203 // Sort by first, put baseline on top
204 final Milestone baseline = this.milestones.baseline>=0 ? this.milestones.milestones.get( this.milestones.baseline ) : null;
205 Collections.sort(this.milestones.milestones, new Comparator<Milestone>() {
206 public int compare(Milestone o1, Milestone o2) {
207 if (o1==baseline) return -1;
208 if (o2==baseline) return 1;
209 return Double.compare(o1.time, o1.time);
212 this.milestones.baseline = hasBaseline ? 0 : -1;
213 double newBasetime = hasBaseline ? this.milestones.milestones.get(this.milestones.baseline).time : 0.;
214 if (newBasetime != horizRuler.basetime) {
215 horizRuler.basetime = newBasetime;
221 public void selectVertRuler(int index) {
222 vertRulerIndex = index;
223 if (index<0 || index>=vertRulers.size()) {
224 vertRuler = vertRulers.get(0);
226 vertRuler = vertRulers.get(index);
232 public void cleanup() {
233 spec = new TrendSpec();
238 historian = DUMMY_HISTORY;
242 private static TObjectIntMap<String> itemIndexMap(List<TrendItem> items) {
243 TObjectIntMap<String> map = new TObjectIntHashMap<>(items.size(), 0.5f, -1);
244 for (int i = 0; i < items.size(); ++i) {
245 TrendItem it = items.get(i);
247 map.put(it.groupItemId, i);
252 private static <T> TObjectIntMap<T> subtract(TObjectIntMap<T> a, TObjectIntMap<T> b) {
253 TObjectIntMap<T> r = new TObjectIntHashMap<>(a);
254 b.forEachKey(new TObjectProcedure<T>() {
256 public boolean execute(T key) {
266 * new trending specification, cannot not be <code>null</code>.
267 * Use {@link TrendSpec#EMPTY} instead of <code>null</code>.
269 public void setTrendSpec(TrendSpec newSpec) {
270 //System.out.println(newSpec);
271 // Check if equal & Read spec
272 if (newSpec.equals(this.spec)) return;
274 boolean timeWindowChange = !this.spec.viewProfile.timeWindow.equals( newSpec.viewProfile.timeWindow );
275 boolean yaxisModeChanged = this.spec.axisMode != newSpec.axisMode;
277 TObjectIntMap<String> newItemMap = itemIndexMap(newSpec.items);
278 TObjectIntMap<String> currentItemMap = itemIndexMap(spec.items);
279 TObjectIntMap<String> removedItemMap = subtract(currentItemMap, newItemMap);
280 Map<String, VertRuler> existingRulers = new HashMap<>();
281 if (this.spec.axisMode == YAxisMode.MultiAxis) {
282 for (ItemNode item : analogItems)
283 if (item.ruler != null)
284 existingRulers.put(item.item.groupItemId, item.ruler);
287 this.spec.readFrom( newSpec );
288 this.spec.sortItems();
289 this.renderingProfile.read(this.spec.viewProfile);
292 if (titleNode != null) titleNode.setText( spec.name );
294 // Setup trend item nodes
296 for (ItemNode item : allItems) removeNode(item);
301 ItemManager itemManager = ItemManager.createUnchecked( getHistoryItems() );
302 for (int i = 0; i<spec.items.size(); i++) {
303 TrendItem item = spec.items.get(i);
307 ItemNode node = createItemNode(item, itemManager);
308 for (Bean historyItem : node.historyItems) {
310 itemIds.add( (String) historyItem.getField("id") );
311 } catch (BindingException e) {
315 if (item.renderer == Renderer.Analog) {
316 analogItems.add(node);
318 binaryItems.add(node);
323 // Setup vertical ruler nodes
324 singleAxis = spec.axisMode == YAxisMode.SingleAxis;
326 if (yaxisModeChanged || vertRulers.size() != 1 || vertRuler == null) {
327 for (VertRuler vr : vertRulers) removeNode(vr);
330 vertRuler = addNode("VertRuler", VertRuler.class);
331 vertRulers.add( vertRuler );
334 vertRuler.manualscale = true;
335 for (int i=0; i<analogItems.size(); i++) {
336 ItemNode item = analogItems.get(i);
337 item.ruler = vertRuler;
338 item.trendNode = this;
339 if (item.item.scale instanceof Scale.Manual == false) vertRuler.manualscale = false;
342 if (yaxisModeChanged) {
343 // Recreate all rulers
344 for (VertRuler vr : vertRulers) removeNode(vr);
346 for (int i=0; i<analogItems.size(); i++)
347 vertRulers.add( addNode(VertRuler.class) );
349 // Remove rulers of the items that were removed
350 // and add new rulers to have enough of them for
351 // each separate analog signal.
352 removedItemMap.forEachKey(new TObjectProcedure<String>() {
354 public boolean execute(String id) {
355 VertRuler vr = existingRulers.get(id);
358 vertRulers.remove(vr);
363 for (int i = vertRulers.size(); i < analogItems.size(); ++i) {
364 VertRuler ruler = addNode(VertRuler.class);
365 vertRulers.add(ruler);
369 for (int i = 0; i < analogItems.size(); i++) {
370 ItemNode item = analogItems.get(i);
371 VertRuler vr = vertRulers.get(i);
372 vr.setZIndex(1000 + i);
373 vr.color = item.color;
374 vr.label = item.item.label;
375 vr.manualscale = item.item.scale instanceof Scale.Manual;
377 item.trendNode = this;
380 vertRuler = vertRulers.isEmpty() ? null : vertRulers.get( vertRulerIndex <= 0 || vertRulerIndex >= vertRulers.size() ? 0 : vertRulerIndex );
384 TimeWindow tw = spec.viewProfile.timeWindow;
385 horizRuler.manualscale = tw.timeWindowLength!=null && tw.timeWindowStart!=null;
387 if (timeWindowChange) {
388 horizRuler.autoscale();
393 private ItemNode createItemNode(TrendItem item, ItemManager itemManager) {
394 ItemNode node = addNode( ItemNode.class );
395 //node.trendNode = this;
396 node.setTrendItem(item, itemManager);
397 node.color = toColor(item.customColor);
398 if (node.color == null)
399 node.color = JarisPaints.getColor( item.index );
400 node.stroke = item.customStrokeWidth != null
401 ? withStrokeWidth(item.customStrokeWidth, Plot.TREND_LINE_STROKE)
402 : Plot.TREND_LINE_STROKE;
406 private static BasicStroke withStrokeWidth(float width, BasicStroke stroke) {
407 return new BasicStroke(width,
408 stroke.getEndCap(), stroke.getLineJoin(), stroke.getMiterLimit(),
409 stroke.getDashArray(), stroke.getDashPhase());
412 private static Color toColor(float[] components) {
413 if (components == null)
415 switch (components.length) {
416 case 3: return new Color(components[0], components[1], components[2]);
417 case 4: return new Color(components[0], components[1], components[2], components[3]);
418 default: return null;
423 * Layout graphical nodes based on bounds
425 public void layout() {
426 double w = bounds.getWidth();
427 double h = bounds.getHeight();
428 if ( titleNode != null ) {
429 titleNode.setSize(w, h * 0.02);
430 titleNode.setTranslate(0, VERT_MARGIN);
434 // Plot-Ruler area width
435 double praw = w-HORIZ_MARGIN*2;
438 double ph = h-VERT_MARGIN*2-MILESTONE_HEIGHT-HORIZ_RULER_HEIGHT;
439 if ( titleNode != null ) {
443 // Analog & Binary area height
445 if (!analogItems.isEmpty()) {
446 bah = binaryItems.size() * BINARY[3];
447 aah = Math.max(0, ph-bah);
448 if (aah+bah>ph) bah = ph-aah;
455 for (VertRuler vertRuler : vertRulers) {
456 vertRuler.setHeight(aah);
459 plot.analogAreaHeight = aah;
460 plot.binaryAreaHeight = bah;
462 // Vertical ruler widths
464 for (VertRuler vertRuler : vertRulers) {
465 vrws += vertRuler.getWidth();
468 // Add room for Binary label
469 if ( !binaryItems.isEmpty() ) {
470 double maxLabelWidth = BINARY_LABEL_WIDTH;
471 for (ItemNode node : binaryItems) {
472 if (node.item != null) {
473 GlyphVector glyphVector = RULER_FONT.createGlyphVector(GridUtil.frc, node.item.label);
474 double labelWidth = glyphVector.getVisualBounds().getWidth();
475 maxLabelWidth = Math.max( maxLabelWidth, labelWidth );
478 vrws = Math.max(maxLabelWidth, vrws);
482 double pw = praw - vrws;
483 plot.setTranslate(HORIZ_MARGIN, (titleNode!=null?titleNode.th:0)+VERT_MARGIN+MILESTONE_HEIGHT);
484 plot.setSize(pw, ph);
487 horizRuler.setTranslate(HORIZ_MARGIN, plot.getY()+plot.getHeight()+3);
488 boolean l = horizRuler.setWidth(pw);
489 l |= horizRuler.setFromEnd(horizRuler.from, horizRuler.end);
490 if (l) horizRuler.layout();
492 // Move vertical rulers
493 double vrx = plot.getX() + plot.getWidth() + 3;
494 for (VertRuler vertRuler : vertRulers) {
495 vertRuler.setTranslate(vrx, plot.getY());
496 vrx += vertRuler.getWidth() + 3;
501 public void setSize(double width, double height) {
502 bounds.setFrame(0, 0, width, height);
506 public void render(Graphics2D g2d) {
508 QualityHints qh = QualityHints.getQuality(g2d);
509 g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, quality.textQuality==LineQuality.Antialias?RenderingHints.VALUE_TEXT_ANTIALIAS_ON:RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
512 Rectangle bounds = g2d.getClipBounds();
513 if (bounds.getWidth()!=this.bounds.getWidth() || bounds.getHeight()!=this.bounds.getHeight()) {
514 setSize(bounds.getWidth(), bounds.getHeight());
518 // Flush history subscriptions
524 // Render plot's value tip
525 plot.renderValueTip(g2d);
532 public Rectangle2D getBoundsInLocal() {
537 * Return true if the viewport is not moving and shows only past values
540 public boolean allPast() {
541 TimeWindow timeWindow = spec.viewProfile.timeWindow;
542 boolean fixedWindow = !horizRuler.autoscroll || (timeWindow.timeWindowStart!=null && timeWindow.timeWindowLength!=null);
544 for (ItemNode item : allItems) {
545 if (item.end <= horizRuler.end) return false;
553 public void flushHistory() {
554 if (collector == null || collector instanceof CollectorImpl == false) return;
555 CollectorImpl fh = (CollectorImpl) collector;
560 * Read values of min,max,from,end to all items.
561 * Put iMin,iMax,iFrom,iEnd to all axes.
563 public void readMinMaxFromEnd() {
565 // Read min,max,from,end from all items - Gather collective ranges
566 for (ItemNode item : allItems) {
567 item.readMinMaxFromEnd();
569 horizRuler.setKnownFromEnd();
570 for (VertRuler vertRuler : vertRulers) vertRuler.setKnownMinMax();
576 * Flushes history, resets streams,
577 * reads the latest time and value ranges from disc.
579 * Sets dirty true if there was change in axis.
581 public boolean autoscale(boolean timeAxis, boolean valueAxis) {
582 if (!timeAxis && !valueAxis) return false;
585 boolean changed = false;
589 changed |= horizRuler.autoscale();
590 if ( changed && !printing ) horizRuler.fireListener();
595 for (VertRuler vertRuler : vertRulers) changed |= vertRuler.autoscale();
601 public boolean updateValueTipTime() {
602 if (valueTipTime != null && spec.experimentIsRunning && spec.viewProfile.trackExperimentTime) {
603 double endTime = horizRuler.getItemEndTime();
604 if (!Double.isNaN(endTime)) {
605 boolean changed = Double.compare(valueTipTime, endTime) != 0;
606 valueTipTime = endTime;
613 public void zoomIn(double x, double y, double width, double height, boolean horiz, boolean vert) {
615 horizRuler.zoomIn(x, width);
619 for (VertRuler vertRuler : vertRulers) {
620 vertRuler.zoomIn(y, height);
626 public void zoomOut() {
627 horizRuler.zoomOut();
628 for (VertRuler vertRuler : vertRulers) {
634 public TrendSpec getTrendSpec() {
638 public Viewport getViewport()
640 Viewport vp = new Viewport();
642 vp.from = horizRuler.from;
643 vp.end = horizRuler.end;
644 for (VertRuler vr : vertRulers) {
645 AxisViewport avp = new AxisViewport();
648 vp.axesports.add( avp );
653 public void setViewport( Viewport vp )
655 horizRuler.from = vp.from;
656 horizRuler.end = vp.end;
658 for ( AxisViewport avp : vp.axesports ) {
659 if ( i>=vertRulers.size() ) break;
660 VertRuler vr = vertRulers.get(i++);
666 public void setQuality(TrendQualitySpec quality)
668 this.quality = quality;
671 public TrendQualitySpec getQuality()
676 public Double snapToValue(double time, double snapToleranceInTime) throws HistoryException, AccessorException
678 double from = horizRuler.from;
679 double end = horizRuler.end;
680 double pixelsPerSecond = (end-from) / plot.getWidth();
682 TreeSet<Double> values = new TreeSet<Double>(Bindings.DOUBLE);
683 for (ItemNode item : allItems) {
684 Stream s = item.openStream( pixelsPerSecond );
685 if ( s==null ) continue;
686 int pos = s.binarySearch(Bindings.DOUBLE, time);
687 ValueBand vb = new ValueBand(s.sampleBinding, s.sampleBinding.createDefaultUnchecked());
695 int count = s.count();
696 Double prevTime = null, nextTime = null;
698 if ( prev>=0 && prev<count ) {
699 s.accessor.get(prev, s.sampleBinding, vb.getSample());
700 if ( !vb.isNanSample() ) {
701 prevTime = vb.getTimeDouble();
702 if ( vb.hasEndTime() ) {
703 Double nTime = vb.getEndTimeDouble();
704 if (nTime!=null && nTime+snapToleranceInTime>time) nextTime = nTime;
709 if ( nextTime==null && next>=0 && next<count ) {
710 s.accessor.get(next, s.sampleBinding, vb.getSample());
711 if ( !vb.isNanSample() ) {
712 nextTime = vb.getTimeDouble();
716 if (prevTime==null && nextTime==null) continue;
718 if (prevTime==null) {
719 if ( nextTime - time < snapToleranceInTime )
720 values.add(nextTime);
721 } else if (nextTime==null) {
722 if ( time - prevTime < snapToleranceInTime )
723 values.add(prevTime);
725 values.add(nextTime);
726 values.add(prevTime);
729 if (values.isEmpty()) return null;
731 Double lower = values.floor( time );
732 Double higher = values.ceiling( time );
734 if ( lower == null ) return higher;
735 if ( higher == null ) return lower;
736 double result = time-lower < higher-time ? lower : higher;