5b142f312393a34d6be94403c936d3c8be6ba898
[pithos-web-client] / src / gr / grnet / pithos / web / client / MessagePanel.java
1 /*\r
2  * Copyright 2011-2012 GRNET S.A. All rights reserved.\r
3  *\r
4  * Redistribution and use in source and binary forms, with or\r
5  * without modification, are permitted provided that the following\r
6  * conditions are met:\r
7  *\r
8  *   1. Redistributions of source code must retain the above\r
9  *      copyright notice, this list of conditions and the following\r
10  *      disclaimer.\r
11  *\r
12  *   2. Redistributions in binary form must reproduce the above\r
13  *      copyright notice, this list of conditions and the following\r
14  *      disclaimer in the documentation and/or other materials\r
15  *      provided with the distribution.\r
16  *\r
17  * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS\r
18  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR\r
20  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR\r
21  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\r
22  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\r
23  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF\r
24  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED\r
25  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT\r
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN\r
27  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\r
28  * POSSIBILITY OF SUCH DAMAGE.\r
29  *\r
30  * The views and conclusions contained in the software and\r
31  * documentation are those of the authors and should not be\r
32  * interpreted as representing official policies, either expressed\r
33  * or implied, of GRNET S.A.\r
34  */\r
35 package gr.grnet.pithos.web.client;\r
36 \r
37 import gr.grnet.pithos.web.client.animation.FadeIn;\r
38 import gr.grnet.pithos.web.client.animation.FadeOut;\r
39 \r
40 import com.google.gwt.core.client.GWT;\r
41 import com.google.gwt.event.dom.client.ClickEvent;\r
42 import com.google.gwt.event.dom.client.ClickHandler;\r
43 import com.google.gwt.resources.client.ClientBundle;\r
44 import com.google.gwt.resources.client.ImageResource;\r
45 import com.google.gwt.user.client.Window;\r
46 import com.google.gwt.user.client.ui.AbstractImagePrototype;\r
47 import com.google.gwt.user.client.ui.Composite;\r
48 import com.google.gwt.user.client.ui.HTML;\r
49 import com.google.gwt.user.client.ui.HasHorizontalAlignment;\r
50 import com.google.gwt.user.client.ui.HasVerticalAlignment;\r
51 import com.google.gwt.user.client.ui.HorizontalPanel;\r
52 import com.google.gwt.user.client.ui.SimplePanel;\r
53 import com.google.gwt.user.client.ui.VerticalPanel;\r
54 \r
55 /**\r
56  * A panel that displays various system messages.\r
57  */\r
58 public class MessagePanel extends Composite {\r
59         /**\r
60          * An image bundle for this widget's images.\r
61          */\r
62         public interface Images extends ClientBundle {\r
63                 @Source("gr/grnet/pithos/resources/messagebox_info.png")\r
64                 ImageResource info();\r
65 \r
66                 @Source("gr/grnet/pithos/resources/messagebox_warning.png")\r
67                 ImageResource warn();\r
68 \r
69                 @Source("gr/grnet/pithos/resources/messagebox_critical.png")\r
70                 ImageResource error();\r
71         }\r
72 \r
73         /**\r
74          * The widget's images.\r
75          */\r
76         public static Images images;\r
77 \r
78         /**\r
79          * The system message to be displayed.\r
80          */\r
81         private HTML message;\r
82 \r
83         /**\r
84          * The panel that contains the messages.\r
85          */\r
86         private HorizontalPanel inner;\r
87 \r
88         /**\r
89          * The panel that enables special effects for this widget.\r
90          */\r
91         protected SimplePanel simplePanel;\r
92 \r
93         /**\r
94          * A link to send feedBack about the error.\r
95          */\r
96         private HTML feedbackLink;\r
97         \r
98         Pithos app;\r
99         \r
100         /**\r
101          * The widget's constructor.\r
102          *\r
103          * @param newImages a bundle that provides the images for this widget\r
104          */\r
105         public MessagePanel(Pithos _app, final Images newImages) {\r
106                 app = _app;\r
107                 images = newImages;\r
108                 simplePanel = new SimplePanel();\r
109                 simplePanel.setStyleName("effectPanel");\r
110                 \r
111                 inner = new HorizontalPanel();\r
112                 inner.setStyleName("effectPanel-inner");\r
113                 inner.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);\r
114                 inner.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);\r
115                 \r
116                 message = new HTML(" ");\r
117                 message.addClickHandler(new ClickHandler() {\r
118 \r
119                         @Override\r
120                         public void onClick(ClickEvent event) {\r
121                                 FadeOut anim = new FadeOut(simplePanel){\r
122 \r
123                                         @Override\r
124                                         protected void onComplete() {\r
125                                                 super.onComplete();\r
126                                                 hideMessage();\r
127                                         }\r
128                                 };\r
129                                 anim.run(500);\r
130                         }\r
131                 });\r
132                 inner.add(message);\r
133                 inner.setCellVerticalAlignment(message, HasVerticalAlignment.ALIGN_MIDDLE);\r
134 \r
135                 VerticalPanel linkPanel = new VerticalPanel();\r
136                 linkPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);\r
137                 /**\r
138                  * A link to clear the displayed message.\r
139                  */\r
140                 HTML clearMessageLink = new HTML("<a class='pithos-clearMessage' href='javascript:;'>Clear</a>");\r
141                 clearMessageLink.addClickHandler(new ClickHandler() {\r
142 \r
143                         @Override\r
144                         public void onClick(ClickEvent event) {\r
145                                 FadeOut anim = new FadeOut(simplePanel){\r
146                                         @Override\r
147                                         protected void onComplete() {\r
148                                                 super.onComplete();\r
149                                                 hideMessage();\r
150                                         }\r
151                                 };\r
152                                 anim.run(500);\r
153                         }\r
154                 });\r
155                 linkPanel.add(clearMessageLink);\r
156 \r
157                 feedbackLink = new HTML("<a class='pithos-clearMessage' href='javascript:;'>Send feedback</a>");\r
158                 feedbackLink.addClickHandler(new ClickHandler() {\r
159                         \r
160                         @Override\r
161                         public void onClick(ClickEvent event) {\r
162                                 new FeedbackDialog(app, app.getErrorData()).center();\r
163                         }\r
164                 });\r
165                 feedbackLink.setVisible(false);\r
166                 linkPanel.add(feedbackLink);\r
167 \r
168                 inner.add(linkPanel);\r
169                 simplePanel.add(inner);\r
170                 \r
171                 initWidget(simplePanel);\r
172         }\r
173 \r
174         /**\r
175          * Display an error message.\r
176          *\r
177          * @param msg the message to display\r
178          */\r
179         public void displayError(final String msg) {\r
180                 GWT.log(msg, null);\r
181                 message.setHTML("<table class='pithos-errorMessage'><tr><td>" + AbstractImagePrototype.create(images.error()).getHTML() + "</td><td>" + msg + "</td></tr></table>");\r
182                 feedbackLink.setVisible(true);\r
183                 setVisible(true);\r
184                 FadeIn anim = new FadeIn(simplePanel);\r
185                 anim.run(500);\r
186         }\r
187 \r
188         /**\r
189          * Display a warning message.\r
190          *\r
191          * @param msg the message to display\r
192          */\r
193         public void displayWarning(final String msg) {\r
194                 message.setHTML("<table class='pithos-warnMessage'><tr><td>" + AbstractImagePrototype.create(images.warn()).getHTML() + "</td><td>" + msg + "</td></tr></table>");\r
195                 feedbackLink.setVisible(false);\r
196                 setVisible(true);\r
197                 FadeIn anim = new FadeIn(simplePanel);\r
198                 anim.run(500);\r
199         }\r
200 \r
201         /**\r
202          * Display an informational message.\r
203          *\r
204          * @param msg the message to display\r
205          */\r
206         public void displayInformation(final String msg) {\r
207                 message.setHTML("<table class='pithos-infoMessage'><tr><td>" + AbstractImagePrototype.create(images.info()).getHTML() + "</td><td>" + msg + "</td></tr></table>");\r
208                 feedbackLink.setVisible(false);\r
209                 setVisible(true);\r
210                 FadeIn anim = new FadeIn(simplePanel);\r
211                 anim.run(500);\r
212         }\r
213 \r
214         /**\r
215          * Clear the displayed message and hide the panel.\r
216          */\r
217         public void hideMessage() {\r
218                 message = new HTML("&nbsp;");\r
219                 this.setVisible(false);\r
220                 app.onWindowResized(Window.getClientHeight());\r
221         }\r
222 }\r