KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > web > ui > common > component > UIStatusMessage


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.web.ui.common.component;
18
19 import java.io.IOException JavaDoc;
20 import java.util.Date JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.LinkedList JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.Map JavaDoc;
25
26 import javax.faces.application.FacesMessage;
27 import javax.faces.component.NamingContainer;
28 import javax.faces.component.UIComponent;
29 import javax.faces.component.UIForm;
30 import javax.faces.context.FacesContext;
31 import javax.faces.context.ResponseWriter;
32 import javax.faces.el.ValueBinding;
33 import javax.faces.event.AbortProcessingException;
34 import javax.faces.event.ActionEvent;
35 import javax.faces.event.FacesEvent;
36
37 import org.alfresco.web.app.Application;
38 import org.alfresco.web.ui.common.PanelGenerator;
39 import org.alfresco.web.ui.common.Utils;
40 import org.alfresco.web.ui.common.WebResources;
41
42 /**
43  * @author Kevin Roast
44  */

45 public class UIStatusMessage extends SelfRenderingComponent
46 {
47    /**
48     * Default Constructor
49     */

50    public UIStatusMessage()
51    {
52       setRendererType(null);
53       
54       // add default message to display
55
FacesContext fc = FacesContext.getCurrentInstance();
56       String JavaDoc msg = Application.getMessage(fc, MSG_DEFAULT_STATUS);
57       String JavaDoc time = Utils.getTimeFormat(fc).format(new Date JavaDoc(System.currentTimeMillis()));
58       this.messages.add(new FacesMessage(FacesMessage.SEVERITY_INFO, time, msg));
59    }
60
61    /**
62     * @see javax.faces.component.UIComponent#getFamily()
63     */

64    public String JavaDoc getFamily()
65    {
66       return "org.alfresco.faces.StatusMessage";
67    }
68    
69    /**
70     * @see javax.faces.component.StateHolder#restoreState(javax.faces.context.FacesContext, java.lang.Object)
71     */

72    public void restoreState(FacesContext context, Object JavaDoc state)
73    {
74       Object JavaDoc values[] = (Object JavaDoc[])state;
75       // standard component attributes are restored by the super class
76
super.restoreState(context, values[0]);
77       this.border = (String JavaDoc)values[1];
78       this.bgcolor = (String JavaDoc)values[2];
79       this.messages = (List JavaDoc)values[3];
80       this.currentMessage = ((Integer JavaDoc)values[4]).intValue();
81    }
82    
83    /**
84     * @see javax.faces.component.StateHolder#saveState(javax.faces.context.FacesContext)
85     */

86    public Object JavaDoc saveState(FacesContext context)
87    {
88       Object JavaDoc values[] = new Object JavaDoc[5];
89       // standard component attributes are saved by the super class
90
values[0] = super.saveState(context);
91       values[1] = this.border;
92       values[2] = this.bgcolor;
93       values[3] = this.messages;
94       values[4] = Integer.valueOf(this.currentMessage);
95       return values;
96    }
97
98    /**
99     * @see javax.faces.component.UIComponentBase#encodeBegin(javax.faces.context.FacesContext)
100     */

101    public void encodeBegin(FacesContext context) throws IOException JavaDoc
102    {
103       if (isRendered() == false)
104       {
105          return;
106       }
107       
108       ResponseWriter out = context.getResponseWriter();
109       
110       String JavaDoc bgColor = getBgcolor();
111       if (bgColor == null)
112       {
113          bgColor = PanelGenerator.BGCOLOR_WHITE;
114       }
115       
116       String JavaDoc panel = getBorder();
117       if (panel != null)
118       {
119          PanelGenerator.generatePanelStart(out,
120                context.getExternalContext().getRequestContextPath(),
121                panel,
122                bgColor);
123       }
124       
125       // Previous Message icon image - clicking shows previous message
126
out.write("<table width=100% cellspacing=0 cellpadding=0><tr><td>");
127       String JavaDoc field = getHiddenFieldName();
128       String JavaDoc leftValue = getClientId(context) + NamingContainer.SEPARATOR_CHAR + Integer.toString(ACTION_PREVIOUS);
129       String JavaDoc leftOnclick = Utils.generateFormSubmit(context, this, field, leftValue);
130       out.write(Utils.buildImageTag(context, WebResources.IMAGE_MOVELEFT, 12, 12, null, leftOnclick, "absmiddle"));
131       out.write("</td><td width=100% align=center>");
132       
133       // get messages for the component and crop the stack to the maximum history size
134
Iterator JavaDoc<FacesMessage> msgIterator = context.getMessages(STATUS_MESSAGE);
135       while (msgIterator.hasNext())
136       {
137          if (messages.size() >= HISTORY_SIZE)
138          {
139             messages.remove(HISTORY_SIZE);
140          }
141          // add new messages to the stack in turn
142
messages.add(0, msgIterator.next());
143          // reset current message to top if new one added
144
currentMessage = 0;
145       }
146       
147       // TODO: show different icon depending on SEVERITY of the message?
148
// Message text
149
String JavaDoc style = CSS_ERROR;
150       String JavaDoc icon = WebResources.IMAGE_INFO;
151       FacesMessage msg = messages.get(currentMessage);
152       if (msg.getSeverity() == FacesMessage.SEVERITY_INFO)
153       {
154          style = CSS_INFO;
155       }
156       else if (msg.getSeverity() == FacesMessage.SEVERITY_WARN)
157       {
158          style = CSS_WARNING;
159       }
160       
161       out.write(Utils.buildImageTag(context, icon, null, "absmiddle"));
162       out.write("&nbsp;<span class='");
163       out.write(style);
164       out.write("'>");
165       out.write(msg.getSummary());
166       out.write(" - ");
167       out.write(Utils.encode(msg.getDetail()));
168       out.write("</span>");
169       out.write("</td><td>");
170       
171       // Next Message icon image - clicking shows next message
172
String JavaDoc rightValue = getClientId(context) + NamingContainer.SEPARATOR_CHAR + Integer.toString(ACTION_NEXT);
173       String JavaDoc rightOnclick = Utils.generateFormSubmit(context, this, field, rightValue);
174       out.write(Utils.buildImageTag(context, WebResources.IMAGE_MOVERIGHT, 12, 12, null, rightOnclick, "absmiddle"));
175       out.write("</td></tr></table>");
176       
177       if (panel != null)
178       {
179          PanelGenerator.generatePanelEnd(out,
180                context.getExternalContext().getRequestContextPath(),
181                panel);
182       }
183    }
184    
185    /**
186     * @see javax.faces.component.UIComponentBase#decode(javax.faces.context.FacesContext)
187     */

188    public void decode(FacesContext context)
189    {
190       Map JavaDoc requestMap = context.getExternalContext().getRequestParameterMap();
191       String JavaDoc fieldId = getHiddenFieldName();
192       String JavaDoc value = (String JavaDoc)requestMap.get(fieldId);
193       
194       // we encoded the value to start with our Id
195
if (value != null && value.startsWith(getClientId(context)))
196       {
197          // we were clicked, strip out the value
198
int action = Integer.parseInt(value.substring(getClientId(context).length() + 1));
199          
200          // raise an event to represent the requested action
201
MessageEvent event = new MessageEvent(this, action);
202          queueEvent(event);
203       }
204    }
205    
206    /**
207     * @see javax.faces.component.UICommand#broadcast(javax.faces.event.FacesEvent)
208     */

209    public void broadcast(FacesEvent event) throws AbortProcessingException
210    {
211       if (event instanceof MessageEvent)
212       {
213          switch (((MessageEvent)event).Action)
214          {
215             case ACTION_NEXT:
216                currentMessage++;
217                if (currentMessage >= this.messages.size())
218                {
219                   currentMessage = 0;
220                }
221                break;
222                
223             case ACTION_PREVIOUS:
224                currentMessage--;
225                if (currentMessage < 0)
226                {
227                   currentMessage = this.messages.size() - 1;
228                }
229                break;
230          }
231       }
232       else
233       {
234          super.broadcast(event);
235       }
236    }
237    
238    /**
239     * @return Returns the bgcolor.
240     */

241    public String JavaDoc getBgcolor()
242    {
243       ValueBinding vb = getValueBinding("bgcolor");
244       if (vb != null)
245       {
246          this.bgcolor = (String JavaDoc)vb.getValue(getFacesContext());
247       }
248       
249       return this.bgcolor;
250    }
251    
252    /**
253     * @param bgcolor The bgcolor to set.
254     */

255    public void setBgcolor(String JavaDoc bgcolor)
256    {
257       this.bgcolor = bgcolor;
258    }
259
260    /**
261     * @return Returns the border name.
262     */

263    public String JavaDoc getBorder()
264    {
265       ValueBinding vb = getValueBinding("border");
266       if (vb != null)
267       {
268          this.border = (String JavaDoc)vb.getValue(getFacesContext());
269       }
270       
271       return this.border;
272    }
273
274    /**
275     * @param border The border name to user.
276     */

277    public void setBorder(String JavaDoc border)
278    {
279       this.border = border;
280    }
281    
282    
283    // ------------------------------------------------------------------------------
284
// Private helpers
285

286    /**
287     * We use a hidden field name based on the parent form component Id and
288     * the string "status" to give a hidden field name that can be shared by all status messages
289     * within a single UIForm component.
290     *
291     * @return hidden field name
292     */

293    private String JavaDoc getHiddenFieldName()
294    {
295       UIForm form = Utils.getParentForm(getFacesContext(), this);
296       return form.getClientId(getFacesContext()) + NamingContainer.SEPARATOR_CHAR + "status";
297    }
298    
299    
300    // ------------------------------------------------------------------------------
301
// Private members
302

303    public static final String JavaDoc STATUS_MESSAGE = "status-message";
304    
305    private final static String JavaDoc CSS_INFO = "statusInfoText";
306    private final static String JavaDoc CSS_WARNING = "statusWarningText";
307    private final static String JavaDoc CSS_ERROR = "statusErrorText";
308    
309    private final static int ACTION_PREVIOUS = 0;
310    private final static int ACTION_NEXT = 1;
311    
312    private final static int HISTORY_SIZE = 10;
313    
314    private final static String JavaDoc MSG_DEFAULT_STATUS = "status_message_default";
315    
316    private List JavaDoc<FacesMessage> messages = new LinkedList JavaDoc<FacesMessage>();
317    private int currentMessage = 0;
318    
319    // component settings
320
private String JavaDoc border = null;
321    private String JavaDoc bgcolor = null;
322    
323    
324    // ------------------------------------------------------------------------------
325
// Inner classes
326

327    /**
328     * Class representing the an action that occurs when the previous/next buttons are clicked.
329     */

330    public static class MessageEvent extends ActionEvent
331    {
332       public MessageEvent(UIComponent component, int action)
333       {
334          super(component);
335          Action = action;
336       }
337       
338       public int Action;
339    }
340 }
341
Popular Tags