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

Last change on this file since 6980 was 6980, checked in by mlg, 14 years ago

Translation of the comments
French -> English

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