1 33 34 package com.icesoft.faces.context.effects; 35 36 import com.icesoft.faces.context.DOMContext; 37 import com.icesoft.faces.renderkit.dom_html_basic.HTML; 38 import org.apache.commons.logging.Log; 39 import org.apache.commons.logging.LogFactory; 40 import org.w3c.dom.Element ; 41 42 import javax.faces.component.UIComponent; 43 import javax.faces.context.FacesContext; 44 import javax.faces.el.ValueBinding; 45 import javax.servlet.http.HttpServletRequest ; 46 import java.util.HashMap ; 47 import java.util.Map ; 48 49 53 public class CurrentStyle { 54 55 58 public static String CSS_UPDATE_FIELD = "icefacesCssUpdates"; 59 62 private String cssString; 63 64 67 private String lastCssString; 68 69 72 private static String DISPLAY_ON = "display:block;"; 73 74 77 private static String DISPLAY_OFF = "display:none;"; 78 79 private static final Log log = LogFactory.getLog(CurrentStyle.class); 80 81 84 public CurrentStyle(String cssString) { 85 this.cssString = cssString; 86 } 87 88 public String getCssString() { 89 return cssString; 90 } 91 92 public String getLastCssString() { 93 return lastCssString; 94 } 95 96 public void setLastCssString(String lastCssString) { 97 this.lastCssString = lastCssString; 98 } 99 100 106 public static void apply(FacesContext facesContext, 107 UIComponent uiComponent) { 108 apply(facesContext, uiComponent, null, null); 109 } 110 111 119 public static void apply(FacesContext facesContext, UIComponent uiComponent, 120 Element targetElement, String style) { 121 if(targetElement == null) { 122 DOMContext domContext = 123 DOMContext.getDOMContext(facesContext, uiComponent); 124 Object node = domContext.getRootNode(); 125 if (node == null || !(node instanceof Element )) { 126 return; 127 } 128 Element root = (Element ) node; 129 targetElement = root; 130 } 131 String jspStyle = (String ) uiComponent.getAttributes().get("style"); 132 if (log.isTraceEnabled()) { 133 if (jspStyle != null) { 134 log.trace("Existing style [" + jspStyle + "]"); 135 } 136 } 137 138 if (style != null) { 139 if (jspStyle == null) { 140 jspStyle = ""; 141 } 142 jspStyle += style; 143 } 144 145 Boolean visibility = 146 (Boolean ) uiComponent.getAttributes().get("visible"); 147 boolean visible = true; 149 if (visibility != null) { 150 visible = visibility.booleanValue(); 151 } 152 CurrentStyle currentStyle = 153 (CurrentStyle) uiComponent.getAttributes().get("currentStyle"); 154 if (currentStyle != null) { 155 String appendedStyle = currentStyle.cssString; 156 157 currentStyle.lastCssString = currentStyle.cssString; 158 if (appendedStyle != null) { 159 if (jspStyle == null) { 160 jspStyle = appendedStyle; 161 } else { 162 jspStyle += ";" + appendedStyle; 163 } 164 } 165 166 } 167 168 if (visible) { 169 if (jspStyle != null) { 170 int startI = jspStyle.indexOf(DISPLAY_OFF); 171 if (startI != -1) { 172 String start = ""; 173 if (startI > 0) { 174 start = jspStyle.substring(0, startI); 175 } 176 int endI = startI + DISPLAY_OFF.length(); 177 String end = ""; 178 if (endI < jspStyle.length()) { 179 end = jspStyle.substring(endI); 180 } 181 jspStyle = start + end; 182 } 183 } 184 } else { 185 186 if (jspStyle == null) { 187 jspStyle = DISPLAY_OFF; 188 } else { 189 jspStyle += DISPLAY_OFF; 190 } 191 } 192 if (log.isTraceEnabled()) { 193 if (jspStyle != null) { 194 log.trace("JSP Style [" + jspStyle + "]"); 195 } 196 } 197 if (targetElement != null) { 198 if(jspStyle != null && jspStyle.length() > 0) 199 targetElement.setAttribute(HTML.STYLE_ATTR, jspStyle); 200 else 201 targetElement.removeAttribute(HTML.STYLE_ATTR); 202 } 203 } 204 205 209 public static Map decode(FacesContext facesContext) { 210 211 Map parameters = facesContext.getExternalContext().getRequestParameterMap(); 212 String cssUpdate = (String ) parameters.get(CSS_UPDATE_FIELD); 213 Map requestMap = facesContext.getExternalContext().getRequestMap(); 214 String oldCssUpdate = (String ) requestMap.get(CSS_UPDATE_FIELD); 215 216 if (cssUpdate == null || cssUpdate.length() == 0) { 217 return null; 218 } 219 Map updates = null; 220 if (!cssUpdate.equals(oldCssUpdate)) { 221 updates = new HashMap (); 222 223 int rightBrace = 0; 224 do { 225 rightBrace = cssUpdate.indexOf("}"); 226 if (rightBrace != -1) { 227 String update = cssUpdate.substring(0, ++rightBrace); 228 229 cssUpdate = cssUpdate.substring(rightBrace); 230 int leftBrace = update.indexOf("{"); 231 String id = update.substring(0, leftBrace); 232 233 leftBrace++; 234 String style = update.substring(leftBrace, update.length() - 1); 235 if (log.isTraceEnabled()) { 236 log.trace("Adding id[" + id + "] Style [" + style + "]"); 237 } 238 updates.put(id, style); 239 } 240 } while (rightBrace != -1); 241 facesContext.getExternalContext().getSessionMap().put(CurrentStyle.class.getName(), updates); 242 requestMap.put(CSS_UPDATE_FIELD, cssUpdate); 243 } 244 return updates; 245 } 246 247 253 public static void decode(FacesContext facesContext, 254 UIComponent uiComponent) { 255 256 decode(facesContext); 257 Map map = (Map ) facesContext.getExternalContext().getSessionMap() 258 .get(CurrentStyle.class.getName()); 259 if (map == null) { 260 return; 261 } 262 if (uiComponent == null) { 263 return; 264 } 265 String clientId = uiComponent.getClientId(facesContext); 266 String style = (String ) map.get(clientId); 267 if (style == null) { 268 return; 269 } 270 271 if (log.isTraceEnabled()) { 272 log.trace("Decode Applying Style to [" + clientId + "] Css [" + 273 style + "]"); 274 } 275 CurrentStyle cs = 276 (CurrentStyle) uiComponent.getAttributes().get("currentStyle"); 277 if (cs != null) { 278 cs.cssString = style; 279 } else { 280 cs = new CurrentStyle(style); 281 282 uiComponent.getAttributes() 283 .put("currentStyle", new CurrentStyle(style)); 284 } 285 286 Boolean value = Boolean.valueOf("true"); 288 if (cs.cssString.endsWith(DISPLAY_OFF)) { 289 value = Boolean.valueOf("false"); 290 } 291 ValueBinding vb = uiComponent.getValueBinding("visible"); 292 if (vb == null) { 293 uiComponent.getAttributes().put("visible", value); 294 } else { 295 try { 296 vb.setValue(facesContext, value); 297 } catch (Exception e) { 298 299 if (log.isErrorEnabled()) { 300 log.error("Exception setting visible. Value Binding [" + 301 vb.getExpressionString() + "]", e); 302 if (facesContext == null) { 303 log.error("Faces Context is null"); 304 } 305 } 306 } 307 } 308 } 309 310 } 311 | Popular Tags |