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

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

Changes the look of the comment panels

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;
26import fr.mael.jiwigo.ui.mainframe.MainFrame;
27
28/**
29   Copyright (c) 2010, Mael
30   All rights reserved.
31
32   Redistribution and use in source and binary forms, with or without
33   modification, are permitted provided that the following conditions are met:
34    * Redistributions of source code must retain the above copyright
35      notice, this list of conditions and the following disclaimer.
36    * Redistributions in binary form must reproduce the above copyright
37      notice, this list of conditions and the following disclaimer in the
38      documentation and/or other materials provided with the distribution.
39    * Neither the name of jiwigo nor the
40      names of its contributors may be used to endorse or promote products
41      derived from this software without specific prior written permission.
42
43   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
44   ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
45   WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
46   DISCLAIMED. IN NO EVENT SHALL Mael BE LIABLE FOR ANY
47   DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
48   (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
49   LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
50   ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
51   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
52   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
53
54 * @author mael
55 * dialog that displays the comments of an image and allows to add one
56 */
57public class CommentsDialog extends JDialog implements ActionListener {
58    /**
59     * Logger
60     */
61    public static final org.apache.commons.logging.Log LOG = org.apache.commons.logging.LogFactory
62            .getLog(CommentsDialog.class);
63    /**
64     * booleen that allows to alernate the colors
65     */
66    private boolean alternate = true;
67    /**
68     * area to write a new comment
69     */
70    private JTextArea textArea = new JTextArea();
71    /**
72     * sends the new comment
73     */
74    private JButton boutonEnvoyer = new JButton("Ok");
75
76    /**
77     * id of the image
78     */
79    private Integer imageId;
80    /**
81     * panel that allows to scroll
82     */
83    private JPanel panelScrollCommentaires = new JPanel(new GridBagLayout());
84    /**
85     * panel that allows to add a comment
86     */
87    private JPanel panelAjouterCommentaire = new JPanel(new FlowLayout());
88
89    /**
90     * Scrollpane
91     */
92    private JScrollPane scrollPaneArea;
93    /**
94     * scrollpane that allows to scroll
95     */
96    private JScrollPane scrollPaneCommentaires;
97
98    /**
99     * Constructor
100     * @param imageId the id of the image
101     */
102    public CommentsDialog(BrowserPanel parent, Integer imageId) {
103        super(MainFrame.getInstance());
104        this.imageId = imageId;
105        this.setTitle(Messages.getMessage("commentaires"));
106
107        panelScrollCommentaires.setOpaque(true);
108        scrollPaneCommentaires = new JScrollPane(panelScrollCommentaires, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
109                JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
110        scrollPaneArea = new JScrollPane(textArea, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
111                JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
112        scrollPaneArea.setPreferredSize(new Dimension(400, 100));
113        scrollPaneCommentaires.setPreferredSize(new Dimension(700, 500));
114        rafraichir();
115        this.setSize(new Dimension(700, 700));
116        this.setLocationRelativeTo(null);
117        this.setVisible(true);
118
119    }
120
121    private void rafraichir() {
122        this.getContentPane().removeAll();
123        try {
124            List<Comment> commentsForImage = SpringUtils.getCommentService().list(imageId);
125            GridBagConstraints c = new GridBagConstraints();
126
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(0, 153, 255), comment.getContent(), comment.getAuthor(),
135                            comment.getDate());
136                } else {
137                    panel = new CommentPanel(new Color(20, 50, 110), comment.getContent(), comment.getAuthor(),
138                            comment.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"),
147                    Messages.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 = SpringUtils.getCommentService().create(text, this.imageId,
170                        SpringUtils.getSessionManager().getLogin());
171            } catch (Exception 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"),
179                        Messages.getMessage("error"), JOptionPane.ERROR_MESSAGE);
180            }
181        }
182
183    }
184}
Note: See TracBrowser for help on using the repository browser.