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

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

Bug correction :
bug:0001837 : there is now a default translation file (in english), so the application won't crash when a translation is not found
bug:0001833 : the accents are manage when creating a new category
bug:0001832 : on a right click on the categories list, the selection is now visible
bug:0001830 : there is no bug on refreshing the categories tree

Features :
feature:001828 : exif and iptc tags are kept after resizing an image
feature:001827 : pwg.images.addChunk is now fully used : images are split into chunks before being sent

Other features :

  • The user can manage his preferences :

-The web images size
-The chunks size

  • The user can save the login informations (url, login, password) /!\ in a plain text file !
File size: 13.5 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     * Panel qui contient l'image
107     */
108    private BrowserImagePanel imagePanel;
109
110    /**
111     * Menus
112     */
113    private JMenuBar menuBar;
114    private JMenu menuFichier, menuAffichage, menuImage;
115    private JMenuItem menuItemSave, menuItemPrint, menuItemClose, menuItemZoomPlus;
116    private JMenuItem menuItemZoomMoins, menuItemNormal, menuItemFlipH, menuItemFlipV;
117    private JMenuItem menuItemRotationG, menuItemRotationD;
118
119    /**
120     * Listener général. Permet d'ajouter les écouteurs sur les touches
121     * pour la navigation entres les différentes images grace aux touches
122     * droite et gauche.
123     * Je n'ai pas trouvé mieux que de faire ce listener général.
124     * Il est instancié et ajouté à la création de la frame, et retiré lorsqu'on la quitte
125     */
126    private AWTEventListener generalListener;
127
128    public BrowserFrame(Image image) {
129        this.setIconImage(java.awt.Toolkit.getDefaultToolkit().getImage(Outil.getURL("fr/mael/jiwigo/img/icon.png")));
130        this.setTitle(Messages.getMessage("titleBrowser"));
131        panelBoutons = new JPanel(new FlowLayout());
132        //ajout des images sur les boutons
133        next.setIcon(new ImageIcon(Outil.getURL("fr/mael/jiwigo/img/next.png")));
134        previous.setIcon(new ImageIcon(Outil.getURL("fr/mael/jiwigo/img/previous.png")));
135        rotateLeft.setIcon(new ImageIcon(Outil.getURL("fr/mael/jiwigo/img/left.png")));
136        rotateRight.setIcon(new ImageIcon(Outil.getURL("fr/mael/jiwigo/img/right.png")));
137        save.setIcon(new ImageIcon(Outil.getURL("fr/mael/jiwigo/img/save.png")));
138        comment.setIcon(new ImageIcon(Outil.getURL("fr/mael/jiwigo/img/comment.png")));
139        //on rend les boutons invisibles, pour ne voir que l'image
140        next.setFocusPainted(false);
141        next.setBorderPainted(false);
142        next.setContentAreaFilled(false);
143        previous.setFocusPainted(false);
144        previous.setBorderPainted(false);
145        previous.setContentAreaFilled(false);
146        rotateLeft.setFocusPainted(false);
147        rotateLeft.setBorderPainted(false);
148        rotateLeft.setContentAreaFilled(false);
149        rotateRight.setFocusPainted(false);
150        rotateRight.setBorderPainted(false);
151        rotateRight.setContentAreaFilled(false);
152        save.setFocusPainted(false);
153        save.setBorderPainted(false);
154        save.setContentAreaFilled(false);
155        comment.setFocusPainted(false);
156        comment.setBorderPainted(false);
157        comment.setContentAreaFilled(false);
158        //ajout des action listeners
159        comment.addActionListener(this);
160        save.addActionListener(this);
161        rotateRight.addActionListener(this);
162        rotateLeft.addActionListener(this);
163        previous.addActionListener(this);
164        next.addActionListener(this);
165        //ajout des boutons
166        panelBoutons.add(previous);
167        panelBoutons.add(next);
168        panelBoutons.add(rotateLeft);
169        panelBoutons.add(rotateRight);
170        panelBoutons.add(save);
171        panelBoutons.add(comment);
172
173        BufferedImage img;
174        try {
175            img = ImageIO.read(new URL(ImagesManagement.getCurrentImage().getUrl()));
176            imagePanel = new BrowserImagePanel(img);
177        } catch (Exception e1) {
178            LOG.error(Outil.getStackTrace(e1));
179        }
180
181        this.getContentPane().setLayout(new BorderLayout());
182        JScrollPane scrollpane = new JScrollPane(imagePanel);
183        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
184        this.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
185        this.setExtendedState(getExtendedState() | JFrame.MAXIMIZED_BOTH);
186        //      this.setExtendedState(JFrame.ICONIFIED | this.getExtendedState());
187        panelBoutons.add(imagePanel.getSlider());
188        imagePanel.setLayout(new BorderLayout());
189        panelBoutons.setOpaque(false);
190        this.add(scrollpane, BorderLayout.CENTER);
191        this.add(panelBoutons, BorderLayout.SOUTH);
192
193        this.setSize(screenSize);
194        this.setLocationRelativeTo(null);
195        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
196        setUpMenu();
197        //instanciation du listener general
198        this.generalListener = new AWTEventListener() {
199
200            public void eventDispatched(AWTEvent event) {
201                KeyEvent ke = (KeyEvent) event;
202                if (ke.getID() == KeyEvent.KEY_PRESSED) {
203                    if (ke.getKeyCode() == KeyEvent.VK_RIGHT) {
204                        next.doClick();
205                    } else if (ke.getKeyCode() == KeyEvent.VK_LEFT) {
206                        previous.doClick();
207                    }
208                }
209            }
210        };
211        this.setVisible(true);
212
213        //ajout du listener general
214        //Toolkit.getDefaultToolkit().addAWTEventListener(generalListener, AWTEvent.KEY_EVENT_MASK);
215
216    }
217
218    private void setUpMenu() {
219        menuBar = new JMenuBar();
220
221        menuBar = new JMenuBar();
222        menuFichier = new JMenu(Messages.getMessage("imageBrowser_file"));
223        menuAffichage = new JMenu(Messages.getMessage("imageBrowser_display"));
224        menuImage = new JMenu(Messages.getMessage("imageBrowser_image"));
225        menuBar.add(menuFichier);
226        menuBar.add(menuAffichage);
227        menuBar.add(menuImage);
228        menuItemSave = new JMenuItem(Messages.getMessage("imageBrowser_save"), new ImageIcon(Outil
229                .getURL("fr/mael/jiwigo/img/save_mini.png")));
230        menuItemPrint = new JMenuItem(Messages.getMessage("imageBrowser_print"), new ImageIcon(Outil
231                .getURL("fr/mael/jiwigo/img/print.gif")));
232        menuItemClose = new JMenuItem(Messages.getMessage("imageBrowser_close"), new ImageIcon(Outil
233                .getURL("fr/mael/jiwigo/img/close.png")));
234        menuItemZoomPlus = new JMenuItem(Messages.getMessage("imageBrowser_zoomPlus"), new ImageIcon(Outil
235                .getURL("fr/mael/jiwigo/img/zoom_in.png")));
236        menuItemZoomMoins = new JMenuItem(Messages.getMessage("imageBrowser_zoomMoins"), new ImageIcon(Outil
237                .getURL("fr/mael/jiwigo/img/zoom_out.png")));
238        menuItemNormal = new JMenuItem(Messages.getMessage("imageBrowser_zoomNormal"), new ImageIcon(Outil
239                .getURL("fr/mael/jiwigo/img/zoom_n.png")));
240        menuItemFlipH = new JMenuItem(Messages.getMessage("imageBrowser_flipH"), new ImageIcon(Outil
241                .getURL("fr/mael/jiwigo/img/flip_h.png")));
242        menuItemFlipV = new JMenuItem(Messages.getMessage("imageBrowser_flipV"), new ImageIcon(Outil
243                .getURL("fr/mael/jiwigo/img/flip_v.png")));
244        menuItemRotationG = new JMenuItem(Messages.getMessage("imageBrowser_rotationL"), new ImageIcon(Outil
245                .getURL("fr/mael/jiwigo/img/left.png")));
246        menuItemRotationD = new JMenuItem(Messages.getMessage("imageBrowser_rotationR"), new ImageIcon(Outil
247                .getURL("fr/mael/jiwigo/img/right.png")));
248        menuFichier.add(menuItemSave);
249        menuFichier.addSeparator();
250        menuFichier.add(menuItemPrint);
251        menuFichier.addSeparator();
252        menuFichier.add(menuItemClose);
253        menuAffichage.add(menuItemZoomMoins);
254        menuAffichage.add(menuItemNormal);
255        menuAffichage.add(menuItemZoomPlus);
256        menuImage.add(menuItemFlipH);
257        menuImage.add(menuItemFlipV);
258        menuImage.addSeparator();
259        menuImage.add(menuItemRotationG);
260        menuImage.add(menuItemRotationD);
261        menuBar.add(menuFichier);
262        menuBar.add(menuAffichage);
263        menuBar.add(menuImage);
264        menuItemSave.addActionListener(this);
265        menuItemPrint.addActionListener(this);
266        menuItemClose.addActionListener(this);
267        menuItemZoomPlus.addActionListener(this);
268        menuItemZoomMoins.addActionListener(this);
269        menuItemNormal.addActionListener(this);
270        menuItemFlipH.addActionListener(this);
271        menuItemFlipV.addActionListener(this);
272        menuItemRotationG.addActionListener(this);
273        menuItemRotationD.addActionListener(this);
274        this.setJMenuBar(menuBar);
275
276    }
277
278    @Override
279    public void dispose() {
280        super.dispose();
281        //on enleve le listener general
282        Toolkit.getDefaultToolkit().removeAWTEventListener(generalListener);
283    }
284
285    @Override
286    public void actionPerformed(ActionEvent e) {
287        if (e.getSource().equals(comment)) {
288            new CommentsDialog(this, ImagesManagement.getCurrentImage().getIdentifiant());
289        } else if (e.getSource().equals(save)) {
290            //ouverture du dialog de sauvegarde d'une image
291            JFileChooser chooser = new JFileChooser();
292            if (JFileChooser.APPROVE_OPTION == chooser.showSaveDialog(null)) {
293                File chosenFile = chooser.getSelectedFile();
294                if (chosenFile.getName().endsWith(".png")) {
295                    try {
296                        //sauvegarde de l'image
297                        ImagesUtil.writeImageToPNG(chosenFile, imagePanel.getImage());
298                    } catch (IOException ex) {
299                        LOG.error(Outil.getStackTrace(ex));
300                    }
301                } else {
302                    try {
303                        ImagesUtil.writeImageToPNG(new File(chosenFile.getParentFile(), chosenFile.getName() + ".png"),
304                                imagePanel.getImage());
305                    } catch (IOException ex) {
306                        LOG.error(Outil.getStackTrace(ex));
307                    }
308                }
309            }
310        } else if (e.getSource().equals(next)) {
311            //on appelle d'abord la fonction de l'images panel qui increment l'image courante
312            ImagesManagement.next();
313            //on rafraichit le panel qui contient l'image
314            BufferedImage img;
315            try {
316                img = ImagesManagement.getCurrentBufferedImage();
317                imagePanel.setImage(img);
318            } catch (Exception e1) {
319                LOG.error(Outil.getStackTrace(e1));
320            }
321
322        } else if (e.getSource().equals(previous)) {
323            //on appelle d'abord la fonction de l'images panel qui décrémente l'image courante
324            ImagesManagement.previous();
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(rotateLeft)) {
335            imagePanel.rotationGauche();
336        } else if (e.getSource().equals(rotateRight)) {
337            imagePanel.rotationDroite();
338        } else if (e.getSource().equals(menuItemSave)) {
339            save.doClick();
340        } else if (e.getSource().equals(menuItemClose)) {
341            this.dispose();
342        } else if (e.getSource().equals(menuItemZoomPlus)) {
343            imagePanel.zoomIn();
344        } else if (e.getSource().equals(menuItemZoomMoins)) {
345            imagePanel.zoomOut();
346        } else if (e.getSource().equals(menuItemFlipH)) {
347            imagePanel.flipH = !imagePanel.flipH;
348            imagePanel.repaint();
349            imagePanel.revalidate();
350        } else if (e.getSource().equals(menuItemFlipV)) {
351            imagePanel.flipV = !imagePanel.flipV;
352            imagePanel.repaint();
353            imagePanel.revalidate();
354        } else if (e.getSource().equals(menuItemRotationD)) {
355            rotateRight.doClick();
356        } else if (e.getSource().equals(menuItemRotationG)) {
357            rotateLeft.doClick();
358        } else if (e.getSource().equals(menuItemPrint)) {
359            PrinterJob pj = PrinterJob.getPrinterJob();
360            pj.setPrintable(imagePanel);
361            if (pj.printDialog()) {
362                try {
363                    pj.print();
364                } catch (PrinterException ex) {
365                    ex.printStackTrace();
366                }
367            }
368        } else if (e.getSource().equals(menuItemNormal)) {
369            imagePanel.zoomNormal();
370        }
371
372    }
373
374}
Note: See TracBrowser for help on using the repository browser.