1 package com.icesoft.faces.component.panelcollapsible; 2 3 import com.icesoft.faces.context.effects.JavascriptContext; 4 import com.icesoft.faces.component.CSS_DEFAULT; 5 import com.icesoft.faces.component.ext.taglib.Util; 6 7 import javax.faces.application.FacesMessage; 8 import javax.faces.context.FacesContext; 9 import javax.faces.el.EvaluationException; 10 import javax.faces.el.MethodBinding; 11 import javax.faces.el.ValueBinding; 12 import javax.faces.event.ActionEvent; 13 import javax.faces.event.FacesEvent; 14 import javax.faces.event.ActionListener; 15 import javax.faces.component.UIComponentBase; 16 import javax.faces.component.ActionSource; 17 18 public class PanelCollapsible extends UIComponentBase implements ActionSource { 19 public static final String COMPONENET_TYPE = "com.icesoft.faces.PanelCollapsible"; 20 public static final String DEFAULT_RENDERER_TYPE = "com.icesoft.faces.PanelCollapsibleRenderer"; 21 public static final String COMPONENT_FAMILY = "javax.faces.Command"; 22 private static final boolean DEFAULT_IMMEDIATE = false; 23 24 private String label; 25 private Boolean expanded; 26 private MethodBinding actionListener; 27 private String style; 28 private String styleClass; 29 private Boolean toggleOnClick; 30 private Boolean disabled; 31 private Boolean immediate; 32 33 36 private String enabledOnUserRole = null; 37 40 private String renderedOnUserRole = null; 41 42 43 public PanelCollapsible() { 44 super(); 45 JavascriptContext.includeLib(JavascriptContext.ICE_EXTRAS, FacesContext.getCurrentInstance()); 46 47 } 48 49 public String getFamily() { 50 return COMPONENT_FAMILY; 51 } 52 53 public String getRendererType() { 54 return DEFAULT_RENDERER_TYPE; 55 } 56 57 58 public String getStyle() { 59 if (style != null) { 60 return style; 61 } 62 ValueBinding vb = getValueBinding("style"); 63 if (vb != null) { 64 return (String ) vb.getValue(getFacesContext()); 65 } 66 return null; 67 } 68 69 public void setStyle(String style) { 70 this.style = style; 71 } 72 73 public String getStyleClass() { 74 return Util.getQualifiedStyleClass(this, 75 getCollapsedStyle(styleClass), 76 getCollapsedStyle(CSS_DEFAULT.PANEL_COLLAPSIBLE_DEFAULT_STYLE_CLASS), 77 "styleClass", 78 isDisabled()); 79 } 80 81 public String getHeaderClass() { 82 return Util.getQualifiedStyleClass(this, 83 CSS_DEFAULT.PANEL_COLLAPSIBLE_HEADER, 84 isDisabled()); 85 } 86 87 public String getContentClass() { 88 return Util.getQualifiedStyleClass(this, 89 CSS_DEFAULT.PANEL_COLLAPSIBLE_CONTENT, 90 isDisabled()); 91 } 92 93 private String getCollapsedStyle(String style) { 94 if (!getExpanded().booleanValue() && style != null) { 95 style += CSS_DEFAULT.PANEL_COLLAPSIBLE_STATE_COLLAPSED; 96 } 97 return style; 98 } 99 100 String getStyleClassForJs() { 101 String [] styleClassArray = getStyleClass() 102 .replaceAll(CSS_DEFAULT.PANEL_COLLAPSIBLE_STATE_COLLAPSED, "") 103 .split(" "); 104 String styleClass =""; 105 if (styleClassArray.length >= 1) { 106 styleClass = "'"+ styleClassArray[0] + "'"; 107 } 108 109 if (styleClassArray.length == 2) { 110 styleClass += ", '"+ styleClassArray[1]+ "'"; 111 } 112 return styleClass; 113 } 114 public void setStyleClass(String styleClass) { 115 this.styleClass = styleClass; 116 } 117 118 public String getLabel() { 119 ValueBinding vb = getValueBinding("label"); 120 if (vb != null) { 121 return (String ) vb.getValue(getFacesContext()); 122 } 123 if (label != null) { 124 return label; 125 } 126 return null; 127 } 128 129 public void setLabel(String label) { 130 ValueBinding vb = getValueBinding("label"); 131 if (vb != null) { 132 vb.setValue(getFacesContext(), label); 133 } else { 134 this.label = label; 135 } 136 } 137 138 139 public Boolean getExpanded() { 140 if (null != expanded) { 141 return expanded; 142 } 143 ValueBinding vb = getValueBinding("expanded"); 144 if (vb != null) { 145 if (vb.getValue(getFacesContext()) != null) { 146 return (Boolean ) vb.getValue(getFacesContext()); 147 } 148 } 149 return Boolean.FALSE; 150 } 151 152 public void setExpanded(Boolean expanded) { 153 this.expanded = expanded; 154 } 155 156 public Boolean getToggleOnClick() { 157 ValueBinding vb = getValueBinding("toggleOnClick"); 158 if (vb != null) { 159 return (Boolean ) vb.getValue(getFacesContext()); 160 } 161 if (toggleOnClick!= null) { 162 return toggleOnClick; 163 } 164 return Boolean.TRUE; 165 } 166 167 public void setToggleOnClick(Boolean toggleOnClick) { 168 ValueBinding vb = getValueBinding("toggleOnClick"); 169 if (vb != null) { 170 vb.setValue(getFacesContext(), toggleOnClick); 171 } else { 172 this.toggleOnClick= toggleOnClick; 173 } 174 } 175 176 public MethodBinding getAction() { 177 return null; 178 } 179 180 public void setAction(MethodBinding methodBinding) { 181 throw new UnsupportedOperationException ( 182 "Defining an action is not supported. Use an actionListener"); 183 } 184 185 public MethodBinding getActionListener() { 186 return actionListener; 187 } 188 189 public void setActionListener(MethodBinding actionListener) { 190 this.actionListener = actionListener; 191 } 192 193 public boolean isImmediate() { 194 if (immediate != null) { 195 return immediate.booleanValue(); 196 } 197 ValueBinding vb = getValueBinding("immediate"); 198 Boolean v = 199 vb != null ? (Boolean ) vb.getValue(getFacesContext()) : null; 200 return v != null ? v.booleanValue() : DEFAULT_IMMEDIATE; 201 } 202 203 public void setImmediate(boolean immediate) { 204 this.immediate = Boolean.valueOf(immediate); 205 } 206 207 public void addActionListener(ActionListener actionListener) { 208 addFacesListener(actionListener); 209 } 210 211 public ActionListener[] getActionListeners() { 212 return (ActionListener[]) getFacesListeners(ActionListener.class); 213 } 214 215 public void removeActionListener(ActionListener actionListener) { 216 removeFacesListener(actionListener); 217 } 218 219 public void broadcast(FacesEvent event) { 220 super.broadcast(event); 221 if (event instanceof ActionEvent) { 222 ActionEvent actionEvent = (ActionEvent) event; 223 if(actionListener != null) { 224 actionListener.invoke( 225 getFacesContext(), new Object []{actionEvent}); 226 } 227 } 235 } 236 237 240 public void setDisabled(boolean disabled) { 241 this.disabled = new Boolean (disabled); 242 ValueBinding vb = getValueBinding("disabled"); 243 if (vb != null) { 244 vb.setValue(getFacesContext(), this.disabled); 245 this.disabled = null; 246 } 247 } 248 249 252 public boolean isDisabled() { 253 if (!Util.isEnabledOnUserRole(this)) { 254 return true; 255 } 256 257 if (disabled != null) { 258 return disabled.booleanValue(); 259 } 260 ValueBinding vb = getValueBinding("disabled"); 261 Boolean v = 262 vb != null ? (Boolean ) vb.getValue(getFacesContext()) : null; 263 return v != null ? v.booleanValue() : false; 264 } 265 266 269 public void setEnabledOnUserRole(String enabledOnUserRole) { 270 this.enabledOnUserRole = enabledOnUserRole; 271 } 272 273 276 public String getEnabledOnUserRole() { 277 if (enabledOnUserRole != null) { 278 return enabledOnUserRole; 279 } 280 ValueBinding vb = getValueBinding("enabledOnUserRole"); 281 return vb != null ? (String ) vb.getValue(getFacesContext()) : null; 282 } 283 284 287 public void setRenderedOnUserRole(String renderedOnUserRole) { 288 this.renderedOnUserRole = renderedOnUserRole; 289 } 290 291 294 public String getRenderedOnUserRole() { 295 if (renderedOnUserRole != null) { 296 return renderedOnUserRole; 297 } 298 ValueBinding vb = getValueBinding("renderedOnUserRole"); 299 return vb != null ? (String ) vb.getValue(getFacesContext()) : null; 300 } 301 302 303 304 public Object saveState(FacesContext context) { 305 Object [] state = new Object [10]; 306 state[0] = super.saveState(context); 307 state[1] = label; 308 state[2] = expanded; 309 state[3] = saveAttachedState(context, actionListener); 310 state[4] = style; 311 state[5] = styleClass; 312 state[6] = disabled; 313 state[7] = enabledOnUserRole; 314 state[8] = renderedOnUserRole; 315 state[9] = immediate; 316 return state; 317 } 318 319 public void restoreState(FacesContext context, Object stateIn) { 320 Object [] state = (Object []) stateIn; 321 super.restoreState(context, state[0]); 322 label = (String )state[1]; 323 expanded= (Boolean )state[2]; 324 actionListener = (MethodBinding) restoreAttachedState(context, state[3]); 325 style = (String )state[4]; 326 styleClass = (String )state[5]; 327 disabled = (Boolean )state[6]; 328 enabledOnUserRole = (String )state[7]; 329 renderedOnUserRole = (String )state[8]; 330 immediate = (Boolean )state[9]; 331 } 332 333 334 335 336 337 public Object processSaveState(FacesContext facesContext) { 338 return super.processSaveState(facesContext); 339 } 340 341 public void processRestoreState(FacesContext facesContext, Object object) { 342 super.processRestoreState(facesContext, object); 343 } 344 345 public void processUpdates(FacesContext context) { 346 ValueBinding vb = getValueBinding("expanded"); 347 if (vb != null) { 348 try { 349 vb.setValue(context, expanded); 350 return; 351 } catch (EvaluationException e) { 352 String messageStr = e.getMessage(); 353 FacesMessage message = null; 354 if (null == messageStr) { 355 messageStr = "Conversion error"; 356 } 357 else { 358 message = new FacesMessage(messageStr); 359 } 360 message.setSeverity(FacesMessage.SEVERITY_ERROR); 361 context.addMessage(getClientId(context), message); 362 } catch (Exception e) { 363 String messageStr = e.getMessage(); 364 FacesMessage message = null; 365 if (null == messageStr) { 366 messageStr = "Conversion error"; 367 } 368 message = new FacesMessage(messageStr); 369 message.setSeverity(FacesMessage.SEVERITY_ERROR); 370 context.addMessage(getClientId(context), message); 371 372 } 373 } 374 } 375 } | Popular Tags |