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

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

Servlet _ Contour en cours

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