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

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

Modifications to use the last version of jiwigo-ws-api

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