source: extensions/jiwigo/trunk/src/main/java/fr/mael/jiwigo/ui/mainframe/ThumbnailPanel.java @ 10717

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

Adds support for pwg.images.delete : images can be deleted from the thumbnail panel (right click).

File size: 9.0 KB
Line 
1package fr.mael.jiwigo.ui.mainframe;
2
3import java.awt.BorderLayout;
4import java.awt.Cursor;
5import java.awt.Dimension;
6import java.awt.FlowLayout;
7import java.awt.Graphics;
8import java.awt.Graphics2D;
9import java.awt.HeadlessException;
10import java.awt.RenderingHints;
11import java.awt.event.ActionEvent;
12import java.awt.event.ActionListener;
13import java.awt.event.MouseEvent;
14import java.awt.event.MouseListener;
15import java.util.List;
16
17import javax.swing.JButton;
18import javax.swing.JDialog;
19import javax.swing.JLabel;
20import javax.swing.JList;
21import javax.swing.JMenuItem;
22import javax.swing.JOptionPane;
23import javax.swing.JPanel;
24import javax.swing.JPopupMenu;
25import javax.swing.JScrollPane;
26import javax.swing.ListSelectionModel;
27
28import fr.mael.jiwigo.om.Image;
29import fr.mael.jiwigo.om.Tag;
30import fr.mael.jiwigo.transverse.ImagesManagement;
31import fr.mael.jiwigo.transverse.exception.JiwigoException;
32import fr.mael.jiwigo.transverse.util.Messages;
33import fr.mael.jiwigo.transverse.util.Tools;
34import fr.mael.jiwigo.transverse.util.spring.SpringUtils;
35import fr.mael.jiwigo.ui.browser.BrowserPanel;
36import fr.mael.jiwigo.ui.layout.VerticalLayout;
37
38/**
39   Copyright (c) 2010, Mael
40   All rights reserved.
41
42   Redistribution and use in source and binary forms, with or without
43   modification, are permitted provided that the following conditions are met:
44    * Redistributions of source code must retain the above copyright
45      notice, this list of conditions and the following disclaimer.
46    * Redistributions in binary form must reproduce the above copyright
47      notice, this list of conditions and the following disclaimer in the
48      documentation and/or other materials provided with the distribution.
49    * Neither the name of jiwigo nor the
50      names of its contributors may be used to endorse or promote products
51      derived from this software without specific prior written permission.
52
53   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
54   ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
55   WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
56   DISCLAIMED. IN NO EVENT SHALL Mael BE LIABLE FOR ANY
57   DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
58   (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
59   LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
60   ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
61   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
62   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
63
64 * @author mael
65 * Panel that contains the thumbnail of an image
66 */
67public class ThumbnailPanel extends JLabel implements MouseListener, ActionListener {
68
69    /**
70     * Logger
71     */
72    public static final org.apache.commons.logging.Log LOG = org.apache.commons.logging.LogFactory
73            .getLog(ThumbnailPanel.class);
74    /**
75     * the image
76     */
77    private Image image;
78
79    /**
80     * Popup menu to edit images info
81     */
82    private JMenuItem menuAjouterTag;
83
84    /**
85     * Popup menu to edit images info
86     */
87    private JMenuItem menuDeleteImage;
88
89    /**
90     * Button to add tags
91     */
92    private JButton boutonOkAjouterTag;
93
94    /**
95     * List of tags
96     */
97    private JList listTags;
98
99    /**
100     * Dialog that allows to choose tags
101     */
102    private JDialog dialogChoixTags;
103    /**
104     * The parent
105     */
106    private IThumbnailPanel parent;
107
108    /**
109     * Constructeur
110     * @param image the image
111     * @param imagesPanel the panel
112     */
113    public ThumbnailPanel(Image image, IThumbnailPanel parent) {
114        this.image = image;
115        this.parent = parent;
116        setToolTipText("<html><center>" + image.getName() + "<br/>" + image.getSeen() + " "
117                + Messages.getMessage("hits") + "</center></html>");
118        this.addMouseListener(this);
119    }
120
121    protected void paintComponent(Graphics g) {
122        super.paintComponent(g);
123        Graphics2D g2 = (Graphics2D) g;
124        g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
125        double x = (getWidth() * ImagesManagement.getInstance().getMiniatureBufferedImage(image).getWidth()) / 2;
126        double y = (getHeight() * ImagesManagement.getInstance().getMiniatureBufferedImage(image).getHeight()) / 2;
127        g2.drawRenderedImage(ImagesManagement.getInstance().getMiniatureBufferedImage(image), null);
128    }
129
130    /* (non-Javadoc)
131     * @see javax.swing.JComponent#getPreferredSize()
132     */
133    public Dimension getPreferredSize() {
134        int w = (int) (ImagesManagement.getInstance().getMiniatureBufferedImage(image).getWidth());
135        int h = (int) (ImagesManagement.getInstance().getMiniatureBufferedImage(image).getHeight() + 10);
136        return new Dimension(w, h);
137    }
138
139    @Override
140    public void mouseClicked(MouseEvent paramMouseEvent) {
141        // on affiche l'image en grand
142        ImagesManagement.getInstance().setCurrentImage(image);
143        try {
144            if (paramMouseEvent.getButton() == 1) {
145                BrowserPanel.getInstance().changeImage();
146            } else if (paramMouseEvent.getButton() == 3) {
147                JPopupMenu popup = new JPopupMenu();
148                menuAjouterTag = new JMenuItem(Messages.getMessage("thumbviewer_addTag"));
149                menuAjouterTag.addActionListener(this);
150                popup.add(menuAjouterTag);
151                menuDeleteImage = new JMenuItem(Messages.getMessage("thumbviewer_deleteImage"));
152                menuDeleteImage.addActionListener(this);
153                popup.add(menuDeleteImage);
154                popup.show(this, paramMouseEvent.getX(), paramMouseEvent.getY());
155            }
156        } catch (Exception e) {
157            e.printStackTrace();
158        }
159
160    }
161
162    @Override
163    public void mouseEntered(MouseEvent paramMouseEvent) {
164        this.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
165    }
166
167    @Override
168    public void mouseExited(MouseEvent paramMouseEvent) {
169    }
170
171    @Override
172    public void mousePressed(MouseEvent paramMouseEvent) {
173        // TODO Auto-generated method stub
174
175    }
176
177    @Override
178    public void mouseReleased(MouseEvent paramMouseEvent) {
179        // TODO Auto-generated method stub
180
181    }
182
183    @Override
184    public void actionPerformed(ActionEvent arg0) {
185        if (arg0.getSource().equals(menuAjouterTag)) {
186            try {
187                //getting the list of tags
188                List<Tag> tagsDispo = SpringUtils.getTagService().list();
189                //list to array (cause fucking JList does not support Lists)
190                Tag[] tableauTagDispo = (Tag[]) tagsDispo.toArray(new Tag[tagsDispo.size()]);
191                //getting the image's tags to preselect them
192                List<Tag> tagsDeLimage = SpringUtils.getTagService().tagsForImage(image);
193                listTags = new JList(tableauTagDispo);
194                //multiple selection is allowed
195                listTags.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
196                listTags.setPreferredSize(new Dimension(100, 200));
197                //construct an array of the indices to select in the jlist
198                int[] indices = new int[tagsDeLimage.size()];
199                int compteur = 0;
200                for (int i = 0; i < tableauTagDispo.length; i++) {
201                    for (Tag tag : tagsDeLimage) {
202                        if (tag.getIdentifier().equals(tableauTagDispo[i].getIdentifier())) {
203                            indices[compteur++] = i;
204
205                        }
206                    }
207                }
208                listTags.setSelectedIndices(indices);
209
210                JScrollPane scrollPane = new JScrollPane(listTags);
211                dialogChoixTags = new JDialog();
212                dialogChoixTags.setLayout(new BorderLayout());
213                JPanel panelNorth = new JPanel(new VerticalLayout());
214                panelNorth.add(new JLabel(Messages.getMessage("thumbviewer_selectTag")));
215                panelNorth.add(scrollPane);
216                dialogChoixTags.add(panelNorth, BorderLayout.NORTH);
217                JPanel panelBouton = new JPanel(new FlowLayout());
218                boutonOkAjouterTag = new JButton("Ok");
219                panelBouton.add(boutonOkAjouterTag);
220                boutonOkAjouterTag.addActionListener(this);
221                dialogChoixTags.add(panelBouton, BorderLayout.CENTER);
222                dialogChoixTags.setSize(new Dimension(400, 280));
223                dialogChoixTags.setLocationRelativeTo(null);
224                dialogChoixTags.setVisible(true);
225            } catch (JiwigoException e) {
226                LOG.error(Tools.getStackTrace(e));
227            }
228        } else if (arg0.getSource().equals(boutonOkAjouterTag)) {
229            StringBuffer tagIds = new StringBuffer("");
230            for (Object object : listTags.getSelectedValues()) {
231                Tag tag = (Tag) object;
232                tagIds.append(tag.getIdentifier() + ",");
233            }
234            tagIds.deleteCharAt(tagIds.lastIndexOf(","));
235            try {
236                if (!SpringUtils.getImageService().addTags(image, tagIds.toString())) {
237                    JOptionPane.showMessageDialog(this, Messages.getMessage("addingTagsError"),
238                            Messages.getMessage("error"), JOptionPane.ERROR_MESSAGE);
239                } else {
240                    dialogChoixTags.dispose();
241                }
242            } catch (HeadlessException e) {
243                LOG.error(Tools.getStackTrace(e));
244            } catch (JiwigoException e) {
245                LOG.error(Tools.getStackTrace(e));
246            }
247
248        } else if (arg0.getSource().equals(menuDeleteImage)) {
249            try {
250                if (SpringUtils.getImageService().delete(image)) {
251                    parent.refresh();
252                } else {
253                    JOptionPane.showMessageDialog(this, Messages.getMessage("deletingImageError"),
254                            Messages.getMessage("error"), JOptionPane.ERROR_MESSAGE);
255                }
256            } catch (JiwigoException e) {
257                LOG.error(Tools.getStackTrace(e));
258                JOptionPane.showMessageDialog(this, Messages.getMessage("deletingImageError"),
259                        Messages.getMessage("error"), JOptionPane.ERROR_MESSAGE);
260            }
261        }
262    }
263
264}
Note: See TracBrowser for help on using the repository browser.