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