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;
14 import java.awt.Color;
15 import java.util.concurrent.Executor;
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;
46 public class TrendInitializer {
48 public static CanvasContext defaultInitializeCanvas(CanvasContext cvsCtx, HistoryManager historian, Collector collector, final Datasource simulation, TrendSpec trendSpec)
50 // Create canvas context and a layer of interactors
51 IHintContext h = cvsCtx.getDefaultHintContext();
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
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() );
68 TrendNode trendNode = cvsCtx.getSceneGraph().addNode(TrendNode.class);
69 trendNode.setHistorian( historian, collector );
70 trendNode.setTrendSpec( trendSpec );
72 // Add trend participant/interactor
73 TrendParticipant tp = new TrendParticipant();
74 tp.setTrend( trendNode );
77 if (simulation != null)
78 attachStepListener(simulation, cvsCtx, tp);
80 // Debug participant(s)
81 //canvasContext.add( new PointerPainter() );
82 // canvasContext.add( new HandPainter() );
83 h.setHint(PointerPainter.KEY_PAINT_POINTER, true);
85 // Pan & Zoom & Rotate
86 //canvasContext.add( new MousePanZoomInteractor() );
87 //canvasContext.add( new MultitouchPanZoomRotateInteractor() );
88 //canvasContext.add( new OrientationRestorer() );
90 // Grid & Ruler & Background
91 //cvsCtx.add( new GridPainter() );
92 //cvsCtx.add( new RulerPainter() );
93 cvsCtx.add( new BackgroundPainter() );
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);
104 cvsCtx.add( new KeyToCommand( CommandKeyBinding.DEFAULT_BINDINGS ) );
106 ////// Diagram Participants //////
108 cvsCtx.add( new TooltipParticipant());
110 h.setHint(Hints.KEY_TOOL, Hints.POINTERTOOL);
112 //NodeUtil.printSceneGraph( cvsCtx.getSceneGraph() );
114 cvsCtx.assertParticipantDependencies();
119 public static TrendNode getTrendNode(CanvasContext ctx)
121 TrendParticipant tp = ctx.getSingleItem( TrendParticipant.class );
122 return tp.getTrend();
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() {
131 public void onDisposed(IDisposable sender) {
132 datasource.removeListener(stepListener);
140 public static class StepListener implements DatasourceListener {
141 private final TrendParticipant trendParticipant;
143 public StepListener(TrendParticipant trendParticipant) {
144 this.trendParticipant = trendParticipant;
148 public void onStep(Datasource source) {
149 TrendNode node = trendParticipant.getTrend();
150 if (!node.allPast()) node.datadirty = true;
154 public Executor getExecutor() {
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);