1 17 package org.alfresco.web.ui.common.component; 18 19 import java.io.IOException ; 20 import java.util.Date ; 21 import java.util.Iterator ; 22 import java.util.LinkedList ; 23 import java.util.List ; 24 import java.util.Map ; 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 45 public class UIStatusMessage extends SelfRenderingComponent 46 { 47 50 public UIStatusMessage() 51 { 52 setRendererType(null); 53 54 FacesContext fc = FacesContext.getCurrentInstance(); 56 String msg = Application.getMessage(fc, MSG_DEFAULT_STATUS); 57 String time = Utils.getTimeFormat(fc).format(new Date (System.currentTimeMillis())); 58 this.messages.add(new FacesMessage(FacesMessage.SEVERITY_INFO, time, msg)); 59 } 60 61 64 public String getFamily() 65 { 66 return "org.alfresco.faces.StatusMessage"; 67 } 68 69 72 public void restoreState(FacesContext context, Object state) 73 { 74 Object values[] = (Object [])state; 75 super.restoreState(context, values[0]); 77 this.border = (String )values[1]; 78 this.bgcolor = (String )values[2]; 79 this.messages = (List )values[3]; 80 this.currentMessage = ((Integer )values[4]).intValue(); 81 } 82 83 86 public Object saveState(FacesContext context) 87 { 88 Object values[] = new Object [5]; 89 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 101 public void encodeBegin(FacesContext context) throws IOException 102 { 103 if (isRendered() == false) 104 { 105 return; 106 } 107 108 ResponseWriter out = context.getResponseWriter(); 109 110 String bgColor = getBgcolor(); 111 if (bgColor == null) 112 { 113 bgColor = PanelGenerator.BGCOLOR_WHITE; 114 } 115 116 String panel = getBorder(); 117 if (panel != null) 118 { 119 PanelGenerator.generatePanelStart(out, 120 context.getExternalContext().getRequestContextPath(), 121 panel, 122 bgColor); 123 } 124 125 out.write("<table width=100% cellspacing=0 cellpadding=0><tr><td>"); 127 String field = getHiddenFieldName(); 128 String leftValue = getClientId(context) + NamingContainer.SEPARATOR_CHAR + Integer.toString(ACTION_PREVIOUS); 129 String 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 Iterator <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 messages.add(0, msgIterator.next()); 143 currentMessage = 0; 145 } 146 147 String style = CSS_ERROR; 150 String 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(" <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 String rightValue = getClientId(context) + NamingContainer.SEPARATOR_CHAR + Integer.toString(ACTION_NEXT); 173 String 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 188 public void decode(FacesContext context) 189 { 190 Map requestMap = context.getExternalContext().getRequestParameterMap(); 191 String fieldId = getHiddenFieldName(); 192 String value = (String )requestMap.get(fieldId); 193 194 if (value != null && value.startsWith(getClientId(context))) 196 { 197 int action = Integer.parseInt(value.substring(getClientId(context).length() + 1)); 199 200 MessageEvent event = new MessageEvent(this, action); 202 queueEvent(event); 203 } 204 } 205 206 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 241 public String getBgcolor() 242 { 243 ValueBinding vb = getValueBinding("bgcolor"); 244 if (vb != null) 245 { 246 this.bgcolor = (String )vb.getValue(getFacesContext()); 247 } 248 249 return this.bgcolor; 250 } 251 252 255 public void setBgcolor(String bgcolor) 256 { 257 this.bgcolor = bgcolor; 258 } 259 260 263 public String getBorder() 264 { 265 ValueBinding vb = getValueBinding("border"); 266 if (vb != null) 267 { 268 this.border = (String )vb.getValue(getFacesContext()); 269 } 270 271 return this.border; 272 } 273 274 277 public void setBorder(String border) 278 { 279 this.border = border; 280 } 281 282 283 286 293 private String getHiddenFieldName() 294 { 295 UIForm form = Utils.getParentForm(getFacesContext(), this); 296 return form.getClientId(getFacesContext()) + NamingContainer.SEPARATOR_CHAR + "status"; 297 } 298 299 300 303 public static final String STATUS_MESSAGE = "status-message"; 304 305 private final static String CSS_INFO = "statusInfoText"; 306 private final static String CSS_WARNING = "statusWarningText"; 307 private final static String 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 MSG_DEFAULT_STATUS = "status_message_default"; 315 316 private List <FacesMessage> messages = new LinkedList <FacesMessage>(); 317 private int currentMessage = 0; 318 319 private String border = null; 321 private String bgcolor = null; 322 323 324 327 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 |