]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.trend/example/org/simantics/trend/DemoTrend.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.trend / example / org / simantics / trend / DemoTrend.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.BorderLayout;
15 import java.awt.Dimension;
16 import java.awt.event.WindowAdapter;
17 import java.awt.event.WindowEvent;
18 import java.io.File;
19
20 import javax.swing.JFrame;
21
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;
38
39 public class DemoTrend {
40
41     public static void main(String[] args) throws Exception {
42         System.out.println(DefaultImages.HAND);
43
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;
49         // Memory history
50         //final TestData data = new TestData();
51
52         final TrendSpec trendSpec = new TrendSpec();
53         trendSpec.init();
54         trendSpec.viewProfile.showMilestones = true;
55         trendSpec.name = "Multiple Y-Axis DemoTrend";
56         trendSpec.axisMode = YAxisMode.MultiAxis;        
57
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;
65
66         // Alternative spec - Switch with Spacebarman button
67 //        TrendSpec altSpec = new TrendSpec();
68 //        altSpec.init();
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;
79
80
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() {
85             @Override
86             public void windowClosing(WindowEvent e) {
87                 data.dispose();
88                 System.exit(0);
89             }
90         });
91
92         data.solver.start();
93         
94         
95
96         // This is an empty content area in the frame
97         final AWTChassis chassis = new AWTChassis();
98
99         chassis.setPreferredSize(new Dimension(480, 320));
100         
101         frame.getContentPane().add(chassis, BorderLayout.CENTER);
102         frame.pack();
103
104         frame.setVisible(true);
105         chassis.requestFocus();
106
107         IThreadWorkQueue thread  = AWTThread.getThreadAccess();
108
109         final CanvasContext    ctx     = TrendInitializer.createDefaultCanvas(thread, data.historyManager, data.collector, data.solver, trendSpec);
110         TrendNode                node    = TrendInitializer.getTrendNode( ctx );
111         
112         
113         ctx.getAtMostOneItemOfClass(TrendParticipant.class).setHintAsync(TrendParticipant.KEY_TREND_DRAW_INTERVAL, 1000L);
114         
115         MilestoneSpec milestones = new MilestoneSpec();
116         milestones.init();
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 );
127         
128         @SuppressWarnings("unused")
129         IHintContext     hintCtx = ctx.getDefaultHintContext();
130
131         thread.asyncExec(new Runnable() {
132             @Override
133             public void run() {
134                 chassis.setCanvasContext(ctx);
135             }
136         });
137     }
138
139 }