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

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

Modifications to use the last version of jiwigo-ws-api which manages proxy errors.

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