source: ether_statistics/service/implementation/gov/noaa/pmel/sgt/demo/JTimeSeriesDemo.java @ 569

Last change on this file since 569 was 569, checked in by vmipsl, 12 years ago

Nouveau projet

File size: 6.1 KB
Line 
1/*
2 * $Id: JTimeSeriesDemo.java,v 1.8 2003/08/22 23:02:38 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
13package gov.noaa.pmel.sgt.demo;
14
15import gov.noaa.pmel.sgt.swing.JPlotLayout;
16import gov.noaa.pmel.sgt.swing.JClassTree;
17import gov.noaa.pmel.sgt.swing.prop.LineAttributeDialog;
18import gov.noaa.pmel.sgt.JPane;
19import gov.noaa.pmel.sgt.LineCartesianRenderer;
20import gov.noaa.pmel.sgt.LineAttribute;
21
22import gov.noaa.pmel.util.GeoDate;
23import gov.noaa.pmel.util.TimeRange;
24import gov.noaa.pmel.util.IllegalTimeValue;
25
26import gov.noaa.pmel.sgt.dm.SGTData;
27import gov.noaa.pmel.sgt.dm.SGTLine;
28
29import java.awt.*;
30import javax.swing.*;
31import java.awt.event.MouseAdapter;
32import java.awt.event.MouseEvent;
33/**
34 * Example demonstrating how to use <code>JPlotLayout</code>
35 * to create a time series plot.
36 *
37 * @author Donald Denbo
38 * @version $Revision: 1.8 $, $Date: 2003/08/22 23:02:38 $
39 * @since 2.0
40 */
41
42public class JTimeSeriesDemo extends JApplet {
43  JButton tree_;
44  JButton space_ = null;
45  JPane pane_;
46  MyMouse myMouse_;
47  LineAttributeDialog lad_;
48
49  public void init() {
50    /*
51     * init is used when run as an JApplet
52     */
53    getContentPane().setLayout(new BorderLayout(0,0));
54    setSize(600,500);
55    pane_ = makeGraph();
56    pane_.setBatch(true);
57    JPanel button = makeButtonPanel(false);
58    getContentPane().add(pane_, BorderLayout.CENTER);
59    getContentPane().add(button, "South");
60    pane_.setBatch(false);
61  }
62
63  JPanel makeButtonPanel(boolean mark) {
64    JPanel button = new JPanel();
65    button.setLayout(new FlowLayout());
66    tree_ = new JButton("Tree View");
67    MyAction myAction = new MyAction();
68    tree_.addActionListener(myAction);
69    button.add(tree_);
70    /*
71     * optionally include "mark" button
72     */
73    if(mark) {
74      space_ = new JButton("Add Mark");
75      space_.addActionListener(myAction);
76      button.add(space_);
77    }
78    return button;
79  }
80
81  public static void main(String[] args) {
82    /*
83     * main() is used when run as an application
84     */
85    JTimeSeriesDemo tsd = new JTimeSeriesDemo();
86    /*
87     * Create a JFrame to run JTimeSeriesDemo in.
88     */
89    JFrame frame = new JFrame("Time Series Demo");
90    JPanel button = tsd.makeButtonPanel(true);
91    frame.getContentPane().setLayout(new BorderLayout());
92    frame.addWindowListener(new java.awt.event.WindowAdapter() {
93        public void windowClosing(java.awt.event.WindowEvent event) {
94          JFrame fr = (JFrame)event.getSource();
95          fr.setVisible(false);
96          fr.dispose();
97          System.exit(0);
98        }
99      });
100    tsd.pane_ = tsd.makeGraph();
101    tsd.pane_.setBatch(true);
102    frame.setSize(600, 500);
103    frame.getContentPane().add(tsd.pane_, BorderLayout.CENTER);
104    frame.getContentPane().add(button, BorderLayout.SOUTH);
105    frame.setVisible(true);
106    tsd.pane_.setBatch(false);
107  }
108
109  JPlotLayout makeGraph() {
110    /*
111     * This example uses a pre-created "Layout" for time
112     * series to simplify the construction of a plot. The
113     * LineTimeSeriesLayout can plot multiple lines, with
114     * a legend and provides zooming and line hi-lighting
115     * capabilities.
116     */
117    SGTData newData;
118    TestData td;
119    JPlotLayout ltsl;
120    /*
121     * Create a test time series with random data.
122     */
123    GeoDate start = new GeoDate();
124    GeoDate stop = new GeoDate();
125    try {
126      start = new GeoDate("1968-11-01", "yyyy-MM-dd");
127      stop  = new GeoDate("2001-02-20", "yyyy-MM-dd");
128    } catch (IllegalTimeValue e) {}
129    TimeRange tr = new TimeRange(start, stop);
130    td = new TestData(TestData.TIME_SERIES, tr, 10.0f,
131                      TestData.RANDOM, 1.2f, 0.5f, 30.0f);
132    newData = td.getSGTData();
133    System.out.println("series length = " + ((SGTLine)newData).getYArray().length);
134    /*
135     * Create the layout without a Logo image and with the
136     * LineKey on the main Pane object.
137     */
138    ltsl = new JPlotLayout(newData, "Time Series Demo", null, false);
139    /*
140     * Add the time series to the layout and give a label for
141     * the legend.
142     */
143    ltsl.addData(newData, "Random Data");
144    /*
145     * Change the layout's three title lines and place the Pane
146     * on the Applet.
147     */
148    ltsl.setTitles("Time Series Demo",
149                   "using JPlotLayout",
150                   "");
151
152    myMouse_ = new MyMouse();
153    ltsl.addMouseListener(myMouse_);
154
155    return ltsl;
156  }
157
158  void tree_actionPerformed(java.awt.event.ActionEvent e) {
159    /*
160     * Create JClassTree showing object tree.
161     */
162    JClassTree ct = new JClassTree();
163    ct.setModal(false);
164    ct.setJPane(pane_);
165    ct.show();
166  }
167
168  class MyAction implements java.awt.event.ActionListener {
169    public void actionPerformed(java.awt.event.ActionEvent event) {
170      Object obj = event.getSource();
171      if(obj == space_) {
172        System.out.println("  <<Mark>>");
173      }
174      if(obj == tree_)
175        tree_actionPerformed(event);
176    }
177  }
178
179  class MyMouse extends MouseAdapter {
180    /*
181     * process mouse events.
182     */
183    public void mouseReleased(MouseEvent event) {
184      Object object = event.getSource();
185      if(object == pane_)
186        maybeShowLineAttributeDialog(event);
187    }
188
189    void maybeShowLineAttributeDialog(MouseEvent e) {
190      if(e.isPopupTrigger() || e.getClickCount() == 2) {
191        Object obj = pane_.getObjectAt(e.getX(), e.getY());
192        pane_.setSelectedObject(obj);
193        if(obj instanceof LineCartesianRenderer) {
194          LineAttribute attr = ((LineCartesianRenderer)obj).getLineAttribute();
195          if(lad_ == null) {
196            lad_ = new LineAttributeDialog();
197          }
198          lad_.setLineAttribute(attr);
199          if(!lad_.isShowing())
200            lad_.setVisible(true);
201        }
202      }
203    }
204  }
205}
Note: See TracBrowser for help on using the repository browser.