source: extensions/jiwigo/trunk/src/main/java/fr/mael/jiwigo/ui/comments/CommentsDialog.java @ 10697

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

Integrates last jiwigo-ws-api modifications

File size: 6.3 KB
Line 
1package fr.mael.jiwigo.ui.comments;
2
3import java.awt.BorderLayout;
4import java.awt.Color;
5import java.awt.Dimension;
6import java.awt.FlowLayout;
7import java.awt.GridBagConstraints;
8import java.awt.GridBagLayout;
9import java.awt.Insets;
10import java.awt.event.ActionEvent;
11import java.awt.event.ActionListener;
12import java.util.List;
13
14import javax.swing.JButton;
15import javax.swing.JDialog;
16import javax.swing.JOptionPane;
17import javax.swing.JPanel;
18import javax.swing.JScrollPane;
19import javax.swing.JTextArea;
20
21import fr.mael.jiwigo.om.Comment;
22import fr.mael.jiwigo.transverse.util.Messages;
23import fr.mael.jiwigo.transverse.util.Tools;
24import fr.mael.jiwigo.transverse.util.spring.SpringUtils;
25import fr.mael.jiwigo.ui.browser.BrowserPanel;
26
27/**
28   Copyright (c) 2010, Mael
29   All rights reserved.
30
31   Redistribution and use in source and binary forms, with or without
32   modification, are permitted provided that the following conditions are met:
33    * Redistributions of source code must retain the above copyright
34      notice, this list of conditions and the following disclaimer.
35    * Redistributions in binary form must reproduce the above copyright
36      notice, this list of conditions and the following disclaimer in the
37      documentation and/or other materials provided with the distribution.
38    * Neither the name of jiwigo nor the
39      names of its contributors may be used to endorse or promote products
40      derived from this software without specific prior written permission.
41
42   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
43   ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
44   WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
45   DISCLAIMED. IN NO EVENT SHALL Mael BE LIABLE FOR ANY
46   DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
47   (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
48   LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
49   ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
50   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
51   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
52
53 * @author mael
54 * dialog that displays the comments of an image and allows to add one
55 */
56public class CommentsDialog extends JDialog implements ActionListener {
57    /**
58     * Logger
59     */
60    public static final org.apache.commons.logging.Log LOG = org.apache.commons.logging.LogFactory
61            .getLog(CommentsDialog.class);
62    /**
63     * booleen that allows to alernate the colors
64     */
65    private boolean alternate = true;
66    /**
67     * area to write a new comment
68     */
69    private JTextArea textArea = new JTextArea();
70    /**
71     * sends the new comment
72     */
73    private JButton boutonEnvoyer = new JButton("Ok");
74
75    /**
76     * id of the image
77     */
78    private Integer imageId;
79    /**
80     * panel that allows to scroll
81     */
82    private JPanel panelScrollCommentaires = new JPanel(new GridBagLayout());
83    /**
84     * panel that allows to add a comment
85     */
86    private JPanel panelAjouterCommentaire = new JPanel(new FlowLayout());
87
88    /**
89     * Scrollpane
90     */
91    private JScrollPane scrollPaneArea;
92    /**
93     * scrollpane that allows to scroll
94     */
95    private JScrollPane scrollPaneCommentaires;
96
97    /**
98     * Constructor
99     * @param imageId the id of the image
100     */
101    public CommentsDialog(BrowserPanel parent, Integer imageId) {
102        //      super(parent);
103        this.imageId = imageId;
104        this.setTitle(Messages.getMessage("commentaires"));
105
106        panelScrollCommentaires.setOpaque(true);
107        scrollPaneCommentaires = new JScrollPane(panelScrollCommentaires, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
108                JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
109        scrollPaneArea = new JScrollPane(textArea, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
110                JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
111        scrollPaneArea.setPreferredSize(new Dimension(400, 100));
112        scrollPaneCommentaires.setPreferredSize(new Dimension(700, 500));
113        rafraichir();
114        this.setSize(new Dimension(700, 700));
115        this.setLocationRelativeTo(null);
116        this.setVisible(true);
117
118    }
119
120    private void rafraichir() {
121        this.getContentPane().removeAll();
122        try {
123            List<Comment> commentsForImage = SpringUtils.getCommentService().list(imageId);
124            GridBagConstraints c = new GridBagConstraints();
125            c.insets = new Insets(3, 3, 3, 3);
126            c.gridx = 0;
127            c.gridy = 0;
128            //boucle sur tous les commentaires
129            for (Comment comment : commentsForImage) {
130                CommentPanel panel;
131                if (alternate) {
132                    panel = new CommentPanel(new Color(153, 204, 51), comment.getContent(), comment.getAuthor(),
133                            comment.getDate());
134                } else {
135                    panel = new CommentPanel(new Color(77, 204, 51), comment.getContent(), comment.getAuthor(),
136                            comment.getDate());
137                }
138                alternate = !alternate;
139                panelScrollCommentaires.add(panel, c);
140                c.gridy++;
141            }
142        } catch (Exception e) {
143            LOG.error(Tools.getStackTrace(e));
144            JOptionPane.showMessageDialog(null, Messages.getMessage("commentListingError"),
145                    Messages.getMessage("error"), JOptionPane.ERROR_MESSAGE);
146        }
147        panelAjouterCommentaire.add(scrollPaneArea);
148        boutonEnvoyer.addActionListener(this);
149        panelAjouterCommentaire.add(boutonEnvoyer);
150        this.getContentPane().setLayout(new BorderLayout());
151        this.getContentPane().add(scrollPaneCommentaires, BorderLayout.NORTH);
152        this.getContentPane().add(panelAjouterCommentaire, BorderLayout.CENTER);
153        this.getContentPane().repaint();
154        ((JPanel) this.getContentPane()).revalidate();
155    }
156
157    @Override
158    public void actionPerformed(ActionEvent arg0) {
159        String text = textArea.getText();
160        //on n'envoie pas de commentaire vide
161        if (text.equals("")) {
162            JOptionPane.showMessageDialog(null, Messages.getMessage("emptyCommentError"), Messages.getMessage("error"),
163                    JOptionPane.ERROR_MESSAGE);
164        } else {
165            boolean reussite = true;
166            try {
167                reussite = SpringUtils.getCommentService().create(text, this.imageId,
168                        SpringUtils.getSessionManager().getLogin());
169            } catch (Exception e) {
170                reussite = false;
171                LOG.error(Tools.getStackTrace(e));
172            }
173            if (reussite) {
174                rafraichir();
175            } else {
176                JOptionPane.showMessageDialog(null, Messages.getMessage("addCommentError"),
177                        Messages.getMessage("error"), JOptionPane.ERROR_MESSAGE);
178            }
179        }
180
181    }
182}
Note: See TracBrowser for help on using the repository browser.