package fr.mael.jiwigo.ui.browser; import java.awt.AWTEvent; import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Toolkit; import java.awt.event.AWTEventListener; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.image.BufferedImage; import java.awt.print.PrinterException; import java.awt.print.PrinterJob; import java.io.File; import java.io.IOException; import java.net.URL; import javax.imageio.ImageIO; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFileChooser; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JPanel; import javax.swing.JScrollPane; import fr.mael.jiwigo.transverse.ImagesManagement; import fr.mael.jiwigo.transverse.util.ImagesUtil; import fr.mael.jiwigo.transverse.util.Messages; import fr.mael.jiwigo.transverse.util.Tools; import fr.mael.jiwigo.ui.comments.CommentsDialog; import fr.mael.jiwigo.ui.mainframe.MainFrame; /** * Copyright (c) 2010, Mael All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of jiwigo nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Mael BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * @author mael * * Frame de navigation dans les images * */ public class BrowserPanel extends JPanel implements ActionListener { /** * Logger */ public static final org.apache.commons.logging.Log LOG = org.apache.commons.logging.LogFactory .getLog(BrowserPanel.class); /** * bouton to go to the next image */ private JButton next = new JButton(); /** * bouton to go to the previous image */ private JButton previous = new JButton(); /** * rotation on the left */ private JButton rotateLeft = new JButton(); /** * rotation on the right */ private JButton rotateRight = new JButton(); /** * saves the image */ private JButton save = new JButton(); /** * displays the comments for the current image */ private JButton comment = new JButton(); /** * panel that contains the previous buttons */ private JPanel panelBoutons; // private JButton delete = new JButton(); /** * clip the image */ private JButton cut = new JButton(); /** * Panel containing the image */ private BrowserImagePanel imagePanel; /** * Menus */ private JMenuBar menuBar; private JMenu menuFichier, menuAffichage, menuImage; private JMenuItem menuItemSave, menuItemPrint, menuItemClose, menuItemZoomPlus; private JMenuItem menuItemZoomMoins, menuItemNormal, menuItemFlipH, menuItemFlipV; private JMenuItem menuItemRotationG, menuItemRotationD; /** * Listener général. Permet d'ajouter les écouteurs sur les touches * pour la navigation entres les différentes images grace aux touches * droite et gauche. * Je n'ai pas trouvé mieux que de faire ce listener général. * Il est instancié et ajouté à la création de la frame, et retiré lorsqu'on la quitte */ @Deprecated private AWTEventListener generalListener; /** * Instance of the browser frame */ private static BrowserPanel instance; public static BrowserPanel getInstance() { if (instance == null) { instance = new BrowserPanel(); } if (instance.getPanelBoutons() == null && ImagesManagement.getInstance().getCurrentImage() != null) { instance.construct(); } return instance; } /** * @return the panelBoutons */ public JPanel getPanelBoutons() { return panelBoutons; } private BrowserPanel() { } public void construct() { // this.setIconImage(java.awt.Toolkit.getDefaultToolkit().getImage(Outil.getURL("fr/mael/jiwigo/img/icon.png"))); // this.setTitle(Messages.getMessage("titleBrowser")); panelBoutons = new JPanel(new FlowLayout()); //ajout des images sur les boutons next.setIcon(new ImageIcon(Tools.getURL("fr/mael/jiwigo/img/next.png"))); previous.setIcon(new ImageIcon(Tools.getURL("fr/mael/jiwigo/img/previous.png"))); rotateLeft.setIcon(new ImageIcon(Tools.getURL("fr/mael/jiwigo/img/left.png"))); rotateRight.setIcon(new ImageIcon(Tools.getURL("fr/mael/jiwigo/img/right.png"))); save.setIcon(new ImageIcon(Tools.getURL("fr/mael/jiwigo/img/save.png"))); comment.setIcon(new ImageIcon(Tools.getURL("fr/mael/jiwigo/img/comment.png"))); cut.setIcon(new ImageIcon(Tools.getURL("fr/mael/jiwigo/img/cut.png"))); // delete.setIcon(new ImageIcon(Tools.getURL("fr/mael/jiwigo/img/delete.png"))); //on rend les boutons invisibles, pour ne voir que l'image next.setFocusPainted(false); next.setBorderPainted(false); next.setContentAreaFilled(false); previous.setFocusPainted(false); previous.setBorderPainted(false); previous.setContentAreaFilled(false); rotateLeft.setFocusPainted(false); rotateLeft.setBorderPainted(false); rotateLeft.setContentAreaFilled(false); rotateRight.setFocusPainted(false); rotateRight.setBorderPainted(false); rotateRight.setContentAreaFilled(false); save.setFocusPainted(false); save.setBorderPainted(false); save.setContentAreaFilled(false); comment.setFocusPainted(false); comment.setBorderPainted(false); comment.setContentAreaFilled(false); cut.setFocusPainted(false); cut.setBorderPainted(false); cut.setContentAreaFilled(false); // delete.setFocusPainted(false); // delete.setBorderPainted(false); // delete.setContentAreaFilled(false); //ajout des action listeners comment.addActionListener(this); save.addActionListener(this); rotateRight.addActionListener(this); rotateLeft.addActionListener(this); previous.addActionListener(this); next.addActionListener(this); cut.addActionListener(this); // delete.addActionListener(this); //ajout des boutons panelBoutons.add(previous); panelBoutons.add(next); panelBoutons.add(rotateLeft); panelBoutons.add(rotateRight); // panelBoutons.add(delete); // panelBoutons.add(cut); panelBoutons.add(save); panelBoutons.add(comment); BufferedImage img; try { img = ImageIO.read(new URL(ImagesManagement.getInstance().getCurrentImage().getUrl())); imagePanel = new BrowserImagePanel(); } catch (Exception e1) { LOG.error(Tools.getStackTrace(e1)); } this.setLayout(new BorderLayout()); JScrollPane scrollpane = new JScrollPane(imagePanel); Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); // this.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG); // this.setExtendedState(getExtendedState() | JFrame.MAXIMIZED_BOTH); // this.setExtendedState(JFrame.ICONIFIED | this.getExtendedState()); panelBoutons.add(imagePanel.getSlider()); imagePanel.setLayout(new BorderLayout()); panelBoutons.setOpaque(false); this.add(scrollpane, BorderLayout.CENTER); this.add(panelBoutons, BorderLayout.SOUTH); this.setSize(screenSize); // this.setLocationRelativeTo(null); // this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); setUpMenu(); this.generalListener = new AWTEventListener() { public void eventDispatched(AWTEvent event) { KeyEvent ke = (KeyEvent) event; if (ke.getID() == KeyEvent.KEY_PRESSED) { if (ke.getKeyCode() == KeyEvent.VK_RIGHT) { next.doClick(); } else if (ke.getKeyCode() == KeyEvent.VK_LEFT) { previous.doClick(); } } } }; this.setPreferredSize(new Dimension(300, 300)); //Toolkit.getDefaultToolkit().addAWTEventListener(generalListener, AWTEvent.KEY_EVENT_MASK); } /** * Changes the current image of the browser frame */ public void changeImage() { imagePanel.changeImage(); // MainFrame.getInstance().getCollapsiblePanel().revalidate(); // MainFrame.getInstance().getCollapsiblePanel().repaint(); } private void setUpMenu() { menuBar = new JMenuBar(); menuFichier = new JMenu(Messages.getMessage("imageBrowser_file")); menuAffichage = new JMenu(Messages.getMessage("imageBrowser_display")); menuImage = new JMenu(Messages.getMessage("imageBrowser_image")); menuBar.add(menuFichier); menuBar.add(menuAffichage); menuBar.add(menuImage); menuItemSave = new JMenuItem(Messages.getMessage("imageBrowser_save"), new ImageIcon( Tools.getURL("fr/mael/jiwigo/img/save_mini.png"))); menuItemPrint = new JMenuItem(Messages.getMessage("imageBrowser_print"), new ImageIcon( Tools.getURL("fr/mael/jiwigo/img/print.gif"))); menuItemClose = new JMenuItem(Messages.getMessage("imageBrowser_close"), new ImageIcon( Tools.getURL("fr/mael/jiwigo/img/close.png"))); menuItemZoomPlus = new JMenuItem(Messages.getMessage("imageBrowser_zoomPlus"), new ImageIcon( Tools.getURL("fr/mael/jiwigo/img/zoom_in.png"))); menuItemZoomMoins = new JMenuItem(Messages.getMessage("imageBrowser_zoomMoins"), new ImageIcon( Tools.getURL("fr/mael/jiwigo/img/zoom_out.png"))); menuItemNormal = new JMenuItem(Messages.getMessage("imageBrowser_zoomNormal"), new ImageIcon( Tools.getURL("fr/mael/jiwigo/img/zoom_n.png"))); menuItemFlipH = new JMenuItem(Messages.getMessage("imageBrowser_flipH"), new ImageIcon( Tools.getURL("fr/mael/jiwigo/img/small/flip_h.png"))); menuItemFlipV = new JMenuItem(Messages.getMessage("imageBrowser_flipV"), new ImageIcon( Tools.getURL("fr/mael/jiwigo/img/small/flip_v.png"))); menuItemRotationG = new JMenuItem(Messages.getMessage("imageBrowser_rotationL"), new ImageIcon( Tools.getURL("fr/mael/jiwigo/img/small/left.png"))); menuItemRotationD = new JMenuItem(Messages.getMessage("imageBrowser_rotationR"), new ImageIcon( Tools.getURL("fr/mael/jiwigo/img/small/right.png"))); menuFichier.add(menuItemSave); menuFichier.addSeparator(); menuFichier.add(menuItemPrint); menuFichier.addSeparator(); menuFichier.add(menuItemClose); menuAffichage.add(menuItemZoomMoins); menuAffichage.add(menuItemNormal); menuAffichage.add(menuItemZoomPlus); menuImage.add(menuItemFlipH); menuImage.add(menuItemFlipV); menuImage.addSeparator(); menuImage.add(menuItemRotationG); menuImage.add(menuItemRotationD); menuBar.add(menuFichier); menuBar.add(menuAffichage); menuBar.add(menuImage); menuItemSave.addActionListener(this); menuItemPrint.addActionListener(this); menuItemClose.addActionListener(this); menuItemZoomPlus.addActionListener(this); menuItemZoomMoins.addActionListener(this); menuItemNormal.addActionListener(this); menuItemFlipH.addActionListener(this); menuItemFlipV.addActionListener(this); menuItemRotationG.addActionListener(this); menuItemRotationD.addActionListener(this); this.add(menuBar, BorderLayout.NORTH); } public void callRepaint() { if (menuBar != null) { menuBar.repaint(); menuBar.revalidate(); } if (panelBoutons != null) { panelBoutons.repaint(); panelBoutons.revalidate(); } if (imagePanel != null) { imagePanel.repaint(); imagePanel.revalidate(); } repaint(); revalidate(); } // @Override // public void dispose() { // // super.dispose(); // Toolkit.getDefaultToolkit().removeAWTEventListener(generalListener); // } @Override public void actionPerformed(ActionEvent e) { if (e.getSource().equals(comment)) { new CommentsDialog(this, ImagesManagement.getInstance().getCurrentImage().getIdentifier()); } else if (e.getSource().equals(save)) { //opens the dialog to save the file JFileChooser chooser = new JFileChooser(); if (JFileChooser.APPROVE_OPTION == chooser.showSaveDialog(null)) { File chosenFile = chooser.getSelectedFile(); if (chosenFile.getName().endsWith(".png")) { try { //saves the image ImagesUtil.writeImageToPNG(chosenFile, imagePanel.getImage()); } catch (IOException ex) { LOG.error(Tools.getStackTrace(ex)); } } else { try { ImagesUtil.writeImageToPNG(new File(chosenFile.getParentFile(), chosenFile.getName() + ".png"), imagePanel.getImage()); } catch (IOException ex) { LOG.error(Tools.getStackTrace(ex)); } } } } else if (e.getSource().equals(next)) { MainFrame.getInstance().showWaitCursor(true); imagePanel.setDrawSelection(false); ImagesManagement.getInstance().next(); try { changeImage(); } catch (Exception e1) { LOG.error(Tools.getStackTrace(e1)); } MainFrame.getInstance().showWaitCursor(false); } else if (e.getSource().equals(previous)) { MainFrame.getInstance().showWaitCursor(true); imagePanel.setDrawSelection(false); ImagesManagement.getInstance().previous(); try { changeImage(); } catch (Exception e1) { LOG.error(Tools.getStackTrace(e1)); } MainFrame.getInstance().showWaitCursor(false); } else if (e.getSource().equals(rotateLeft)) { imagePanel.rotationGauche(); } else if (e.getSource().equals(rotateRight)) { imagePanel.rotationDroite(); } else if (e.getSource().equals(menuItemSave)) { save.doClick(); } else if (e.getSource().equals(menuItemClose)) { // this.dispose(); } else if (e.getSource().equals(menuItemZoomPlus)) { imagePanel.zoomIn(); } else if (e.getSource().equals(menuItemZoomMoins)) { imagePanel.zoomOut(); } else if (e.getSource().equals(menuItemFlipH)) { imagePanel.flipH = !imagePanel.flipH; imagePanel.repaint(); imagePanel.revalidate(); } else if (e.getSource().equals(menuItemFlipV)) { imagePanel.flipV = !imagePanel.flipV; imagePanel.repaint(); imagePanel.revalidate(); } else if (e.getSource().equals(menuItemRotationD)) { rotateRight.doClick(); } else if (e.getSource().equals(menuItemRotationG)) { rotateLeft.doClick(); } else if (e.getSource().equals(menuItemPrint)) { PrinterJob pj = PrinterJob.getPrinterJob(); pj.setPrintable(imagePanel); if (pj.printDialog()) { try { pj.print(); } catch (PrinterException ex) { ex.printStackTrace(); } } } else if (e.getSource().equals(menuItemNormal)) { imagePanel.zoomNormal(); } else if (e.getSource().equals(cut)) { imagePanel.rogner(); } } }