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

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

Adds an exception management + changes privacy level dialog.

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