source: extensions/jiwigo/trunk/src/main/java/fr/mael/jiwigo/ui/browser/BrowserPanel.java @ 10811

Last change on this file since 10811 was 10811, checked in by mlg, 13 years ago

Adds a wait cursor for time consuming operations.

File size: 15.0 KB
Line 
1package fr.mael.jiwigo.ui.browser;
2
3import java.awt.AWTEvent;
4import java.awt.BorderLayout;
5import java.awt.Dimension;
6import java.awt.FlowLayout;
7import java.awt.Toolkit;
8import java.awt.event.AWTEventListener;
9import java.awt.event.ActionEvent;
10import java.awt.event.ActionListener;
11import java.awt.event.KeyEvent;
12import java.awt.image.BufferedImage;
13import java.awt.print.PrinterException;
14import java.awt.print.PrinterJob;
15import java.io.File;
16import java.io.IOException;
17import java.net.URL;
18
19import javax.imageio.ImageIO;
20import javax.swing.ImageIcon;
21import javax.swing.JButton;
22import javax.swing.JFileChooser;
23import javax.swing.JMenu;
24import javax.swing.JMenuBar;
25import javax.swing.JMenuItem;
26import javax.swing.JPanel;
27import javax.swing.JScrollPane;
28
29import fr.mael.jiwigo.transverse.ImagesManagement;
30import fr.mael.jiwigo.transverse.util.ImagesUtil;
31import fr.mael.jiwigo.transverse.util.Messages;
32import fr.mael.jiwigo.transverse.util.Tools;
33import fr.mael.jiwigo.ui.comments.CommentsDialog;
34import 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 */
68public 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    //    private JButton delete = new JButton();
104
105    /**
106     * clip the image
107     */
108    private JButton cut = new JButton();
109
110    /**
111     * Panel containing the 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    @Deprecated
132    private AWTEventListener generalListener;
133
134    /**
135     * Instance of the browser frame
136     */
137    private static BrowserPanel instance;
138
139    public static BrowserPanel getInstance() {
140        if (instance == null) {
141            instance = new BrowserPanel();
142        }
143        if (instance.getPanelBoutons() == null && ImagesManagement.getInstance().getCurrentImage() != null) {
144            instance.construct();
145        }
146        return instance;
147    }
148
149    /**
150     * @return the panelBoutons
151     */
152    public JPanel getPanelBoutons() {
153        return panelBoutons;
154    }
155
156    private BrowserPanel() {
157    }
158
159    public void construct() {
160        //      this.setIconImage(java.awt.Toolkit.getDefaultToolkit().getImage(Outil.getURL("fr/mael/jiwigo/img/icon.png")));
161        //      this.setTitle(Messages.getMessage("titleBrowser"));
162        panelBoutons = new JPanel(new FlowLayout());
163        //ajout des images sur les boutons
164        next.setIcon(new ImageIcon(Tools.getURL("fr/mael/jiwigo/img/next.png")));
165        previous.setIcon(new ImageIcon(Tools.getURL("fr/mael/jiwigo/img/previous.png")));
166        rotateLeft.setIcon(new ImageIcon(Tools.getURL("fr/mael/jiwigo/img/left.png")));
167        rotateRight.setIcon(new ImageIcon(Tools.getURL("fr/mael/jiwigo/img/right.png")));
168        save.setIcon(new ImageIcon(Tools.getURL("fr/mael/jiwigo/img/save.png")));
169        comment.setIcon(new ImageIcon(Tools.getURL("fr/mael/jiwigo/img/comment.png")));
170        cut.setIcon(new ImageIcon(Tools.getURL("fr/mael/jiwigo/img/cut.png")));
171        //      delete.setIcon(new ImageIcon(Tools.getURL("fr/mael/jiwigo/img/delete.png")));
172        //on rend les boutons invisibles, pour ne voir que l'image
173        next.setFocusPainted(false);
174        next.setBorderPainted(false);
175        next.setContentAreaFilled(false);
176        previous.setFocusPainted(false);
177        previous.setBorderPainted(false);
178        previous.setContentAreaFilled(false);
179        rotateLeft.setFocusPainted(false);
180        rotateLeft.setBorderPainted(false);
181        rotateLeft.setContentAreaFilled(false);
182        rotateRight.setFocusPainted(false);
183        rotateRight.setBorderPainted(false);
184        rotateRight.setContentAreaFilled(false);
185        save.setFocusPainted(false);
186        save.setBorderPainted(false);
187        save.setContentAreaFilled(false);
188        comment.setFocusPainted(false);
189        comment.setBorderPainted(false);
190        comment.setContentAreaFilled(false);
191        cut.setFocusPainted(false);
192        cut.setBorderPainted(false);
193        cut.setContentAreaFilled(false);
194        //      delete.setFocusPainted(false);
195        //      delete.setBorderPainted(false);
196        //      delete.setContentAreaFilled(false);
197        //ajout des action listeners
198        comment.addActionListener(this);
199        save.addActionListener(this);
200        rotateRight.addActionListener(this);
201        rotateLeft.addActionListener(this);
202        previous.addActionListener(this);
203        next.addActionListener(this);
204        cut.addActionListener(this);
205        //      delete.addActionListener(this);
206        //ajout des boutons
207        panelBoutons.add(previous);
208        panelBoutons.add(next);
209        panelBoutons.add(rotateLeft);
210        panelBoutons.add(rotateRight);
211        //      panelBoutons.add(delete);
212        //      panelBoutons.add(cut);
213        panelBoutons.add(save);
214        panelBoutons.add(comment);
215
216        BufferedImage img;
217        try {
218            img = ImageIO.read(new URL(ImagesManagement.getInstance().getCurrentImage().getUrl()));
219            imagePanel = new BrowserImagePanel();
220        } catch (Exception e1) {
221            LOG.error(Tools.getStackTrace(e1));
222        }
223
224        this.setLayout(new BorderLayout());
225        JScrollPane scrollpane = new JScrollPane(imagePanel);
226        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
227        //      this.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
228        //      this.setExtendedState(getExtendedState() | JFrame.MAXIMIZED_BOTH);
229        //      this.setExtendedState(JFrame.ICONIFIED | this.getExtendedState());
230        panelBoutons.add(imagePanel.getSlider());
231        imagePanel.setLayout(new BorderLayout());
232        panelBoutons.setOpaque(false);
233        this.add(scrollpane, BorderLayout.CENTER);
234        this.add(panelBoutons, BorderLayout.SOUTH);
235
236        this.setSize(screenSize);
237        //      this.setLocationRelativeTo(null);
238        //      this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
239        setUpMenu();
240        this.generalListener = new AWTEventListener() {
241
242            public void eventDispatched(AWTEvent event) {
243                KeyEvent ke = (KeyEvent) event;
244                if (ke.getID() == KeyEvent.KEY_PRESSED) {
245                    if (ke.getKeyCode() == KeyEvent.VK_RIGHT) {
246                        next.doClick();
247                    } else if (ke.getKeyCode() == KeyEvent.VK_LEFT) {
248                        previous.doClick();
249                    }
250                }
251            }
252        };
253        this.setPreferredSize(new Dimension(300, 300));
254        //Toolkit.getDefaultToolkit().addAWTEventListener(generalListener, AWTEvent.KEY_EVENT_MASK);
255
256    }
257
258    /**
259     * Changes the current image of the browser frame
260     */
261    public void changeImage() {
262        imagePanel.changeImage();
263        //      MainFrame.getInstance().getCollapsiblePanel().revalidate();
264        //      MainFrame.getInstance().getCollapsiblePanel().repaint();
265    }
266
267    private void setUpMenu() {
268
269        menuBar = new JMenuBar();
270        menuFichier = new JMenu(Messages.getMessage("imageBrowser_file"));
271        menuAffichage = new JMenu(Messages.getMessage("imageBrowser_display"));
272        menuImage = new JMenu(Messages.getMessage("imageBrowser_image"));
273        menuBar.add(menuFichier);
274        menuBar.add(menuAffichage);
275        menuBar.add(menuImage);
276        menuItemSave = new JMenuItem(Messages.getMessage("imageBrowser_save"), new ImageIcon(
277                Tools.getURL("fr/mael/jiwigo/img/save_mini.png")));
278        menuItemPrint = new JMenuItem(Messages.getMessage("imageBrowser_print"), new ImageIcon(
279                Tools.getURL("fr/mael/jiwigo/img/print.gif")));
280        menuItemClose = new JMenuItem(Messages.getMessage("imageBrowser_close"), new ImageIcon(
281                Tools.getURL("fr/mael/jiwigo/img/close.png")));
282        menuItemZoomPlus = new JMenuItem(Messages.getMessage("imageBrowser_zoomPlus"), new ImageIcon(
283                Tools.getURL("fr/mael/jiwigo/img/zoom_in.png")));
284        menuItemZoomMoins = new JMenuItem(Messages.getMessage("imageBrowser_zoomMoins"), new ImageIcon(
285                Tools.getURL("fr/mael/jiwigo/img/zoom_out.png")));
286        menuItemNormal = new JMenuItem(Messages.getMessage("imageBrowser_zoomNormal"), new ImageIcon(
287                Tools.getURL("fr/mael/jiwigo/img/zoom_n.png")));
288        menuItemFlipH = new JMenuItem(Messages.getMessage("imageBrowser_flipH"), new ImageIcon(
289                Tools.getURL("fr/mael/jiwigo/img/small/flip_h.png")));
290        menuItemFlipV = new JMenuItem(Messages.getMessage("imageBrowser_flipV"), new ImageIcon(
291                Tools.getURL("fr/mael/jiwigo/img/small/flip_v.png")));
292        menuItemRotationG = new JMenuItem(Messages.getMessage("imageBrowser_rotationL"), new ImageIcon(
293                Tools.getURL("fr/mael/jiwigo/img/small/left.png")));
294        menuItemRotationD = new JMenuItem(Messages.getMessage("imageBrowser_rotationR"), new ImageIcon(
295                Tools.getURL("fr/mael/jiwigo/img/small/right.png")));
296        menuFichier.add(menuItemSave);
297        menuFichier.addSeparator();
298        menuFichier.add(menuItemPrint);
299        menuFichier.addSeparator();
300        menuFichier.add(menuItemClose);
301        menuAffichage.add(menuItemZoomMoins);
302        menuAffichage.add(menuItemNormal);
303        menuAffichage.add(menuItemZoomPlus);
304        menuImage.add(menuItemFlipH);
305        menuImage.add(menuItemFlipV);
306        menuImage.addSeparator();
307        menuImage.add(menuItemRotationG);
308        menuImage.add(menuItemRotationD);
309        menuBar.add(menuFichier);
310        menuBar.add(menuAffichage);
311        menuBar.add(menuImage);
312        menuItemSave.addActionListener(this);
313        menuItemPrint.addActionListener(this);
314        menuItemClose.addActionListener(this);
315        menuItemZoomPlus.addActionListener(this);
316        menuItemZoomMoins.addActionListener(this);
317        menuItemNormal.addActionListener(this);
318        menuItemFlipH.addActionListener(this);
319        menuItemFlipV.addActionListener(this);
320        menuItemRotationG.addActionListener(this);
321        menuItemRotationD.addActionListener(this);
322        this.add(menuBar, BorderLayout.NORTH);
323
324    }
325
326    public void callRepaint() {
327        if (menuBar != null) {
328            menuBar.repaint();
329            menuBar.revalidate();
330        }
331        if (panelBoutons != null) {
332            panelBoutons.repaint();
333            panelBoutons.revalidate();
334        }
335        if (imagePanel != null) {
336            imagePanel.repaint();
337            imagePanel.revalidate();
338        }
339
340        repaint();
341        revalidate();
342    }
343
344    //    @Override
345    //    public void dispose() {
346    //  //      super.dispose();
347    //  Toolkit.getDefaultToolkit().removeAWTEventListener(generalListener);
348    //    }
349
350    @Override
351    public void actionPerformed(ActionEvent e) {
352        if (e.getSource().equals(comment)) {
353            new CommentsDialog(this, ImagesManagement.getInstance().getCurrentImage().getIdentifier());
354        } else if (e.getSource().equals(save)) {
355            //opens the dialog to save the file
356            JFileChooser chooser = new JFileChooser();
357            if (JFileChooser.APPROVE_OPTION == chooser.showSaveDialog(null)) {
358                File chosenFile = chooser.getSelectedFile();
359                if (chosenFile.getName().endsWith(".png")) {
360                    try {
361                        //saves the image
362                        ImagesUtil.writeImageToPNG(chosenFile, imagePanel.getImage());
363                    } catch (IOException ex) {
364                        LOG.error(Tools.getStackTrace(ex));
365                    }
366                } else {
367                    try {
368                        ImagesUtil.writeImageToPNG(new File(chosenFile.getParentFile(), chosenFile.getName() + ".png"),
369                                imagePanel.getImage());
370                    } catch (IOException ex) {
371                        LOG.error(Tools.getStackTrace(ex));
372                    }
373                }
374            }
375        } else if (e.getSource().equals(next)) {
376            MainFrame.getInstance().showWaitCursor(true);
377            imagePanel.setDrawSelection(false);
378            ImagesManagement.getInstance().next();
379            try {
380                changeImage();
381            } catch (Exception e1) {
382                LOG.error(Tools.getStackTrace(e1));
383            }
384            MainFrame.getInstance().showWaitCursor(false);
385        } else if (e.getSource().equals(previous)) {
386            MainFrame.getInstance().showWaitCursor(true);
387            imagePanel.setDrawSelection(false);
388            ImagesManagement.getInstance().previous();
389            try {
390                changeImage();
391            } catch (Exception e1) {
392                LOG.error(Tools.getStackTrace(e1));
393            }
394            MainFrame.getInstance().showWaitCursor(false);
395        } else if (e.getSource().equals(rotateLeft)) {
396            imagePanel.rotationGauche();
397        } else if (e.getSource().equals(rotateRight)) {
398            imagePanel.rotationDroite();
399        } else if (e.getSource().equals(menuItemSave)) {
400            save.doClick();
401        } else if (e.getSource().equals(menuItemClose)) {
402            //      this.dispose();
403        } else if (e.getSource().equals(menuItemZoomPlus)) {
404            imagePanel.zoomIn();
405        } else if (e.getSource().equals(menuItemZoomMoins)) {
406            imagePanel.zoomOut();
407        } else if (e.getSource().equals(menuItemFlipH)) {
408            imagePanel.flipH = !imagePanel.flipH;
409            imagePanel.repaint();
410            imagePanel.revalidate();
411        } else if (e.getSource().equals(menuItemFlipV)) {
412            imagePanel.flipV = !imagePanel.flipV;
413            imagePanel.repaint();
414            imagePanel.revalidate();
415        } else if (e.getSource().equals(menuItemRotationD)) {
416            rotateRight.doClick();
417        } else if (e.getSource().equals(menuItemRotationG)) {
418            rotateLeft.doClick();
419        } else if (e.getSource().equals(menuItemPrint)) {
420            PrinterJob pj = PrinterJob.getPrinterJob();
421            pj.setPrintable(imagePanel);
422            if (pj.printDialog()) {
423                try {
424                    pj.print();
425                } catch (PrinterException ex) {
426                    ex.printStackTrace();
427                }
428            }
429        } else if (e.getSource().equals(menuItemNormal)) {
430            imagePanel.zoomNormal();
431        } else if (e.getSource().equals(cut)) {
432            imagePanel.rogner();
433        }
434
435    }
436
437}
Note: See TracBrowser for help on using the repository browser.