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.BorderLayout;
15 import java.awt.Dimension;
16 import java.awt.event.WindowAdapter;
17 import java.awt.event.WindowEvent;
20 import javax.swing.JFrame;
22 import org.simantics.g2d.canvas.impl.CanvasContext;
23 import org.simantics.g2d.chassis.AWTChassis;
24 import org.simantics.g2d.image.DefaultImages;
25 import org.simantics.history.impl.FileHistory;
26 import org.simantics.trend.configuration.Scale;
27 import org.simantics.trend.configuration.TrendItem;
28 import org.simantics.trend.configuration.TrendSpec;
29 import org.simantics.trend.configuration.YAxisMode;
30 import org.simantics.trend.impl.Milestone;
31 import org.simantics.trend.impl.MilestoneSpec;
32 import org.simantics.trend.impl.TrendNode;
33 import org.simantics.trend.impl.TrendParticipant;
34 import org.simantics.utils.FileUtils;
35 import org.simantics.utils.datastructures.hints.IHintContext;
36 import org.simantics.utils.threads.AWTThread;
37 import org.simantics.utils.threads.IThreadWorkQueue;
39 public class DemoTrend {
41 public static void main(String[] args) throws Exception {
42 System.out.println(DefaultImages.HAND);
44 // Initialize file history
45 final File workarea = FileUtils.createTmpDir();
46 final TestData data = new TestData(workarea);
47 FileHistory fh = ((FileHistory)data.historyManager);
48 fh.asyncUsage = false;
50 //final TestData data = new TestData();
52 final TrendSpec trendSpec = new TrendSpec();
54 trendSpec.viewProfile.showMilestones = true;
55 trendSpec.name = "Multiple Y-Axis DemoTrend";
56 trendSpec.axisMode = YAxisMode.MultiAxis;
58 trendSpec.items.add( new TrendItem( 1, "Sine", data.subscriptionId, "Sine", new Scale.Manual( -1, 1), TrendItem.Renderer.Analog ) );
59 trendSpec.items.add( new TrendItem( 2, "Ramp", data.subscriptionId,"Ramp", new Scale.Manual(-100,100), TrendItem.Renderer.Binary ) );
60 trendSpec.items.add( new TrendItem( 3, "Random", data.subscriptionId, "Random", new Scale.Auto(), TrendItem.Renderer.Analog ) );
61 trendSpec.viewProfile.profileName = "Profile";
62 // trendSpec.viewProfile.timeWindow.timeWindowLength = 180.0;
63 // trendSpec.viewProfile.timeWindow.timeWindowStart = 0.0;
64 trendSpec.viewProfile.timeWindow.timeWindowIncrement = 75.0;
66 // Alternative spec - Switch with Spacebarman button
67 // TrendSpec altSpec = new TrendSpec();
69 // altSpec.viewProfile.showMilestones = true;
70 // altSpec.name = "Single Axis";
71 // altSpec.axisMode = YAxisMode.SingleAxis;
72 // altSpec.items.add( new TrendItem( 1, "Sine", data.subscriptionId,"Sine", new Scale.Auto(), TrendItem.Renderer.Analog, 0, 1 ) );
73 // altSpec.items.add( new TrendItem( 2, "Ramp", data.subscriptionId,"Ramp", new Scale.Auto(), TrendItem.Renderer.Binary ) );
74 // //altSpec.items.add( new TrendItem( "Random", data.subscriptionId,"Random", new Scale.FitAll(), TrendItem.Renderer.Analog, 0, 1, DrawMode.Deviation ) );
75 // altSpec.viewProfile.profileName = "Profile";
76 // //altSpec.viewProfile.timeWindow.timeWindowLength = 30.0;
77 // //altSpec.viewProfile.timeWindow.timeWindowStart = 0.0;
78 // altSpec.viewProfile.timeWindow.timeWindowIncrement = 25.0;
81 // JFrame has double buffering enabled by default
82 JFrame frame = new JFrame("Demo Trend");
83 // Add a window listener for close button
84 frame.addWindowListener(new WindowAdapter() {
86 public void windowClosing(WindowEvent e) {
96 // This is an empty content area in the frame
97 final AWTChassis chassis = new AWTChassis();
99 chassis.setPreferredSize(new Dimension(480, 320));
101 frame.getContentPane().add(chassis, BorderLayout.CENTER);
104 frame.setVisible(true);
105 chassis.requestFocus();
107 IThreadWorkQueue thread = AWTThread.getThreadAccess();
109 final CanvasContext ctx = TrendInitializer.createDefaultCanvas(thread, data.historyManager, data.collector, data.solver, trendSpec);
110 TrendNode node = TrendInitializer.getTrendNode( ctx );
113 ctx.getAtMostOneItemOfClass(TrendParticipant.class).setHintAsync(TrendParticipant.KEY_TREND_DRAW_INTERVAL, 1000L);
115 MilestoneSpec milestones = new MilestoneSpec();
117 Milestone m1 = new Milestone("1", "1", "Event 1", 50);
118 Milestone m2 = new Milestone("2", "2", "Event 2", 60);
119 Milestone m3 = new Milestone("3", "3", "Event 3", 80);
120 Milestone m4 = new Milestone("4", "4", "Event 4", 90);
121 milestones.milestones.add( m1 );
122 milestones.milestones.add( m2 );
123 milestones.milestones.add( m3 );
124 milestones.milestones.add( m4 );
125 milestones.baseline = 2;
126 node.setMilestones( milestones );
128 @SuppressWarnings("unused")
129 IHintContext hintCtx = ctx.getDefaultHintContext();
131 thread.asyncExec(new Runnable() {
134 chassis.setCanvasContext(ctx);