1 | package fr.mael.jiwigo.ui.browser; |
---|
2 | |
---|
3 | import java.awt.AWTEvent; |
---|
4 | import java.awt.BorderLayout; |
---|
5 | import java.awt.Dimension; |
---|
6 | import java.awt.FlowLayout; |
---|
7 | import java.awt.Toolkit; |
---|
8 | import java.awt.event.AWTEventListener; |
---|
9 | import java.awt.event.ActionEvent; |
---|
10 | import java.awt.event.ActionListener; |
---|
11 | import java.awt.event.KeyEvent; |
---|
12 | import java.awt.image.BufferedImage; |
---|
13 | import java.awt.print.PrinterException; |
---|
14 | import java.awt.print.PrinterJob; |
---|
15 | import java.io.File; |
---|
16 | import java.io.IOException; |
---|
17 | import java.net.URL; |
---|
18 | |
---|
19 | import javax.imageio.ImageIO; |
---|
20 | import javax.swing.ImageIcon; |
---|
21 | import javax.swing.JButton; |
---|
22 | import javax.swing.JFileChooser; |
---|
23 | import javax.swing.JMenu; |
---|
24 | import javax.swing.JMenuBar; |
---|
25 | import javax.swing.JMenuItem; |
---|
26 | import javax.swing.JPanel; |
---|
27 | import javax.swing.JScrollPane; |
---|
28 | |
---|
29 | import fr.mael.jiwigo.transverse.ImagesManagement; |
---|
30 | import fr.mael.jiwigo.transverse.util.ImagesUtil; |
---|
31 | import fr.mael.jiwigo.transverse.util.Messages; |
---|
32 | import fr.mael.jiwigo.transverse.util.Outil; |
---|
33 | import fr.mael.jiwigo.ui.comments.CommentsDialog; |
---|
34 | import fr.mael.jiwigo.ui.mainframe.MainFrame; |
---|
35 | |
---|
36 | /** |
---|
37 | * |
---|
38 | Copyright (c) 2010, Mael |
---|
39 | All rights reserved. |
---|
40 | |
---|
41 | Redistribution and use in source and binary forms, with or without |
---|
42 | modification, are permitted provided that the following conditions are met: |
---|
43 | * Redistributions of source code must retain the above copyright |
---|
44 | notice, this list of conditions and the following disclaimer. |
---|
45 | * Redistributions in binary form must reproduce the above copyright |
---|
46 | notice, this list of conditions and the following disclaimer in the |
---|
47 | documentation and/or other materials provided with the distribution. |
---|
48 | * Neither the name of jiwigo nor the |
---|
49 | names of its contributors may be used to endorse or promote products |
---|
50 | derived from this software without specific prior written permission. |
---|
51 | |
---|
52 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
---|
53 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
---|
54 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
---|
55 | DISCLAIMED. IN NO EVENT SHALL Mael BE LIABLE FOR ANY |
---|
56 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
---|
57 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
---|
58 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
---|
59 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
---|
60 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
---|
61 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
62 | |
---|
63 | * @author mael |
---|
64 | * |
---|
65 | * Frame de navigation dans les images |
---|
66 | * |
---|
67 | */ |
---|
68 | public class BrowserPanel extends JPanel implements ActionListener { |
---|
69 | /** |
---|
70 | * Logger |
---|
71 | */ |
---|
72 | public static final org.apache.commons.logging.Log LOG = org.apache.commons.logging.LogFactory |
---|
73 | .getLog(BrowserPanel.class); |
---|
74 | /** |
---|
75 | * bouton to go to the next image |
---|
76 | */ |
---|
77 | private JButton next = new JButton(); |
---|
78 | /** |
---|
79 | * bouton to go to the previous image |
---|
80 | */ |
---|
81 | private JButton previous = new JButton(); |
---|
82 | /** |
---|
83 | * rotation on the left |
---|
84 | */ |
---|
85 | private JButton rotateLeft = new JButton(); |
---|
86 | /** |
---|
87 | * rotation on the right |
---|
88 | */ |
---|
89 | private JButton rotateRight = new JButton(); |
---|
90 | /** |
---|
91 | * saves the image |
---|
92 | */ |
---|
93 | private JButton save = new JButton(); |
---|
94 | /** |
---|
95 | * displays the comments for the current image |
---|
96 | */ |
---|
97 | private JButton comment = new JButton(); |
---|
98 | /** |
---|
99 | * panel that contains the previous buttons |
---|
100 | */ |
---|
101 | private JPanel panelBoutons; |
---|
102 | |
---|
103 | /** |
---|
104 | * clip the image |
---|
105 | */ |
---|
106 | private JButton cut = new JButton(); |
---|
107 | |
---|
108 | /** |
---|
109 | * Panel containing the image |
---|
110 | */ |
---|
111 | private BrowserImagePanel imagePanel; |
---|
112 | |
---|
113 | /** |
---|
114 | * Menus |
---|
115 | */ |
---|
116 | private JMenuBar menuBar; |
---|
117 | private JMenu menuFichier, menuAffichage, menuImage; |
---|
118 | private JMenuItem menuItemSave, menuItemPrint, menuItemClose, menuItemZoomPlus; |
---|
119 | private JMenuItem menuItemZoomMoins, menuItemNormal, menuItemFlipH, menuItemFlipV; |
---|
120 | private JMenuItem menuItemRotationG, menuItemRotationD; |
---|
121 | |
---|
122 | /** |
---|
123 | * Listener général. Permet d'ajouter les écouteurs sur les touches |
---|
124 | * pour la navigation entres les différentes images grace aux touches |
---|
125 | * droite et gauche. |
---|
126 | * Je n'ai pas trouvé mieux que de faire ce listener général. |
---|
127 | * Il est instancié et ajouté à la création de la frame, et retiré lorsqu'on la quitte |
---|
128 | */ |
---|
129 | @Deprecated |
---|
130 | private AWTEventListener generalListener; |
---|
131 | |
---|
132 | /** |
---|
133 | * Instance of the browser frame |
---|
134 | */ |
---|
135 | private static BrowserPanel instance; |
---|
136 | |
---|
137 | public static BrowserPanel getInstance() { |
---|
138 | if (instance == null) { |
---|
139 | instance = new BrowserPanel(); |
---|
140 | } |
---|
141 | if (instance.getPanelBoutons() == null && ImagesManagement.getInstance().getCurrentImage() != null) { |
---|
142 | instance.construct(); |
---|
143 | } |
---|
144 | return instance; |
---|
145 | } |
---|
146 | |
---|
147 | /** |
---|
148 | * @return the panelBoutons |
---|
149 | */ |
---|
150 | public JPanel getPanelBoutons() { |
---|
151 | return panelBoutons; |
---|
152 | } |
---|
153 | |
---|
154 | private BrowserPanel() { |
---|
155 | } |
---|
156 | |
---|
157 | public void construct() { |
---|
158 | // this.setIconImage(java.awt.Toolkit.getDefaultToolkit().getImage(Outil.getURL("fr/mael/jiwigo/img/icon.png"))); |
---|
159 | // this.setTitle(Messages.getMessage("titleBrowser")); |
---|
160 | panelBoutons = new JPanel(new FlowLayout()); |
---|
161 | //ajout des images sur les boutons |
---|
162 | next.setIcon(new ImageIcon(Outil.getURL("fr/mael/jiwigo/img/next.png"))); |
---|
163 | previous.setIcon(new ImageIcon(Outil.getURL("fr/mael/jiwigo/img/previous.png"))); |
---|
164 | rotateLeft.setIcon(new ImageIcon(Outil.getURL("fr/mael/jiwigo/img/left.png"))); |
---|
165 | rotateRight.setIcon(new ImageIcon(Outil.getURL("fr/mael/jiwigo/img/right.png"))); |
---|
166 | save.setIcon(new ImageIcon(Outil.getURL("fr/mael/jiwigo/img/save.png"))); |
---|
167 | comment.setIcon(new ImageIcon(Outil.getURL("fr/mael/jiwigo/img/comment.png"))); |
---|
168 | cut.setIcon(new ImageIcon(Outil.getURL("fr/mael/jiwigo/img/cut.png"))); |
---|
169 | //on rend les boutons invisibles, pour ne voir que l'image |
---|
170 | next.setFocusPainted(false); |
---|
171 | next.setBorderPainted(false); |
---|
172 | next.setContentAreaFilled(false); |
---|
173 | previous.setFocusPainted(false); |
---|
174 | previous.setBorderPainted(false); |
---|
175 | previous.setContentAreaFilled(false); |
---|
176 | rotateLeft.setFocusPainted(false); |
---|
177 | rotateLeft.setBorderPainted(false); |
---|
178 | rotateLeft.setContentAreaFilled(false); |
---|
179 | rotateRight.setFocusPainted(false); |
---|
180 | rotateRight.setBorderPainted(false); |
---|
181 | rotateRight.setContentAreaFilled(false); |
---|
182 | save.setFocusPainted(false); |
---|
183 | save.setBorderPainted(false); |
---|
184 | save.setContentAreaFilled(false); |
---|
185 | comment.setFocusPainted(false); |
---|
186 | comment.setBorderPainted(false); |
---|
187 | comment.setContentAreaFilled(false); |
---|
188 | cut.setFocusPainted(false); |
---|
189 | cut.setBorderPainted(false); |
---|
190 | cut.setContentAreaFilled(false); |
---|
191 | //ajout des action listeners |
---|
192 | comment.addActionListener(this); |
---|
193 | save.addActionListener(this); |
---|
194 | rotateRight.addActionListener(this); |
---|
195 | rotateLeft.addActionListener(this); |
---|
196 | previous.addActionListener(this); |
---|
197 | next.addActionListener(this); |
---|
198 | cut.addActionListener(this); |
---|
199 | //ajout des boutons |
---|
200 | panelBoutons.add(previous); |
---|
201 | panelBoutons.add(next); |
---|
202 | panelBoutons.add(rotateLeft); |
---|
203 | panelBoutons.add(rotateRight); |
---|
204 | // panelBoutons.add(cut); |
---|
205 | panelBoutons.add(save); |
---|
206 | panelBoutons.add(comment); |
---|
207 | |
---|
208 | BufferedImage img; |
---|
209 | try { |
---|
210 | img = ImageIO.read(new URL(ImagesManagement.getInstance().getCurrentImage().getUrl())); |
---|
211 | imagePanel = new BrowserImagePanel(); |
---|
212 | } catch (Exception e1) { |
---|
213 | LOG.error(Outil.getStackTrace(e1)); |
---|
214 | } |
---|
215 | |
---|
216 | this.setLayout(new BorderLayout()); |
---|
217 | JScrollPane scrollpane = new JScrollPane(imagePanel); |
---|
218 | Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); |
---|
219 | // this.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG); |
---|
220 | // this.setExtendedState(getExtendedState() | JFrame.MAXIMIZED_BOTH); |
---|
221 | // this.setExtendedState(JFrame.ICONIFIED | this.getExtendedState()); |
---|
222 | panelBoutons.add(imagePanel.getSlider()); |
---|
223 | imagePanel.setLayout(new BorderLayout()); |
---|
224 | panelBoutons.setOpaque(false); |
---|
225 | this.add(scrollpane, BorderLayout.CENTER); |
---|
226 | this.add(panelBoutons, BorderLayout.SOUTH); |
---|
227 | |
---|
228 | this.setSize(screenSize); |
---|
229 | // this.setLocationRelativeTo(null); |
---|
230 | // this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); |
---|
231 | setUpMenu(); |
---|
232 | this.generalListener = new AWTEventListener() { |
---|
233 | |
---|
234 | public void eventDispatched(AWTEvent event) { |
---|
235 | KeyEvent ke = (KeyEvent) event; |
---|
236 | if (ke.getID() == KeyEvent.KEY_PRESSED) { |
---|
237 | if (ke.getKeyCode() == KeyEvent.VK_RIGHT) { |
---|
238 | next.doClick(); |
---|
239 | } else if (ke.getKeyCode() == KeyEvent.VK_LEFT) { |
---|
240 | previous.doClick(); |
---|
241 | } |
---|
242 | } |
---|
243 | } |
---|
244 | }; |
---|
245 | this.setPreferredSize(new Dimension(300, 300)); |
---|
246 | //Toolkit.getDefaultToolkit().addAWTEventListener(generalListener, AWTEvent.KEY_EVENT_MASK); |
---|
247 | |
---|
248 | } |
---|
249 | |
---|
250 | /** |
---|
251 | * Changes the current image of the browser frame |
---|
252 | */ |
---|
253 | public void changeImage() { |
---|
254 | imagePanel.changeImage(); |
---|
255 | MainFrame.getInstance().getCollapsiblePanel().revalidate(); |
---|
256 | MainFrame.getInstance().getCollapsiblePanel().repaint(); |
---|
257 | } |
---|
258 | |
---|
259 | private void setUpMenu() { |
---|
260 | |
---|
261 | menuBar = new JMenuBar(); |
---|
262 | menuFichier = new JMenu(Messages.getMessage("imageBrowser_file")); |
---|
263 | menuAffichage = new JMenu(Messages.getMessage("imageBrowser_display")); |
---|
264 | menuImage = new JMenu(Messages.getMessage("imageBrowser_image")); |
---|
265 | menuBar.add(menuFichier); |
---|
266 | menuBar.add(menuAffichage); |
---|
267 | menuBar.add(menuImage); |
---|
268 | menuItemSave = new JMenuItem(Messages.getMessage("imageBrowser_save"), new ImageIcon(Outil |
---|
269 | .getURL("fr/mael/jiwigo/img/save_mini.png"))); |
---|
270 | menuItemPrint = new JMenuItem(Messages.getMessage("imageBrowser_print"), new ImageIcon(Outil |
---|
271 | .getURL("fr/mael/jiwigo/img/print.gif"))); |
---|
272 | menuItemClose = new JMenuItem(Messages.getMessage("imageBrowser_close"), new ImageIcon(Outil |
---|
273 | .getURL("fr/mael/jiwigo/img/close.png"))); |
---|
274 | menuItemZoomPlus = new JMenuItem(Messages.getMessage("imageBrowser_zoomPlus"), new ImageIcon(Outil |
---|
275 | .getURL("fr/mael/jiwigo/img/zoom_in.png"))); |
---|
276 | menuItemZoomMoins = new JMenuItem(Messages.getMessage("imageBrowser_zoomMoins"), new ImageIcon(Outil |
---|
277 | .getURL("fr/mael/jiwigo/img/zoom_out.png"))); |
---|
278 | menuItemNormal = new JMenuItem(Messages.getMessage("imageBrowser_zoomNormal"), new ImageIcon(Outil |
---|
279 | .getURL("fr/mael/jiwigo/img/zoom_n.png"))); |
---|
280 | menuItemFlipH = new JMenuItem(Messages.getMessage("imageBrowser_flipH"), new ImageIcon(Outil |
---|
281 | .getURL("fr/mael/jiwigo/img/small/flip_h.png"))); |
---|
282 | menuItemFlipV = new JMenuItem(Messages.getMessage("imageBrowser_flipV"), new ImageIcon(Outil |
---|
283 | .getURL("fr/mael/jiwigo/img/small/flip_v.png"))); |
---|
284 | menuItemRotationG = new JMenuItem(Messages.getMessage("imageBrowser_rotationL"), new ImageIcon(Outil |
---|
285 | .getURL("fr/mael/jiwigo/img/small/left.png"))); |
---|
286 | menuItemRotationD = new JMenuItem(Messages.getMessage("imageBrowser_rotationR"), new ImageIcon(Outil |
---|
287 | .getURL("fr/mael/jiwigo/img/small/right.png"))); |
---|
288 | menuFichier.add(menuItemSave); |
---|
289 | menuFichier.addSeparator(); |
---|
290 | menuFichier.add(menuItemPrint); |
---|
291 | menuFichier.addSeparator(); |
---|
292 | menuFichier.add(menuItemClose); |
---|
293 | menuAffichage.add(menuItemZoomMoins); |
---|
294 | menuAffichage.add(menuItemNormal); |
---|
295 | menuAffichage.add(menuItemZoomPlus); |
---|
296 | menuImage.add(menuItemFlipH); |
---|
297 | menuImage.add(menuItemFlipV); |
---|
298 | menuImage.addSeparator(); |
---|
299 | menuImage.add(menuItemRotationG); |
---|
300 | menuImage.add(menuItemRotationD); |
---|
301 | menuBar.add(menuFichier); |
---|
302 | menuBar.add(menuAffichage); |
---|
303 | menuBar.add(menuImage); |
---|
304 | menuItemSave.addActionListener(this); |
---|
305 | menuItemPrint.addActionListener(this); |
---|
306 | menuItemClose.addActionListener(this); |
---|
307 | menuItemZoomPlus.addActionListener(this); |
---|
308 | menuItemZoomMoins.addActionListener(this); |
---|
309 | menuItemNormal.addActionListener(this); |
---|
310 | menuItemFlipH.addActionListener(this); |
---|
311 | menuItemFlipV.addActionListener(this); |
---|
312 | menuItemRotationG.addActionListener(this); |
---|
313 | menuItemRotationD.addActionListener(this); |
---|
314 | this.add(menuBar, BorderLayout.NORTH); |
---|
315 | |
---|
316 | } |
---|
317 | |
---|
318 | // @Override |
---|
319 | // public void dispose() { |
---|
320 | // // super.dispose(); |
---|
321 | // Toolkit.getDefaultToolkit().removeAWTEventListener(generalListener); |
---|
322 | // } |
---|
323 | |
---|
324 | @Override |
---|
325 | public void actionPerformed(ActionEvent e) { |
---|
326 | if (e.getSource().equals(comment)) { |
---|
327 | new CommentsDialog(this, ImagesManagement.getInstance().getCurrentImage().getIdentifiant()); |
---|
328 | } else if (e.getSource().equals(save)) { |
---|
329 | //opens the dialog to save the file |
---|
330 | JFileChooser chooser = new JFileChooser(); |
---|
331 | if (JFileChooser.APPROVE_OPTION == chooser.showSaveDialog(null)) { |
---|
332 | File chosenFile = chooser.getSelectedFile(); |
---|
333 | if (chosenFile.getName().endsWith(".png")) { |
---|
334 | try { |
---|
335 | //saves the image |
---|
336 | ImagesUtil.writeImageToPNG(chosenFile, imagePanel.getImage()); |
---|
337 | } catch (IOException ex) { |
---|
338 | LOG.error(Outil.getStackTrace(ex)); |
---|
339 | } |
---|
340 | } else { |
---|
341 | try { |
---|
342 | ImagesUtil.writeImageToPNG(new File(chosenFile.getParentFile(), chosenFile.getName() + ".png"), |
---|
343 | imagePanel.getImage()); |
---|
344 | } catch (IOException ex) { |
---|
345 | LOG.error(Outil.getStackTrace(ex)); |
---|
346 | } |
---|
347 | } |
---|
348 | } |
---|
349 | } else if (e.getSource().equals(next)) { |
---|
350 | imagePanel.setDrawSelection(false); |
---|
351 | ImagesManagement.getInstance().next(); |
---|
352 | try { |
---|
353 | changeImage(); |
---|
354 | } catch (Exception e1) { |
---|
355 | LOG.error(Outil.getStackTrace(e1)); |
---|
356 | } |
---|
357 | |
---|
358 | } else if (e.getSource().equals(previous)) { |
---|
359 | imagePanel.setDrawSelection(false); |
---|
360 | ImagesManagement.getInstance().previous(); |
---|
361 | try { |
---|
362 | changeImage(); |
---|
363 | } catch (Exception e1) { |
---|
364 | LOG.error(Outil.getStackTrace(e1)); |
---|
365 | } |
---|
366 | |
---|
367 | } else if (e.getSource().equals(rotateLeft)) { |
---|
368 | imagePanel.rotationGauche(); |
---|
369 | } else if (e.getSource().equals(rotateRight)) { |
---|
370 | imagePanel.rotationDroite(); |
---|
371 | } else if (e.getSource().equals(menuItemSave)) { |
---|
372 | save.doClick(); |
---|
373 | } else if (e.getSource().equals(menuItemClose)) { |
---|
374 | // this.dispose(); |
---|
375 | } else if (e.getSource().equals(menuItemZoomPlus)) { |
---|
376 | imagePanel.zoomIn(); |
---|
377 | } else if (e.getSource().equals(menuItemZoomMoins)) { |
---|
378 | imagePanel.zoomOut(); |
---|
379 | } else if (e.getSource().equals(menuItemFlipH)) { |
---|
380 | imagePanel.flipH = !imagePanel.flipH; |
---|
381 | imagePanel.repaint(); |
---|
382 | imagePanel.revalidate(); |
---|
383 | } else if (e.getSource().equals(menuItemFlipV)) { |
---|
384 | imagePanel.flipV = !imagePanel.flipV; |
---|
385 | imagePanel.repaint(); |
---|
386 | imagePanel.revalidate(); |
---|
387 | } else if (e.getSource().equals(menuItemRotationD)) { |
---|
388 | rotateRight.doClick(); |
---|
389 | } else if (e.getSource().equals(menuItemRotationG)) { |
---|
390 | rotateLeft.doClick(); |
---|
391 | } else if (e.getSource().equals(menuItemPrint)) { |
---|
392 | PrinterJob pj = PrinterJob.getPrinterJob(); |
---|
393 | pj.setPrintable(imagePanel); |
---|
394 | if (pj.printDialog()) { |
---|
395 | try { |
---|
396 | pj.print(); |
---|
397 | } catch (PrinterException ex) { |
---|
398 | ex.printStackTrace(); |
---|
399 | } |
---|
400 | } |
---|
401 | } else if (e.getSource().equals(menuItemNormal)) { |
---|
402 | imagePanel.zoomNormal(); |
---|
403 | } else if (e.getSource().equals(cut)) { |
---|
404 | imagePanel.rogner(); |
---|
405 | } |
---|
406 | |
---|
407 | } |
---|
408 | |
---|
409 | } |
---|