1 16 package org.apache.myfaces.custom.tabbedpane; 17 18 import org.apache.myfaces.renderkit.RendererUtils; 19 import org.apache.myfaces.renderkit.html.HTML; 20 import org.apache.myfaces.renderkit.html.HtmlRenderer; 21 import org.apache.myfaces.renderkit.html.HtmlRendererUtils; 22 import org.apache.myfaces.component.UserRoleUtils; 23 24 import javax.faces.application.ViewHandler; 25 import javax.faces.component.NamingContainer; 26 import javax.faces.component.UIComponent; 27 import javax.faces.component.UIForm; 28 import javax.faces.component.UIViewRoot; 29 import javax.faces.component.UINamingContainer; 30 import javax.faces.context.FacesContext; 31 import javax.faces.context.ResponseWriter; 32 import java.io.IOException ; 33 import java.util.List ; 34 import java.util.Map ; 35 36 68 public class HtmlTabbedPaneRenderer 69 extends HtmlRenderer 70 { 71 73 private static final String TABLE_STYLE = 74 "border-style: none; " + 75 "padding: 0px; " + 76 "border-spacing: 0px; " + 77 "empty-cells: show; "; 78 79 private static final String ACTIVE_HEADER_CELL_STYLE = 80 "border-top: 2px outset #CCCCCC; " + 81 "border-right: 2px outset #CCCCCC; " + 82 "border-bottom: 0px none; " + 83 "border-left: 2px outset #CCCCCC; " + 84 "text-align: center; "; 85 86 private static final String INACTIVE_HEADER_CELL_STYLE = 87 "border-top: 1px outset #CCCCCC; " + 88 "border-right: 1px outset #CCCCCC; " + 89 "border-bottom: 0px none; " + 90 "border-left: 1px outset #CCCCCC; " + 91 "text-align: center; " + 92 "background-color: #CCCCCC; "; 93 94 private static final String EMPTY_HEADER_CELL_STYLE = 95 "border-top: 0px none; " + 96 "border-right: 0px none; " + 97 "border-bottom: 0px none; " + 98 "border-left: 0px none; "; 99 100 private static final String SUB_HEADER_CELL_STYLE = 101 "height: 2px; " + 102 "line-height: 0px; font-size: 0px; " + 103 "border-bottom: 0px none; "; 104 105 private static final String TAB_CELL_STYLE = 106 "border-top: 0px none; " + 107 "border-right: 2px outset #CCCCCC; " + 108 "border-bottom: 2px outset #CCCCCC; " + 109 "border-left: 2px outset #CCCCCC; " + 110 "padding: 10px; "; 111 112 private static final String NO_BORDER_STYLE = 113 "0px none; "; 114 115 private static final String BORDER_STYLE = 116 "2px outset #CCCCCC; "; 117 118 private static final String BUTTON_STYLE_ACTIVE 119 = "border-style:none; width:100%; cursor:pointer;"; 120 private static final String BUTTON_STYLE_INACTIVE 121 = "border-style:none; width:100%; cursor:pointer; background-color:#CCCCCC;"; 122 123 private static final String BUTTON_STYLE_DISABLED 124 = "border-style:none; width:100%; cursor:normal;"; 125 126 private static final String DEFAULT_BG_COLOR = "#FFFFFF"; 127 128 private static final String AUTO_FORM_SUFFIX = ".autoform"; 129 130 131 public void encodeBegin(FacesContext facesContext, UIComponent uiComponent) throws IOException 132 { 133 } 134 135 public boolean getRendersChildren() 136 { 137 return true; 138 } 139 140 public void encodeChildren(FacesContext facescontext, UIComponent uicomponent) throws IOException 141 { 142 } 143 144 public void encodeEnd(FacesContext facesContext, UIComponent uiComponent) throws IOException 145 { 146 RendererUtils.checkParamValidity(facesContext, uiComponent, HtmlPanelTabbedPane.class); 147 148 ResponseWriter writer = facesContext.getResponseWriter(); 149 150 HtmlRendererUtils.writePrettyLineSeparator(facesContext); 151 152 HtmlPanelTabbedPane tabbedPane = (HtmlPanelTabbedPane)uiComponent; 153 int selectedIndex = tabbedPane.getSelectedIndex(); 154 155 if (tabbedPane.getBgcolor() == null) 156 { 157 tabbedPane.setBgcolor(DEFAULT_BG_COLOR); 158 } 159 160 UIForm parentForm = RendererUtils.findParentForm(tabbedPane); 161 if (parentForm == null) 162 { 163 writeFormStart(writer, facesContext, tabbedPane); 164 } 165 166 writeTableStart(writer, facesContext, tabbedPane); 167 HtmlRendererUtils.writePrettyLineSeparator(facesContext); 168 writer.startElement(HTML.TR_ELEM, uiComponent); 169 170 int tabIdx = 0; 172 int visibleTabCount = 0; 173 int visibleTabSelectedIdx = -1; 174 List children = tabbedPane.getChildren(); 175 for (int i = 0, len = children.size(); i < len; i++) 176 { 177 UIComponent child = getUIComponent((UIComponent)children.get(i)); 178 if (child instanceof HtmlPanelTab) 179 { 180 if (child.isRendered()) 181 { 182 writeHeaderCell(writer, facesContext, tabbedPane, 183 (HtmlPanelTab)child, tabIdx, tabIdx == selectedIndex, isDisabled(facesContext, child)); 184 if (tabIdx == selectedIndex) 185 { 186 visibleTabSelectedIdx = visibleTabCount; 187 } 188 visibleTabCount++; 189 } 190 tabIdx++; 191 } 192 } 193 194 HtmlRendererUtils.writePrettyLineSeparator(facesContext); 196 HtmlRendererUtils.writePrettyIndent(facesContext); 197 writer.startElement(HTML.TD_ELEM, uiComponent); 198 writer.writeAttribute(HTML.STYLE_ATTR, EMPTY_HEADER_CELL_STYLE, null); 199 writer.write(" "); 200 writer.endElement(HTML.TD_ELEM); 201 HtmlRendererUtils.writePrettyLineSeparator(facesContext); 202 writer.endElement(HTML.TR_ELEM); 203 204 HtmlRendererUtils.writePrettyLineSeparator(facesContext); 206 writer.startElement(HTML.TR_ELEM, uiComponent); 207 writeSubHeaderCells(writer, facesContext, tabbedPane, visibleTabCount, visibleTabSelectedIdx, false); 208 HtmlRendererUtils.writePrettyLineSeparator(facesContext); 209 writer.endElement(HTML.TR_ELEM); 210 211 HtmlRendererUtils.writePrettyLineSeparator(facesContext); 213 writer.startElement(HTML.TR_ELEM, uiComponent); 214 writeTabCell(writer, facesContext, tabbedPane, visibleTabCount, selectedIndex); 215 HtmlRendererUtils.writePrettyLineSeparator(facesContext); 216 writer.endElement(HTML.TR_ELEM); 217 218 HtmlRendererUtils.writePrettyLineSeparator(facesContext); 219 writer.endElement(HTML.TABLE_ELEM); 220 221 if (parentForm == null) 222 { 223 writeFormEnd(writer, facesContext); 224 } 225 } 226 227 228 public void decode(FacesContext facesContext, UIComponent uiComponent) 229 { 230 RendererUtils.checkParamValidity(facesContext, uiComponent, HtmlPanelTabbedPane.class); 231 232 HtmlPanelTabbedPane tabbedPane = (HtmlPanelTabbedPane)uiComponent; 233 234 Map paramMap = facesContext.getExternalContext().getRequestParameterMap(); 235 236 int tabIdx = 0; 237 List children = tabbedPane.getChildren(); 238 for (int i = 0, len = children.size(); i < len; i++) 239 { 240 UIComponent child = getUIComponent((UIComponent)children.get(i)); 241 if (child instanceof HtmlPanelTab) 242 { 243 String paramName = tabbedPane.getClientId(facesContext) + "." + tabIdx; 244 String paramValue = (String )paramMap.get(paramName); 245 if (paramValue != null && paramValue.length() > 0) 246 { 247 tabbedPane.queueEvent(new TabChangeEvent(tabbedPane, 248 tabbedPane.getSelectedIndex(), 249 tabIdx)); 250 return; 251 } 252 tabIdx++; 253 } 254 } 255 } 256 257 258 protected void writeFormStart(ResponseWriter writer, 259 FacesContext facesContext, 260 UIComponent tabbedPane) 261 throws IOException 262 { 263 ViewHandler viewHandler = facesContext.getApplication().getViewHandler(); 264 String viewId = facesContext.getViewRoot().getViewId(); 265 String actionURL = viewHandler.getActionURL(facesContext, viewId); 266 267 writer.startElement(HTML.FORM_ELEM, null); 269 writer.writeAttribute(HTML.NAME_ATTR, tabbedPane.getClientId(facesContext) + AUTO_FORM_SUFFIX, null); 270 writer.writeAttribute(HTML.STYLE_ATTR, "display:inline", null); 271 writer.writeAttribute(HTML.METHOD_ATTR, "post", null); 272 writer.writeURIAttribute(HTML.ACTION_ATTR, 273 facesContext.getExternalContext().encodeActionURL(actionURL), 274 null); 275 writer.flush(); 276 } 277 278 279 protected void writeTableStart(ResponseWriter writer, 280 FacesContext facesContext, 281 HtmlPanelTabbedPane tabbedPane) 282 throws IOException 283 { 284 String oldStyle = tabbedPane.getStyle(); 285 if (oldStyle == null) 286 { 287 tabbedPane.setStyle(TABLE_STYLE); 288 } 289 else 290 { 291 tabbedPane.setStyle(TABLE_STYLE + "; " + oldStyle); 292 } 293 294 String oldBgColor = tabbedPane.getBgcolor(); 295 tabbedPane.setBgcolor(null); 296 297 writer.startElement(HTML.TABLE_ELEM, tabbedPane); 298 writer.writeAttribute(HTML.CELLSPACING_ATTR, "0", null); 299 HtmlRendererUtils.renderHTMLAttributes(writer, tabbedPane, HTML.TABLE_PASSTHROUGH_ATTRIBUTES); 300 writer.flush(); 301 302 tabbedPane.setStyle(oldStyle); 303 tabbedPane.setBgcolor(oldBgColor); 304 } 305 306 307 protected void writeHeaderCell(ResponseWriter writer, 308 FacesContext facesContext, 309 HtmlPanelTabbedPane tabbedPane, 310 HtmlPanelTab tab, 311 int tabIndex, 312 boolean active, 313 boolean disabled) 314 throws IOException 315 { 316 HtmlRendererUtils.writePrettyLineSeparator(facesContext); 317 HtmlRendererUtils.writePrettyIndent(facesContext); 318 writer.startElement(HTML.TD_ELEM, tabbedPane); 319 if (active) 320 { 321 writer.writeAttribute(HTML.STYLE_ATTR, 322 ACTIVE_HEADER_CELL_STYLE + "background-color:" + tabbedPane.getBgcolor(), 323 null); 324 325 HtmlRendererUtils.renderHTMLAttribute(writer, tabbedPane, "activeTabStyleClass", HTML.STYLE_CLASS_ATTR); 326 } 327 else if (disabled) 328 { 329 writer.writeAttribute(HTML.STYLE_ATTR, 330 INACTIVE_HEADER_CELL_STYLE, 331 null); 332 HtmlRendererUtils.renderHTMLAttribute(writer, tabbedPane, "disabledTabStyleClass", HTML.STYLE_CLASS_ATTR); 333 } 334 else 335 { 336 writer.writeAttribute(HTML.STYLE_ATTR, 337 INACTIVE_HEADER_CELL_STYLE, 338 null); 339 HtmlRendererUtils.renderHTMLAttribute(writer, tabbedPane, "inactiveTabStyleClass", HTML.STYLE_CLASS_ATTR); 340 } 341 342 343 String label = tab.getLabel(); 344 if (label == null || label.length() == 0) 345 { 346 label = "Tab " + tabIndex; 347 } 348 349 if (disabled) { 350 writer.startElement(HTML.LABEL_ELEM, tabbedPane); 351 writer.writeAttribute(HTML.NAME_ATTR, tabbedPane.getClientId(facesContext) + "." + tabIndex, null); 352 writer.writeAttribute(HTML.STYLE_ATTR, BUTTON_STYLE_DISABLED, null); 353 writer.writeText(label, null); 354 writer.endElement(HTML.LABEL_ELEM); 355 } else { 356 writer.startElement(HTML.INPUT_ELEM, tabbedPane); 358 writer.writeAttribute(HTML.TYPE_ATTR, "submit", null); 359 writer.writeAttribute(HTML.NAME_ATTR, tabbedPane.getClientId(facesContext) + "." + tabIndex, null); 360 writer.writeAttribute(HTML.VALUE_ATTR, label, null); 361 362 if (active) 363 { 364 writer.writeAttribute(HTML.STYLE_ATTR, 365 BUTTON_STYLE_ACTIVE + "background-color:" + tabbedPane.getBgcolor(), 366 null); 367 } 368 else 369 { 370 writer.writeAttribute(HTML.STYLE_ATTR, 371 BUTTON_STYLE_INACTIVE, 372 null); 373 } 374 writer.endElement(HTML.INPUT_ELEM); 375 } 376 writer.endElement(HTML.TD_ELEM); 377 } 378 379 380 protected void writeSubHeaderCells(ResponseWriter writer, 381 FacesContext facesContext, 382 HtmlPanelTabbedPane tabbedPane, 383 int visibleTabCount, 384 int visibleTabSelectedIndex, 385 boolean disabled) 386 throws IOException 387 { 388 StringBuffer buf = new StringBuffer (); 389 for (int i = 0, cnt = visibleTabCount + 1; i < cnt; i++) 390 { 391 HtmlRendererUtils.writePrettyLineSeparator(facesContext); 392 HtmlRendererUtils.writePrettyIndent(facesContext); 393 writer.startElement(HTML.TD_ELEM, tabbedPane); 394 buf.setLength(0); 395 buf.append(SUB_HEADER_CELL_STYLE); 396 buf.append("border-top:").append(i == visibleTabSelectedIndex ? NO_BORDER_STYLE : BORDER_STYLE); 397 buf.append("border-right:").append(i + 1 < cnt ? NO_BORDER_STYLE : BORDER_STYLE); 398 buf.append("border-left:").append(i > 0 ? NO_BORDER_STYLE : BORDER_STYLE); 399 buf.append("background-color:").append(tabbedPane.getBgcolor()); 400 writer.writeAttribute(HTML.STYLE_ATTR, buf.toString(), null); 401 402 if (i == visibleTabSelectedIndex) { 403 HtmlRendererUtils.renderHTMLAttribute(writer, tabbedPane, "activeSubStyleClass", HTML.STYLE_CLASS_ATTR); 404 } else { 405 HtmlRendererUtils.renderHTMLAttribute(writer, tabbedPane, "inactiveSubStyleClass", HTML.STYLE_CLASS_ATTR); 406 } 407 408 writer.write(" "); 409 writer.endElement(HTML.TD_ELEM); 410 } 411 } 412 413 414 protected void writeTabCell(ResponseWriter writer, 415 FacesContext facesContext, 416 HtmlPanelTabbedPane tabbedPane, 417 int tabCount, 418 int selectedIndex) 419 throws IOException 420 { 421 HtmlRendererUtils.writePrettyLineSeparator(facesContext); 422 HtmlRendererUtils.writePrettyIndent(facesContext); 423 writer.startElement(HTML.TD_ELEM, tabbedPane); 424 writer.writeAttribute(HTML.COLSPAN_ATTR, Integer.toString(tabCount + 1), null); 425 writer.writeAttribute(HTML.STYLE_ATTR, TAB_CELL_STYLE + "background-color:" + tabbedPane.getBgcolor(), null); 426 HtmlRendererUtils.renderHTMLAttribute(writer, tabbedPane, "tabContentStyleClass", HTML.STYLE_CLASS_ATTR); 427 428 int tabIdx = 0; 429 List children = tabbedPane.getChildren(); 430 for (int i = 0, len = children.size(); i < len; i++) 431 { 432 UIComponent child = getUIComponent((UIComponent)children.get(i)); 433 if (child instanceof HtmlPanelTab) 434 { 435 if (tabIdx == selectedIndex) 436 { 437 RendererUtils.renderChild(facesContext, child); 438 } 439 tabIdx++; 440 } 441 else 442 { 443 RendererUtils.renderChild(facesContext, child); 444 } 445 } 446 447 writer.endElement(HTML.TD_ELEM); 448 } 449 450 private UIComponent getUIComponent(UIComponent uiComponent) 451 { 452 if (uiComponent instanceof UIForm || uiComponent instanceof UINamingContainer) 453 { 454 List children = uiComponent.getChildren(); 455 for (int i = 0, len = children.size(); i < len; i++) 456 { 457 uiComponent = getUIComponent((UIComponent)children.get(i)); 458 } 459 } 460 return uiComponent; 461 } 462 463 464 protected void writeFormEnd(ResponseWriter writer, 465 FacesContext facesContext) 466 throws IOException 467 { 468 ViewHandler viewHandler = facesContext.getApplication().getViewHandler(); 470 viewHandler.writeState(facesContext); 471 472 writer.endElement(HTML.FORM_ELEM); 473 } 474 475 476 protected boolean isDisabled(FacesContext facesContext, UIComponent uiComponent) 477 { 478 return !UserRoleUtils.isEnabledOnUserRole(uiComponent); 479 } 480 } 481 | Popular Tags |