Automated merge with https://gss.googlecode.com/hg/
[pithos] / src / gr / ebs / gss / client / MessagePanel.java
1 /*\r
2  * Copyright 2007, 2008, 2009 Electronic Business Systems Ltd.\r
3  *\r
4  * This file is part of GSS.\r
5  *\r
6  * GSS is free software: you can redistribute it and/or modify\r
7  * it under the terms of the GNU General Public License as published by\r
8  * the Free Software Foundation, either version 3 of the License, or\r
9  * (at your option) any later version.\r
10  *\r
11  * GSS is distributed in the hope that it will be useful,\r
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14  * GNU General Public License for more details.\r
15  *\r
16  * You should have received a copy of the GNU General Public License\r
17  * along with GSS.  If not, see <http://www.gnu.org/licenses/>.\r
18  */\r
19 package gr.ebs.gss.client;\r
20 \r
21 import gr.ebs.gss.client.animation.FadeIn;\r
22 import gr.ebs.gss.client.animation.FadeOut;\r
23 \r
24 import com.google.gwt.core.client.GWT;\r
25 import com.google.gwt.event.dom.client.ClickEvent;\r
26 import com.google.gwt.event.dom.client.ClickHandler;\r
27 import com.google.gwt.resources.client.ClientBundle;\r
28 import com.google.gwt.resources.client.ImageResource;\r
29 import com.google.gwt.user.client.DOM;\r
30 import com.google.gwt.user.client.ui.AbstractImagePrototype;\r
31 import com.google.gwt.user.client.ui.Composite;\r
32 import com.google.gwt.user.client.ui.HTML;\r
33 import com.google.gwt.user.client.ui.HasHorizontalAlignment;\r
34 import com.google.gwt.user.client.ui.HasVerticalAlignment;\r
35 import com.google.gwt.user.client.ui.HorizontalPanel;\r
36 import com.google.gwt.user.client.ui.SimplePanel;\r
37 \r
38 /**\r
39  * A panel that displays various system messages.\r
40  */\r
41 public class MessagePanel extends Composite {\r
42         /**\r
43          * An image bundle for this widget's images.\r
44          */\r
45         public interface Images extends ClientBundle {\r
46                 @Source("gr/ebs/gss/resources/messagebox_info.png")\r
47                 ImageResource info();\r
48 \r
49                 @Source("gr/ebs/gss/resources/messagebox_warning.png")\r
50                 ImageResource warn();\r
51 \r
52                 @Source("gr/ebs/gss/resources/messagebox_critical.png")\r
53                 ImageResource error();\r
54         }\r
55 \r
56         /**\r
57          * The widget's images.\r
58          */\r
59         public static Images images;\r
60 \r
61         /**\r
62          * The system message to be displayed.\r
63          */\r
64         private HTML message = new HTML("&nbsp;");\r
65 \r
66         /**\r
67          * A link to clear the displayed message.\r
68          */\r
69         private HTML clearMessageLink = new HTML("<a class='gss-clearMessage' href='javascript:;'>Clear</a>");\r
70 \r
71         /**\r
72          * The panel that contains the messages.\r
73          */\r
74         private HorizontalPanel inner = new HorizontalPanel();\r
75 \r
76         /**\r
77          * The panel that enables special effects for this widget.\r
78          */\r
79         private SimplePanel simplePanel = new SimplePanel();\r
80 \r
81         /**\r
82          * The widget's constructor.\r
83          *\r
84          * @param newImages a bundle that provides the images for this widget\r
85          */\r
86         public MessagePanel(final Images newImages) {\r
87                 images = newImages;\r
88                 buildPanel();\r
89                 simplePanel.setStyleName("effectPanel");\r
90                 inner.setStyleName("effectPanel-inner");\r
91                 DOM.setStyleAttribute(simplePanel.getElement(), "zoom", "1");\r
92                 simplePanel.add(inner);\r
93                 initWidget(simplePanel);\r
94         }\r
95 \r
96         /**\r
97          * Build the panel that contains the icon, the message and the 'clear' link.\r
98          */\r
99         private void buildPanel() {\r
100                 inner.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);\r
101                 inner.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);\r
102                 inner.setSpacing(4);\r
103                 inner.add(message);\r
104                 inner.add(clearMessageLink);\r
105                 inner.setCellVerticalAlignment(message, HasVerticalAlignment.ALIGN_MIDDLE);\r
106                 clearMessageLink.addClickHandler(new ClickHandler() {\r
107 \r
108                         @Override\r
109                         public void onClick(ClickEvent event) {\r
110                                 FadeOut anim = new FadeOut(simplePanel){\r
111                                         @Override\r
112                                         protected void onComplete() {\r
113                                                 super.onComplete();\r
114                                                 hideMessage();\r
115                                         }\r
116                                 };\r
117                                 anim.run(500);\r
118                         }\r
119                 });\r
120         }\r
121 \r
122         /**\r
123          * Display an error message.\r
124          *\r
125          * @param msg the message to display\r
126          */\r
127         public void displayError(final String msg) {\r
128                 GWT.log(msg, null);\r
129                 message = new HTML("<table class='gss-errorMessage'><tr><td>" + AbstractImagePrototype.create(images.error()).getHTML() + "</td><td>" + msg + "</td></tr></table>");\r
130                 message.addClickHandler(new ClickHandler() {\r
131 \r
132                         @Override\r
133                         public void onClick(ClickEvent event) {\r
134                                 FadeOut anim = new FadeOut(simplePanel){\r
135 \r
136                                         @Override\r
137                                         protected void onComplete() {\r
138                                                 super.onComplete();\r
139                                                 hideMessage();\r
140                                         }\r
141                                 };\r
142                                 anim.run(500);\r
143                         }\r
144                 });\r
145                 buildPanel();\r
146                 setVisible(true);\r
147                 FadeIn anim = new FadeIn(simplePanel);\r
148                 anim.run(500);\r
149         }\r
150 \r
151         /**\r
152          * Display a warning message.\r
153          *\r
154          * @param msg the message to display\r
155          */\r
156         public void displayWarning(final String msg) {\r
157                 message = new HTML("<table class='gss-warnMessage'><tr><td>" + AbstractImagePrototype.create(images.warn()).getHTML() + "</td><td>" + msg + "</td></tr></table>");\r
158                 message.addClickHandler(new ClickHandler() {\r
159 \r
160                         @Override\r
161                         public void onClick(ClickEvent event) {\r
162                                 FadeOut anim = new FadeOut(simplePanel){\r
163 \r
164                                         @Override\r
165                                         protected void onComplete() {\r
166                                                 super.onComplete();\r
167                                                 hideMessage();\r
168                                         }\r
169                                 };\r
170                                 anim.run(500);\r
171                         }\r
172                 });\r
173 \r
174                 buildPanel();\r
175                 setVisible(true);\r
176                 FadeIn anim = new FadeIn(simplePanel);\r
177                 anim.run(500);\r
178         }\r
179 \r
180         /**\r
181          * Display an informational message.\r
182          *\r
183          * @param msg the message to display\r
184          */\r
185         public void displayInformation(final String msg) {\r
186                 message = new HTML("<table class='gss-infoMessage'><tr><td>" + AbstractImagePrototype.create(images.info()).getHTML() + "</td><td>" + msg + "</td></tr></table>");\r
187                 message.addClickHandler(new ClickHandler() {\r
188 \r
189                         @Override\r
190                         public void onClick(ClickEvent event) {\r
191                                 FadeOut anim = new FadeOut(simplePanel){\r
192 \r
193                                         @Override\r
194                                         protected void onComplete() {\r
195                                                 super.onComplete();\r
196                                                 hideMessage();\r
197                                         }\r
198                                 };\r
199                                 anim.run(500);\r
200                         }\r
201                 });\r
202 \r
203                 buildPanel();\r
204                 setVisible(true);\r
205                 FadeIn anim = new FadeIn(simplePanel);\r
206                 anim.run(500);\r
207         }\r
208 \r
209         /**\r
210          * Clear the displayed message and hide the panel.\r
211          */\r
212         public void hideMessage() {\r
213                 inner.clear();\r
214                 message = new HTML("&nbsp;");\r
215                 this.setVisible(false);\r
216         }\r
217 \r
218 }\r