]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.trend/src/org/simantics/trend/TrendInitializer.java
Render last known value in time series chart hairline value tip
[simantics/platform.git] / bundles / org.simantics.trend / src / org / simantics / trend / TrendInitializer.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2011 Association for Decentralized Information Management in
3  * 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.trend;
13
14 import java.awt.Color;
15 import java.util.concurrent.Executor;
16
17 import org.simantics.g2d.canvas.Hints;
18 import org.simantics.g2d.canvas.ICanvasContext;
19 import org.simantics.g2d.canvas.impl.CanvasContext;
20 import org.simantics.g2d.participant.BackgroundPainter;
21 import org.simantics.g2d.participant.CanvasBoundsParticipant;
22 import org.simantics.g2d.participant.CanvasGrab;
23 import org.simantics.g2d.participant.KeyToCommand;
24 import org.simantics.g2d.participant.KeyUtil;
25 import org.simantics.g2d.participant.MouseUtil;
26 import org.simantics.g2d.participant.PanZoomRotateHandler;
27 import org.simantics.g2d.participant.PointerPainter;
28 import org.simantics.g2d.participant.RulerPainter;
29 import org.simantics.g2d.participant.SymbolUtil;
30 import org.simantics.g2d.participant.TimeParticipant;
31 import org.simantics.g2d.participant.TransformUtil;
32 import org.simantics.g2d.tooltip.TooltipParticipant;
33 import org.simantics.history.Collector;
34 import org.simantics.history.HistoryManager;
35 import org.simantics.scenegraph.g2d.events.command.CommandKeyBinding;
36 import org.simantics.simulation.data.Datasource;
37 import org.simantics.simulation.data.Datasource.DatasourceListener;
38 import org.simantics.trend.configuration.TrendSpec;
39 import org.simantics.trend.impl.TrendNode;
40 import org.simantics.trend.impl.TrendParticipant;
41 import org.simantics.utils.datastructures.disposable.IDisposable;
42 import org.simantics.utils.datastructures.disposable.IDisposeListener;
43 import org.simantics.utils.datastructures.hints.IHintContext;
44 import org.simantics.utils.threads.IThreadWorkQueue;
45
46 public class TrendInitializer {
47
48     public static CanvasContext defaultInitializeCanvas(CanvasContext cvsCtx, HistoryManager historian, Collector collector, final Datasource simulation, TrendSpec trendSpec)
49     {
50         // Create canvas context and a layer of interactors
51         IHintContext h = cvsCtx.getDefaultHintContext();
52
53         h.setHint(PanZoomRotateHandler.KEY_ADAPT_VIEWPORT_TO_RESIZED_CONTROL, false);
54         h.setHint(PanZoomRotateHandler.KEY_DISABLE_PAN, true);
55         h.setHint(PanZoomRotateHandler.KEY_DISABLE_ZOOM, true);
56         cvsCtx.add( new PanZoomRotateHandler( false ) ); // Must be before TransformUtil
57
58         // Support & Util Participants
59         cvsCtx.add( new TransformUtil() );
60         cvsCtx.add( new MouseUtil() );
61         cvsCtx.add( new KeyUtil() );
62         cvsCtx.add( new CanvasGrab() );
63         cvsCtx.add( new SymbolUtil() );
64         cvsCtx.add( new TimeParticipant() );
65         cvsCtx.add( new CanvasBoundsParticipant() );
66
67         // Add trend node
68         TrendNode trendNode = cvsCtx.getSceneGraph().addNode(TrendNode.class);
69         trendNode.setHistorian( historian, collector );
70         trendNode.setTrendSpec( trendSpec );
71
72         // Add trend participant/interactor
73         TrendParticipant tp = new TrendParticipant();
74         tp.setTrend( trendNode );
75         cvsCtx.add( tp );
76
77         if (simulation != null)
78                 attachStepListener(simulation, cvsCtx, tp);
79
80         // Debug participant(s)
81         //canvasContext.add( new PointerPainter() );
82 //        canvasContext.add( new HandPainter() );
83         h.setHint(PointerPainter.KEY_PAINT_POINTER, true);
84
85         // Pan & Zoom & Rotate
86         //canvasContext.add( new MousePanZoomInteractor() );
87         //canvasContext.add( new MultitouchPanZoomRotateInteractor() );
88         //canvasContext.add( new OrientationRestorer() );
89
90         // Grid & Ruler & Background
91         //cvsCtx.add( new GridPainter() );
92         //cvsCtx.add( new RulerPainter() );
93         cvsCtx.add( new BackgroundPainter() );
94
95         h.setHint(Hints.KEY_GRID_COLOR, new Color(0.95f, 0.95f, 0.95f));
96         h.setHint(Hints.KEY_BACKGROUND_COLOR, Color.WHITE);
97         h.setHint(RulerPainter.KEY_RULER_BACKGROUND_COLOR, new Color(0.9f, 0.9f, 0.9f, 0.75f));
98         h.setHint(RulerPainter.KEY_RULER_TEXT_COLOR, Color.BLACK);
99         h.setHint(TimeParticipant.KEY_TIME_PULSE_INTERVAL, 50L);
100         h.setHint(TimeParticipant.KEY_TIMER_ENABLED, true);
101         h.setHint(TrendParticipant.KEY_TREND_DRAW_INTERVAL, 100L);
102
103         // Key bindings
104         cvsCtx.add( new KeyToCommand( CommandKeyBinding.DEFAULT_BINDINGS ) );
105
106         ////// Diagram Participants //////
107
108         cvsCtx.add( new TooltipParticipant());
109
110         h.setHint(Hints.KEY_TOOL, Hints.POINTERTOOL);
111
112         //NodeUtil.printSceneGraph( cvsCtx.getSceneGraph() );
113
114         cvsCtx.assertParticipantDependencies();
115
116         return cvsCtx;
117     }
118     
119     public static TrendNode getTrendNode(CanvasContext ctx)
120     {
121         TrendParticipant tp = ctx.getSingleItem( TrendParticipant.class );
122         return tp.getTrend();
123     }
124
125     public static void attachStepListener(final Datasource datasource, ICanvasContext context, TrendParticipant tp) {
126         // Add simulation listener - Note listener must be removed when trend is closed
127         final DatasourceListener stepListener = new StepListener( tp );
128         datasource.addListener( stepListener );
129         context.addDisposeListener(new IDisposeListener() {
130             @Override
131             public void onDisposed(IDisposable sender) {
132                 datasource.removeListener(stepListener);
133             }
134         });
135     }
136
137     /**
138      *
139      */
140     public static class StepListener implements DatasourceListener {
141         private final TrendParticipant trendParticipant;
142
143         public StepListener(TrendParticipant trendParticipant) {
144             this.trendParticipant = trendParticipant;
145         }
146
147         @Override
148         public void onStep(Datasource source) {
149             TrendNode node = trendParticipant.getTrend();
150             if (!node.allPast()) node.datadirty = true;
151         }
152
153         @Override
154         public Executor getExecutor() {
155             return null;
156         }
157     }
158
159         public static CanvasContext createDefaultCanvas(IThreadWorkQueue threadAccess, HistoryManager historian, Collector collector, Datasource simulation, TrendSpec trendSpec) {
160                 return defaultInitializeCanvas(new CanvasContext(threadAccess), historian, collector, simulation, trendSpec);
161         }
162
163 }