1 | package fr.mael.jiwigo.ui.comments; |
---|
2 | |
---|
3 | import java.awt.BorderLayout; |
---|
4 | import java.awt.Color; |
---|
5 | import java.awt.Dimension; |
---|
6 | import java.awt.FlowLayout; |
---|
7 | import java.awt.GridBagConstraints; |
---|
8 | import java.awt.GridBagLayout; |
---|
9 | import java.awt.Insets; |
---|
10 | import java.awt.event.ActionEvent; |
---|
11 | import java.awt.event.ActionListener; |
---|
12 | import java.io.IOException; |
---|
13 | import java.util.List; |
---|
14 | |
---|
15 | import javax.swing.JButton; |
---|
16 | import javax.swing.JDialog; |
---|
17 | import javax.swing.JOptionPane; |
---|
18 | import javax.swing.JPanel; |
---|
19 | import javax.swing.JScrollPane; |
---|
20 | import javax.swing.JTextArea; |
---|
21 | |
---|
22 | import fr.mael.jiwigo.Main; |
---|
23 | import fr.mael.jiwigo.om.Comment; |
---|
24 | import fr.mael.jiwigo.service.CommentService; |
---|
25 | import fr.mael.jiwigo.transverse.util.Messages; |
---|
26 | import fr.mael.jiwigo.transverse.util.Outil; |
---|
27 | import 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 | */ |
---|
58 | public 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).lister(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(Outil.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).creer(text, this.imageId, |
---|
170 | Main.sessionManager.getLogin()); |
---|
171 | } catch (IOException e) { |
---|
172 | reussite = false; |
---|
173 | LOG.error(Outil.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 | } |
---|