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

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

Adds a wait cursor for time consuming operations.

File size: 6.4 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;
26import fr.mael.jiwigo.ui.mainframe.CursorSetter;
27import fr.mael.jiwigo.ui.mainframe.MainFrame;
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(MainFrame.getInstance());
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 = SpringUtils.getCommentService().list(imageId);
126            GridBagConstraints c = new GridBagConstraints();
127
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(0, 153, 255), comment.getContent(), comment.getAuthor(),
136                            comment.getDate());
137                } else {
138                    panel = new CommentPanel(new Color(20, 50, 110), comment.getContent(), comment.getAuthor(),
139                            comment.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"),
148                    Messages.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        CursorSetter.setWaitCursor(this);
163        String text = textArea.getText();
164        //on n'envoie pas de commentaire vide
165        if (text.equals("")) {
166            JOptionPane.showMessageDialog(null, Messages.getMessage("emptyCommentError"), Messages.getMessage("error"),
167                    JOptionPane.ERROR_MESSAGE);
168        } else {
169            boolean reussite = true;
170            try {
171                reussite = SpringUtils.getCommentService().create(text, this.imageId,
172                        SpringUtils.getSessionManager().getLogin());
173            } catch (Exception e) {
174                reussite = false;
175                LOG.error(Tools.getStackTrace(e));
176            }
177            if (reussite) {
178                rafraichir();
179            } else {
180                JOptionPane.showMessageDialog(null, Messages.getMessage("addCommentError"),
181                        Messages.getMessage("error"), JOptionPane.ERROR_MESSAGE);
182            }
183        }
184        CursorSetter.setDefaultCursor(this);
185
186    }
187}
Note: See TracBrowser for help on using the repository browser.