1 33 34 package com.icesoft.faces.component.panelborder; 35 36 import com.icesoft.faces.component.util.CustomComponentUtils; 37 import com.icesoft.faces.context.DOMContext; 38 import com.icesoft.faces.renderkit.dom_html_basic.DomBasicRenderer; 39 import com.icesoft.faces.renderkit.dom_html_basic.HTML; 40 import com.icesoft.faces.renderkit.dom_html_basic.PassThruAttributeRenderer; 41 import org.w3c.dom.Element ; 42 43 import javax.faces.component.UIComponent; 44 import javax.faces.context.FacesContext; 45 import java.io.IOException ; 46 import java.util.List ; 47 48 52 public class PanelBorderRenderer extends DomBasicRenderer { 53 public static final String DEFAULT_LAYOUT = "default"; 54 public static final String REVERSE_W_E = "horizontal reverse"; 55 public static final String REVERSE_N_S = "vertical reverse"; 56 public static final String CENTER_ONLY = "center only"; 57 public static final String HIDE_N = "hide north"; 58 public static final String HIDE_E = "hide east"; 59 public static final String HIDE_S = "hide south"; 60 public static final String HIDE_W = "hide west"; 61 62 65 public boolean getRendersChildren() { 66 return true; 67 } 68 69 72 public void encodeChildren(FacesContext facesContext, 73 UIComponent uiComponent) throws IOException { 74 super.encodeChildren(facesContext, uiComponent); 75 } 76 77 80 public void encodeEnd(FacesContext facesContext, UIComponent uiComponent) 81 throws IOException { 82 validateParameters(facesContext, uiComponent, PanelBorder.class); 83 84 DOMContext domContext = 85 DOMContext.attachDOMContext(facesContext, uiComponent); 86 PanelBorder borderLayout = (PanelBorder) uiComponent; 87 List layout = borderLayout.getLayoutAsList(); 88 String clientId = uiComponent.getClientId(facesContext); 89 if (!domContext.isInitialized()) { 90 Element table = domContext.createRootElement(HTML.TABLE_ELEM); 91 setRootElementId(facesContext, table, uiComponent); 92 table.setAttribute(HTML.NAME_ATTR, clientId); 93 } 94 95 Element table = (Element ) domContext.getRootNode(); 96 PassThruAttributeRenderer 97 .renderAttributes(facesContext, uiComponent, null); 98 table.setAttribute(HTML.CLASS_ATTR, borderLayout.getStyleClass()); 99 String style = borderLayout.getStyle(); 100 if(style != null && style.length() > 0) 101 table.setAttribute(HTML.STYLE_ATTR, style); 102 else 103 table.removeAttribute(HTML.STYLE_ATTR); 104 105 DOMContext.removeChildren(table); 106 107 renderPanel(facesContext, layout, table, borderLayout, domContext); 108 109 if (borderLayout.getChildCount() > 0) { 110 throw new RuntimeException ( 111 "PanelBorder must not have children, only facets allowed!"); 112 } 113 114 115 domContext.stepOver(); 116 domContext.streamWrite(facesContext, uiComponent); 117 } 118 119 private void renderPanel(FacesContext facesContext, List facets, 120 Element table, PanelBorder borderLayout, 121 DOMContext domContext) throws IOException { 122 String order = borderLayout.getLayout(); 123 if (order.equalsIgnoreCase(REVERSE_N_S)) { 124 renderSouth(facesContext, domContext, borderLayout, facets, table); 125 renderEastWestCenter(facesContext, domContext, borderLayout, facets, 126 table); 127 renderNorth(facesContext, domContext, borderLayout, facets, table); 128 129 } else { 130 renderNorth(facesContext, domContext, borderLayout, facets, table); 131 renderEastWestCenter(facesContext, domContext, borderLayout, facets, 132 table); 133 renderSouth(facesContext, domContext, borderLayout, facets, table); 134 } 135 } 136 137 private void renderEastWestCenter(FacesContext facesContext, 138 DOMContext domContext, 139 PanelBorder borderLayout, List facets, 140 Element table) throws IOException { 141 if (borderLayout.getWest() != null || 142 borderLayout.getCenter() != null || 143 borderLayout.getEast() != null) { 144 Element tr = domContext.createElement(HTML.TR_ELEM); 145 table.appendChild(tr); 146 String order = borderLayout.getLayout(); 147 if (order.equalsIgnoreCase(REVERSE_W_E)) { 148 renderEast(facesContext, domContext, borderLayout, facets, tr); 149 renderCenter(facesContext, domContext, borderLayout, facets, 150 tr); 151 renderWest(facesContext, domContext, borderLayout, facets, tr); 152 } else { 153 renderWest(facesContext, domContext, borderLayout, facets, tr); 154 renderCenter(facesContext, domContext, borderLayout, facets, 155 tr); 156 renderEast(facesContext, domContext, borderLayout, facets, tr); 157 } 158 159 } 160 } 161 162 private void renderWest(FacesContext facesContext, DOMContext domContext, 163 PanelBorder borderLayout, List facets, Element tr) 164 throws IOException { 165 if (facets.contains(PanelBorder.WEST_LAYOUT)) { 166 renderTableCells(facesContext, borderLayout, tr, 167 PanelBorder.WEST_LAYOUT, domContext); 168 } 169 } 170 171 private void renderCenter(FacesContext facesContext, DOMContext domContext, 172 PanelBorder borderLayout, List facets, Element tr) 173 throws IOException { 174 if (facets.contains(PanelBorder.CENTER_LAYOUT)) { 175 renderTableCells(facesContext, borderLayout, tr, 176 PanelBorder.CENTER_LAYOUT, domContext); 177 } 178 } 179 180 private void renderEast(FacesContext facesContext, DOMContext domContext, 181 PanelBorder borderLayout, List facets, Element tr) 182 throws IOException { 183 if (facets.contains(PanelBorder.EAST_LAYOUT)) { 184 renderTableCells(facesContext, borderLayout, tr, 185 PanelBorder.EAST_LAYOUT, domContext); 186 } 187 } 188 189 190 private void renderNorth(FacesContext facesContext, DOMContext domContext, 191 PanelBorder borderLayout, List facets, 192 Element table) throws IOException { 193 if ((borderLayout.getNorth() != null) && 194 (facets.contains(PanelBorder.NORTH_LAYOUT))) { 195 Element tr = domContext.createElement(HTML.TR_ELEM); 196 table.appendChild(tr); 197 renderTableCells(facesContext, borderLayout, tr, 198 PanelBorder.NORTH_LAYOUT, domContext); 199 } 200 } 201 202 private void renderSouth(FacesContext facesContext, DOMContext domContext, 203 PanelBorder borderLayout, List facets, 204 Element table) throws IOException { 205 if ((borderLayout.getSouth() != null) && 206 (facets.contains(PanelBorder.SOUTH_LAYOUT))) { 207 Element tr = domContext.createElement(HTML.TR_ELEM); 208 table.appendChild(tr); 209 renderTableCells(facesContext, borderLayout, tr, 210 PanelBorder.SOUTH_LAYOUT, domContext); 211 } 212 } 213 214 private Element getTD(FacesContext facesContext, PanelBorder borderLayout, 215 DOMContext domContext, Element tr) 216 throws IOException { 217 Element td = domContext.createElement(HTML.TD_ELEM); 218 tr.appendChild(td); 219 domContext.setCursorParent(td); 220 return td; 221 } 222 223 private void renderTableCells(FacesContext facesContext, 224 PanelBorder borderLayout, Element tr, 225 String facet, DOMContext domContext) 226 throws IOException { 227 UIComponent north = borderLayout.getNorth(); 228 UIComponent west = borderLayout.getWest(); 229 UIComponent east = borderLayout.getEast(); 230 UIComponent center = borderLayout.getCenter(); 231 UIComponent south = borderLayout.getSouth(); 232 233 if (facet.equals(PanelBorder.NORTH_LAYOUT) && north != null) { 234 int width = 0; 237 238 if (west != null) { 239 width++; 240 } 241 if (east != null) { 242 width++; 243 } 244 if (center != null) { 245 width++; 246 } 247 248 renderTableCell(facesContext, north, 249 getTD(facesContext, borderLayout, domContext, tr), 250 (width == 0) ? 1 : width, 251 borderLayout.getNorthClass(), null, borderLayout); 252 } 253 254 if (facet.equals(PanelBorder.WEST_LAYOUT) && west != null) { 255 renderTableCell(facesContext, west, 256 getTD(facesContext, borderLayout, domContext, tr), 257 1, 258 borderLayout.getWestClass(), null, borderLayout); 259 } 260 261 if (facet.equals(PanelBorder.EAST_LAYOUT) && east != null) { 262 renderTableCell(facesContext, east, 263 getTD(facesContext, borderLayout, domContext, tr), 264 1, 265 borderLayout.getEastClass(), null, borderLayout); 266 } 267 268 if (facet.equals(PanelBorder.CENTER_LAYOUT) && center != null) { 269 renderTableCell(facesContext, center, 270 getTD(facesContext, borderLayout, domContext, tr), 271 1, 272 borderLayout.getCenterClass(), null, borderLayout); 273 } 274 275 if (facet.equals(PanelBorder.SOUTH_LAYOUT) && south != null) { 276 int width = 0; 279 280 if (west != null) { 281 width++; 282 } 283 if (east != null) { 284 width++; 285 } 286 if (center != null) { 287 width++; 288 } 289 290 renderTableCell(facesContext, south, 291 getTD(facesContext, borderLayout, domContext, tr), 292 (width == 0) ? 1 : width, 293 borderLayout.getSouthClass(), null, borderLayout); 294 } 295 } 296 297 private void renderTableCell(FacesContext facesContext, 298 UIComponent component, 299 Element td, int colspan, String styleClass, 300 String style, PanelBorder panelBorder) 301 throws IOException { 302 if (colspan > 0) { 303 td.setAttribute(HTML.COLSPAN_ATTR, Integer.toString(colspan)); 304 } 305 else { 306 td.removeAttribute(HTML.COLSPAN_ATTR); 307 } 308 if (styleClass != null && styleClass.length() > 0) { 309 td.setAttribute(HTML.CLASS_ATTR, styleClass); 310 } 311 else { 312 td.removeAttribute(HTML.CLASS_ATTR); 313 } 314 if (style != null && style.length() > 0) { 315 td.setAttribute(HTML.STYLE_ATTR, style); 316 } 317 else { 318 td.removeAttribute(HTML.STYLE_ATTR); 319 } 320 321 DOMContext domContext = 322 DOMContext.getDOMContext(facesContext, panelBorder); 323 domContext.streamWrite(facesContext, panelBorder, 324 domContext.getRootNode(), td); 325 CustomComponentUtils.renderChild(facesContext, component); 326 } 327 } | Popular Tags |