1 | package com.ether; |
---|
2 | |
---|
3 | import com.medias.megapoli.utils.MegapoliInitialisation; |
---|
4 | import gov.noaa.pmel.sgt.JPane; |
---|
5 | import org.apache.commons.logging.Log; |
---|
6 | import org.apache.commons.logging.LogFactory; |
---|
7 | import org.jetbrains.annotations.NotNull; |
---|
8 | import org.jetbrains.annotations.Nullable; |
---|
9 | |
---|
10 | import javax.swing.*; |
---|
11 | import java.awt.*; |
---|
12 | import java.awt.image.BufferedImage; |
---|
13 | import java.util.Calendar; |
---|
14 | import java.util.Locale; |
---|
15 | import java.util.ResourceBundle; |
---|
16 | |
---|
17 | /** |
---|
18 | * @author vmipsl |
---|
19 | * @date 05 july 2011 |
---|
20 | */ |
---|
21 | public 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 | if( megapoliPlot.isTimeSerie() ) |
---|
90 | { |
---|
91 | final JLabel jLabelPlot = transformJPaneToJLabel( jPaneCenter, true ); |
---|
92 | |
---|
93 | final JPane jPaneCenterToImage = new JPane( "Center jPane", new Dimension( jLabelPlot.getWidth(), jLabelPlot.getHeight() ) ); |
---|
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 | else |
---|
104 | { |
---|
105 | final JLabel jLabelPlot2D = transformJPaneToJLabel( jPaneCenter, true ); |
---|
106 | // final JPane keyPane = ( (JPlotLayout) jPaneCenter ).getKeyPane(); |
---|
107 | // final JLabel jLabelPlotLegend = transformJPaneToJLabel( keyPane, false ); |
---|
108 | // final JPane jPane2D = new JPane( "2D jPane", new Dimension( jPaneCenter.getWidth(), jPaneCenter.getHeight() + keyPane.getHeight() ) ); |
---|
109 | // jPane2D.setLayout( new BorderLayout() ); |
---|
110 | // jPane2D.setBackground( Color.white ); |
---|
111 | // jPane2D.add( jLabelPlot2D, BorderLayout.SOUTH ); |
---|
112 | // jPane2D.add( jLabelPlotLegend, BorderLayout.NORTH ); |
---|
113 | // jPane2D.setVisible( true ); |
---|
114 | // jPane2D.setOpaque( true ); |
---|
115 | // jPane2D.validate(); |
---|
116 | |
---|
117 | // final JPanelToImageManager jPanelToImageManager = new JPanelToImageManager( jPane2D ); |
---|
118 | // final BufferedImage bufferedImage = jPanelToImageManager.saveAsImage(); |
---|
119 | // |
---|
120 | // final ImageIcon imageIcon = new ImageIcon( bufferedImage ); |
---|
121 | // final JLabel jLabelPlot = new JLabel( imageIcon, JLabel.CENTER ); |
---|
122 | // jLabelPlot.setBorder( BorderFactory.createLineBorder( Color.black, 1 ) ); |
---|
123 | // |
---|
124 | final JPane jPaneCenterToImage = new JPane( "Center jPane", new Dimension( jLabelPlot2D.getWidth(), jLabelPlot2D.getHeight() ) ); |
---|
125 | jPaneCenterToImage.setLayout( new GridBagLayout() ); |
---|
126 | jPaneCenterToImage.add( jLabelPlot2D, |
---|
127 | new GridBagConstraints( 0, 0, 1, 1, 0, 0, |
---|
128 | GridBagConstraints.CENTER, |
---|
129 | GridBagConstraints.CENTER, |
---|
130 | new Insets( 0, 0, 0, 0 ), 0, 0 ) ); |
---|
131 | |
---|
132 | jPane.add( jPaneCenterToImage, BorderLayout.CENTER ); |
---|
133 | } |
---|
134 | } |
---|
135 | } |
---|
136 | else |
---|
137 | { |
---|
138 | final JPane jPaneCenter = createEmptyCenterPane( locale ); |
---|
139 | jPane.add( jPaneCenter, BorderLayout.CENTER ); |
---|
140 | } |
---|
141 | |
---|
142 | final JPanelToImageManager jPanelToImageManager = new JPanelToImageManager( jPane ); |
---|
143 | return jPanelToImageManager.saveAsImage(); |
---|
144 | } |
---|
145 | |
---|
146 | private JLabel transformJPaneToJLabel( final JPane jPane, final boolean needeBorder ) |
---|
147 | { |
---|
148 | final BufferedImage jPaneBufferedImage = new BufferedImage( jPane.getWidth(), jPane.getHeight(), BufferedImage.TYPE_INT_ARGB ); |
---|
149 | final Graphics2D graphics2D = jPaneBufferedImage.createGraphics(); |
---|
150 | jPane.addNotify(); |
---|
151 | jPane.validate(); |
---|
152 | jPane.draw( graphics2D ); |
---|
153 | |
---|
154 | final ImageIcon imageIcon = new ImageIcon( jPaneBufferedImage ); |
---|
155 | final JLabel jLabelPlot = new JLabel( imageIcon, JLabel.CENTER ); |
---|
156 | if( needeBorder ) |
---|
157 | jLabelPlot.setBorder( BorderFactory.createLineBorder( Color.black, 1 ) ); |
---|
158 | return jLabelPlot; |
---|
159 | } |
---|
160 | |
---|
161 | /** |
---|
162 | * Create the top JPane with the logos and title |
---|
163 | * |
---|
164 | * @param megapoliPlot |
---|
165 | * @return |
---|
166 | */ |
---|
167 | @NotNull |
---|
168 | public JPane createTopPane( @Nullable final MegapoliPlot megapoliPlot ) |
---|
169 | { |
---|
170 | // Logos |
---|
171 | final ImageIcon logoMegapoli = new ImageIcon( megapoliPlot.getLogoMegapoli() ); |
---|
172 | final JLabel jLabelLogoMegapoli = new JLabel( logoMegapoli ); |
---|
173 | |
---|
174 | final ImageIcon logoEther = new ImageIcon( megapoliPlot.getLogoEther() ); |
---|
175 | final JLabel jLabelLogoEther = new JLabel( logoEther ); |
---|
176 | |
---|
177 | // Max height |
---|
178 | final Integer maxHeights = Math.max( logoMegapoli.getIconHeight(), logoEther.getIconHeight() ); |
---|
179 | |
---|
180 | // jPane |
---|
181 | final JPane jPaneTop = new JPane( "Top jPane", new Dimension( 0, maxHeights ) ); |
---|
182 | jPaneTop.setLayout( new BorderLayout() ); |
---|
183 | |
---|
184 | jPaneTop.add( jLabelLogoMegapoli, BorderLayout.LINE_START ); |
---|
185 | jPaneTop.add( jLabelLogoEther, BorderLayout.LINE_END ); |
---|
186 | |
---|
187 | |
---|
188 | // Title |
---|
189 | final String formatedTitle = formatTitle( megapoliPlot.getTitle() ); |
---|
190 | final JLabel jLabelTitle = new JLabel( formatedTitle, JLabel.CENTER ); |
---|
191 | final Font police = new Font( "Arial", Font.BOLD, TITLE_FONT_SIZE ); |
---|
192 | jLabelTitle.setFont( police ); |
---|
193 | jPaneTop.add( jLabelTitle, BorderLayout.CENTER ); |
---|
194 | |
---|
195 | return jPaneTop; |
---|
196 | } |
---|
197 | |
---|
198 | /** |
---|
199 | * Create the bottom JPane with the text "published .. (date)" |
---|
200 | * |
---|
201 | * @param dataNumber : the number of datas extracted from the base |
---|
202 | * @param locale |
---|
203 | * @return |
---|
204 | */ |
---|
205 | @NotNull |
---|
206 | public JPane createBottomPane( @Nullable final Integer dataNumber, @Nullable final Locale locale ) |
---|
207 | { |
---|
208 | final ResourceBundle bundle = WebHelper.getBundle( locale ); |
---|
209 | |
---|
210 | final JPane jPaneBottom = new JPane(); |
---|
211 | jPaneBottom.setLayout( new BorderLayout() ); |
---|
212 | |
---|
213 | // Published |
---|
214 | final Calendar calendar = Calendar.getInstance(); |
---|
215 | final String messagePublished = bundle.getString( "plot.published" ); |
---|
216 | final JLabel jLabel = new JLabel( messagePublished + ' ' + DateHelper.formatDate( calendar.getTime(), DateHelper.ENGLISH_DATE_PATTERN ) + " " ); |
---|
217 | final Font police = new Font( "Arial", Font.BOLD, FONT_SIZE ); |
---|
218 | jLabel.setFont( police ); |
---|
219 | jPaneBottom.add( jLabel, BorderLayout.EAST ); |
---|
220 | |
---|
221 | if( null != dataNumber ) |
---|
222 | { |
---|
223 | final String messageDataNumber = bundle.getString( "plot.dataNumber" ); |
---|
224 | final JLabel jLabelDataNumber = new JLabel( messageDataNumber + ' ' + dataNumber ); |
---|
225 | jLabelDataNumber.setFont( police ); |
---|
226 | jPaneBottom.add( jLabelDataNumber, BorderLayout.WEST ); |
---|
227 | } |
---|
228 | |
---|
229 | return jPaneBottom; |
---|
230 | } |
---|
231 | |
---|
232 | /** |
---|
233 | * Create an error JPane when plot can be created |
---|
234 | * |
---|
235 | * @param mainError |
---|
236 | * @param errorText |
---|
237 | * @param locale |
---|
238 | * @return |
---|
239 | */ |
---|
240 | @NotNull |
---|
241 | public BufferedImage createErrorPane( @NotNull final String mainError, @Nullable final String errorText, @Nullable final Locale locale ) |
---|
242 | { |
---|
243 | // Image |
---|
244 | final ImageIcon errorImage = new ImageIcon( MegapoliInitialisation.pathImages + "/erreur.gif" ); |
---|
245 | final JLabel jLabelErrorImage = new JLabel( errorImage ); |
---|
246 | |
---|
247 | // Text |
---|
248 | final ResourceBundle bundle = WebHelper.getBundle( locale ); |
---|
249 | final String mainMessage = bundle.getString( "plot.errorMessage" ); |
---|
250 | |
---|
251 | final JLabel jLabelError; |
---|
252 | if( null != errorText ) |
---|
253 | jLabelError = new JLabel( "<html>" + mainMessage + "<br><br>" + mainError + "<br>" + errorText + "</html>", JLabel.CENTER ); |
---|
254 | else |
---|
255 | jLabelError = new JLabel( "<html>" + mainMessage + "<br><br>" + mainError + "</html>", JLabel.CENTER ); |
---|
256 | |
---|
257 | final Font police = new Font( "Arial", Font.BOLD, ERROR_FONT_SIZE ); |
---|
258 | jLabelError.setFont( police ); |
---|
259 | |
---|
260 | // jPane |
---|
261 | final JPane jPane = new JPane( "jPane", new Dimension( MAX_WIDTH, errorImage.getIconHeight() ) ); |
---|
262 | jPane.setLayout( new BorderLayout() ); |
---|
263 | jPane.setBackground( Color.white ); |
---|
264 | jPane.setOpaque( true ); |
---|
265 | jPane.add( jLabelErrorImage, BorderLayout.WEST ); |
---|
266 | jPane.add( jLabelError, BorderLayout.CENTER ); |
---|
267 | |
---|
268 | final JPanelToImageManager jPanelToImageManager = new JPanelToImageManager( jPane ); |
---|
269 | return jPanelToImageManager.saveAsImage(); |
---|
270 | } |
---|
271 | |
---|
272 | /** |
---|
273 | * Create the center JPane with no graph (if no value) |
---|
274 | * |
---|
275 | * @param locale |
---|
276 | * @return |
---|
277 | */ |
---|
278 | @NotNull |
---|
279 | public JPane createEmptyCenterPane( @Nullable final Locale locale ) |
---|
280 | { |
---|
281 | final ResourceBundle bundle = WebHelper.getBundle( locale ); |
---|
282 | final String messageData = bundle.getString( "plot.noData" ); |
---|
283 | |
---|
284 | final JPane jPaneCenter = new JPane(); |
---|
285 | jPaneCenter.setLayout( new BorderLayout() ); |
---|
286 | |
---|
287 | final JLabel jLabelData = new JLabel( messageData, JLabel.CENTER ); |
---|
288 | final Font police = new Font( "Arial", Font.BOLD, FONT_SIZE ); |
---|
289 | jLabelData.setFont( police ); |
---|
290 | |
---|
291 | jPaneCenter.add( jLabelData, BorderLayout.CENTER ); |
---|
292 | return jPaneCenter; |
---|
293 | } |
---|
294 | |
---|
295 | /** |
---|
296 | * Create the center JPane with the graph |
---|
297 | * |
---|
298 | * @param megapoliPlot |
---|
299 | * @param maxWidth |
---|
300 | * @param maxHeight |
---|
301 | * @return |
---|
302 | */ |
---|
303 | @Nullable |
---|
304 | public JPane createCenterPane( @NotNull final MegapoliPlot megapoliPlot, @NotNull final Integer maxWidth, @NotNull final Integer maxHeight, @Nullable final Locale locale ) |
---|
305 | throws ServiceException |
---|
306 | { |
---|
307 | final Integer plotWidth = Math.min( maxWidth - MARGIN_LEFT_RIGHT, maxWidth ); |
---|
308 | final Integer plotHeight = Math.min( maxHeight - MARGIN_LEFT_RIGHT, maxHeight ); |
---|
309 | |
---|
310 | if( megapoliPlot.isTimeSerie() ) |
---|
311 | return createTimeSeriePlot( megapoliPlot, plotWidth, plotHeight, locale ); |
---|
312 | else |
---|
313 | return create2DPlot( megapoliPlot, plotWidth, plotHeight, locale ); |
---|
314 | } |
---|
315 | |
---|
316 | @NotNull |
---|
317 | private String formatTitle( @Nullable final String title ) |
---|
318 | { |
---|
319 | if( null == title ) |
---|
320 | return ""; |
---|
321 | else |
---|
322 | { |
---|
323 | final String formatTitle = title.replaceAll( "\\n", "<br>" ); |
---|
324 | return "<html>" + formatTitle + "</html>"; |
---|
325 | } |
---|
326 | } |
---|
327 | |
---|
328 | private static final Log LOGGER = LogFactory.getLog( EtherPlotServiceImpl.class ); |
---|
329 | } |
---|