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