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

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

Modifications to use the last version of jiwigo-ws-api which manages proxy errors.

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