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

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

Servlet _ TimeSerie? :

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