source: ether_statistics/service/implementation/com/ether/EtherPlotServiceImpl.java @ 569

Last change on this file since 569 was 569, checked in by vmipsl, 12 years ago

Nouveau projet

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