[6821] | 1 | package fr.mael.jiwigo.ui.mainframe; |
---|
| 2 | |
---|
[6968] | 3 | import java.awt.BorderLayout; |
---|
[6821] | 4 | import java.awt.Cursor; |
---|
| 5 | import java.awt.Dimension; |
---|
[6968] | 6 | import java.awt.FlowLayout; |
---|
[6821] | 7 | import java.awt.Graphics; |
---|
| 8 | import java.awt.Graphics2D; |
---|
[9431] | 9 | import java.awt.HeadlessException; |
---|
[6821] | 10 | import java.awt.RenderingHints; |
---|
[6965] | 11 | import java.awt.event.ActionEvent; |
---|
| 12 | import java.awt.event.ActionListener; |
---|
[6821] | 13 | import java.awt.event.MouseEvent; |
---|
| 14 | import java.awt.event.MouseListener; |
---|
[6968] | 15 | import java.util.List; |
---|
[8829] | 16 | |
---|
[6968] | 17 | import javax.swing.JButton; |
---|
| 18 | import javax.swing.JDialog; |
---|
[6821] | 19 | import javax.swing.JLabel; |
---|
[6968] | 20 | import javax.swing.JList; |
---|
[6965] | 21 | import javax.swing.JMenuItem; |
---|
[6968] | 22 | import javax.swing.JOptionPane; |
---|
| 23 | import javax.swing.JPanel; |
---|
[6965] | 24 | import javax.swing.JPopupMenu; |
---|
[6968] | 25 | import javax.swing.JScrollPane; |
---|
| 26 | import javax.swing.ListSelectionModel; |
---|
[8829] | 27 | |
---|
[6821] | 28 | import fr.mael.jiwigo.om.Image; |
---|
[6968] | 29 | import fr.mael.jiwigo.om.Tag; |
---|
[7070] | 30 | import fr.mael.jiwigo.transverse.ImagesManagement; |
---|
[10697] | 31 | import fr.mael.jiwigo.transverse.exception.JiwigoException; |
---|
[6833] | 32 | import fr.mael.jiwigo.transverse.util.Messages; |
---|
[9431] | 33 | import fr.mael.jiwigo.transverse.util.Tools; |
---|
[9921] | 34 | import fr.mael.jiwigo.transverse.util.spring.SpringUtils; |
---|
[8829] | 35 | import fr.mael.jiwigo.ui.browser.BrowserPanel; |
---|
[6968] | 36 | import fr.mael.jiwigo.ui.layout.VerticalLayout; |
---|
[6821] | 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. |
---|
[9385] | 63 | |
---|
[6821] | 64 | * @author mael |
---|
[6980] | 65 | * Panel that contains the thumbnail of an image |
---|
[6821] | 66 | */ |
---|
[6965] | 67 | public class ThumbnailPanel extends JLabel implements MouseListener, ActionListener { |
---|
[6821] | 68 | |
---|
[8829] | 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; |
---|
[6965] | 78 | |
---|
[8829] | 79 | /** |
---|
| 80 | * Popup menu to edit images info |
---|
| 81 | */ |
---|
| 82 | private JMenuItem menuAjouterTag; |
---|
[6968] | 83 | |
---|
[8829] | 84 | /** |
---|
[10717] | 85 | * Popup menu to edit images info |
---|
| 86 | */ |
---|
| 87 | private JMenuItem menuDeleteImage; |
---|
| 88 | |
---|
| 89 | /** |
---|
[8829] | 90 | * Button to add tags |
---|
| 91 | */ |
---|
| 92 | private JButton boutonOkAjouterTag; |
---|
[6968] | 93 | |
---|
[8829] | 94 | /** |
---|
| 95 | * List of tags |
---|
| 96 | */ |
---|
| 97 | private JList listTags; |
---|
[6968] | 98 | |
---|
[8829] | 99 | /** |
---|
| 100 | * Dialog that allows to choose tags |
---|
| 101 | */ |
---|
| 102 | private JDialog dialogChoixTags; |
---|
[10717] | 103 | /** |
---|
| 104 | * The parent |
---|
| 105 | */ |
---|
| 106 | private IThumbnailPanel parent; |
---|
[6821] | 107 | |
---|
[8829] | 108 | /** |
---|
| 109 | * Constructeur |
---|
| 110 | * @param image the image |
---|
| 111 | * @param imagesPanel the panel |
---|
| 112 | */ |
---|
[10717] | 113 | public ThumbnailPanel(Image image, IThumbnailPanel parent) { |
---|
[8829] | 114 | this.image = image; |
---|
[10717] | 115 | this.parent = parent; |
---|
[9393] | 116 | setToolTipText("<html><center>" + image.getName() + "<br/>" + image.getSeen() + " " |
---|
[8829] | 117 | + Messages.getMessage("hits") + "</center></html>"); |
---|
| 118 | this.addMouseListener(this); |
---|
| 119 | } |
---|
[6821] | 120 | |
---|
[8829] | 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 | } |
---|
[6821] | 129 | |
---|
[8829] | 130 | /* (non-Javadoc) |
---|
| 131 | * @see javax.swing.JComponent#getPreferredSize() |
---|
| 132 | */ |
---|
| 133 | public Dimension getPreferredSize() { |
---|
| 134 | int w = (int) (ImagesManagement.getInstance().getMiniatureBufferedImage(image).getWidth()); |
---|
| 135 | int h = (int) (ImagesManagement.getInstance().getMiniatureBufferedImage(image).getHeight() + 10); |
---|
| 136 | return new Dimension(w, h); |
---|
| 137 | } |
---|
[7221] | 138 | |
---|
[8829] | 139 | @Override |
---|
| 140 | public void mouseClicked(MouseEvent paramMouseEvent) { |
---|
| 141 | // on affiche l'image en grand |
---|
| 142 | ImagesManagement.getInstance().setCurrentImage(image); |
---|
| 143 | try { |
---|
| 144 | if (paramMouseEvent.getButton() == 1) { |
---|
| 145 | BrowserPanel.getInstance().changeImage(); |
---|
| 146 | } else if (paramMouseEvent.getButton() == 3) { |
---|
| 147 | JPopupMenu popup = new JPopupMenu(); |
---|
| 148 | menuAjouterTag = new JMenuItem(Messages.getMessage("thumbviewer_addTag")); |
---|
| 149 | menuAjouterTag.addActionListener(this); |
---|
| 150 | popup.add(menuAjouterTag); |
---|
[10717] | 151 | menuDeleteImage = new JMenuItem(Messages.getMessage("thumbviewer_deleteImage")); |
---|
| 152 | menuDeleteImage.addActionListener(this); |
---|
| 153 | popup.add(menuDeleteImage); |
---|
[8829] | 154 | popup.show(this, paramMouseEvent.getX(), paramMouseEvent.getY()); |
---|
| 155 | } |
---|
| 156 | } catch (Exception e) { |
---|
| 157 | e.printStackTrace(); |
---|
[6831] | 158 | } |
---|
| 159 | |
---|
[8829] | 160 | } |
---|
[6821] | 161 | |
---|
[8829] | 162 | @Override |
---|
| 163 | public void mouseEntered(MouseEvent paramMouseEvent) { |
---|
| 164 | this.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); |
---|
| 165 | } |
---|
[6821] | 166 | |
---|
[8829] | 167 | @Override |
---|
| 168 | public void mouseExited(MouseEvent paramMouseEvent) { |
---|
| 169 | } |
---|
[6821] | 170 | |
---|
[8829] | 171 | @Override |
---|
| 172 | public void mousePressed(MouseEvent paramMouseEvent) { |
---|
| 173 | // TODO Auto-generated method stub |
---|
[6821] | 174 | |
---|
[8829] | 175 | } |
---|
[6821] | 176 | |
---|
[8829] | 177 | @Override |
---|
| 178 | public void mouseReleased(MouseEvent paramMouseEvent) { |
---|
| 179 | // TODO Auto-generated method stub |
---|
[6821] | 180 | |
---|
[8829] | 181 | } |
---|
[6821] | 182 | |
---|
[8829] | 183 | @Override |
---|
| 184 | public void actionPerformed(ActionEvent arg0) { |
---|
| 185 | if (arg0.getSource().equals(menuAjouterTag)) { |
---|
| 186 | try { |
---|
| 187 | //getting the list of tags |
---|
[9921] | 188 | List<Tag> tagsDispo = SpringUtils.getTagService().list(); |
---|
[8829] | 189 | //list to array (cause fucking JList does not support Lists) |
---|
| 190 | Tag[] tableauTagDispo = (Tag[]) tagsDispo.toArray(new Tag[tagsDispo.size()]); |
---|
| 191 | //getting the image's tags to preselect them |
---|
[9921] | 192 | List<Tag> tagsDeLimage = SpringUtils.getTagService().tagsForImage(image); |
---|
[8829] | 193 | listTags = new JList(tableauTagDispo); |
---|
| 194 | //multiple selection is allowed |
---|
| 195 | listTags.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); |
---|
| 196 | listTags.setPreferredSize(new Dimension(100, 200)); |
---|
| 197 | //construct an array of the indices to select in the jlist |
---|
| 198 | int[] indices = new int[tagsDeLimage.size()]; |
---|
| 199 | int compteur = 0; |
---|
| 200 | for (int i = 0; i < tableauTagDispo.length; i++) { |
---|
| 201 | for (Tag tag : tagsDeLimage) { |
---|
[9393] | 202 | if (tag.getIdentifier().equals(tableauTagDispo[i].getIdentifier())) { |
---|
[8829] | 203 | indices[compteur++] = i; |
---|
[6965] | 204 | |
---|
[6968] | 205 | } |
---|
[8829] | 206 | } |
---|
| 207 | } |
---|
| 208 | listTags.setSelectedIndices(indices); |
---|
[6968] | 209 | |
---|
[8829] | 210 | JScrollPane scrollPane = new JScrollPane(listTags); |
---|
| 211 | dialogChoixTags = new JDialog(); |
---|
| 212 | dialogChoixTags.setLayout(new BorderLayout()); |
---|
| 213 | JPanel panelNorth = new JPanel(new VerticalLayout()); |
---|
| 214 | panelNorth.add(new JLabel(Messages.getMessage("thumbviewer_selectTag"))); |
---|
| 215 | panelNorth.add(scrollPane); |
---|
| 216 | dialogChoixTags.add(panelNorth, BorderLayout.NORTH); |
---|
| 217 | JPanel panelBouton = new JPanel(new FlowLayout()); |
---|
| 218 | boutonOkAjouterTag = new JButton("Ok"); |
---|
| 219 | panelBouton.add(boutonOkAjouterTag); |
---|
| 220 | boutonOkAjouterTag.addActionListener(this); |
---|
| 221 | dialogChoixTags.add(panelBouton, BorderLayout.CENTER); |
---|
| 222 | dialogChoixTags.setSize(new Dimension(400, 280)); |
---|
| 223 | dialogChoixTags.setLocationRelativeTo(null); |
---|
| 224 | dialogChoixTags.setVisible(true); |
---|
[10697] | 225 | } catch (JiwigoException e) { |
---|
[9431] | 226 | LOG.error(Tools.getStackTrace(e)); |
---|
[8829] | 227 | } |
---|
| 228 | } else if (arg0.getSource().equals(boutonOkAjouterTag)) { |
---|
| 229 | StringBuffer tagIds = new StringBuffer(""); |
---|
| 230 | for (Object object : listTags.getSelectedValues()) { |
---|
| 231 | Tag tag = (Tag) object; |
---|
[9393] | 232 | tagIds.append(tag.getIdentifier() + ","); |
---|
[8829] | 233 | } |
---|
| 234 | tagIds.deleteCharAt(tagIds.lastIndexOf(",")); |
---|
| 235 | try { |
---|
[10697] | 236 | if (!SpringUtils.getImageService().addTags(image, tagIds.toString())) { |
---|
| 237 | JOptionPane.showMessageDialog(this, Messages.getMessage("addingTagsError"), |
---|
| 238 | Messages.getMessage("error"), JOptionPane.ERROR_MESSAGE); |
---|
| 239 | } else { |
---|
| 240 | dialogChoixTags.dispose(); |
---|
[6968] | 241 | } |
---|
[10697] | 242 | } catch (HeadlessException e) { |
---|
| 243 | LOG.error(Tools.getStackTrace(e)); |
---|
| 244 | } catch (JiwigoException e) { |
---|
| 245 | LOG.error(Tools.getStackTrace(e)); |
---|
[8829] | 246 | } |
---|
| 247 | |
---|
[10717] | 248 | } else if (arg0.getSource().equals(menuDeleteImage)) { |
---|
| 249 | try { |
---|
| 250 | if (SpringUtils.getImageService().delete(image)) { |
---|
| 251 | parent.refresh(); |
---|
| 252 | } else { |
---|
| 253 | JOptionPane.showMessageDialog(this, Messages.getMessage("deletingImageError"), |
---|
| 254 | Messages.getMessage("error"), JOptionPane.ERROR_MESSAGE); |
---|
| 255 | } |
---|
| 256 | } catch (JiwigoException e) { |
---|
| 257 | LOG.error(Tools.getStackTrace(e)); |
---|
| 258 | JOptionPane.showMessageDialog(this, Messages.getMessage("deletingImageError"), |
---|
| 259 | Messages.getMessage("error"), JOptionPane.ERROR_MESSAGE); |
---|
| 260 | } |
---|
[6968] | 261 | } |
---|
[8829] | 262 | } |
---|
[10717] | 263 | |
---|
[6821] | 264 | } |
---|