source: ether_megapoli/trunk/service/implementation/com/ether/EtherPlotServiceImpl.java @ 171

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

Fixe application : english/french _ ok before applet

File size: 19.3 KB
Line 
1package com.ether;
2
3import com.medias.megapoli.utils.MegapoliInitialisation;
4import gov.noaa.pmel.sgt.ColorMap;
5import gov.noaa.pmel.sgt.ContourLevels;
6import gov.noaa.pmel.sgt.GridAttribute;
7import gov.noaa.pmel.sgt.IndexedColorMap;
8import gov.noaa.pmel.sgt.JPane;
9import gov.noaa.pmel.sgt.LinearTransform;
10import gov.noaa.pmel.sgt.demo.TestData;
11import gov.noaa.pmel.sgt.dm.SGTData;
12import gov.noaa.pmel.sgt.dm.SGTMetaData;
13import gov.noaa.pmel.sgt.dm.SimpleGrid;
14import gov.noaa.pmel.sgt.dm.SimpleLine;
15import gov.noaa.pmel.sgt.swing.JPlotLayout;
16import gov.noaa.pmel.util.GeoDateArray;
17import gov.noaa.pmel.util.Range2D;
18import org.apache.commons.logging.Log;
19import org.apache.commons.logging.LogFactory;
20import org.jetbrains.annotations.NotNull;
21import org.jetbrains.annotations.Nullable;
22
23import javax.swing.*;
24import java.awt.*;
25import java.awt.image.BufferedImage;
26import java.util.Calendar;
27import java.util.Date;
28import java.util.Locale;
29import java.util.ResourceBundle;
30
31/**
32 * @author vmipsl
33 * @date 05 july 2011
34 */
35public class EtherPlotServiceImpl
36        implements EtherPlotService
37{
38    public static Integer getMaxWidth()
39    {
40        final Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
41        return Math.min( Double.valueOf( screenSize.getWidth() ).intValue(), MAX_WIDTH );
42    }
43
44    public static Integer getMaxHeight()
45    {
46        final Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
47        return Math.min( Double.valueOf( screenSize.getHeight() ).intValue(), MAX_HEIGHT );
48    }
49
50    /**
51     * Create the main JPane with the 3 jPanes
52     *
53     * @param megapoliPlot
54     * @param locale
55     * @return
56     */
57    @NotNull
58    public BufferedImage createMainPane( @NotNull final MegapoliPlot megapoliPlot, @Nullable final Locale locale )
59    {
60        final Integer maxWidth = getMaxWidth();
61        final Integer maxHeight = getMaxHeight();
62
63        final JPane jPane = new JPane( "Main jPane", new Dimension( maxWidth, maxHeight ) );
64        jPane.setLayout( new BorderLayout() );
65        jPane.setBackground( Color.white );
66        jPane.setOpaque( true );
67
68        // Top Pane
69        final JPane jPaneTop = createTopPane( megapoliPlot );
70        jPane.add( jPaneTop, BorderLayout.NORTH );
71
72        final Integer valuesNumber = megapoliPlot.getValuesNumber();
73
74        // Bottom Pane
75        final JPane jPaneBottom = createBottomPane( valuesNumber, locale );
76        jPane.add( jPaneBottom, BorderLayout.SOUTH );
77
78        // Center Pane
79        final Integer centerHeight = maxHeight - jPaneTop.getHeight() - jPaneBottom.getHeight();
80        final JPane jPaneCenter;
81        if( valuesNumber > 0 )
82            jPaneCenter = createCenterPane( megapoliPlot, maxWidth, centerHeight, locale );
83        else
84            jPaneCenter = createEmptyCenterPane( locale );
85        jPane.add( jPaneCenter, BorderLayout.CENTER );
86
87
88        final JPanelToImageManager jPanelToImageManager = new JPanelToImageManager( jPane );
89        return jPanelToImageManager.saveAsImage();
90    }
91
92    /**
93     * Create the top JPane with the logos and title
94     *
95     * @param megapoliPlot
96     * @return
97     */
98    @NotNull
99    public JPane createTopPane( @Nullable final MegapoliPlot megapoliPlot )
100    {
101        // Logos
102        final ImageIcon logoMegapoli = new ImageIcon( megapoliPlot.getLogoMegapoli() );
103        final JLabel jLabelLogoMegapoli = new JLabel( logoMegapoli );
104
105        final ImageIcon logoEther = new ImageIcon( megapoliPlot.getLogoEther() );
106        final JLabel jLabelLogoEther = new JLabel( logoEther );
107
108        // Max height
109        final Integer maxHeights = Math.max( logoMegapoli.getIconHeight(), logoEther.getIconHeight() );
110
111        // jPane
112        final JPane jPaneTop = new JPane( "Top jPane", new Dimension( 0, maxHeights ) );
113        jPaneTop.setLayout( new BorderLayout() );
114
115        jPaneTop.add( jLabelLogoMegapoli, BorderLayout.LINE_START );
116        jPaneTop.add( jLabelLogoEther, BorderLayout.LINE_END );
117
118
119        // Title
120        final String formatedTitle = formatTitle( megapoliPlot.getTitle() );
121        final JLabel jLabelTitle = new JLabel( formatedTitle, JLabel.CENTER );
122        final Font police = new Font( "Arial", Font.BOLD, TITLE_FONT_SIZE );
123        jLabelTitle.setFont( police );
124        jPaneTop.add( jLabelTitle, BorderLayout.CENTER );
125
126        return jPaneTop;
127    }
128
129    /**
130     * Create the center JPane with the graph
131     *
132     * @param megapoliPlot
133     * @param maxWidth
134     * @param maxHeight
135     * @return
136     */
137    @NotNull
138    public JPane createCenterPane( @NotNull final MegapoliPlot megapoliPlot, @NotNull final Integer maxWidth, @NotNull final Integer maxHeight, @Nullable final Locale locale )
139    {
140        final Integer plotWidth = Math.min( maxWidth - MARGIN_LEFT_RIGHT, maxWidth );
141        final Integer plotHeight = Math.min( maxHeight - MARGIN_LEFT_RIGHT, maxHeight );
142
143        final JPlotLayout jPlotLayout;
144        final BufferedImage bufferedImage;
145        if( megapoliPlot.isTimeSerie() )
146        {
147            jPlotLayout = createJPlotLayoutForTimeSerie( megapoliPlot, plotWidth, plotHeight );
148
149            // TODO : remove this change to bufferedImage to insert directly the jPlotLayout !!!!
150            bufferedImage = new BufferedImage( jPlotLayout.getWidth(), jPlotLayout.getHeight(), BufferedImage.TYPE_INT_RGB );
151            final Graphics2D graphics2D = bufferedImage.createGraphics();
152            jPlotLayout.draw( graphics2D );
153        }
154        else
155        {
156            jPlotLayout = createJPlotLayoutFor2D( megapoliPlot, plotWidth, plotHeight, locale );
157
158            final JPanelToImageManager jPanelToImageManager = new JPanelToImageManager( jPlotLayout );
159            bufferedImage = jPanelToImageManager.saveAsImage();
160        }
161
162        final ImageIcon imageIcon = new ImageIcon( bufferedImage );
163        final JLabel jLabelPlot = new JLabel( imageIcon, JLabel.CENTER );
164        jLabelPlot.setBorder( BorderFactory.createLineBorder( Color.black, 1 ) );
165
166        final JPane jPaneCenter = new JPane( "Center jPane", new Dimension( jPlotLayout.getWidth(), jPlotLayout.getHeight() ) );
167        jPaneCenter.setLayout( new GridBagLayout() );
168        jPaneCenter.add( jLabelPlot,
169                new GridBagConstraints( 0, 0, 1, 1, 0, 0,
170                        GridBagConstraints.CENTER,
171                        GridBagConstraints.CENTER,
172                        new Insets( 0, 0, 0, 0 ), 0, 0 ) );
173
174        return jPaneCenter;
175    }
176
177    /**
178     * Create the bottom JPane with the text "published .. (date)"
179     *
180     * @param dataNumber : the number of datas extracted from the base
181     * @param locale
182     * @return
183     */
184    @NotNull
185    public JPane createBottomPane( @Nullable final Integer dataNumber, @Nullable final Locale locale )
186    {
187        final ResourceBundle bundle = WebHelper.getBundle( locale );
188
189        final JPane jPaneBottom = new JPane();
190        jPaneBottom.setLayout( new BorderLayout() );
191
192        // Published
193        final Calendar calendar = Calendar.getInstance();
194        final String messagePublished = bundle.getString( "plot.published" );
195        final JLabel jLabel = new JLabel( messagePublished + ' ' + DateHelper.formatDate( calendar.getTime(), DateHelper.ENGLISH_DATE_PATTERN ) + " " );
196        final Font police = new Font( "Arial", Font.BOLD, FONT_SIZE );
197        jLabel.setFont( police );
198        jPaneBottom.add( jLabel, BorderLayout.EAST );
199
200        if( null != dataNumber )
201        {
202            final String messageDataNumber = bundle.getString( "plot.dataNumber" );
203            final JLabel jLabelDataNumber = new JLabel( messageDataNumber + ' ' + dataNumber );
204            jLabelDataNumber.setFont( police );
205            jPaneBottom.add( jLabelDataNumber, BorderLayout.WEST );
206        }
207
208        return jPaneBottom;
209    }
210
211    /**
212     * Create an error JPane when plot can be created
213     *
214     * @param mainError
215     * @param errorText
216     * @param locale
217     * @return
218     */
219    @NotNull
220    public BufferedImage createErrorPane( @NotNull final String mainError, @Nullable final String errorText, @Nullable final Locale locale )
221    {
222        // Image
223        final ImageIcon errorImage = new ImageIcon( MegapoliInitialisation.pathImages + "/erreur.gif" );
224        final JLabel jLabelErrorImage = new JLabel( errorImage );
225
226        // Text
227        final ResourceBundle bundle = WebHelper.getBundle( locale );
228        final String mainMessage = bundle.getString( "plot.errorMessage" );
229
230        final JLabel jLabelError;
231        if( null != errorText )
232            jLabelError = new JLabel( "<html>" + mainMessage + "<br><br>" + mainError + "<br>" + errorText + "</html>", JLabel.CENTER );
233        else
234            jLabelError = new JLabel( "<html>" + mainMessage + "<br><br>" + mainError + "</html>", JLabel.CENTER );
235
236        final Font police = new Font( "Arial", Font.BOLD, ERROR_FONT_SIZE );
237        jLabelError.setFont( police );
238
239        // jPane
240        final JPane jPane = new JPane( "jPane", new Dimension( MAX_WIDTH, errorImage.getIconHeight() ) );
241        jPane.setLayout( new BorderLayout() );
242        jPane.setBackground( Color.white );
243        jPane.setOpaque( true );
244        jPane.add( jLabelErrorImage, BorderLayout.WEST );
245        jPane.add( jLabelError, BorderLayout.CENTER );
246
247        final JPanelToImageManager jPanelToImageManager = new JPanelToImageManager( jPane );
248        return jPanelToImageManager.saveAsImage();
249    }
250
251    /**
252     * Create the center JPane with no graph (if no value)
253     *
254     * @param locale
255     * @return
256     */
257    public JPane createEmptyCenterPane( @Nullable final Locale locale )
258    {
259        final ResourceBundle bundle = WebHelper.getBundle( locale );
260        final String messageData = bundle.getString( "plot.noData" );
261
262        final JPane jPaneCenter = new JPane();
263        jPaneCenter.setLayout( new BorderLayout() );
264
265        final JLabel jLabelData = new JLabel( messageData, JLabel.CENTER );
266        final Font police = new Font( "Arial", Font.BOLD, FONT_SIZE );
267        jLabelData.setFont( police );
268
269        jPaneCenter.add( jLabelData, BorderLayout.CENTER );
270        return jPaneCenter;
271    }
272
273    @NotNull
274    private JPlotLayout createJPlotLayoutForTimeSerie( final MegapoliPlot megapoliPlot, final Integer plotWidth, final Integer plotHeight )
275    {
276        final double[] dataArray = (double[]) megapoliPlot.getData().getFirstArray();
277        final Date[] dateValues = (Date[]) megapoliPlot.getData().getSecondArray();
278
279        final SimpleLine data = new SimpleLine( new GeoDateArray( dateValues ), dataArray, "legend" );
280        SGTMetaData meta = new SGTMetaData( null, null, false, false );
281        data.setXMetaData( meta );
282
283        meta = new SGTMetaData( megapoliPlot.getParameterName(), megapoliPlot.getParameterUnitCode(), false, false );
284        data.setYMetaData( meta );
285
286        // Graph
287//        final JPlotLayoutEther jPlotLayout = new JPlotLayoutEther( false, true, false, "Trajectory data", null, megapoliPlot.isLegendToHide() );
288        final JPlotLayout jPlotLayout = new JPlotLayout( data, "Profile Demo", null, false );
289        jPlotLayout.setTitles( "", "", "" );
290
291        // Replace legend
292//        final String legend = megapoliPlot.getData().getTitle();
293//        final Dimension2D layerSizeP = jPlotLayout.getLayerSizeP();
294//        final double factorForPlotWidthInPhysicalUnits = layerSizeP.getWidth() / Double.valueOf( plotWidth );
295//        final double legendSizeInPhysicalUnits = legend.length() * factorForPlotWidthInPhysicalUnits * 7;
296//        jPlotLayout.setKeyLocationP( new Point2D.Double( layerSizeP.getWidth() - legendSizeInPhysicalUnits, layerSizeP.getHeight() - 0.1 ) );
297
298        // Add data and legend
299        jPlotLayout.addData( data, "" );
300        jPlotLayout.setSize( plotWidth, plotHeight );
301        jPlotLayout.setVisible( true );
302
303        jPlotLayout.addNotify();
304        jPlotLayout.validate();
305        jPlotLayout.setOpaque( true );
306        return jPlotLayout;
307    }
308
309    @NotNull
310    private JPlotLayout createJPlotLayoutFor2D( final MegapoliPlot megapoliPlot, final Integer plotWidth, final Integer plotHeight, @Nullable final Locale locale )
311    {
312        SGTData newData;
313        TestData td;
314        JPlotLayout rpl;
315        ContourLevels clevels;
316        /*
317       * Create a test grid with sinasoidal-ramp data.
318        */
319        Range2D xr = new Range2D( 190.0f, 250.0f, 1.0f );
320        Range2D yr = new Range2D( 0.0f, 45.0f, 1.0f );
321        td = new TestData( TestData.XY_GRID, xr, yr,
322                TestData.SINE_RAMP, 12.0f, 30.f, 5.0f );
323        newData = td.getSGTData();
324        /*
325       * Create the layout without a Logo image and with the
326       * ColorKey on a separate Pane object.
327        */
328        rpl = new JPlotLayout( true, false, false, "JGridDemo Pane", null, true );
329        rpl.setEditClasses( false );
330        /*
331       * Create a GridAttribute for CONTOUR style.
332        */
333        Range2D datar = new Range2D( -20.0f, 45.0f, 5.0f );
334        clevels = ContourLevels.getDefault( datar );
335        final GridAttribute gridAttr_ = new GridAttribute( clevels );
336        /*
337       * Create a ColorMap and change the style to RASTER_CONTOUR.
338        */
339        ColorMap cmap = createColorMap( datar );
340        gridAttr_.setColorMap( cmap );
341        gridAttr_.setStyle( GridAttribute.RASTER_CONTOUR );
342        /*
343       * Add the grid to the layout and give a label for
344       * the ColorKey.
345        */
346        rpl.addData( newData, gridAttr_, "First Data" );
347        /*
348       * Change the layout's three title lines.
349        */
350        rpl.setTitles( "Raster Plot Demo", "using a JPlotLayout", "" );
351        /*
352       * Resize the graph  and place in the "Center" of the frame.
353        */
354        rpl.setSize( new Dimension( 600, 400 ) );
355        /*
356       * Resize the key Pane, both the device size and the physical
357       * size. Set the size of the key in physical units and place
358       * the key pane at the "South" of the frame.
359        */
360//    rpl.setKeyLayerSizeP(new Dimension2D(6.0, 1.02));
361//    rpl.setKeyBoundsP(new Rectangle2D.Double(0.01, 1.01, 5.98, 1.0));
362
363        return rpl;
364    }
365
366
367    @NotNull
368    private JPlotLayout createJPlotLayoutFor2DOrig( final MegapoliPlot megapoliPlot, final Integer plotWidth, final Integer plotHeight, @Nullable final Locale locale )
369    {
370        final ResourceBundle bundle = WebHelper.getBundle( locale );
371
372        final double[] parameterArray = (double[]) megapoliPlot.getData().getFirstArray();
373        final double[] latitudeArray = (double[]) megapoliPlot.getData().getSecondArray();
374        final double[] longitudeValues = (double[]) megapoliPlot.getData().getThirdArray();
375
376//        final SimpleLine data = new SimpleLine( longitudeValues, latitudeArray, "legend" );
377        final SimpleGrid data = new SimpleGrid( parameterArray, longitudeValues, latitudeArray, "Test Series" );
378        SGTMetaData meta = new SGTMetaData( bundle.getString( "plot.longitude" ), bundle.getString( "plot.degres" ), false, true );
379        // TODO : vérifier modulo = 360
380//        meta.setModuloValue( 360 );
381        data.setXMetaData( meta );
382
383        meta = new SGTMetaData( bundle.getString( "plot.latitude" ), bundle.getString( "plot.degres" ), false, true );
384        // TODO : vérifier modulo = 360
385//        meta.setModuloValue( 360 );
386        data.setYMetaData( meta );
387
388        // Graph
389        final JPlotLayout jPlotLayout = new JPlotLayout( true, false, false, "JGridDemo Pane", null, true );
390        jPlotLayout.setEditClasses( false );
391        /*
392       * Create a GridAttribute for CONTOUR style.
393        */
394        final Range2D datar = new Range2D( -20.0f, 45.0f, 5.0f );
395        final ContourLevels clevels = ContourLevels.getDefault( datar );
396        final GridAttribute gridAttr = new GridAttribute( clevels );
397        /*
398       * Create a ColorMap and change the style to RASTER_CONTOUR.
399        */
400        final ColorMap cmap = createColorMap( datar );
401        gridAttr.setColorMap( cmap );
402        gridAttr.setStyle( GridAttribute.RASTER_CONTOUR );
403        /*
404       * Add the grid to the layout and give a label for
405       * the ColorKey.
406        */
407        jPlotLayout.addData( data, gridAttr, "First Data" );
408
409//        final JPlotLayoutEther jPlotLayout = new JPlotLayoutEther( false, megapoliPlot.isXTime(), false, "Trajectory data", null, megapoliPlot.isLegendToHide() );
410//        jPlotLayout.setTitles( "", "", "" );
411
412        // Replace legend
413//        final String legend = megapoliPlot.getData().getTitle();
414//        final Dimension2D layerSizeP = jPlotLayout.getLayerSizeP();
415//        final double factorForPlotWidthInPhysicalUnits = layerSizeP.getWidth() / Double.valueOf( plotWidth );
416//        final double legendSizeInPhysicalUnits = legend.length() * factorForPlotWidthInPhysicalUnits * 7;
417//        jPlotLayout.setKeyLocationP( new Point2D.Double( layerSizeP.getWidth() - legendSizeInPhysicalUnits, layerSizeP.getHeight() - 0.1 ) );
418
419        // Add data and legend
420//        jPlotLayout.addData( megapoliPlot.getData(), legend );
421        jPlotLayout.setSize( plotWidth, plotHeight );
422        jPlotLayout.setVisible( true );
423
424        jPlotLayout.addNotify();
425        jPlotLayout.validate();
426        jPlotLayout.setOpaque( true );
427        return jPlotLayout;
428    }
429
430    @NotNull
431    private String formatTitle( @Nullable final String title )
432    {
433        if( null == title )
434            return "";
435        else
436        {
437            final String formatTitle = title.replaceAll( "\\n", "<br>" );
438            return "<html>" + formatTitle + "</html>";
439        }
440    }
441
442    @NotNull
443    private ColorMap createColorMap( final Range2D datar )
444    {
445        final int[] red =
446                {0, 0, 0, 0, 0, 0, 0, 0,
447                        0, 0, 0, 0, 0, 0, 0, 0,
448                        0, 0, 0, 0, 0, 0, 0, 0,
449                        0, 7, 23, 39, 55, 71, 87, 103,
450                        119, 135, 151, 167, 183, 199, 215, 231,
451                        247, 255, 255, 255, 255, 255, 255, 255,
452                        255, 255, 255, 255, 255, 255, 255, 255,
453                        255, 246, 228, 211, 193, 175, 158, 140};
454        final int[] green =
455                {0, 0, 0, 0, 0, 0, 0, 0,
456                        0, 11, 27, 43, 59, 75, 91, 107,
457                        123, 139, 155, 171, 187, 203, 219, 235,
458                        251, 255, 255, 255, 255, 255, 255, 255,
459                        255, 255, 255, 255, 255, 255, 255, 255,
460                        255, 247, 231, 215, 199, 183, 167, 151,
461                        135, 119, 103, 87, 71, 55, 39, 23,
462                        7, 0, 0, 0, 0, 0, 0, 0};
463        final int[] blue =
464                {0, 143, 159, 175, 191, 207, 223, 239,
465                        255, 255, 255, 255, 255, 255, 255, 255,
466                        255, 255, 255, 255, 255, 255, 255, 255,
467                        255, 247, 231, 215, 199, 183, 167, 151,
468                        135, 119, 103, 87, 71, 55, 39, 23,
469                        7, 0, 0, 0, 0, 0, 0, 0,
470                        0, 0, 0, 0, 0, 0, 0, 0,
471                        0, 0, 0, 0, 0, 0, 0, 0};
472
473        final IndexedColorMap cmap = new IndexedColorMap( red, green, blue );
474        cmap.setTransform( new LinearTransform( 0.0, (double) red.length, datar.start, datar.end ) );
475        return cmap;
476    }
477
478    private static final Log LOGGER = LogFactory.getLog( EtherPlotServiceImpl.class );
479
480    // Dimensions of the jPanes
481    private static final int MAX_WIDTH = 800;
482    private static final int MAX_HEIGHT = 700;
483    private static final int MARGIN_LEFT_RIGHT = 50;
484
485    private static final int TITLE_FONT_SIZE = 20;
486    private static final int FONT_SIZE = 12;
487    private static final int ERROR_FONT_SIZE = 10;
488}
Note: See TracBrowser for help on using the repository browser.