1 | /* |
---|
2 | * $Id: JDesktopDemo.java,v 1.3 2002/12/13 21:35:48 dwd Exp $ |
---|
3 | * |
---|
4 | * This software is provided by NOAA for full, free and open release. It is |
---|
5 | * understood by the recipient/user that NOAA assumes no liability for any |
---|
6 | * errors contained in the code. Although this software is released without |
---|
7 | * conditions or restrictions in its use, it is expected that appropriate |
---|
8 | * credit be given to its author and to the National Oceanic and Atmospheric |
---|
9 | * Administration should the software be included by the recipient as an |
---|
10 | * element in other product development. |
---|
11 | */ |
---|
12 | package gov.noaa.pmel.sgt.demo; |
---|
13 | |
---|
14 | import java.awt.*; |
---|
15 | import java.awt.event.*; |
---|
16 | import java.applet.*; |
---|
17 | import javax.swing.*; |
---|
18 | import java.beans.PropertyChangeEvent; |
---|
19 | import java.beans.PropertyChangeListener; |
---|
20 | |
---|
21 | import gov.noaa.pmel.sgt.*; |
---|
22 | import gov.noaa.pmel.util.SoTPoint; |
---|
23 | import gov.noaa.pmel.util.SoTRange; |
---|
24 | import gov.noaa.pmel.util.Point2D; |
---|
25 | import gov.noaa.pmel.util.Dimension2D; |
---|
26 | |
---|
27 | /** |
---|
28 | * Example demonstrating the use of <code>PropertyChangeEvents</code> |
---|
29 | * in the datamodel. <code>JDesktopDemo</code> constructs the plot |
---|
30 | * from basic <code>sgt</code> objects. |
---|
31 | * |
---|
32 | * @author Donald Denbo |
---|
33 | * @version $Revision: 1.3 $, $Date: 2002/12/13 21:35:48 $ |
---|
34 | * @since 2.1 |
---|
35 | */ |
---|
36 | |
---|
37 | public class JDesktopDemo extends JApplet implements PropertyChangeListener { |
---|
38 | PseudoRealTimeData rtData_; |
---|
39 | JPane pane_; |
---|
40 | Layer layer_; |
---|
41 | TimeAxis xbot_; |
---|
42 | PlainAxis yleft_; |
---|
43 | LinearTransform xt_, yt_; |
---|
44 | boolean isStandalone = false; |
---|
45 | BorderLayout borderLayout1 = new BorderLayout(); |
---|
46 | JPanel buttonPanel = new JPanel(); |
---|
47 | JButton startButton = new JButton(); |
---|
48 | JButton stopButton = new JButton(); |
---|
49 | JButton resetButton = new JButton(); |
---|
50 | |
---|
51 | /**Construct the applet*/ |
---|
52 | public JDesktopDemo() { |
---|
53 | } |
---|
54 | /**Initialize the applet*/ |
---|
55 | public void init() { |
---|
56 | /* |
---|
57 | * Create the data source |
---|
58 | */ |
---|
59 | rtData_ = new PseudoRealTimeData("rtDataSource", "Sea Level"); |
---|
60 | try { |
---|
61 | jbInit(); |
---|
62 | } |
---|
63 | catch(Exception e) { |
---|
64 | e.printStackTrace(); |
---|
65 | } |
---|
66 | /* |
---|
67 | * add listener for data source. JDesktopDemo is listening |
---|
68 | * for rangeModified events |
---|
69 | */ |
---|
70 | rtData_.addPropertyChangeListener(this); |
---|
71 | } |
---|
72 | /**Component initialization*/ |
---|
73 | private void jbInit() throws Exception { |
---|
74 | this.setSize(new Dimension(800, 440)); |
---|
75 | this.getContentPane().setLayout(borderLayout1); |
---|
76 | startButton.setText("start"); |
---|
77 | startButton.addActionListener(new JDesktopDemo_startButton_actionAdapter(this)); |
---|
78 | stopButton.setText("stop"); |
---|
79 | stopButton.addActionListener(new JDesktopDemo_stopButton_actionAdapter(this)); |
---|
80 | resetButton.setText("reset"); |
---|
81 | resetButton.addActionListener(new JDesktopDemo_resetButton_actionAdapter(this)); |
---|
82 | buttonPanel.setBorder(BorderFactory.createEtchedBorder()); |
---|
83 | this.getContentPane().add(buttonPanel, BorderLayout.SOUTH); |
---|
84 | buttonPanel.add(startButton, null); |
---|
85 | buttonPanel.add(stopButton, null); |
---|
86 | buttonPanel.add(resetButton, null); |
---|
87 | // |
---|
88 | // construct JPane |
---|
89 | // |
---|
90 | pane_ = new JPane("Real Time Data Demo", new Dimension(800, 400)); |
---|
91 | pane_.setBatch(true); |
---|
92 | pane_.setLayout(new StackedLayout()); |
---|
93 | pane_.setBackground(Color.white); |
---|
94 | /* |
---|
95 | * xsize, ysize are the width and height in physical units |
---|
96 | * of the Layer graphics region. |
---|
97 | * |
---|
98 | * xstart, xend are the start and end points for the X axis |
---|
99 | * ystart, yend are the start and end points for the Y axis |
---|
100 | */ |
---|
101 | double xsize = 6.0; |
---|
102 | double xstart = 0.6; |
---|
103 | double xend = 5.5; |
---|
104 | double ysize = 3.0; |
---|
105 | double ystart = 0.6; |
---|
106 | double yend = 2.75; |
---|
107 | /* |
---|
108 | * Create the layer and add it to the Pane. |
---|
109 | */ |
---|
110 | CartesianGraph graph; |
---|
111 | /* |
---|
112 | * Get x and y ranges from data source. |
---|
113 | */ |
---|
114 | SoTRange.GeoDate xrange = (SoTRange.GeoDate)rtData_.getXRange(); |
---|
115 | SoTRange.Double yrange = (SoTRange.Double)rtData_.getYRange(); |
---|
116 | |
---|
117 | xt_ = new LinearTransform(xstart, xend, xrange.start, xrange.end); |
---|
118 | yt_ = new LinearTransform(ystart, yend, yrange.start, yrange.end); |
---|
119 | |
---|
120 | layer_ = new Layer("Layer 1", new Dimension2D(xsize, ysize)); |
---|
121 | pane_.add(layer_); |
---|
122 | |
---|
123 | SGLabel title = new SGLabel("title", |
---|
124 | "Real Time Demo", |
---|
125 | new Point2D.Double((xstart+xend)/2.0, |
---|
126 | ysize-0.05)); |
---|
127 | title.setAlign(SGLabel.TOP, SGLabel.CENTER); |
---|
128 | title.setFont(new Font("Serif", Font.PLAIN, 14)); |
---|
129 | title.setHeightP(0.25); |
---|
130 | title.setColor(Color.blue.darker()); |
---|
131 | layer_.addChild(title); |
---|
132 | /* |
---|
133 | * Create a CartesianGraph and set transforms. |
---|
134 | */ |
---|
135 | graph = new CartesianGraph("Time Graph"); |
---|
136 | layer_.setGraph(graph); |
---|
137 | graph.setXTransform(xt_); |
---|
138 | graph.setYTransform(yt_); |
---|
139 | /* |
---|
140 | * Create the bottom axis, set its range in user units |
---|
141 | * and its origin. Add the axis to the graph. |
---|
142 | */ |
---|
143 | SoTPoint origin = new SoTPoint(xrange.start, yrange.start); |
---|
144 | xbot_ = new TimeAxis("Botton Axis", TimeAxis.AUTO); |
---|
145 | xbot_.setRangeU(xrange); |
---|
146 | xbot_.setLocationU(origin); |
---|
147 | Font xbfont = new Font("Helvetica", Font.PLAIN, 14); |
---|
148 | xbot_.setLabelFont(xbfont); |
---|
149 | graph.addXAxis(xbot_); |
---|
150 | /* |
---|
151 | * Create the left axis, set its range in user units |
---|
152 | * and its origin. Add the axis to the graph. |
---|
153 | */ |
---|
154 | String yLabel = "Latitude"; |
---|
155 | |
---|
156 | yleft_ = new PlainAxis("Left Axis"); |
---|
157 | yleft_.setRangeU(yrange); |
---|
158 | yleft_.setLocationU(origin); |
---|
159 | yleft_.setLabelFont(xbfont); |
---|
160 | SGLabel ytitle = new SGLabel("yaxis title", yLabel, |
---|
161 | new Point2D.Double(0.0, 0.0)); |
---|
162 | Font ytfont = new Font("Helvetica", Font.PLAIN, 14); |
---|
163 | ytitle.setFont(ytfont); |
---|
164 | ytitle.setHeightP(0.2); |
---|
165 | yleft_.setTitle(ytitle); |
---|
166 | graph.addYAxis(yleft_); |
---|
167 | |
---|
168 | LineAttribute attr = new LineAttribute(); |
---|
169 | graph.setData(rtData_, attr); |
---|
170 | |
---|
171 | this.getContentPane().add(pane_, BorderLayout.CENTER); |
---|
172 | if(!isStandalone) pane_.setBatch(false); |
---|
173 | } |
---|
174 | /**Start the applet*/ |
---|
175 | public void start() { |
---|
176 | } |
---|
177 | /**Stop the applet*/ |
---|
178 | public void stop() { |
---|
179 | rtData_.stopData(); |
---|
180 | } |
---|
181 | /**Destroy the applet*/ |
---|
182 | public void destroy() { |
---|
183 | rtData_.stopData(); |
---|
184 | } |
---|
185 | /**Get Applet information*/ |
---|
186 | public String getAppletInfo() { |
---|
187 | return "Applet Information"; |
---|
188 | } |
---|
189 | /**Main method*/ |
---|
190 | public static void main(String[] args) { |
---|
191 | JFrame desk = new JFrame("Desktop Demo"); |
---|
192 | //EXIT_ON_CLOSE == 3 |
---|
193 | desk.setDefaultCloseOperation(3); |
---|
194 | desk.getContentPane().setLayout(new BorderLayout()); |
---|
195 | desk.setSize(850, 500); |
---|
196 | Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); |
---|
197 | desk.setLocation((d.width - desk.getSize().width) / 2, |
---|
198 | (d.height - desk.getSize().height) / 2); |
---|
199 | JDesktopPane desktop = new JDesktopPane(); |
---|
200 | desk.getContentPane().add(desktop, BorderLayout.CENTER); |
---|
201 | JDesktopDemo applet = new JDesktopDemo(); |
---|
202 | applet.isStandalone = true; |
---|
203 | JInternalFrame frame = new JInternalFrame("Real Time Data Demo", |
---|
204 | false, false, false, true); |
---|
205 | frame.getContentPane().add(applet, BorderLayout.CENTER); |
---|
206 | applet.init(); |
---|
207 | applet.start(); |
---|
208 | frame.setSize(800,440); |
---|
209 | desktop.add(frame); |
---|
210 | applet.pane_.setBatch(false); |
---|
211 | desk.setVisible(true); |
---|
212 | frame.setVisible(true); |
---|
213 | } |
---|
214 | |
---|
215 | //static initializer for setting look & feel |
---|
216 | static { |
---|
217 | try { |
---|
218 | //UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); |
---|
219 | //UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName()); |
---|
220 | } |
---|
221 | catch(Exception e) { |
---|
222 | } |
---|
223 | } |
---|
224 | |
---|
225 | void startButton_actionPerformed(ActionEvent e) { |
---|
226 | rtData_.startData(); |
---|
227 | } |
---|
228 | |
---|
229 | void stopButton_actionPerformed(ActionEvent e) { |
---|
230 | rtData_.stopData(); |
---|
231 | } |
---|
232 | |
---|
233 | void resetButton_actionPerformed(ActionEvent e) { |
---|
234 | rtData_.stopData(); |
---|
235 | rtData_.resetData(); |
---|
236 | resetRange(); |
---|
237 | } |
---|
238 | private void resetRange() { |
---|
239 | /* |
---|
240 | * A change in the range has occured. Get new range |
---|
241 | * and set transforms, axes, and origin appropriately. |
---|
242 | */ |
---|
243 | pane_.setBatch(true); |
---|
244 | SoTRange.GeoDate xrange = (SoTRange.GeoDate)rtData_.getXRange(); |
---|
245 | SoTRange.Double yrange = (SoTRange.Double)rtData_.getYRange(); |
---|
246 | SoTPoint origin = new SoTPoint(xrange.start, yrange.start); |
---|
247 | xt_.setRangeU(xrange); |
---|
248 | yt_.setRangeU(yrange); |
---|
249 | xbot_.setRangeU(xrange); |
---|
250 | xbot_.setLocationU(origin); |
---|
251 | yleft_.setRangeU(yrange); |
---|
252 | yleft_.setLocationU(origin); |
---|
253 | pane_.setBatch(false); |
---|
254 | } |
---|
255 | public void propertyChange(PropertyChangeEvent evt) { |
---|
256 | /** |
---|
257 | * dataModified property is handled by CartesianGraph |
---|
258 | * only need to look for rangeModified here to make sure |
---|
259 | * range is properly updated |
---|
260 | */ |
---|
261 | if("rangeModified".equals(evt.getPropertyName())) { |
---|
262 | resetRange(); |
---|
263 | } |
---|
264 | } |
---|
265 | } |
---|
266 | /* |
---|
267 | * wrappers for button events created by JBuilder |
---|
268 | */ |
---|
269 | class JDesktopDemo_startButton_actionAdapter implements ActionListener { |
---|
270 | JDesktopDemo adaptee; |
---|
271 | |
---|
272 | JDesktopDemo_startButton_actionAdapter(JDesktopDemo adaptee) { |
---|
273 | this.adaptee = adaptee; |
---|
274 | } |
---|
275 | public void actionPerformed(ActionEvent e) { |
---|
276 | adaptee.startButton_actionPerformed(e); |
---|
277 | } |
---|
278 | } |
---|
279 | |
---|
280 | class JDesktopDemo_stopButton_actionAdapter implements ActionListener { |
---|
281 | JDesktopDemo adaptee; |
---|
282 | |
---|
283 | JDesktopDemo_stopButton_actionAdapter(JDesktopDemo adaptee) { |
---|
284 | this.adaptee = adaptee; |
---|
285 | } |
---|
286 | public void actionPerformed(ActionEvent e) { |
---|
287 | adaptee.stopButton_actionPerformed(e); |
---|
288 | } |
---|
289 | } |
---|
290 | |
---|
291 | class JDesktopDemo_resetButton_actionAdapter implements ActionListener { |
---|
292 | JDesktopDemo adaptee; |
---|
293 | |
---|
294 | JDesktopDemo_resetButton_actionAdapter(JDesktopDemo adaptee) { |
---|
295 | this.adaptee = adaptee; |
---|
296 | } |
---|
297 | public void actionPerformed(ActionEvent e) { |
---|
298 | adaptee.resetButton_actionPerformed(e); |
---|
299 | } |
---|
300 | } |
---|