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

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

Servlet _ 2D ok

File size: 10.8 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 JLabel jLabelPlot = transformJPaneToJLabel( jPaneCenter );
90
91                final JPane jPaneCenterToImage = new JPane();
92                jPaneCenterToImage.setLayout( new GridBagLayout() );
93                jPaneCenterToImage.add( jLabelPlot,
94                        new GridBagConstraints( 0, 0, 1, 1, 0, 0,
95                                GridBagConstraints.CENTER,
96                                GridBagConstraints.CENTER,
97                                new Insets( 0, 0, 0, 0 ), 0, 0 ) );
98
99                jPane.add( jPaneCenterToImage, BorderLayout.CENTER );
100            }
101        }
102        else
103        {
104            final JPane jPaneCenter = createEmptyCenterPane( locale );
105            jPane.add( jPaneCenter, BorderLayout.CENTER );
106        }
107
108        final JPanelToImageManager jPanelToImageManager = new JPanelToImageManager( jPane );
109        return jPanelToImageManager.saveAsImage();
110    }
111
112    /**
113     * Create the top JPane with the logos and title
114     *
115     * @param megapoliPlot
116     * @return
117     */
118    @NotNull
119    public JPane createTopPane( @Nullable final MegapoliPlot megapoliPlot )
120    {
121        // Logos
122        final ImageIcon logoMegapoli = new ImageIcon( megapoliPlot.getLogoMegapoli() );
123        final JLabel jLabelLogoMegapoli = new JLabel( logoMegapoli );
124
125        final ImageIcon logoEther = new ImageIcon( megapoliPlot.getLogoEther() );
126        final JLabel jLabelLogoEther = new JLabel( logoEther );
127
128        // Max height
129        final Integer maxHeights = Math.max( logoMegapoli.getIconHeight(), logoEther.getIconHeight() );
130
131        // jPane
132        final JPane jPaneTop = new JPane( "Top jPane", new Dimension( 0, maxHeights ) );
133        jPaneTop.setLayout( new BorderLayout() );
134
135        jPaneTop.add( jLabelLogoMegapoli, BorderLayout.LINE_START );
136        jPaneTop.add( jLabelLogoEther, BorderLayout.LINE_END );
137
138
139        // Title
140        final String formatedTitle = formatTitle( megapoliPlot.getTitle() );
141        final JLabel jLabelTitle = new JLabel( formatedTitle, JLabel.CENTER );
142        final Font police = new Font( "Arial", Font.BOLD, TITLE_FONT_SIZE );
143        jLabelTitle.setFont( police );
144        jPaneTop.add( jLabelTitle, BorderLayout.CENTER );
145
146        return jPaneTop;
147    }
148
149    /**
150     * Create the bottom JPane with the text "published .. (date)"
151     *
152     * @param dataNumber : the number of datas extracted from the base
153     * @param locale
154     * @return
155     */
156    @NotNull
157    public JPane createBottomPane( @Nullable final Integer dataNumber, @Nullable final Locale locale )
158    {
159        final ResourceBundle bundle = WebHelper.getBundle( locale );
160
161        final JPane jPaneBottom = new JPane();
162        jPaneBottom.setLayout( new BorderLayout() );
163
164        // Published
165        final Calendar calendar = Calendar.getInstance();
166        final String messagePublished = bundle.getString( "plot.published" );
167        final JLabel jLabel = new JLabel( messagePublished + ' ' + DateHelper.formatDate( calendar.getTime(), DateHelper.ENGLISH_DATE_PATTERN ) + " " );
168        final Font police = new Font( "Arial", Font.BOLD, FONT_SIZE );
169        jLabel.setFont( police );
170        jPaneBottom.add( jLabel, BorderLayout.EAST );
171
172        if( null != dataNumber )
173        {
174            final String messageDataNumber = bundle.getString( "plot.dataNumber" );
175            final JLabel jLabelDataNumber = new JLabel( messageDataNumber + ' ' + dataNumber );
176            jLabelDataNumber.setFont( police );
177            jPaneBottom.add( jLabelDataNumber, BorderLayout.WEST );
178        }
179
180        return jPaneBottom;
181    }
182
183    /**
184     * Create an error JPane when plot can be created
185     *
186     * @param mainError
187     * @param errorText
188     * @param locale
189     * @return
190     */
191    @NotNull
192    public BufferedImage createErrorPane( @NotNull final String mainError, @Nullable final String errorText, @Nullable final Locale locale )
193    {
194        // Image
195        final ImageIcon errorImage = new ImageIcon( MegapoliInitialisation.pathImages + "/erreur.gif" );
196        final JLabel jLabelErrorImage = new JLabel( errorImage );
197
198        // Text
199        final ResourceBundle bundle = WebHelper.getBundle( locale );
200        final String mainMessage = bundle.getString( "plot.errorMessage" );
201
202        final JLabel jLabelError;
203        if( null != errorText )
204            jLabelError = new JLabel( "<html>" + mainMessage + "<br><br>" + mainError + "<br>" + errorText + "</html>", JLabel.CENTER );
205        else
206            jLabelError = new JLabel( "<html>" + mainMessage + "<br><br>" + mainError + "</html>", JLabel.CENTER );
207
208        final Font police = new Font( "Arial", Font.BOLD, ERROR_FONT_SIZE );
209        jLabelError.setFont( police );
210
211        // jPane
212        final JPane jPane = new JPane( "jPane", new Dimension( MAX_WIDTH, errorImage.getIconHeight() ) );
213        jPane.setLayout( new BorderLayout() );
214        jPane.setBackground( Color.white );
215        jPane.setOpaque( true );
216        jPane.add( jLabelErrorImage, BorderLayout.WEST );
217        jPane.add( jLabelError, BorderLayout.CENTER );
218
219        final JPanelToImageManager jPanelToImageManager = new JPanelToImageManager( jPane );
220        return jPanelToImageManager.saveAsImage();
221    }
222
223    /**
224     * Create the center JPane with no graph (if no value)
225     *
226     * @param locale
227     * @return
228     */
229    @NotNull
230    public JPane createEmptyCenterPane( @Nullable final Locale locale )
231    {
232        final ResourceBundle bundle = WebHelper.getBundle( locale );
233        final String messageData = bundle.getString( "plot.noData" );
234
235        final JPane jPaneCenter = new JPane();
236        jPaneCenter.setLayout( new BorderLayout() );
237
238        final JLabel jLabelData = new JLabel( messageData, JLabel.CENTER );
239        final Font police = new Font( "Arial", Font.BOLD, FONT_SIZE );
240        jLabelData.setFont( police );
241
242        jPaneCenter.add( jLabelData, BorderLayout.CENTER );
243        return jPaneCenter;
244    }
245
246    /**
247     * Create the center JPane with the graph
248     *
249     * @param megapoliPlot
250     * @param maxWidth
251     * @param maxHeight
252     * @return
253     */
254    @Nullable
255    public JPane createCenterPane( @NotNull final MegapoliPlot megapoliPlot, @NotNull final Integer maxWidth, @NotNull final Integer maxHeight, @Nullable final Locale locale )
256            throws ServiceException
257    {
258        final Integer plotWidth = Math.min( maxWidth - MARGIN_LEFT_RIGHT, maxWidth );
259        final Integer plotHeight = Math.min( maxHeight - MARGIN_LEFT_RIGHT, maxHeight );
260
261        if( megapoliPlot.isTimeSerie() )
262            return createTimeSeriePlot( megapoliPlot, plotWidth, plotHeight, locale );
263        else
264            return create2DPlot( megapoliPlot, plotWidth, plotHeight, locale );
265    }
266
267    /**
268     * This method transform a jPane to a jLabel with a BufferedImage inside and a black border
269     *
270     * @param jPane
271     * @return
272     */
273    @NotNull
274    private JLabel transformJPaneToJLabel( @NotNull final JPane jPane )
275    {
276        final BufferedImage jPaneBufferedImage = new BufferedImage( jPane.getWidth(), jPane.getHeight(), BufferedImage.TYPE_INT_ARGB );
277        final Graphics2D graphics2D = jPaneBufferedImage.createGraphics();
278        jPane.addNotify();
279        jPane.validate();
280        jPane.draw( graphics2D );
281
282        final ImageIcon imageIcon = new ImageIcon( jPaneBufferedImage );
283        final JLabel jLabelPlot = new JLabel( imageIcon, JLabel.CENTER );
284        jLabelPlot.setBorder( BorderFactory.createLineBorder( Color.black, 1 ) );
285        return jLabelPlot;
286    }
287
288    @NotNull
289    private String formatTitle( @Nullable final String title )
290    {
291        if( null == title )
292            return "";
293        else
294        {
295            final String formatTitle = title.replaceAll( "\\n", "<br>" );
296            return "<html>" + formatTitle + "</html>";
297        }
298    }
299
300    private static final Log LOGGER = LogFactory.getLog( EtherPlotServiceImpl.class );
301}
Note: See TracBrowser for help on using the repository browser.