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

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