source: extensions/jiwigo/trunk/src/main/java/fr/mael/jiwigo/ui/browser/BrowserFrame.java @ 6958

Last change on this file since 6958 was 6958, checked in by mlg, 14 years ago

New features

  • When clicking on a category, the category is opened in a tab. When clicking on the same category again, no tab is opened but the previously opened tab is shown. The user can close the tabs (close current tab, close others, close all).
  • Ability to clip the images. There are still some bugs (unable to clip the images when zooming, and the rectangle stays when changing the image).
File size: 13.9 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.JFrame;
24import javax.swing.JMenu;
25import javax.swing.JMenuBar;
26import javax.swing.JMenuItem;
27import javax.swing.JPanel;
28import javax.swing.JRootPane;
29import javax.swing.JScrollPane;
30
31import fr.mael.jiwigo.om.Image;
32import fr.mael.jiwigo.transverse.util.ImagesUtil;
33import fr.mael.jiwigo.transverse.util.Messages;
34import fr.mael.jiwigo.transverse.util.Outil;
35import fr.mael.jiwigo.ui.ImagesManagement;
36import 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 */
70public 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            //on appelle d'abord la fonction de l'images panel qui increment l'image courante
323            ImagesManagement.next();
324            //on rafraichit le panel qui contient l'image
325            BufferedImage img;
326            try {
327                img = ImagesManagement.getCurrentBufferedImage();
328                imagePanel.setImage(img);
329            } catch (Exception e1) {
330                LOG.error(Outil.getStackTrace(e1));
331            }
332
333        } else if (e.getSource().equals(previous)) {
334            //on appelle d'abord la fonction de l'images panel qui décrémente l'image courante
335            ImagesManagement.previous();
336            //on rafraichit le panel qui contient l'image
337            BufferedImage img;
338            try {
339                img = ImagesManagement.getCurrentBufferedImage();
340                imagePanel.setImage(img);
341            } catch (Exception e1) {
342                LOG.error(Outil.getStackTrace(e1));
343            }
344
345        } else if (e.getSource().equals(rotateLeft)) {
346            imagePanel.rotationGauche();
347        } else if (e.getSource().equals(rotateRight)) {
348            imagePanel.rotationDroite();
349        } else if (e.getSource().equals(menuItemSave)) {
350            save.doClick();
351        } else if (e.getSource().equals(menuItemClose)) {
352            this.dispose();
353        } else if (e.getSource().equals(menuItemZoomPlus)) {
354            imagePanel.zoomIn();
355        } else if (e.getSource().equals(menuItemZoomMoins)) {
356            imagePanel.zoomOut();
357        } else if (e.getSource().equals(menuItemFlipH)) {
358            imagePanel.flipH = !imagePanel.flipH;
359            imagePanel.repaint();
360            imagePanel.revalidate();
361        } else if (e.getSource().equals(menuItemFlipV)) {
362            imagePanel.flipV = !imagePanel.flipV;
363            imagePanel.repaint();
364            imagePanel.revalidate();
365        } else if (e.getSource().equals(menuItemRotationD)) {
366            rotateRight.doClick();
367        } else if (e.getSource().equals(menuItemRotationG)) {
368            rotateLeft.doClick();
369        } else if (e.getSource().equals(menuItemPrint)) {
370            PrinterJob pj = PrinterJob.getPrinterJob();
371            pj.setPrintable(imagePanel);
372            if (pj.printDialog()) {
373                try {
374                    pj.print();
375                } catch (PrinterException ex) {
376                    ex.printStackTrace();
377                }
378            }
379        } else if (e.getSource().equals(menuItemNormal)) {
380            imagePanel.zoomNormal();
381        } else if (e.getSource().equals(cut)) {
382            imagePanel.rogner();
383        }
384
385    }
386
387}
Note: See TracBrowser for help on using the repository browser.