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

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

Changes ImagesManagement access from static to object.

File size: 7.9 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.ImagesManagement;
33import fr.mael.jiwigo.transverse.util.Messages;
34import fr.mael.jiwigo.ui.browser.BrowserPanel;
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    /**
69     * Logger
70     */
71    public static final org.apache.commons.logging.Log LOG = org.apache.commons.logging.LogFactory
72            .getLog(ThumbnailPanel.class);
73    /**
74     * the image
75     */
76    private Image image;
77
78    /**
79     * Popup menu to edit images info
80     */
81    private JMenuItem menuAjouterTag;
82
83    /**
84     * Button to add tags
85     */
86    private JButton boutonOkAjouterTag;
87
88    /**
89     * List of tags
90     */
91    private JList listTags;
92
93    /**
94     * Dialog that allows to choose tags
95     */
96    private JDialog dialogChoixTags;
97
98    /**
99     * Constructeur
100     * @param image the image
101     * @param imagesPanel the panel
102     */
103    public ThumbnailPanel(Image image) {
104        this.image = image;
105        setToolTipText("<html><center>" + image.getName() + "<br/>" + image.getVue() + " "
106                + Messages.getMessage("hits") + "</center></html>");
107        this.addMouseListener(this);
108    }
109
110    protected void paintComponent(Graphics g) {
111        super.paintComponent(g);
112        Graphics2D g2 = (Graphics2D) g;
113        g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
114        double x = (getWidth() * ImagesManagement.getInstance().getMiniatureBufferedImage(image).getWidth()) / 2;
115        double y = (getHeight() * ImagesManagement.getInstance().getMiniatureBufferedImage(image).getHeight()) / 2;
116        g2.drawRenderedImage(ImagesManagement.getInstance().getMiniatureBufferedImage(image), null);
117    }
118
119    /* (non-Javadoc)
120     * @see javax.swing.JComponent#getPreferredSize()
121     */
122    public Dimension getPreferredSize() {
123        int w = (int) (ImagesManagement.getInstance().getMiniatureBufferedImage(image).getWidth());
124        int h = (int) (ImagesManagement.getInstance().getMiniatureBufferedImage(image).getHeight() + 10);
125        return new Dimension(w, h);
126    }
127
128    @Override
129    public void mouseClicked(MouseEvent paramMouseEvent) {
130        // on affiche l'image en grand
131        ImagesManagement.getInstance().setCurrentImage(image);
132        try {
133            if (paramMouseEvent.getButton() == 1) {
134                BrowserPanel.getInstance().changeImage();
135            } else if (paramMouseEvent.getButton() == 3) {
136                JPopupMenu popup = new JPopupMenu();
137                menuAjouterTag = new JMenuItem(Messages.getMessage("thumbviewer_addTag"));
138                menuAjouterTag.addActionListener(this);
139                popup.add(menuAjouterTag);
140                popup.show(this, paramMouseEvent.getX(), paramMouseEvent.getY());
141            }
142        } catch (Exception e) {
143            e.printStackTrace();
144        }
145
146    }
147
148    @Override
149    public void mouseEntered(MouseEvent paramMouseEvent) {
150        this.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
151    }
152
153    @Override
154    public void mouseExited(MouseEvent paramMouseEvent) {
155    }
156
157    @Override
158    public void mousePressed(MouseEvent paramMouseEvent) {
159        // TODO Auto-generated method stub
160
161    }
162
163    @Override
164    public void mouseReleased(MouseEvent paramMouseEvent) {
165        // TODO Auto-generated method stub
166
167    }
168
169    @Override
170    public void actionPerformed(ActionEvent arg0) {
171        if (arg0.getSource().equals(menuAjouterTag)) {
172            try {
173                //getting the list of tags
174                List<Tag> tagsDispo = TagService.getInstance().lister();
175                //list to array (cause fucking JList does not support Lists)
176                Tag[] tableauTagDispo = (Tag[]) tagsDispo.toArray(new Tag[tagsDispo.size()]);
177                //getting the image's tags to preselect them
178                List<Tag> tagsDeLimage = TagService.getInstance().tagsForImage(image);
179                listTags = new JList(tableauTagDispo);
180                //multiple selection is allowed
181                listTags.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
182                listTags.setPreferredSize(new Dimension(100, 200));
183                //construct an array of the indices to select in the jlist
184                int[] indices = new int[tagsDeLimage.size()];
185                int compteur = 0;
186                for (int i = 0; i < tableauTagDispo.length; i++) {
187                    for (Tag tag : tagsDeLimage) {
188                        if (tag.getId().equals(tableauTagDispo[i].getId())) {
189                            indices[compteur++] = i;
190
191                        }
192                    }
193                }
194                listTags.setSelectedIndices(indices);
195
196                JScrollPane scrollPane = new JScrollPane(listTags);
197                dialogChoixTags = new JDialog();
198                dialogChoixTags.setLayout(new BorderLayout());
199                JPanel panelNorth = new JPanel(new VerticalLayout());
200                panelNorth.add(new JLabel(Messages.getMessage("thumbviewer_selectTag")));
201                panelNorth.add(scrollPane);
202                dialogChoixTags.add(panelNorth, BorderLayout.NORTH);
203                JPanel panelBouton = new JPanel(new FlowLayout());
204                boutonOkAjouterTag = new JButton("Ok");
205                panelBouton.add(boutonOkAjouterTag);
206                boutonOkAjouterTag.addActionListener(this);
207                dialogChoixTags.add(panelBouton, BorderLayout.CENTER);
208                dialogChoixTags.setSize(new Dimension(400, 280));
209                dialogChoixTags.setLocationRelativeTo(null);
210                dialogChoixTags.setVisible(true);
211            } catch (IOException e) {
212                e.printStackTrace();
213            }
214        } else if (arg0.getSource().equals(boutonOkAjouterTag)) {
215            StringBuffer tagIds = new StringBuffer("");
216            for (Object object : listTags.getSelectedValues()) {
217                Tag tag = (Tag) object;
218                tagIds.append(tag.getId() + ",");
219            }
220            tagIds.deleteCharAt(tagIds.lastIndexOf(","));
221            try {
222                if (!ImageService.getInstance().addTags(image, tagIds.toString())) {
223                    JOptionPane.showMessageDialog(this, Messages.getMessage("addingTagsError"), Messages
224                            .getMessage("error"), JOptionPane.ERROR_MESSAGE);
225                } else {
226                    dialogChoixTags.dispose();
227                }
228            } catch (IOException e) {
229                e.printStackTrace();
230            }
231
232        }
233    }
234}
Note: See TracBrowser for help on using the repository browser.