1 | package com.ether; |
---|
2 | |
---|
3 | import gov.noaa.pmel.sgt.JPane; |
---|
4 | import gov.noaa.pmel.util.Dimension2D; |
---|
5 | import gov.noaa.pmel.util.Point2D; |
---|
6 | import org.apache.commons.logging.Log; |
---|
7 | import org.apache.commons.logging.LogFactory; |
---|
8 | import org.jetbrains.annotations.NotNull; |
---|
9 | import org.jetbrains.annotations.Nullable; |
---|
10 | import org.medias.megapoli.utils.MegapoliInitialisation; |
---|
11 | |
---|
12 | import javax.swing.*; |
---|
13 | import java.awt.*; |
---|
14 | import java.awt.image.BufferedImage; |
---|
15 | import java.util.Calendar; |
---|
16 | import java.util.Locale; |
---|
17 | import java.util.ResourceBundle; |
---|
18 | |
---|
19 | /** |
---|
20 | * @author vmipsl |
---|
21 | * @date 05 july 2011 |
---|
22 | */ |
---|
23 | public class EtherPlotServiceImpl |
---|
24 | implements EtherPlotService |
---|
25 | { |
---|
26 | public static Integer getMaxWidth() |
---|
27 | { |
---|
28 | final Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize(); |
---|
29 | return Math.min( Double.valueOf( screenSize.getWidth() ).intValue(), MAX_WIDTH ); |
---|
30 | } |
---|
31 | |
---|
32 | public static Integer getMaxHeight() |
---|
33 | { |
---|
34 | final Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize(); |
---|
35 | return Math.min( Double.valueOf( screenSize.getHeight() ).intValue(), MAX_HEIGHT ); |
---|
36 | } |
---|
37 | |
---|
38 | /** |
---|
39 | * Create the main JPane with the 3 jPanes |
---|
40 | * |
---|
41 | * @param megapoliPlot |
---|
42 | * @param locale |
---|
43 | * @return |
---|
44 | */ |
---|
45 | @NotNull |
---|
46 | public BufferedImage createMainPane( @NotNull final MegapoliPlot megapoliPlot, @Nullable final Locale locale ) |
---|
47 | { |
---|
48 | final Integer maxWidth = getMaxWidth(); |
---|
49 | final Integer maxHeight = getMaxHeight(); |
---|
50 | |
---|
51 | final JPane jPane = new JPane( "Main jPane", new Dimension( maxWidth, maxHeight ) ); |
---|
52 | jPane.setLayout( new BorderLayout() ); |
---|
53 | jPane.setBackground( Color.white ); |
---|
54 | jPane.setOpaque( true ); |
---|
55 | |
---|
56 | // Top Pane |
---|
57 | final JPane jPaneTop = createTopPane( megapoliPlot ); |
---|
58 | jPane.add( jPaneTop, BorderLayout.NORTH ); |
---|
59 | |
---|
60 | final Integer valuesNumber = megapoliPlot.getData().getYArray().length; |
---|
61 | |
---|
62 | // Bottom Pane |
---|
63 | final JPane jPaneBottom = createBottomPane( valuesNumber, locale ); |
---|
64 | jPane.add( jPaneBottom, BorderLayout.SOUTH ); |
---|
65 | |
---|
66 | // Center Pane |
---|
67 | final Integer centerHeight = maxHeight - jPaneTop.getHeight() - jPaneBottom.getHeight(); |
---|
68 | final JPane jPaneCenter; |
---|
69 | if( valuesNumber > 0 ) |
---|
70 | jPaneCenter = createCenterPane( megapoliPlot, maxWidth, centerHeight ); |
---|
71 | else |
---|
72 | jPaneCenter = createEmptyCenterPane( locale ); |
---|
73 | jPane.add( jPaneCenter, BorderLayout.CENTER ); |
---|
74 | |
---|
75 | |
---|
76 | final JPanelToImageManager jPanelToImageManager = new JPanelToImageManager( jPane ); |
---|
77 | return jPanelToImageManager.saveAsImage(); |
---|
78 | } |
---|
79 | |
---|
80 | /** |
---|
81 | * Create the top JPane with the logos and title |
---|
82 | * |
---|
83 | * @param megapoliPlot |
---|
84 | * @return |
---|
85 | */ |
---|
86 | @NotNull |
---|
87 | public JPane createTopPane( @Nullable final MegapoliPlot megapoliPlot ) |
---|
88 | { |
---|
89 | // Logos |
---|
90 | final ImageIcon logoMegapoli = new ImageIcon( megapoliPlot.getLogoMegapoli() ); |
---|
91 | final JLabel jLabelLogoMegapoli = new JLabel( logoMegapoli ); |
---|
92 | |
---|
93 | final ImageIcon logoEther = new ImageIcon( megapoliPlot.getLogoEther() ); |
---|
94 | final JLabel jLabelLogoEther = new JLabel( logoEther ); |
---|
95 | |
---|
96 | // Max height |
---|
97 | final Integer maxHeights = Math.max( logoMegapoli.getIconHeight(), logoEther.getIconHeight() ); |
---|
98 | |
---|
99 | // jPane |
---|
100 | final JPane jPaneTop = new JPane( "Top jPane", new Dimension( 0, maxHeights ) ); |
---|
101 | jPaneTop.setLayout( new BorderLayout() ); |
---|
102 | |
---|
103 | jPaneTop.add( jLabelLogoMegapoli, BorderLayout.LINE_START ); |
---|
104 | jPaneTop.add( jLabelLogoEther, BorderLayout.LINE_END ); |
---|
105 | |
---|
106 | |
---|
107 | // Title |
---|
108 | final String formatedTitle = formatTitle( megapoliPlot.getTitle() ); |
---|
109 | final JLabel jLabelTitle = new JLabel( formatedTitle, JLabel.CENTER ); |
---|
110 | final Font police = new Font( "Arial", Font.BOLD, TITLE_FONT_SIZE ); |
---|
111 | jLabelTitle.setFont( police ); |
---|
112 | jPaneTop.add( jLabelTitle, BorderLayout.CENTER ); |
---|
113 | |
---|
114 | return jPaneTop; |
---|
115 | } |
---|
116 | |
---|
117 | /** |
---|
118 | * Create the center JPane with the graph |
---|
119 | * |
---|
120 | * @param megapoliPlot |
---|
121 | * @param maxWidth |
---|
122 | * @param maxHeight |
---|
123 | * @return |
---|
124 | */ |
---|
125 | @NotNull |
---|
126 | public JPane createCenterPane( @NotNull final MegapoliPlot megapoliPlot, @NotNull final Integer maxWidth, @NotNull final Integer maxHeight ) |
---|
127 | { |
---|
128 | final Integer plotWidth = Math.min( maxWidth - MARGIN_LEFT_RIGHT, maxWidth ); |
---|
129 | final Integer plotHeight = Math.min( maxHeight - MARGIN_LEFT_RIGHT, maxHeight ); |
---|
130 | |
---|
131 | // Graph |
---|
132 | final JPlotLayoutEther jPlotLayout = new JPlotLayoutEther( false, megapoliPlot.isXTime(), false, "Trajectory data", null, megapoliPlot.isLegendToHide() ); |
---|
133 | jPlotLayout.setTitles( "", "", "" ); |
---|
134 | |
---|
135 | // Replace legend |
---|
136 | final String legend = megapoliPlot.getData().getTitle(); |
---|
137 | final Dimension2D layerSizeP = jPlotLayout.getLayerSizeP(); |
---|
138 | final double factorForPlotWidthInPhysicalUnits = layerSizeP.getWidth() / Double.valueOf( plotWidth ); |
---|
139 | final double legendSizeInPhysicalUnits = legend.length() * factorForPlotWidthInPhysicalUnits * 7; |
---|
140 | jPlotLayout.setKeyLocationP( new Point2D.Double( layerSizeP.getWidth() - legendSizeInPhysicalUnits, layerSizeP.getHeight() - 0.1 ) ); |
---|
141 | |
---|
142 | // Add data and legend |
---|
143 | jPlotLayout.addData( megapoliPlot.getData(), legend ); |
---|
144 | jPlotLayout.setSize( plotWidth, plotHeight ); |
---|
145 | jPlotLayout.setVisible( true ); |
---|
146 | |
---|
147 | jPlotLayout.addNotify(); |
---|
148 | jPlotLayout.validate(); |
---|
149 | jPlotLayout.setOpaque( true ); |
---|
150 | |
---|
151 | // TODO : remove this change to bufferedImage to insert directly the jPlotLayout !!!! |
---|
152 | final BufferedImage bufferedImage = new BufferedImage( jPlotLayout.getWidth(), jPlotLayout.getHeight(), BufferedImage.TYPE_INT_RGB ); |
---|
153 | final Graphics2D graphics2D = bufferedImage.createGraphics(); |
---|
154 | jPlotLayout.draw( graphics2D ); |
---|
155 | |
---|
156 | final ImageIcon imageIcon = new ImageIcon( bufferedImage ); |
---|
157 | final JLabel jLabelPlot = new JLabel( imageIcon, JLabel.CENTER ); |
---|
158 | jLabelPlot.setBorder( BorderFactory.createLineBorder( Color.black, 1 ) ); |
---|
159 | |
---|
160 | final JPane jPaneCenter = new JPane( "Center jPane", new Dimension( jPlotLayout.getWidth(), jPlotLayout.getHeight() ) ); |
---|
161 | jPaneCenter.setLayout( new GridBagLayout() ); |
---|
162 | jPaneCenter.add( jLabelPlot, |
---|
163 | new GridBagConstraints( 0, 0, 1, 1, 0, 0, |
---|
164 | GridBagConstraints.CENTER, |
---|
165 | GridBagConstraints.CENTER, |
---|
166 | new Insets( 0, 0, 0, 0 ), 0, 0 ) ); |
---|
167 | |
---|
168 | return jPaneCenter; |
---|
169 | } |
---|
170 | |
---|
171 | /** |
---|
172 | * Create the center JPane with no graph (if no value) |
---|
173 | * |
---|
174 | * @param locale |
---|
175 | * @return |
---|
176 | */ |
---|
177 | public JPane createEmptyCenterPane( @Nullable final Locale locale ) |
---|
178 | { |
---|
179 | final ResourceBundle bundle = WebHelper.getBundle( locale ); |
---|
180 | final String messageData = bundle.getString( "plot.noData" ); |
---|
181 | |
---|
182 | final JPane jPaneCenter = new JPane(); |
---|
183 | jPaneCenter.setLayout( new BorderLayout() ); |
---|
184 | |
---|
185 | final JLabel jLabelData = new JLabel( messageData, JLabel.CENTER ); |
---|
186 | final Font police = new Font( "Arial", Font.BOLD, FONT_SIZE ); |
---|
187 | jLabelData.setFont( police ); |
---|
188 | |
---|
189 | jPaneCenter.add( jLabelData, BorderLayout.CENTER ); |
---|
190 | return jPaneCenter; |
---|
191 | } |
---|
192 | |
---|
193 | /** |
---|
194 | * Create the bottom JPane with the text "published .. (date)" |
---|
195 | * |
---|
196 | * @param dataNumber : the number of datas extracted from the base |
---|
197 | * @param locale |
---|
198 | * @return |
---|
199 | */ |
---|
200 | @NotNull |
---|
201 | public JPane createBottomPane( @Nullable final Integer dataNumber, @Nullable final Locale locale ) |
---|
202 | { |
---|
203 | final ResourceBundle bundle = WebHelper.getBundle( locale ); |
---|
204 | |
---|
205 | final JPane jPaneBottom = new JPane(); |
---|
206 | jPaneBottom.setLayout( new BorderLayout() ); |
---|
207 | |
---|
208 | // Published |
---|
209 | final Calendar calendar = Calendar.getInstance(); |
---|
210 | final String messagePublished = bundle.getString( "plot.published" ); |
---|
211 | final JLabel jLabel = new JLabel( messagePublished + ' ' + DateHelper.formatDate( calendar.getTime(), DateHelper.ENGLISH_DATE_PATTERN ) + " " ); |
---|
212 | final Font police = new Font( "Arial", Font.BOLD, FONT_SIZE ); |
---|
213 | jLabel.setFont( police ); |
---|
214 | jPaneBottom.add( jLabel, BorderLayout.EAST ); |
---|
215 | |
---|
216 | if( null != dataNumber ) |
---|
217 | { |
---|
218 | final String messageDataNumber = bundle.getString( "plot.dataNumber" ); |
---|
219 | final JLabel jLabelDataNumber = new JLabel( messageDataNumber + ' ' + dataNumber ); |
---|
220 | jLabelDataNumber.setFont( police ); |
---|
221 | jPaneBottom.add( jLabelDataNumber, BorderLayout.WEST ); |
---|
222 | } |
---|
223 | |
---|
224 | return jPaneBottom; |
---|
225 | } |
---|
226 | |
---|
227 | /** |
---|
228 | * Create an error JPane when plot can be created |
---|
229 | * |
---|
230 | * @param mainError |
---|
231 | * @param errorText |
---|
232 | * @param locale |
---|
233 | * @return |
---|
234 | */ |
---|
235 | @NotNull |
---|
236 | public BufferedImage createErrorPane( @NotNull final String mainError, @Nullable final String errorText, @Nullable final Locale locale ) |
---|
237 | { |
---|
238 | // Image |
---|
239 | final ImageIcon errorImage = new ImageIcon( MegapoliInitialisation.pathImages + "/erreur.gif" ); |
---|
240 | final JLabel jLabelErrorImage = new JLabel( errorImage ); |
---|
241 | |
---|
242 | // Text |
---|
243 | final ResourceBundle bundle = WebHelper.getBundle( locale ); |
---|
244 | final String mainMessage = bundle.getString( "plot.errorMessage" ); |
---|
245 | |
---|
246 | final JLabel jLabelError; |
---|
247 | if( null != errorText ) |
---|
248 | jLabelError = new JLabel( "<html>" + mainMessage + "<br><br>" + mainError + "<br>" + errorText + "</html>", JLabel.CENTER ); |
---|
249 | else |
---|
250 | jLabelError = new JLabel( "<html>" + mainMessage + "<br><br>" + mainError + "</html>", JLabel.CENTER ); |
---|
251 | |
---|
252 | final Font police = new Font( "Arial", Font.BOLD, ERROR_FONT_SIZE ); |
---|
253 | jLabelError.setFont( police ); |
---|
254 | |
---|
255 | // jPane |
---|
256 | final JPane jPane = new JPane( "jPane", new Dimension( MAX_WIDTH, errorImage.getIconHeight() ) ); |
---|
257 | jPane.setLayout( new BorderLayout() ); |
---|
258 | jPane.setBackground( Color.white ); |
---|
259 | jPane.setOpaque( true ); |
---|
260 | jPane.add( jLabelErrorImage, BorderLayout.WEST ); |
---|
261 | jPane.add( jLabelError, BorderLayout.CENTER ); |
---|
262 | |
---|
263 | final JPanelToImageManager jPanelToImageManager = new JPanelToImageManager( jPane ); |
---|
264 | return jPanelToImageManager.saveAsImage(); |
---|
265 | } |
---|
266 | |
---|
267 | @NotNull |
---|
268 | private String formatTitle( @Nullable final String title ) |
---|
269 | { |
---|
270 | if( null == title ) |
---|
271 | return ""; |
---|
272 | else |
---|
273 | { |
---|
274 | final String formatTitle = title.replaceAll( "\\n", "<br>" ); |
---|
275 | return "<html>" + formatTitle + "</html>"; |
---|
276 | } |
---|
277 | } |
---|
278 | |
---|
279 | private static final Log LOGGER = LogFactory.getLog( EtherPlotServiceImpl.class ); |
---|
280 | |
---|
281 | // Dimensions of the jPanes |
---|
282 | private static final int MAX_WIDTH = 800; |
---|
283 | private static final int MAX_HEIGHT = 700; |
---|
284 | private static final int MARGIN_LEFT_RIGHT = 50; |
---|
285 | |
---|
286 | private static final int TITLE_FONT_SIZE = 20; |
---|
287 | private static final int FONT_SIZE = 12; |
---|
288 | private static final int ERROR_FONT_SIZE = 10; |
---|
289 | } |
---|