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