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

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

Adds a wait cursor for time consuming operations.

File size: 9.4 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        try {
135            int w = (int) (ImagesManagement.getInstance().getMiniatureBufferedImage(image).getWidth());
136            int h = (int) (ImagesManagement.getInstance().getMiniatureBufferedImage(image).getHeight() + 10);
137            return new Dimension(w, h);
138        } catch (NullPointerException e) {
139            return new Dimension(100, 100);
140        }
141    }
142
143    @Override
144    public void mouseClicked(MouseEvent paramMouseEvent) {
145        // on affiche l'image en grand
146        MainFrame.getInstance().showWaitCursor(true);
147        ImagesManagement.getInstance().setCurrentImage(image);
148        try {
149            if (paramMouseEvent.getButton() == 1) {
150                BrowserPanel.getInstance().changeImage();
151            } else if (paramMouseEvent.getButton() == 3) {
152                JPopupMenu popup = new JPopupMenu();
153                menuAjouterTag = new JMenuItem(Messages.getMessage("thumbviewer_addTag"));
154                menuAjouterTag.addActionListener(this);
155                popup.add(menuAjouterTag);
156                menuDeleteImage = new JMenuItem(Messages.getMessage("thumbviewer_deleteImage"));
157                menuDeleteImage.addActionListener(this);
158                popup.add(menuDeleteImage);
159                popup.show(this, paramMouseEvent.getX(), paramMouseEvent.getY());
160            }
161        } catch (Exception e) {
162            e.printStackTrace();
163        }
164        MainFrame.getInstance().showWaitCursor(false);
165
166    }
167
168    @Override
169    public void mouseEntered(MouseEvent paramMouseEvent) {
170        this.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
171    }
172
173    @Override
174    public void mouseExited(MouseEvent paramMouseEvent) {
175    }
176
177    @Override
178    public void mousePressed(MouseEvent paramMouseEvent) {
179        // TODO Auto-generated method stub
180
181    }
182
183    @Override
184    public void mouseReleased(MouseEvent paramMouseEvent) {
185        // TODO Auto-generated method stub
186
187    }
188
189    @Override
190    public void actionPerformed(ActionEvent arg0) {
191        if (arg0.getSource().equals(menuAjouterTag)) {
192            try {
193                //getting the list of tags
194                List<Tag> tagsDispo = SpringUtils.getTagService().list();
195                //list to array (cause fucking JList does not support Lists)
196                Tag[] tableauTagDispo = (Tag[]) tagsDispo.toArray(new Tag[tagsDispo.size()]);
197                //getting the image's tags to preselect them
198                List<Tag> tagsDeLimage = SpringUtils.getTagService().tagsForImage(image);
199                listTags = new JList(tableauTagDispo);
200                //multiple selection is allowed
201                listTags.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
202                listTags.setPreferredSize(new Dimension(100, 200));
203                //construct an array of the indices to select in the jlist
204                int[] indices = new int[tagsDeLimage.size()];
205                int compteur = 0;
206                for (int i = 0; i < tableauTagDispo.length; i++) {
207                    for (Tag tag : tagsDeLimage) {
208                        if (tag.getIdentifier().equals(tableauTagDispo[i].getIdentifier())) {
209                            indices[compteur++] = i;
210
211                        }
212                    }
213                }
214                listTags.setSelectedIndices(indices);
215
216                JScrollPane scrollPane = new JScrollPane(listTags);
217                dialogChoixTags = new JDialog();
218                dialogChoixTags.setLayout(new BorderLayout());
219                JPanel panelNorth = new JPanel(new VerticalLayout());
220                panelNorth.add(new JLabel(Messages.getMessage("thumbviewer_selectTag")));
221                panelNorth.add(scrollPane);
222                dialogChoixTags.add(panelNorth, BorderLayout.NORTH);
223                JPanel panelBouton = new JPanel(new FlowLayout());
224                boutonOkAjouterTag = new JButton("Ok");
225                panelBouton.add(boutonOkAjouterTag);
226                boutonOkAjouterTag.addActionListener(this);
227                dialogChoixTags.add(panelBouton, BorderLayout.CENTER);
228                dialogChoixTags.setSize(new Dimension(400, 280));
229                dialogChoixTags.setLocationRelativeTo(null);
230                dialogChoixTags.setVisible(true);
231            } catch (JiwigoException e) {
232                LOG.error(Tools.getStackTrace(e));
233            }
234        } else if (arg0.getSource().equals(boutonOkAjouterTag)) {
235            StringBuffer tagIds = new StringBuffer("");
236            for (Object object : listTags.getSelectedValues()) {
237                Tag tag = (Tag) object;
238                tagIds.append(tag.getIdentifier() + ",");
239            }
240            tagIds.deleteCharAt(tagIds.lastIndexOf(","));
241            try {
242                if (!SpringUtils.getImageService().addTags(image, tagIds.toString())) {
243                    JOptionPane.showMessageDialog(this, Messages.getMessage("addingTagsError"),
244                            Messages.getMessage("error"), JOptionPane.ERROR_MESSAGE);
245                } else {
246                    dialogChoixTags.dispose();
247                }
248            } catch (HeadlessException e) {
249                LOG.error(Tools.getStackTrace(e));
250            } catch (JiwigoException e) {
251                LOG.error(Tools.getStackTrace(e));
252            }
253
254        } else if (arg0.getSource().equals(menuDeleteImage)) {
255            int response = JOptionPane.showConfirmDialog(null,
256                    Messages.getMessage("thumbviewer_deleteImageConfirmation"), Messages.getMessage("confirmation"),
257                    JOptionPane.YES_NO_OPTION);
258            if (response == 0) {
259                try {
260                    if (SpringUtils.getImageService().delete(image)) {
261                        parent.refresh();
262                    } else {
263                        JOptionPane.showMessageDialog(this, Messages.getMessage("deletingImageError"),
264                                Messages.getMessage("error"), JOptionPane.ERROR_MESSAGE);
265                    }
266                } catch (JiwigoException e) {
267                    LOG.error(Tools.getStackTrace(e));
268                    JOptionPane.showMessageDialog(this, Messages.getMessage("deletingImageError"),
269                            Messages.getMessage("error"), JOptionPane.ERROR_MESSAGE);
270                }
271            }
272        }
273    }
274
275}
Note: See TracBrowser for help on using the repository browser.