package fr.mael.jiwigo.ui.mainframe; import java.awt.BorderLayout; import java.awt.Cursor; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.RenderingHints; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.io.IOException; import java.util.List; import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JLabel; import javax.swing.JList; import javax.swing.JMenuItem; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JPopupMenu; import javax.swing.JScrollPane; import javax.swing.ListSelectionModel; import fr.mael.jiwigo.Main; import fr.mael.jiwigo.om.Image; import fr.mael.jiwigo.om.Tag; import fr.mael.jiwigo.service.ImageService; import fr.mael.jiwigo.service.TagService; import fr.mael.jiwigo.transverse.ImagesManagement; import fr.mael.jiwigo.transverse.util.Messages; import fr.mael.jiwigo.ui.browser.BrowserPanel; import fr.mael.jiwigo.ui.layout.VerticalLayout; /** 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 * Panel that contains the thumbnail of an image */ public class ThumbnailPanel extends JLabel implements MouseListener, ActionListener { /** * Logger */ public static final org.apache.commons.logging.Log LOG = org.apache.commons.logging.LogFactory .getLog(ThumbnailPanel.class); /** * the image */ private Image image; /** * Popup menu to edit images info */ private JMenuItem menuAjouterTag; /** * Button to add tags */ private JButton boutonOkAjouterTag; /** * List of tags */ private JList listTags; /** * Dialog that allows to choose tags */ private JDialog dialogChoixTags; /** * Constructeur * @param image the image * @param imagesPanel the panel */ public ThumbnailPanel(Image image) { this.image = image; setToolTipText("
" + image.getName() + "
" + image.getVue() + " " + Messages.getMessage("hits") + "
"); this.addMouseListener(this); } protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D) g; g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC); double x = (getWidth() * ImagesManagement.getInstance().getMiniatureBufferedImage(image).getWidth()) / 2; double y = (getHeight() * ImagesManagement.getInstance().getMiniatureBufferedImage(image).getHeight()) / 2; g2.drawRenderedImage(ImagesManagement.getInstance().getMiniatureBufferedImage(image), null); } /* (non-Javadoc) * @see javax.swing.JComponent#getPreferredSize() */ public Dimension getPreferredSize() { int w = (int) (ImagesManagement.getInstance().getMiniatureBufferedImage(image).getWidth()); int h = (int) (ImagesManagement.getInstance().getMiniatureBufferedImage(image).getHeight() + 10); return new Dimension(w, h); } @Override public void mouseClicked(MouseEvent paramMouseEvent) { // on affiche l'image en grand ImagesManagement.getInstance().setCurrentImage(image); try { if (paramMouseEvent.getButton() == 1) { BrowserPanel.getInstance().changeImage(); } else if (paramMouseEvent.getButton() == 3) { JPopupMenu popup = new JPopupMenu(); menuAjouterTag = new JMenuItem(Messages.getMessage("thumbviewer_addTag")); menuAjouterTag.addActionListener(this); popup.add(menuAjouterTag); popup.show(this, paramMouseEvent.getX(), paramMouseEvent.getY()); } } catch (Exception e) { e.printStackTrace(); } } @Override public void mouseEntered(MouseEvent paramMouseEvent) { this.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); } @Override public void mouseExited(MouseEvent paramMouseEvent) { } @Override public void mousePressed(MouseEvent paramMouseEvent) { // TODO Auto-generated method stub } @Override public void mouseReleased(MouseEvent paramMouseEvent) { // TODO Auto-generated method stub } @Override public void actionPerformed(ActionEvent arg0) { if (arg0.getSource().equals(menuAjouterTag)) { try { //getting the list of tags List tagsDispo = TagService.getInstance(Main.sessionManager).lister(); //list to array (cause fucking JList does not support Lists) Tag[] tableauTagDispo = (Tag[]) tagsDispo.toArray(new Tag[tagsDispo.size()]); //getting the image's tags to preselect them List tagsDeLimage = TagService.getInstance(Main.sessionManager).tagsForImage(image); listTags = new JList(tableauTagDispo); //multiple selection is allowed listTags.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); listTags.setPreferredSize(new Dimension(100, 200)); //construct an array of the indices to select in the jlist int[] indices = new int[tagsDeLimage.size()]; int compteur = 0; for (int i = 0; i < tableauTagDispo.length; i++) { for (Tag tag : tagsDeLimage) { if (tag.getId().equals(tableauTagDispo[i].getId())) { indices[compteur++] = i; } } } listTags.setSelectedIndices(indices); JScrollPane scrollPane = new JScrollPane(listTags); dialogChoixTags = new JDialog(); dialogChoixTags.setLayout(new BorderLayout()); JPanel panelNorth = new JPanel(new VerticalLayout()); panelNorth.add(new JLabel(Messages.getMessage("thumbviewer_selectTag"))); panelNorth.add(scrollPane); dialogChoixTags.add(panelNorth, BorderLayout.NORTH); JPanel panelBouton = new JPanel(new FlowLayout()); boutonOkAjouterTag = new JButton("Ok"); panelBouton.add(boutonOkAjouterTag); boutonOkAjouterTag.addActionListener(this); dialogChoixTags.add(panelBouton, BorderLayout.CENTER); dialogChoixTags.setSize(new Dimension(400, 280)); dialogChoixTags.setLocationRelativeTo(null); dialogChoixTags.setVisible(true); } catch (IOException e) { e.printStackTrace(); } } else if (arg0.getSource().equals(boutonOkAjouterTag)) { StringBuffer tagIds = new StringBuffer(""); for (Object object : listTags.getSelectedValues()) { Tag tag = (Tag) object; tagIds.append(tag.getId() + ","); } tagIds.deleteCharAt(tagIds.lastIndexOf(",")); try { if (!ImageService.getInstance(Main.sessionManager).addTags(image, tagIds.toString())) { JOptionPane.showMessageDialog(this, Messages.getMessage("addingTagsError"), Messages .getMessage("error"), JOptionPane.ERROR_MESSAGE); } else { dialogChoixTags.dispose(); } } catch (IOException e) { e.printStackTrace(); } } } }