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

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

Servlet _ contour et line ok

File size: 9.6 KB
Line 
1package com.ether;
2
3import com.medias.megapoli.utils.MegapoliInitialisation;
4import gov.noaa.pmel.sgt.JPane;
5import org.apache.commons.logging.Log;
6import org.apache.commons.logging.LogFactory;
7import org.jetbrains.annotations.NotNull;
8import org.jetbrains.annotations.Nullable;
9
10import javax.swing.*;
11import java.awt.*;
12import java.awt.image.BufferedImage;
13import java.util.Calendar;
14import java.util.Locale;
15import java.util.ResourceBundle;
16
17/**
18 * @author vmipsl
19 * @date 05 july 2011
20 */
21public class EtherPlotServiceImpl
22        extends EtherPlotContentServiceImpl
23        implements EtherPlotService
24{
25    public static Integer getMaxWidth()
26    {
27        final Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
28        return Math.min( Double.valueOf( screenSize.getWidth() ).intValue(), MAX_WIDTH );
29    }
30
31    public static Integer getMaxHeight()
32    {
33        final Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
34        return Math.min( Double.valueOf( screenSize.getHeight() ).intValue(), MAX_HEIGHT );
35    }
36
37    /**
38     * Create the main JPane with the 3 jPanes : top, center (graph) et bottom
39     *
40     * @param megapoliPlot
41     * @param locale
42     * @return
43     */
44    @NotNull
45    public BufferedImage createMainPane( @NotNull final MegapoliPlot megapoliPlot, @Nullable final Locale locale )
46    {
47        final Integer maxWidth = getMaxWidth();
48        final Integer maxHeight = getMaxHeight();
49
50        final JPane jPane = new JPane( "Main jPane", new Dimension( maxWidth, maxHeight ) );
51        jPane.setLayout( new BorderLayout() );
52        jPane.setBackground( Color.white );
53        jPane.setOpaque( true );
54
55        // Top Pane
56        final JPane jPaneTop = createTopPane( megapoliPlot );
57        jPane.add( jPaneTop, BorderLayout.NORTH );
58
59        final Integer valuesNumber = megapoliPlot.getValuesNumber();
60
61        // Bottom Pane
62        final JPane jPaneBottom = createBottomPane( valuesNumber, locale );
63        jPane.add( jPaneBottom, BorderLayout.SOUTH );
64
65        // Center Pane
66        final Integer centerHeight = maxHeight - jPaneTop.getHeight() - jPaneBottom.getHeight();
67        if( valuesNumber > 0 )
68        {
69            final BufferedImage jPaneCenterBufferedImage = createCenterPane( megapoliPlot, maxWidth, centerHeight, locale );
70
71            final ImageIcon imageIcon = new ImageIcon( jPaneCenterBufferedImage );
72            final JLabel jLabelPlot = new JLabel( imageIcon, JLabel.CENTER );
73            jLabelPlot.setBorder( BorderFactory.createLineBorder( Color.black, 1 ) );
74
75            final JPane jPaneCenter = new JPane( "Center jPane", new Dimension( jPaneCenterBufferedImage.getWidth(), jPaneCenterBufferedImage.getHeight() ) );
76            jPaneCenter.setLayout( new GridBagLayout() );
77            jPaneCenter.add( jLabelPlot,
78                    new GridBagConstraints( 0, 0, 1, 1, 0, 0,
79                            GridBagConstraints.CENTER,
80                            GridBagConstraints.CENTER,
81                            new Insets( 0, 0, 0, 0 ), 0, 0 ) );
82            jPane.add( jPaneCenter, BorderLayout.CENTER );
83        }
84        else
85        {
86            final JPane jPaneCenter = createEmptyCenterPane( locale );
87            jPane.add( jPaneCenter, BorderLayout.CENTER );
88        }
89
90        final JPanelToImageManager jPanelToImageManager = new JPanelToImageManager( jPane );
91        return jPanelToImageManager.saveAsImage();
92    }
93
94    /**
95     * Create the top JPane with the logos and title
96     *
97     * @param megapoliPlot
98     * @return
99     */
100    @NotNull
101    public JPane createTopPane( @Nullable final MegapoliPlot megapoliPlot )
102    {
103        // Logos
104        final ImageIcon logoMegapoli = new ImageIcon( megapoliPlot.getLogoMegapoli() );
105        final JLabel jLabelLogoMegapoli = new JLabel( logoMegapoli );
106
107        final ImageIcon logoEther = new ImageIcon( megapoliPlot.getLogoEther() );
108        final JLabel jLabelLogoEther = new JLabel( logoEther );
109
110        // Max height
111        final Integer maxHeights = Math.max( logoMegapoli.getIconHeight(), logoEther.getIconHeight() );
112
113        // jPane
114        final JPane jPaneTop = new JPane( "Top jPane", new Dimension( 0, maxHeights ) );
115        jPaneTop.setLayout( new BorderLayout() );
116
117        jPaneTop.add( jLabelLogoMegapoli, BorderLayout.LINE_START );
118        jPaneTop.add( jLabelLogoEther, BorderLayout.LINE_END );
119
120
121        // Title
122        final String formatedTitle = formatTitle( megapoliPlot.getTitle() );
123        final JLabel jLabelTitle = new JLabel( formatedTitle, JLabel.CENTER );
124        final Font police = new Font( "Arial", Font.BOLD, TITLE_FONT_SIZE );
125        jLabelTitle.setFont( police );
126        jPaneTop.add( jLabelTitle, BorderLayout.CENTER );
127
128        return jPaneTop;
129    }
130
131    /**
132     * Create the bottom JPane with the text "published .. (date)"
133     *
134     * @param dataNumber : the number of datas extracted from the base
135     * @param locale
136     * @return
137     */
138    @NotNull
139    public JPane createBottomPane( @Nullable final Integer dataNumber, @Nullable final Locale locale )
140    {
141        final ResourceBundle bundle = WebHelper.getBundle( locale );
142
143        final JPane jPaneBottom = new JPane();
144        jPaneBottom.setLayout( new BorderLayout() );
145
146        // Published
147        final Calendar calendar = Calendar.getInstance();
148        final String messagePublished = bundle.getString( "plot.published" );
149        final JLabel jLabel = new JLabel( messagePublished + ' ' + DateHelper.formatDate( calendar.getTime(), DateHelper.ENGLISH_DATE_PATTERN ) + " " );
150        final Font police = new Font( "Arial", Font.BOLD, FONT_SIZE );
151        jLabel.setFont( police );
152        jPaneBottom.add( jLabel, BorderLayout.EAST );
153
154        if( null != dataNumber )
155        {
156            final String messageDataNumber = bundle.getString( "plot.dataNumber" );
157            final JLabel jLabelDataNumber = new JLabel( messageDataNumber + ' ' + dataNumber );
158            jLabelDataNumber.setFont( police );
159            jPaneBottom.add( jLabelDataNumber, BorderLayout.WEST );
160        }
161
162        return jPaneBottom;
163    }
164
165    /**
166     * Create an error JPane when plot can be created
167     *
168     * @param mainError
169     * @param errorText
170     * @param locale
171     * @return
172     */
173    @NotNull
174    public BufferedImage createErrorPane( @NotNull final String mainError, @Nullable final String errorText, @Nullable final Locale locale )
175    {
176        // Image
177        final ImageIcon errorImage = new ImageIcon( MegapoliInitialisation.pathImages + "/erreur.gif" );
178        final JLabel jLabelErrorImage = new JLabel( errorImage );
179
180        // Text
181        final ResourceBundle bundle = WebHelper.getBundle( locale );
182        final String mainMessage = bundle.getString( "plot.errorMessage" );
183
184        final JLabel jLabelError;
185        if( null != errorText )
186            jLabelError = new JLabel( "<html>" + mainMessage + "<br><br>" + mainError + "<br>" + errorText + "</html>", JLabel.CENTER );
187        else
188            jLabelError = new JLabel( "<html>" + mainMessage + "<br><br>" + mainError + "</html>", JLabel.CENTER );
189
190        final Font police = new Font( "Arial", Font.BOLD, ERROR_FONT_SIZE );
191        jLabelError.setFont( police );
192
193        // jPane
194        final JPane jPane = new JPane( "jPane", new Dimension( MAX_WIDTH, errorImage.getIconHeight() ) );
195        jPane.setLayout( new BorderLayout() );
196        jPane.setBackground( Color.white );
197        jPane.setOpaque( true );
198        jPane.add( jLabelErrorImage, BorderLayout.WEST );
199        jPane.add( jLabelError, BorderLayout.CENTER );
200
201        final JPanelToImageManager jPanelToImageManager = new JPanelToImageManager( jPane );
202        return jPanelToImageManager.saveAsImage();
203    }
204
205    /**
206     * Create the center JPane with no graph (if no value)
207     *
208     * @param locale
209     * @return
210     */
211    @NotNull
212    public JPane createEmptyCenterPane( @Nullable final Locale locale )
213    {
214        final ResourceBundle bundle = WebHelper.getBundle( locale );
215        final String messageData = bundle.getString( "plot.noData" );
216
217        final JPane jPaneCenter = new JPane();
218        jPaneCenter.setLayout( new BorderLayout() );
219
220        final JLabel jLabelData = new JLabel( messageData, JLabel.CENTER );
221        final Font police = new Font( "Arial", Font.BOLD, FONT_SIZE );
222        jLabelData.setFont( police );
223
224        jPaneCenter.add( jLabelData, BorderLayout.CENTER );
225        return jPaneCenter;
226    }
227
228    /**
229     * Create the center JPane with the graph
230     *
231     * @param megapoliPlot
232     * @param maxWidth
233     * @param maxHeight
234     * @return
235     */
236    @NotNull
237    public BufferedImage createCenterPane( @NotNull final MegapoliPlot megapoliPlot, @NotNull final Integer maxWidth, @NotNull final Integer maxHeight, @Nullable final Locale locale )
238    {
239        final Integer plotWidth = Math.min( maxWidth - MARGIN_LEFT_RIGHT, maxWidth );
240        final Integer plotHeight = Math.min( maxHeight - MARGIN_LEFT_RIGHT, maxHeight );
241
242        final JPane jPaneGraph;
243
244        if( AxeTypeForFixedPlateform.TIME_LINE.name().equals( megapoliPlot.getAxeType() ) || AxeTypeForFixedPlateform.TIME_POINTS.name().equals( megapoliPlot.getAxeType() ) )
245            jPaneGraph = createTimeSeriePlot( megapoliPlot, plotWidth, plotHeight );
246        else
247            jPaneGraph = create2DPlot( megapoliPlot, plotWidth, plotHeight );
248
249        final BufferedImage bufferedImage = new BufferedImage( jPaneGraph.getWidth(), jPaneGraph.getHeight(), BufferedImage.TYPE_INT_ARGB );
250        final Graphics2D graphics2D = bufferedImage.createGraphics();
251        jPaneGraph.addNotify();
252        jPaneGraph.validate();
253        jPaneGraph.draw( graphics2D );
254
255        return bufferedImage;
256    }
257
258    private static final Log LOGGER = LogFactory.getLog( EtherPlotServiceImpl.class );
259}
Note: See TracBrowser for help on using the repository browser.