1 33 48 49 package com.icesoft.faces.component.ext.taglib; 50 51 import com.icesoft.faces.component.CSS_DEFAULT; 52 import com.icesoft.faces.component.IceExtended; 53 import com.icesoft.faces.component.ext.HtmlInputText; 54 import com.icesoft.faces.component.ext.renderkit.FormRenderer; 55 import com.icesoft.faces.context.effects.Effect; 56 import com.icesoft.faces.context.effects.EffectBuilder; 57 import com.icesoft.faces.renderkit.dom_html_basic.DomBasicRenderer; 58 import org.apache.commons.logging.Log; 59 import org.apache.commons.logging.LogFactory; 60 61 import javax.faces.component.UIComponent; 62 import javax.faces.component.UISelectItem; 63 import javax.faces.component.UISelectItems; 64 import javax.faces.context.FacesContext; 65 import javax.faces.el.ValueBinding; 66 import javax.faces.model.SelectItem; 67 import java.util.ArrayList ; 68 import java.util.Collection ; 69 import java.util.Iterator ; 70 import java.util.List ; 71 import java.util.Map ; 72 import java.util.StringTokenizer ; 73 74 public class Util extends Object { 75 private static Log log = LogFactory.getLog(Util.class); 76 77 85 public static boolean isRenderedOnUserRole(UIComponent component) { 86 String userRole; 87 if (component instanceof IceExtended) { 88 userRole = ((IceExtended) component).getRenderedOnUserRole(); 89 } else { 90 userRole = (String ) component.getAttributes() 91 .get(IceExtended.RENDERED_ON_USER_ROLE_ATTR); 92 } 93 94 if (userRole == null) { 96 return true; 97 } 98 99 FacesContext facesContext = FacesContext.getCurrentInstance(); 100 StringTokenizer st = new StringTokenizer (userRole, ","); 101 while (st.hasMoreTokens()) { 102 if (facesContext.getExternalContext() 103 .isUserInRole(st.nextToken().trim())) { 104 return true; 105 } 106 } 107 return false; 108 } 109 110 111 119 public static boolean isEnabledOnUserRole(UIComponent component) { 120 String userRole; 121 if (component instanceof IceExtended) { 122 userRole = ((IceExtended) component).getEnabledOnUserRole(); 123 } else { 124 userRole = (String ) component.getAttributes() 125 .get(IceExtended.ENABLED_ON_USER_ROLE_ATTR); 126 } 127 128 if (userRole == null) { 130 return true; 131 } 132 133 FacesContext facesContext = FacesContext.getCurrentInstance(); 134 StringTokenizer st = new StringTokenizer (userRole, ","); 135 while (st.hasMoreTokens()) { 136 if (facesContext.getExternalContext() 137 .isUserInRole(st.nextToken().trim())) { 138 return true; 139 } 140 } 141 return false; 142 } 143 144 public static boolean isParentPartialSubmit(UIComponent uiComponent) { 145 UIComponent form = DomBasicRenderer.findForm(uiComponent); 146 if (form instanceof IceExtended) { 147 if (((IceExtended) form).getPartialSubmit()) { 148 return true; 149 } 150 } 151 return false; 152 } 153 154 160 161 public static List getSelectItems(FacesContext context, 162 UIComponent uiComponent) { 163 164 List selectItems = new ArrayList (); 165 Iterator children = uiComponent.getChildren().iterator(); 166 167 while (children.hasNext()) { 168 UIComponent nextSelectItemChild = (UIComponent) children.next(); 169 if (nextSelectItemChild instanceof UISelectItem) { 170 Object selectItemValue = 171 ((UISelectItem) nextSelectItemChild).getValue(); 172 if (selectItemValue != null && 173 selectItemValue instanceof SelectItem) { 174 selectItems.add(selectItemValue); 175 } else { 176 selectItems.add( 177 new SelectItem( 178 ((UISelectItem) nextSelectItemChild).getItemValue(), 179 ((UISelectItem) nextSelectItemChild).getItemLabel(), 180 ((UISelectItem) nextSelectItemChild).getItemDescription(), 181 ((UISelectItem) nextSelectItemChild).isItemDisabled())); 182 } 183 } else if (nextSelectItemChild instanceof UISelectItems) { 184 Object selectItemsValue = 185 ((UISelectItems) nextSelectItemChild).getValue(); 186 187 if (selectItemsValue != null) { 188 if (selectItemsValue instanceof SelectItem) { 189 selectItems.add(selectItemsValue); 190 } else if (selectItemsValue instanceof Collection ) { 191 Iterator selectItemsIterator = 192 ((Collection ) selectItemsValue).iterator(); 193 while (selectItemsIterator.hasNext()) { 194 selectItems.add(selectItemsIterator.next()); 195 } 196 } else if (selectItemsValue instanceof SelectItem[]) { 197 SelectItem selectItemArray[] = 198 (SelectItem[]) selectItemsValue; 199 for (int i = 0; i < selectItemArray.length; i++) { 200 selectItems.add(selectItemArray[i]); 201 } 202 } else if (selectItemsValue instanceof Map ) { 203 Iterator selectItemIterator = 204 ((Map ) selectItemsValue).keySet().iterator(); 205 while (selectItemIterator.hasNext()) { 206 Object nextKey = selectItemIterator.next(); 207 if (nextKey != null) { 208 Object nextValue = 209 ((Map ) selectItemsValue).get(nextKey); 210 if (nextValue != null) { 211 selectItems.add( 212 new SelectItem( 213 nextValue.toString(), 214 nextKey.toString())); 215 } 216 } 217 } 218 } 219 } 220 } 221 } 222 return selectItems; 223 } 224 225 226 public static void addEffect(String effect, UIComponent panel) { 227 if (effect != null) { 228 if (isValueReference(effect)) { 229 ValueBinding vb = Util.getValueBinding(effect); 230 panel.setValueBinding("effect", vb); 231 } else { 232 Effect fx = (Effect) panel.getAttributes().get("effect"); 233 if (fx == null) { 234 fx = EffectBuilder.build(effect); 235 panel.getAttributes().put("effect", fx); 236 } 237 } 238 } 239 } 240 241 public static void addVisible(String visible, UIComponent panel) { 242 if (visible != null) { 243 if (isValueReference(visible)) { 244 ValueBinding vb = Util.getValueBinding(visible); 245 panel.setValueBinding("visible", vb); 246 } else { 247 Boolean boolVisible = new Boolean (visible); 248 panel.getAttributes().put("visible", boolVisible); 249 250 } 251 } 252 } 253 254 public static void addLocalEffect(String effect, String name, 255 UIComponent panel) { 256 257 if (effect != null) { 258 if (log.isTraceEnabled()) { 259 log.trace("AddLocalEffect. String [" + effect + "] name [" + 260 name + "] class [" + panel.getClass().getName() + 261 "]"); 262 } 263 if (isValueReference(effect)) { 264 if (log.isTraceEnabled()) { 265 log.trace("Adding Value Binding"); 266 } 267 ValueBinding vb = Util.getValueBinding(effect); 268 panel.setValueBinding(name, vb); 269 } else { 270 if (log.isTraceEnabled()) { 271 log.trace("Adding Literal String"); 272 } 273 274 Effect fx = (Effect) panel.getAttributes().get(name); 275 if (fx == null) { 276 fx = EffectBuilder.build(effect); 277 panel.getAttributes().put(name, fx); 278 } 279 } 280 } else { 281 if (log.isTraceEnabled()) { 282 log.trace("AddLocalEffect. Effect is null"); 283 } 284 } 285 } 286 287 public static boolean isValueReference(String v) { 288 if (v == null) { 289 return false; 290 } 291 if (v.startsWith("#{") && v.endsWith("}")) { 292 return true; 293 } 294 return false; 295 } 296 297 public static String stripPx(String s) { 298 if (s == null) { 299 return null; 300 } 301 int i = s.indexOf("px"); 302 if (i != -1) { 303 return s.substring(0, i); 304 } 305 return s; 306 } 307 308 309 319 public static String getQualifiedStyleClass(UIComponent uiComponent, 320 String noneAttributeClass) { 321 return getQualifiedStyleClass(uiComponent, 322 noneAttributeClass, 323 null, 324 null, 325 false, 326 true); 327 328 } 329 330 340 public static String getQualifiedStyleClass(UIComponent uiComponent, 341 String noneAttributeClass, 342 boolean disabled) { 343 return getQualifiedStyleClass(uiComponent, 344 noneAttributeClass, 345 null, 346 null, 347 disabled, 348 true); 349 350 } 351 352 364 365 public static String getQualifiedStyleClass(UIComponent uiComponent, 366 String userDefinedStyleClass, 367 String defaulStyleClass, 368 String classAttributeName) { 369 return getQualifiedStyleClass(uiComponent, 370 userDefinedStyleClass, 371 defaulStyleClass, 372 classAttributeName, 373 false, 374 false); 375 376 } 377 378 391 public static String getQualifiedStyleClass(UIComponent uiComponent, 392 String userDefinedStyleClass, 393 String defaulStyleClass, 394 String classAttributeName, 395 boolean disabled) { 396 return getQualifiedStyleClass(uiComponent, 397 userDefinedStyleClass, 398 defaulStyleClass, 399 classAttributeName, 400 disabled, 401 false); 402 403 } 404 405 private static String getQualifiedStyleClass(UIComponent uiComponent, 406 String userDefinedStyleClass, 407 String defaulStyleClass, 408 String classAttributeName, 409 boolean disabled, 410 boolean isNoneAttributeClass) { 411 if (isNoneAttributeClass) { 412 return appendLocalClassToStyleClass(uiComponent, userDefinedStyleClass); 415 } 416 417 String disabledSuffix = disabled ? CSS_DEFAULT.DIS_SUFFIX: ""; 418 String styleClass = null; 419 420 if ("styleClass".equalsIgnoreCase(classAttributeName)) { 421 styleClass = getValue(uiComponent, 425 userDefinedStyleClass, 426 "styleClass"); 427 styleClass = defaulStyleClass + 428 disabledSuffix+ 429 " " + ((styleClass!=null)? styleClass + disabledSuffix :""); 430 return styleClass.trim(); 431 } else { 432 styleClass = String.valueOf(uiComponent.getAttributes() 436 .get("styleClass")); 437 String subClass = getValue(uiComponent, 438 userDefinedStyleClass, 439 classAttributeName); 440 441 String newClass = appendLocalClassToStyleClass(uiComponent, 442 defaulStyleClass); 443 if (subClass != null) { 444 newClass += " " + subClass + disabledSuffix; 445 } 446 return newClass.trim(); 447 } 448 } 449 450 private static String getValue(UIComponent uiComponent, 451 String value, 452 String attributeName) { 453 if (value != null) { 454 return value; 455 } 456 ValueBinding vb = uiComponent.getValueBinding(attributeName); 457 return vb != null ? 458 (String ) vb.getValue(FacesContext.getCurrentInstance()) : 459 null; 460 } 461 462 private static String appendLocalClassToStyleClass(UIComponent uiComponent, 463 String localClass) { 464 String styleClass = String.valueOf(uiComponent.getAttributes() 465 .get("styleClass")).trim(); 466 String [] classes = styleClass.split(" "); 467 StringBuffer name = new StringBuffer (); 468 for (int i =0; i <classes.length; i++) { 469 if (classes[i] != null && classes[i].endsWith(CSS_DEFAULT.DIS_SUFFIX)) { 470 name.append(classes[i].replaceAll(CSS_DEFAULT.DIS_SUFFIX, 471 localClass + CSS_DEFAULT.DIS_SUFFIX)); 472 } else { 473 name.append(classes[i]+ localClass); 474 } 475 name.append(" "); 476 } 477 return name.toString().trim(); 478 } 479 480 public static String getClassName(UIComponent uiComponent, 481 String styleClass, 482 String styleClassAsString, 483 String defaultStyleClass) { 484 if (styleClass != null) { 485 return styleClass; 486 } 487 ValueBinding vb = uiComponent.getValueBinding(styleClassAsString); 488 return vb != null ? 489 (String ) vb.getValue(FacesContext.getCurrentInstance()) : 490 defaultStyleClass; 491 } 492 493 public static ValueBinding getValueBinding(String valueRef) { 494 FacesContext facesContext = FacesContext.getCurrentInstance(); 495 return facesContext.getApplication().createValueBinding(valueRef); 496 } 497 498 public static boolean isEventSource(FacesContext facesContext, UIComponent uiComponent) { 499 Object focusId = facesContext.getExternalContext() 500 .getRequestParameterMap().get(FormRenderer.getFocusElementId()); 501 if (focusId != null) { 502 if (focusId.toString() 503 .equals(uiComponent.getClientId(facesContext))) { 504 ((HtmlInputText) uiComponent).setFocus(true); 505 } else { 506 ((HtmlInputText) uiComponent).setFocus(false); 507 } 508 } 509 Object componenetId = facesContext.getExternalContext() 510 .getRequestParameterMap().get("ice.event.captured"); 511 if (componenetId != null) { 512 if (componenetId.toString() 513 .equals(uiComponent.getClientId(facesContext))) { 514 return true; 515 } else { 516 517 return false; 518 } 519 } 520 return false; 521 } 522 } | Popular Tags |