package fr.mael.jiwigo.ui.comments; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Insets; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.IOException; import java.util.List; import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; import fr.mael.jiwigo.Main; import fr.mael.jiwigo.om.Comment; import fr.mael.jiwigo.service.CommentService; import fr.mael.jiwigo.transverse.exception.ProxyAuthenticationException; import fr.mael.jiwigo.transverse.util.Messages; import fr.mael.jiwigo.transverse.util.Tools; import fr.mael.jiwigo.ui.browser.BrowserPanel; /** Copyright (c) 2010, Mael All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of jiwigo nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Mael BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * @author mael * dialog that displays the comments of an image and allows to add one */ public class CommentsDialog extends JDialog implements ActionListener { /** * Logger */ public static final org.apache.commons.logging.Log LOG = org.apache.commons.logging.LogFactory .getLog(CommentsDialog.class); /** * booleen that allows to alernate the colors */ private boolean alternate = true; /** * area to write a new comment */ private JTextArea textArea = new JTextArea(); /** * sends the new comment */ private JButton boutonEnvoyer = new JButton("Ok"); /** * id of the image */ private Integer imageId; /** * panel that allows to scroll */ private JPanel panelScrollCommentaires = new JPanel(new GridBagLayout()); /** * panel that allows to add a comment */ private JPanel panelAjouterCommentaire = new JPanel(new FlowLayout()); /** * Scrollpane */ private JScrollPane scrollPaneArea; /** * scrollpane that allows to scroll */ private JScrollPane scrollPaneCommentaires; /** * Constructor * @param imageId the id of the image */ public CommentsDialog(BrowserPanel parent, Integer imageId) { // super(parent); this.imageId = imageId; this.setTitle(Messages.getMessage("commentaires")); panelScrollCommentaires.setOpaque(true); scrollPaneCommentaires = new JScrollPane(panelScrollCommentaires, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); scrollPaneArea = new JScrollPane(textArea, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); scrollPaneArea.setPreferredSize(new Dimension(400, 100)); scrollPaneCommentaires.setPreferredSize(new Dimension(700, 500)); rafraichir(); this.setSize(new Dimension(700, 700)); this.setLocationRelativeTo(null); this.setVisible(true); } private void rafraichir() { this.getContentPane().removeAll(); try { List commentsForImage = CommentService.getInstance(Main.sessionManager).list(imageId); GridBagConstraints c = new GridBagConstraints(); c.insets = new Insets(3, 3, 3, 3); c.gridx = 0; c.gridy = 0; //boucle sur tous les commentaires for (Comment comment : commentsForImage) { CommentPanel panel; if (alternate) { panel = new CommentPanel(new Color(153, 204, 51), comment.getContent(), comment.getAuthor(), comment.getDate()); } else { panel = new CommentPanel(new Color(77, 204, 51), comment.getContent(), comment.getAuthor(), comment .getDate()); } alternate = !alternate; panelScrollCommentaires.add(panel, c); c.gridy++; } } catch (Exception e) { LOG.error(Tools.getStackTrace(e)); JOptionPane.showMessageDialog(null, Messages.getMessage("commentListingError"), Messages .getMessage("error"), JOptionPane.ERROR_MESSAGE); } panelAjouterCommentaire.add(scrollPaneArea); boutonEnvoyer.addActionListener(this); panelAjouterCommentaire.add(boutonEnvoyer); this.getContentPane().setLayout(new BorderLayout()); this.getContentPane().add(scrollPaneCommentaires, BorderLayout.NORTH); this.getContentPane().add(panelAjouterCommentaire, BorderLayout.CENTER); this.getContentPane().repaint(); ((JPanel) this.getContentPane()).revalidate(); } @Override public void actionPerformed(ActionEvent arg0) { String text = textArea.getText(); //on n'envoie pas de commentaire vide if (text.equals("")) { JOptionPane.showMessageDialog(null, Messages.getMessage("emptyCommentError"), Messages.getMessage("error"), JOptionPane.ERROR_MESSAGE); } else { boolean reussite = true; try { reussite = CommentService.getInstance(Main.sessionManager).create(text, this.imageId, Main.sessionManager.getLogin()); } catch (IOException e) { reussite = false; LOG.error(Tools.getStackTrace(e)); } catch (ProxyAuthenticationException e) { reussite = false; LOG.error(Tools.getStackTrace(e)); } catch (Exception e) { reussite = false; LOG.error(Tools.getStackTrace(e)); } if (reussite) { rafraichir(); } else { JOptionPane.showMessageDialog(null, Messages.getMessage("addCommentError"), Messages .getMessage("error"), JOptionPane.ERROR_MESSAGE); } } } }