[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; |
---|
| 9 | import java.awt.RenderingHints; |
---|
[6965] | 10 | import java.awt.event.ActionEvent; |
---|
| 11 | import java.awt.event.ActionListener; |
---|
[6821] | 12 | import java.awt.event.MouseEvent; |
---|
| 13 | import java.awt.event.MouseListener; |
---|
[6968] | 14 | import java.io.IOException; |
---|
| 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 | |
---|
[9385] | 28 | import fr.mael.jiwigo.Main; |
---|
[6821] | 29 | import fr.mael.jiwigo.om.Image; |
---|
[6968] | 30 | import fr.mael.jiwigo.om.Tag; |
---|
| 31 | import fr.mael.jiwigo.service.ImageService; |
---|
| 32 | import fr.mael.jiwigo.service.TagService; |
---|
[7070] | 33 | import fr.mael.jiwigo.transverse.ImagesManagement; |
---|
[6833] | 34 | import fr.mael.jiwigo.transverse.util.Messages; |
---|
[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 | /** |
---|
| 85 | * Button to add tags |
---|
| 86 | */ |
---|
| 87 | private JButton boutonOkAjouterTag; |
---|
[6968] | 88 | |
---|
[8829] | 89 | /** |
---|
| 90 | * List of tags |
---|
| 91 | */ |
---|
| 92 | private JList listTags; |
---|
[6968] | 93 | |
---|
[8829] | 94 | /** |
---|
| 95 | * Dialog that allows to choose tags |
---|
| 96 | */ |
---|
| 97 | private JDialog dialogChoixTags; |
---|
[6821] | 98 | |
---|
[8829] | 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 | } |
---|
[6821] | 110 | |
---|
[8829] | 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 | } |
---|
[6821] | 119 | |
---|
[8829] | 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 | } |
---|
[7221] | 128 | |
---|
[8829] | 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(); |
---|
[6831] | 145 | } |
---|
| 146 | |
---|
[8829] | 147 | } |
---|
[6821] | 148 | |
---|
[8829] | 149 | @Override |
---|
| 150 | public void mouseEntered(MouseEvent paramMouseEvent) { |
---|
| 151 | this.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); |
---|
| 152 | } |
---|
[6821] | 153 | |
---|
[8829] | 154 | @Override |
---|
| 155 | public void mouseExited(MouseEvent paramMouseEvent) { |
---|
| 156 | } |
---|
[6821] | 157 | |
---|
[8829] | 158 | @Override |
---|
| 159 | public void mousePressed(MouseEvent paramMouseEvent) { |
---|
| 160 | // TODO Auto-generated method stub |
---|
[6821] | 161 | |
---|
[8829] | 162 | } |
---|
[6821] | 163 | |
---|
[8829] | 164 | @Override |
---|
| 165 | public void mouseReleased(MouseEvent paramMouseEvent) { |
---|
| 166 | // TODO Auto-generated method stub |
---|
[6821] | 167 | |
---|
[8829] | 168 | } |
---|
[6821] | 169 | |
---|
[8829] | 170 | @Override |
---|
| 171 | public void actionPerformed(ActionEvent arg0) { |
---|
| 172 | if (arg0.getSource().equals(menuAjouterTag)) { |
---|
| 173 | try { |
---|
| 174 | //getting the list of tags |
---|
[9385] | 175 | List<Tag> tagsDispo = TagService.getInstance(Main.sessionManager).lister(); |
---|
[8829] | 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 |
---|
[9385] | 179 | List<Tag> tagsDeLimage = TagService.getInstance(Main.sessionManager).tagsForImage(image); |
---|
[8829] | 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; |
---|
[6965] | 191 | |
---|
[6968] | 192 | } |
---|
[8829] | 193 | } |
---|
| 194 | } |
---|
| 195 | listTags.setSelectedIndices(indices); |
---|
[6968] | 196 | |
---|
[8829] | 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 { |
---|
[9385] | 223 | if (!ImageService.getInstance(Main.sessionManager).addTags(image, tagIds.toString())) { |
---|
[8829] | 224 | JOptionPane.showMessageDialog(this, Messages.getMessage("addingTagsError"), Messages |
---|
| 225 | .getMessage("error"), JOptionPane.ERROR_MESSAGE); |
---|
| 226 | } else { |
---|
| 227 | dialogChoixTags.dispose(); |
---|
[6968] | 228 | } |
---|
[8829] | 229 | } catch (IOException e) { |
---|
| 230 | e.printStackTrace(); |
---|
| 231 | } |
---|
| 232 | |
---|
[6968] | 233 | } |
---|
[8829] | 234 | } |
---|
[6821] | 235 | } |
---|