1 33 34 package com.icesoft.faces.component.ext.renderkit; 35 36 import com.icesoft.faces.component.dragdrop.DndEvent; 37 import com.icesoft.faces.component.dragdrop.DragEvent; 38 import com.icesoft.faces.component.dragdrop.DropEvent; 39 import com.icesoft.faces.component.ext.HtmlPanelGroup; 40 import com.icesoft.faces.context.DOMContext; 41 import com.icesoft.faces.context.effects.CurrentStyle; 42 import com.icesoft.faces.context.effects.DragDrop; 43 import com.icesoft.faces.context.effects.JavascriptContext; 44 import com.icesoft.faces.context.effects.LocalEffectEncoder; 45 import com.icesoft.faces.renderkit.dom_html_basic.HTML; 46 import com.icesoft.faces.utils.DnDCache; 47 import org.apache.commons.logging.Log; 48 import org.apache.commons.logging.LogFactory; 49 import org.w3c.dom.Element ; 50 51 import javax.faces.component.NamingContainer; 52 import javax.faces.component.UIComponent; 53 import javax.faces.component.UIViewRoot; 54 import javax.faces.context.FacesContext; 55 import javax.faces.el.MethodBinding; 56 import java.io.IOException ; 57 import java.util.Map ; 58 59 public class GroupRenderer 60 extends com.icesoft.faces.renderkit.dom_html_basic.GroupRenderer { 61 62 protected static final String STATUS = "status"; 63 64 protected static final String DROP = "dropID"; 65 private static Log log = LogFactory.getLog(GroupRenderer.class); 66 67 68 public void encodeBegin(FacesContext facesContext, UIComponent uiComponent) 69 throws IOException { 70 try { 71 72 String viewID = facesContext.getViewRoot().getViewId(); 73 74 String style = ((HtmlPanelGroup) uiComponent).getStyle(); 75 String styleClass = ((HtmlPanelGroup) uiComponent).getStyleClass(); 76 String blockingFlag = (String ) facesContext.getExternalContext() 77 .getRequestMap().get("BlockingServlet"); 78 79 String dndType = getDndType(uiComponent); 80 DOMContext domContext = 81 DOMContext.attachDOMContext(facesContext, uiComponent); 82 83 if (!domContext.isInitialized()) { 84 Element rootSpan = domContext.createElement(HTML.DIV_ELEM); 85 domContext.setRootNode(rootSpan); 86 setRootElementId(facesContext, rootSpan, uiComponent); 87 88 if (dndType != null) { 89 Element statusField = createHiddenField(domContext, 91 facesContext, 92 uiComponent, 93 STATUS); 94 rootSpan.appendChild(statusField); 95 96 Element targetID = createHiddenField(domContext, 97 facesContext, 98 uiComponent, DROP); 99 rootSpan.appendChild(targetID); 100 } 101 102 } 103 104 105 if (dndType != null) { 106 DnDCache.getInstance(facesContext, true).put( 107 uiComponent.getClientId(facesContext), 108 (HtmlPanelGroup) uiComponent, facesContext); 109 addJavascriptCalls(uiComponent, dndType, null, facesContext); 110 } 111 112 Element rootSpan = (Element ) domContext.getRootNode(); 113 if (styleClass != null) { 114 rootSpan.setAttribute("class", styleClass); 115 } 116 JavascriptContext.fireEffect(uiComponent, facesContext); 117 118 LocalEffectEncoder 119 .encodeLocalEffects(uiComponent, rootSpan, facesContext); 120 String extraStyle = null; 121 String scrollWidth = 122 (String ) uiComponent.getAttributes().get("scrollWidth"); 123 String scrollHeight = 124 (String ) uiComponent.getAttributes().get("scrollHeight"); 125 126 127 if (scrollHeight != null || scrollWidth != null) { 128 if (extraStyle == null) { 129 extraStyle = ""; 130 } 131 if (scrollHeight == null) { 132 extraStyle += "width:" + scrollWidth + ";overflow:auto;"; 133 } else if (scrollWidth == null) { 134 extraStyle += "height:" + scrollHeight + ";overflow:auto;"; 135 } else { 136 extraStyle += "width:" + scrollWidth + ";height:" + 137 scrollHeight + ";overflow:auto;"; 138 } 139 } 140 141 142 CurrentStyle.apply(facesContext, uiComponent, null, extraStyle); 143 domContext.streamWrite(facesContext, uiComponent, 144 domContext.getRootNode(), rootSpan); 145 domContext.stepInto(uiComponent); 146 } catch (Exception e) { 148 e.printStackTrace(); 149 } 150 } 151 152 protected String addJavascriptCalls(UIComponent uiComponent, String dndType, 153 String handleId, 154 FacesContext facesContext) { 155 String calls = ""; 156 157 boolean dragListener = 158 uiComponent.getAttributes().get("dragListener") != null; 159 boolean dropListener = 160 uiComponent.getAttributes().get("dropListener") != null; 161 String dragMask = DndEvent.parseMask( 162 (String ) uiComponent.getAttributes().get("dragMask")); 163 String dropMask = DndEvent.parseMask( 164 (String ) uiComponent.getAttributes().get("dropMask")); 165 String dragOptions = 166 (String ) uiComponent.getAttributes().get("dragOptions"); 167 String hoverClass = 168 (String ) uiComponent.getAttributes().get("hoverclass"); 169 if (!dragListener) { 170 if (dragMask == null) { 171 dragMask = DndEvent.MASK_ALL_BUT_DROPS; 172 } 173 } 174 if (!dropListener) { 175 dropMask = DndEvent.MASK_ALL; 176 } 177 178 if ("DRAG".equalsIgnoreCase(dndType)) { 179 180 calls += DragDrop.addDragable(uiComponent.getClientId(facesContext), 181 handleId, dragOptions, dragMask, 182 facesContext); 183 184 } else if ("drop".equalsIgnoreCase(dndType)) { 185 calls += DragDrop.addDroptarget( 186 uiComponent.getClientId(facesContext), null, facesContext, 187 dropMask, hoverClass); 188 } else if ("dragdrop".equalsIgnoreCase(dndType)) { 189 190 calls += DragDrop.addDragable(uiComponent.getClientId(facesContext), 191 handleId, dragOptions, dragMask, 192 facesContext); 193 calls += DragDrop.addDroptarget( 194 uiComponent.getClientId(facesContext), null, facesContext, 195 dropMask, hoverClass); 196 } else { 197 throw new IllegalArgumentException ("Value [" + dndType + 198 "] is not valid for dndType. Please use drag or drop"); 199 } 200 return calls; 201 } 202 203 204 public void encodeEnd(FacesContext facesContext, UIComponent uiComponent) 205 throws IOException { 206 validateParameters(facesContext, uiComponent, null); 207 DOMContext domContext = 208 DOMContext.getDOMContext(facesContext, uiComponent); 209 domContext.stepOver(); 210 211 } 212 213 protected String appendStyle(String currentStyle, String additionalStyle) { 214 String result = ""; 215 if (!isBlank(currentStyle)) { 216 result = currentStyle; 217 } 218 if (!isBlank(additionalStyle)) { 219 result += additionalStyle; 220 } 221 if (isBlank(result)) { 222 return null; 223 } 224 return result; 225 } 226 227 private static boolean isBlank(String s) { 228 return !(s != null && s.trim().length() > 0); 229 } 230 231 232 public void decode(FacesContext context, UIComponent component) { 233 super.decode(context, component); 234 235 if (log.isTraceEnabled()) { 236 log.trace("GroupRenderer:decode"); 237 } 238 if (component instanceof HtmlPanelGroup) { 239 HtmlPanelGroup panel = (HtmlPanelGroup) component; 240 String dndType = getDndType(component); 241 242 if (panel.getDraggable() != null || panel.getDropTarget() != null) { 243 244 Map requestMap = context.getExternalContext() 245 .getRequestParameterValuesMap(); 246 String fieldName = 247 getHiddenFieldName(context, component, STATUS); 248 String status = getParamamterValue( 249 (String []) requestMap.get(fieldName)); 250 if (status == null) { 251 if (log.isTraceEnabled()) { 252 log.trace("Drag Drop Status for ID [" + 253 panel.getClientId(context) + 254 "] Field Name [" + fieldName + 255 "] is null. Returning"); 256 } 257 return; 258 } 259 String targetID = getParamamterValue((String []) requestMap 260 .get(getHiddenFieldName(context, component, DROP))); 261 262 Object targetDragValue = null; 263 Object targetDropValue = null; 264 265 if (targetID != null && targetID.length() > 0) { 266 DnDCache dndCache = DnDCache.getInstance(context, false); 267 targetDragValue = dndCache.getDragValue(targetID); 268 targetDropValue = dndCache.getDropValue(targetID); 269 270 } 271 272 if (log.isTraceEnabled()) { 273 log.trace("Dnd Event Client ID [" + 274 component.getClientId(context) + "] Target ID [" + 275 targetID + "] Status [" + status + "]"); 276 } 277 278 279 if (panel.getDragListener() == null && 280 panel.getDropListener() == null) { 281 282 return; 283 } 284 int type = 0; 285 try { 286 type = Integer.parseInt(status); 287 } catch (NumberFormatException e) { 288 if (status != null || status.length() != 0) 289 290 { 291 return; 292 } 293 } 294 295 296 MethodBinding listener = panel.getDragListener(); 297 if (listener != null) { 298 299 DragEvent event = new DragEvent(component, type, targetID, 300 targetDragValue, 301 targetDropValue); 302 panel.queueEvent(event); 303 } 304 listener = panel.getDropListener(); 305 if (listener != null) { 306 307 DropEvent event = new DropEvent(component, type, targetID, 308 targetDragValue, 309 targetDropValue); 310 panel.queueEvent(event); 311 } 312 } 313 } 314 } 315 316 317 protected Element createHiddenField(DOMContext domContext, 318 FacesContext facesContext, 319 UIComponent uiComponent, String name) { 320 Element ele = domContext.createElement(HTML.INPUT_ELEM); 321 ele.setAttribute(HTML.TYPE_ATTR, "hidden"); 322 String n = getHiddenFieldName(facesContext, uiComponent, name); 323 ele.setAttribute(HTML.NAME_ATTR, n); 324 ele.setAttribute(HTML.ID_ATTR, n); 325 ele.setAttribute(HTML.VALUE_ATTR, ""); 326 return ele; 327 } 328 329 330 protected String getHiddenFieldName(FacesContext facesContext, 331 UIComponent uiComponent, String name) { 332 UIComponent form = findForm(uiComponent); 333 String formId = form.getClientId(facesContext); 334 String clientId = uiComponent.getClientId(facesContext); 335 return formId 336 + NamingContainer.SEPARATOR_CHAR 337 + UIViewRoot.UNIQUE_ID_PREFIX 338 + clientId 339 + name; 340 } 341 342 protected String getDndType(UIComponent uiComponent) { 343 String dndType = null; 344 String draggable = 345 (String ) uiComponent.getAttributes().get("draggable"); 346 String droppable = 347 (String ) uiComponent.getAttributes().get("dropTarget"); 348 if ("true".equalsIgnoreCase(draggable) && 349 "true".equalsIgnoreCase(droppable)) { 350 dndType = "dragdrop"; 351 } else if ("true".equalsIgnoreCase(draggable)) { 352 dndType = "DRAG"; 353 } else if ("true".equalsIgnoreCase(droppable)) { 354 dndType = "drop"; 355 } 356 return dndType; 357 } 358 359 366 private String getParamamterValue(String [] sa) { 367 368 if (sa == null) { 370 if (log.isTraceEnabled()) { 371 log.trace("Null parameter value"); 372 } 373 return null; 374 } 375 376 String result = null; 377 for (int i = 0; i < sa.length; i++) { 378 String s = sa[i]; 379 if (log.isTraceEnabled()) { 380 log.trace("getParameterValue Checking [" + s + "]"); 381 } 382 if (s != null && s.trim().length() > 0) { 383 if (log.isTraceEnabled()) { 384 log.trace("getParameterValue result:" + s); 385 } 386 result = s; 387 } 388 } 389 if (log.isTraceEnabled()) { 390 log.trace("Length [" + sa.length + "] Result [" + result + "]"); 391 } 392 return result; 393 } 394 395 396 } 397 | Popular Tags |