source: ether_megapoli/trunk/applets/src/AppletTimeSerie.java @ 175

Last change on this file since 175 was 175, checked in by vmipsl, 13 years ago

Applets _ essais

File size: 22.3 KB
Line 
1import com.ether.AxeTypeForFixedPlateform;
2import com.ether.Data;
3import com.ether.EtherPlotService;
4import com.ether.EtherService;
5import com.ether.MegapoliPlot;
6import com.medias.Context;
7import com.medias.database.objects.Parametre;
8import com.medias.database.objects.Unite;
9import gov.noaa.pmel.sgt.Axis;
10import gov.noaa.pmel.sgt.CartesianGraph;
11import gov.noaa.pmel.sgt.Graph;
12import gov.noaa.pmel.sgt.JPane;
13import gov.noaa.pmel.sgt.Layer;
14import gov.noaa.pmel.sgt.LineAttribute;
15import gov.noaa.pmel.sgt.LineCartesianRenderer;
16import gov.noaa.pmel.sgt.LineKey;
17import gov.noaa.pmel.sgt.LinearTransform;
18import gov.noaa.pmel.sgt.PlainAxis;
19import gov.noaa.pmel.sgt.SGLabel;
20import gov.noaa.pmel.sgt.StackedLayout;
21import gov.noaa.pmel.sgt.TimeAxis;
22import gov.noaa.pmel.sgt.demo.TestData;
23import gov.noaa.pmel.sgt.dm.SimpleLine;
24import gov.noaa.pmel.sgt.swing.JClassTree;
25import gov.noaa.pmel.util.Dimension2D;
26import gov.noaa.pmel.util.GeoDate;
27import gov.noaa.pmel.util.IllegalTimeValue;
28import gov.noaa.pmel.util.Point2D;
29import gov.noaa.pmel.util.Range2D;
30import gov.noaa.pmel.util.SoTPoint;
31import gov.noaa.pmel.util.SoTRange;
32import gov.noaa.pmel.util.TimeRange;
33import org.springframework.context.support.ClassPathXmlApplicationContext;
34
35import javax.swing.*;
36import javax.swing.border.EtchedBorder;
37import java.applet.Applet;
38import java.awt.*;
39import java.awt.im.InputContext;
40import java.util.Calendar;
41import java.util.Date;
42import java.util.Enumeration;
43import java.util.Locale;
44
45public class AppletTimeSerie
46        extends JApplet
47{
48    public void init()
49    {
50        final String appletContext = "applet-context.xml";
51        final String hibernateContext = "hibernate.cfg.xml";
52        final String[] contextPaths = new String[]{appletContext, hibernateContext};
53
54        final ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( contextPaths );
55        _etherService = (EtherService) context.getBean( "etherService", EtherService.class );
56        _etherPlotService = (EtherPlotService) context.getBean( "etherPlotService", EtherPlotService.class );
57        _locale = InputContext.getInstance().getLocale();
58    }
59
60    public void start()
61    {
62        // Init megapoliPlot with parameters
63        final MegapoliPlot megapoliPlot = initMegapoliPlot();
64
65        // Create graph
66        setLayout( new BorderLayout( 0, 0 ) );
67        setBackground( Color.WHITE );
68        setSize( 800, 800 );
69
70        // Top Pane
71        final JPane jPaneTop = _etherPlotService.createTopPane( megapoliPlot );
72        add( jPaneTop, BorderLayout.NORTH );
73
74        // Bottom Pane
75        final Integer valuesNumber = megapoliPlot.getValuesNumber();
76
77        // Bottom Pane
78        final JPane jPaneBottom = _etherPlotService.createBottomPane( valuesNumber, _locale );
79        add( jPaneBottom, BorderLayout.PAGE_END );
80
81        makeControlPanel();
82        add( controlPanel, BorderLayout.SOUTH );
83
84//        makeGraph( parametre.getParametreNom() );
85        makeGraph();
86        add( mainPane_, BorderLayout.CENTER );
87    }
88
89    void makeGraph()
90    {
91        /*
92        * This example explicitly creates the JPane, Layers, Axes, and SGLabels.
93        */
94        /*
95        * Create JPane, place in the center of the frame
96        * and set the layout to use the StackedLayout.  StackedLayout is
97        * a custom layout manager designed to place Components directly
98        * over one another.
99        */
100        mainPane_ = new JPane( "Layout Demo", new Dimension( 426, 400 ) );
101        mainPane_.setLayout( new StackedLayout() );
102        /*
103        * Create the two random time series using the TestData class and
104        * the SimpleLine classes from sgt.dm
105        */
106        SimpleLine line;
107        SimpleLine line2;
108        GeoDate start = null;
109        GeoDate stop = null;
110        TimeRange tr;
111        TestData td;
112        try
113        {
114            start = new GeoDate( "1992-11-01", "yyyy-MM-dd" );
115            stop = new GeoDate( "1993-02-20", "yyyy-MM-dd" );
116        }
117        catch( IllegalTimeValue e )
118        {
119        }
120        tr = new TimeRange( start, stop );
121        td = new TestData( TestData.TIME_SERIES, tr, 1.0f,
122                TestData.RANDOM, 1.2f, 0.0f, 20.0f );
123        line = (SimpleLine) td.getSGTData();
124        //
125        try
126        {
127            start = new GeoDate( "1992-11-01", "yyyy-MM-dd" );
128            stop = new GeoDate( "1993-02-20", "yyyy-MM-dd" );
129        }
130        catch( IllegalTimeValue e )
131        {
132        }
133        tr = new TimeRange( start, stop );
134        td = new TestData( TestData.TIME_SERIES, tr, 1.0f,
135                TestData.RANDOM, 1.2f, 0.5f, 30.0f );
136        line2 = (SimpleLine) td.getSGTData();
137        /*
138        * Get the axis ranges from SGTLine
139        */
140        SoTRange ynRange, yRange;
141        SoTRange tnRange;
142        String yLabel;
143        yRange = line.getYRange();
144        yRange.add( line2.getYRange() );
145        tnRange = line.getXRange();
146        /*
147        * compute the range for the y and time axes
148        * and get the y axis label from line's metadata
149        */
150        ynRange = Graph.computeRange( yRange, 6 );
151        yLabel = line.getYMetaData().getName();
152        /*
153        * LayoutDemo will have two layers.
154        * One layer for each line to be drawn.
155        * The first layer will contain the axes and labels
156        * and the first set of data. The second layer will
157        * contain the second set of data.
158        */
159        /*
160        * xsize, ysize are the width and height in physical units
161        * of the Layer graphics region.
162        *
163        * xstart, xend are the start and end points for the TimeAxis
164        * ystart, yend are the start and end points for the Y axis
165        */
166        double xsize = 4.0;
167        double xstart = 0.6;
168        double xend = 3.25;
169        double ysize = 3.0;
170        double ystart = 0.6;
171        double yend = 2.50;
172
173        Layer layer, layer2;
174        SGLabel label, title, ytitle;
175        CartesianGraph graph, graph2;
176        LinearTransform xt, yt;
177        PlainAxis yleft;
178        TimeAxis xbot;
179        LineKey lkey;
180        GeoDate stime;
181        /*
182        * create the first layer
183        */
184        layer = new Layer( "First Layer", new Dimension2D( xsize, ysize ) );
185        /*
186        * create a time stamp label for the plot
187        * position the label at the lower left corner
188        * and add to the first layer
189        * (NOTE: the time will be displayed for the GMT time zone)
190        */
191        stime = new GeoDate();
192        label = new SGLabel( "Date Stamp", stime.toString(),
193                new Point2D.Double( 0.05, 0.05 ) );
194        label.setAlign( SGLabel.BOTTOM, SGLabel.LEFT );
195        label.setColor( Color.magenta );
196        label.setHeightP( 0.15 );
197        label.setFont( new Font( "Dialog", Font.PLAIN, 10 ) );
198        layer.addChild( label );
199        /*
200        * create a title for the plot
201        * position the label centered on the graph
202        * and add to the first layer
203        */
204        title = new SGLabel( "Title", "Layout Demo zzzzqsdqdqs",
205                new Point2D.Double( xsize / 2.0, ysize ) );
206        title.setAlign( SGLabel.TOP, SGLabel.CENTER );
207        title.setHeightP( 0.20 );
208        title.setFont( new Font( "Helvetica", Font.BOLD, 14 ) );
209        layer.addChild( title );
210        /*
211        * create a LineKey
212        * the LineKey will be a legend for the two lines created
213        * position the key in the upper right corner
214        * and add to the first layer
215        */
216        lkey = new LineKey();
217        lkey.setId( "Legend" );
218        lkey.setLocationP( new Point2D.Double( xsize - 0.01, ysize ) );
219        lkey.setVAlign( LineKey.TOP );
220        lkey.setHAlign( LineKey.RIGHT );
221        layer.addChild( lkey );
222        /*
223        * add the first layer to the Pane
224        */
225        mainPane_.add( layer );
226        /*
227        * create first CartesianGraph and transforms
228        */
229        graph = new CartesianGraph( "First Graph" );
230        xt = new LinearTransform( new Range2D( xstart, xend ), tnRange );
231        graph.setXTransform( xt );
232        yt = new LinearTransform( new Range2D( ystart, yend ), ynRange );
233        graph.setYTransform( yt );
234        /*
235        * Create the time axis, set its range in user units
236        * and its origin. Add the axis to the first graph.
237        */
238        SoTPoint point = new SoTPoint( ynRange.getStart(), tnRange.getStart() );
239        xbot = new TimeAxis( "Bottom Axis", TimeAxis.MONTH_YEAR );
240        xbot.setRangeU( tnRange );
241        xbot.setLocationU( point );
242        Font xbfont = new Font( "Helvetica", Font.ITALIC, 14 );
243        xbot.setLabelFont( xbfont );
244        xbot.setMinorLabelInterval( 1 );
245        graph.addXAxis( xbot );
246        /*
247        * Create the vertical axis, set its range in user units
248        * and its origin.  Create the axis title and add the
249        * axis to the first graph.
250        */
251        yleft = new PlainAxis( "Left Axis" );
252        yleft.setRangeU( ynRange );
253        yleft.setLocationU( point );
254        yleft.setLabelFont( xbfont );
255        ytitle = new SGLabel( "Y-Axis Title", yLabel,
256                new Point2D.Double( 0.0, 0.0 ) );
257        Font ytfont = new Font( "Helvetica", Font.PLAIN, 14 );
258        ytitle.setFont( ytfont );
259        ytitle.setHeightP( 0.2 );
260        yleft.setTitle( ytitle );
261        graph.addYAxis( yleft );
262        /*
263        * Add the first graph to the first layer.
264        */
265        layer.setGraph( graph );
266        /*
267        * Create a LineAttribute for the display of the first
268        * line. Associate the attribute and the line with the
269        * first graph.  Add the line to the LineKey.
270        */
271        LineAttribute attr;
272
273        attr = new LineAttribute( LineAttribute.MARK, 20, Color.red );
274        attr.setMarkHeightP( 0.1 );
275        graph.setData( line, attr );
276        lkey.addLineGraph( (LineCartesianRenderer) graph.getRenderer(),
277                new SGLabel( "1st line", "Red Data",
278                        new Point2D.Double( 0.0, 0.0 ) ) );
279        /*
280        * Create the second layer and add it the the Pane.
281        * Create the second graph and associate it with the
282        * second layer.
283        */
284        layer2 = new Layer( "Second Layer", new Dimension2D( xsize, ysize ) );
285        mainPane_.add( layer2 );
286        graph2 = new CartesianGraph( "Second Graph", xt, yt );
287        layer2.setGraph( graph2 );
288        /*
289        * Create a LineAttribute for the display of the second
290        * line. Associate the attribute and the line with the
291        * second graph.  Add the line to the LineKey.
292        */
293        LineAttribute attr2;
294        attr2 = new LineAttribute( LineAttribute.MARK, 2, Color.blue );
295        attr2.setMarkHeightP( 0.1 );
296        graph2.setData( line2, attr2 );
297        lkey.addLineGraph( (LineCartesianRenderer) graph2.getRenderer(),
298                new SGLabel( "2nd line", "Blue Data",
299                        new Point2D.Double( 0.0, 0.0 ) ) );
300    }
301
302    /**
303     * Change the Pane layout to StackedLayout.
304     *
305     * @param event
306     */
307
308    void stacked_itemStateChanged( java.awt.event.ItemEvent event )
309    {
310        /*
311        * Get the component list for mainPane_ and change
312        * the layout to StackedLayout.
313        */
314        Component[] comps = mainPane_.getComponents();
315        mainPane_.setBatch( true );
316        mainPane_.setLayout( new StackedLayout() );
317        /*
318        * Remove any axes that have been associated with
319        * the second graph.  With the layers overlayed it
320        * is not necessary to have duplicate axes.
321        */
322        Graph gr2 = ( (Layer) comps[1] ).getGraph();
323        ( (CartesianGraph) gr2 ).removeAllXAxes();
324        ( (CartesianGraph) gr2 ).removeAllYAxes();
325        /*
326        * Tell the Applet that the mainPane_ needs to
327        * be layed out and re-draw the mainPane_.
328        */
329        if( isApplet_ )
330        {
331            validate();
332        }
333        else
334        {
335            frame.validate();
336        }
337        mainPane_.setBatch( false );
338        if( tree_ != null )
339        {
340            if( tree_.isVisible() )
341            {
342                tree_.setJPane( mainPane_ );
343                tree_.expandTree();
344            }
345        }
346    }
347
348    /**
349     * Change the Pane layout to GridLayout.
350     *
351     * @param event
352     */
353
354    void grid_itemStateChanged( java.awt.event.ItemEvent event )
355    {
356        /*
357        * Get the component list for mainPane_ and change
358        * the layout to GridLayout.
359        */
360        Component[] comps = mainPane_.getComponents();
361        mainPane_.setBatch( true );
362        mainPane_.setLayout( new GridLayout( 2, 0 ) );
363        /*
364        * Get the first and second graphs from the first
365        * and second layers, respectively.
366        */
367        Graph gr = ( (Layer) comps[0] ).getGraph();
368        Graph gr2 = ( (Layer) comps[1] ).getGraph();
369        /*
370        * Create copies of all X-Axes associated with the first
371        * graph for the second graph. If the axes are not copied then
372        * the second graph will have the second line plotted, but without
373        * any axes.
374        */
375        for( Enumeration xa = ( (CartesianGraph) gr ).xAxisElements(); xa.hasMoreElements(); )
376        {
377            ( (CartesianGraph) gr2 ).addXAxis( ( (Axis) xa.nextElement() ).copy() );
378        }
379        /*
380        * Create copies of all Y-Axes associated with the first
381        * graph for the second graph.
382        */
383        for( Enumeration ya = ( (CartesianGraph) gr ).yAxisElements(); ya.hasMoreElements(); )
384        {
385            ( (CartesianGraph) gr2 ).addYAxis( ( (Axis) ya.nextElement() ).copy() );
386        }
387        /*
388        * Tell the Applet that the mainPane_ needs to
389        * be layed out and re-draw the mainPane_.
390        */
391        if( isApplet_ )
392        {
393            validate();
394        }
395        else
396        {
397            frame.validate();
398        }
399        //    mainPane_.draw();
400        mainPane_.setBatch( false );
401        if( tree_ != null )
402        {
403            if( tree_.isVisible() )
404            {
405                tree_.setJPane( mainPane_ );
406                tree_.expandTree();
407            }
408        }
409    }
410
411    void showTree_ActionPerformed( java.awt.event.ActionEvent event )
412    {
413        /*
414        * Create the ClassTree dialog to display the classes used
415        * in the mainPane_ and allow editing.
416        */
417        if( tree_ == null )
418        {
419            tree_ = new JClassTree( "Classes for LayoutDemo" );
420        }
421        tree_.setJPane( mainPane_ );
422        tree_.show();
423    }
424
425    private MegapoliPlot initMegapoliPlot()
426    {
427        final Integer plateformId = 14;
428        final Integer parameterId = 90;
429        final String title = "Applet de merd....";
430        final String axeType = AxeTypeForFixedPlateform.TIME_LINE.toString();
431
432//        final Integer plateformId = Integer.valueOf( getParameter( "plateformId" ) );
433//        final Integer parameterId = Integer.valueOf( getParameter( "parameterId" ) );
434//        final String title = getParameter( "title" );
435//        final String axeType = getParameter( "axeType" );
436//
437//        final String dateBegin = getParameter( "dateBegin" );
438//        final String dateEnd = getParameter( "dateEnd" );
439        final String dateBegin = null;
440        final String dateEnd = null;
441        final Calendar calendar = Calendar.getInstance();
442        Date formatedDateBegin = null;
443        Date formatedDateEnd = null;
444        if( null != dateBegin && null != dateEnd && !"false".equals( dateBegin ) && !"false".equals( dateEnd ) )
445            try
446            {
447                calendar.setTimeInMillis( Long.valueOf( dateBegin ) );
448                formatedDateBegin = calendar.getTime();
449                calendar.setTimeInMillis( Long.valueOf( dateEnd ) );
450                formatedDateEnd = calendar.getTime();
451            }
452            catch( Exception e )
453            {
454                // TODO : gérer fenêtre d'erreur
455//                createErrorPane( response, request, WebException.WebCode.INVALID_DATE.toString(), e );
456//                flagException = true;
457//                throw new ServletException( WebException.WebCode.INVALID_DATE.toString(), e );
458            }
459
460        // Parameter
461//            final Parametre parametre = _etherService.getParameterById( parameterId );
462        final Parametre parametre = new Parametre();
463        parametre.setParametreNom( "Ozone" );
464        final Unite unit = new Unite();
465        unit.setUniteCode( "ppb" );
466        parametre.setUnite( unit );
467        if( null == parametre )
468        {
469            // TODO : gérer fenêtre d'erreur
470//            createErrorPane( response, request, WebException.WebCode.PARAMETER_IS_NULL.toString(), null );
471//            flagException = true;
472//            throw new ServletException( WebException.WebCode.PARAMETER_IS_NULL.toString(), null );
473        }
474
475        // Create plot
476        final MegapoliPlot megapoliPlot = new MegapoliPlot();
477        megapoliPlot.setTitle( title );
478        megapoliPlot.setAxeType( axeType );
479        megapoliPlot.setParameterName( parametre.getParametreNom() );
480        if( null != parametre.getUnite() && null != parametre.getUnite().getUniteCode() )
481            megapoliPlot.setParameterUnitCode( parametre.getUnite().getUniteCode() );
482
483
484        if( AxeTypeForFixedPlateform.TIME_LINE.name().equals( axeType ) || AxeTypeForFixedPlateform.TIME_POINTS.name().equals( axeType ) )
485        {
486//            final Data valuesLists = _etherService.getListsByPlateformByParameterByPeriodForTimeSerie( plateformId, parameterId, formatedDateBegin, formatedDateEnd );
487            final double[] values = new double[50];
488            final Date[] dates = new Date[50];
489
490            for( int i = 0; i < 50; i++ )
491            {
492                values[i] = (double) i;
493                dates[i] = new Date();
494            }
495            final Data valuesLists = new Data( values, dates );
496
497            megapoliPlot.setData( valuesLists );
498
499            megapoliPlot.setTimeSerie( true );
500            megapoliPlot.setLegendToHide( true );
501            megapoliPlot.setValuesNumber( ( (double[]) valuesLists.getFirstArray() ).length );
502        }
503        else
504        {
505//            final Data valuesLists = _etherService.getListsByPlateformByParameterByPeriodFor2D( plateformId, parameterId, formatedDateBegin, formatedDateEnd );
506            final double[] latitudeValues = new double[50];
507            final double[] longitudeValues = new double[50];
508            final double[] parameterValues = new double[50];
509
510            for( int i = 0; i < 50; i++ )
511            {
512                parameterValues[i] = Math.random() * i;
513                latitudeValues[i] = Math.random() * i;
514                longitudeValues[i] = Math.random() * i;
515            }
516            final Data valuesLists = new Data( parameterValues, latitudeValues, longitudeValues );
517
518            megapoliPlot.setData( valuesLists );
519//                final double[] latitudeArray = {2.1, 2.2, 2.3};
520//                final double[] longitudeValues = {112.1, 12.2, 22.3};
521
522            megapoliPlot.setTimeSerie( false );
523            megapoliPlot.setLegendToHide( false );
524            megapoliPlot.setValuesNumber( ( (double[]) valuesLists.getFirstArray() ).length );
525        }
526        return megapoliPlot;
527    }
528    public void makeControlPanel()
529    {
530        final JPanel controlPanel = new JPanel();
531        final ButtonGroup gridtype = new ButtonGroup();
532        final JCheckBox stacked = new JCheckBox();
533        final JCheckBox grid = new JCheckBox();
534
535        controlPanel.setLayout( new GridBagLayout() );
536//    controlPanel.setBackground(new java.awt.Color(200,200,200));
537        controlPanel.setBounds( 0, 679, 426, 33 );
538        controlPanel.setBorder( new EtchedBorder() );
539        gridtype.add( stacked );
540        stacked.setSelected( true );
541        stacked.setText( "Overlayed" );
542
543        GridBagConstraints gbc = new GridBagConstraints();
544        gbc.gridx = 0;
545        gbc.gridy = 0;
546        gbc.gridwidth = 1;
547        gbc.gridheight = 1;
548        gbc.weightx = 0.0;
549        gbc.weighty = 1.0;
550        gbc.anchor = GridBagConstraints.EAST;
551        gbc.fill = GridBagConstraints.BOTH;
552        gbc.insets = new Insets( 5, 15, 5, 5 );
553        gbc.ipadx = 0;
554        gbc.ipady = 0;
555        controlPanel.add( stacked, gbc );
556        stacked.setBounds( 15, 5, 84, 23 );
557        gridtype.add( grid );
558        grid.setText( "Grid" );
559
560        gbc.gridx = 1;
561        gbc.gridy = 0;
562        gbc.gridwidth = 1;
563        gbc.gridheight = 1;
564        gbc.weightx = 0.5;
565        gbc.weighty = 1.0;
566        gbc.anchor = GridBagConstraints.WEST;
567        gbc.fill = GridBagConstraints.VERTICAL;
568        gbc.insets = new Insets( 5, 5, 5, 0 );
569        gbc.ipadx = 0;
570        gbc.ipady = 0;
571        controlPanel.add( grid, gbc );
572        grid.setBounds( 109, 5, 53, 23 );
573        showTree.setText( "Show Class Tree" );
574
575        gbc.gridx = 2;
576        gbc.gridy = 0;
577        gbc.gridwidth = 1;
578        gbc.gridheight = 1;
579        gbc.weightx = 1.0;
580        gbc.weighty = 1.0;
581        gbc.anchor = GridBagConstraints.EAST;
582        gbc.fill = GridBagConstraints.VERTICAL;
583        gbc.insets = new Insets( 5, 5, 5, 15 );
584        gbc.ipadx = 0;
585        gbc.ipady = 0;
586        controlPanel.add( showTree, gbc );
587        showTree.setBackground( Color.yellow.brighter() );
588        showTree.setBounds( 302, 5, 109, 23 );
589
590        SymItem lSymItem = new SymItem();
591        stacked.addItemListener( lSymItem );
592        grid.addItemListener( lSymItem );
593        SymAction lSymAction = new SymAction();
594        showTree.addActionListener( lSymAction );
595    }
596
597
598    class SymItem
599            implements java.awt.event.ItemListener
600    {
601        public void itemStateChanged( java.awt.event.ItemEvent event )
602        {
603            Object object = event.getSource();
604            if( object == stacked )
605                stacked_itemStateChanged( event );
606            else if( object == grid )
607                grid_itemStateChanged( event );
608        }
609    }
610
611    class SymAction
612            implements java.awt.event.ActionListener
613    {
614        public void actionPerformed( java.awt.event.ActionEvent event )
615        {
616            Object object = event.getSource();
617            if( object == showTree )
618                showTree_ActionPerformed( event );
619        }
620    }
621
622    JPane mainPane_;
623    JClassTree tree_ = null;
624    boolean isApplet_ = true;
625    JFrame frame = null;
626
627    JPanel controlPanel = new JPanel();
628    JCheckBox stacked = new JCheckBox();
629    ButtonGroup gridtype = new ButtonGroup();
630    JCheckBox grid = new JCheckBox();
631    JButton showTree = new JButton();
632
633    private EtherService _etherService;
634    private EtherPlotService _etherPlotService;
635    private Locale _locale;
636}
Note: See TracBrowser for help on using the repository browser.