]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.trend/example/org/simantics/trend/DemoTrendSingleAxisShowLegends.java
New trend axis mode SingleAxisShowLegends
[simantics/platform.git] / bundles / org.simantics.trend / example / org / simantics / trend / DemoTrendSingleAxisShowLegends.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 DemoTrendSingleAxisShowLegends {
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 = "SingleAxisShowLegends Y-Axis DemoTrend";
56                 trendSpec.axisMode = YAxisMode.SingleAxisShowLegends;
57                 trendSpec.singleAxisShowLegendsMaxLegends = 10;
58                 
59                 trendSpec.items.add(new TrendItem(1, "Sine", data.subscriptionId, "Sine", new Scale.Manual(-100, 100),
60                                 TrendItem.Renderer.Analog));
61                 trendSpec.items.add(new TrendItem(2, "Random", data.subscriptionId, "Random", new Scale.Auto(), TrendItem.Renderer.Analog));
62                 
63                 trendSpec.viewProfile.profileName = "Profile";
64 //        trendSpec.viewProfile.timeWindow.timeWindowLength = 180.0;
65 //        trendSpec.viewProfile.timeWindow.timeWindowStart = 0.0;
66                 trendSpec.viewProfile.timeWindow.timeWindowIncrement = 75.0;
67
68                 // Alternative spec - Switch with Spacebarman button
69 //        TrendSpec altSpec = new TrendSpec();
70 //        altSpec.init();
71 //        altSpec.viewProfile.showMilestones = true;
72 //        altSpec.name = "Single Axis";
73 //        altSpec.axisMode = YAxisMode.SingleAxis;
74 //        altSpec.items.add( new TrendItem( 1, "Sine", data.subscriptionId,"Sine", new Scale.Auto(), TrendItem.Renderer.Analog, 0, 1 ) );
75 //        altSpec.items.add( new TrendItem( 2, "Ramp", data.subscriptionId,"Ramp", new Scale.Auto(), TrendItem.Renderer.Binary ) );
76 //        //altSpec.items.add( new TrendItem( "Random", data.subscriptionId,"Random", new Scale.FitAll(), TrendItem.Renderer.Analog, 0, 1, DrawMode.Deviation ) );              
77 //        altSpec.viewProfile.profileName = "Profile";
78 //        //altSpec.viewProfile.timeWindow.timeWindowLength = 30.0;
79 //        //altSpec.viewProfile.timeWindow.timeWindowStart = 0.0;
80 //        altSpec.viewProfile.timeWindow.timeWindowIncrement = 25.0;
81
82                 // JFrame has double buffering enabled by default
83                 JFrame frame = new JFrame("Demo Trend");
84                 // Add a window listener for close button
85                 frame.addWindowListener(new WindowAdapter() {
86                         @Override
87                         public void windowClosing(WindowEvent e) {
88                                 data.dispose();
89                                 System.exit(0);
90                         }
91                 });
92
93                 data.solver.start();
94
95                 // This is an empty content area in the frame
96                 final AWTChassis chassis = new AWTChassis();
97
98                 chassis.setPreferredSize(new Dimension(480, 320));
99
100                 frame.getContentPane().add(chassis, BorderLayout.CENTER);
101                 frame.pack();
102
103                 frame.setVisible(true);
104                 chassis.requestFocus();
105
106                 IThreadWorkQueue thread = AWTThread.getThreadAccess();
107
108                 final CanvasContext ctx = TrendInitializer.createDefaultCanvas(thread, data.historyManager, data.collector,
109                                 data.solver, trendSpec);
110                 TrendNode node = TrendInitializer.getTrendNode(ctx);
111
112                 ctx.getAtMostOneItemOfClass(TrendParticipant.class).setHintAsync(TrendParticipant.KEY_TREND_DRAW_INTERVAL,
113                                 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 }