KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icesoft > faces > component > ext > renderkit > GroupRenderer


1 /*
2  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3  *
4  * "The contents of this file are subject to the Mozilla Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License at
7  * http://www.mozilla.org/MPL/
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
11  * License for the specific language governing rights and limitations under
12  * the License.
13  *
14  * The Original Code is ICEfaces 1.5 open source software code, released
15  * November 5, 2006. The Initial Developer of the Original Code is ICEsoft
16  * Technologies Canada, Corp. Portions created by ICEsoft are Copyright (C)
17  * 2004-2006 ICEsoft Technologies Canada, Corp. All Rights Reserved.
18  *
19  * Contributor(s): _____________________.
20  *
21  * Alternatively, the contents of this file may be used under the terms of
22  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"
23  * License), in which case the provisions of the LGPL License are
24  * applicable instead of those above. If you wish to allow use of your
25  * version of this file only under the terms of the LGPL License and not to
26  * allow others to use your version of this file under the MPL, indicate
27  * your decision by deleting the provisions above and replace them with
28  * the notice and other provisions required by the LGPL License. If you do
29  * not delete the provisions above, a recipient may use your version of
30  * this file under either the MPL or the LGPL License."
31  *
32  */

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 JavaDoc;
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 JavaDoc;
57 import java.util.Map JavaDoc;
58
59 public class GroupRenderer
60         extends com.icesoft.faces.renderkit.dom_html_basic.GroupRenderer {
61
62     protected static final String JavaDoc STATUS = "status";
63
64     protected static final String JavaDoc DROP = "dropID";
65     private static Log log = LogFactory.getLog(GroupRenderer.class);
66
67
68     public void encodeBegin(FacesContext facesContext, UIComponent uiComponent)
69             throws IOException JavaDoc {
70         try {
71
72             String JavaDoc viewID = facesContext.getViewRoot().getViewId();
73
74             String JavaDoc style = ((HtmlPanelGroup) uiComponent).getStyle();
75             String JavaDoc styleClass = ((HtmlPanelGroup) uiComponent).getStyleClass();
76             String JavaDoc blockingFlag = (String JavaDoc) facesContext.getExternalContext()
77                     .getRequestMap().get("BlockingServlet");
78
79             String JavaDoc dndType = getDndType(uiComponent);
80             DOMContext domContext =
81                     DOMContext.attachDOMContext(facesContext, uiComponent);
82
83             if (!domContext.isInitialized()) {
84                 Element JavaDoc rootSpan = domContext.createElement(HTML.DIV_ELEM);
85                 domContext.setRootNode(rootSpan);
86                 setRootElementId(facesContext, rootSpan, uiComponent);
87
88                 if (dndType != null) {
89                     // Drag an drop needs some hidden fields
90
Element JavaDoc statusField = createHiddenField(domContext,
91                                                             facesContext,
92                                                             uiComponent,
93                                                             STATUS);
94                     rootSpan.appendChild(statusField);
95
96                     Element JavaDoc 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 JavaDoc rootSpan = (Element JavaDoc) 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 JavaDoc extraStyle = null;
121             String JavaDoc scrollWidth =
122                     (String JavaDoc) uiComponent.getAttributes().get("scrollWidth");
123             String JavaDoc scrollHeight =
124                     (String JavaDoc) 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             // domContext.stepOver();
147
} catch (Exception JavaDoc e) {
148             e.printStackTrace();
149         }
150     }
151
152     protected String JavaDoc addJavascriptCalls(UIComponent uiComponent, String JavaDoc dndType,
153                                         String JavaDoc handleId,
154                                         FacesContext facesContext) {
155         String JavaDoc calls = "";
156
157         boolean dragListener =
158                 uiComponent.getAttributes().get("dragListener") != null;
159         boolean dropListener =
160                 uiComponent.getAttributes().get("dropListener") != null;
161         String JavaDoc dragMask = DndEvent.parseMask(
162                 (String JavaDoc) uiComponent.getAttributes().get("dragMask"));
163         String JavaDoc dropMask = DndEvent.parseMask(
164                 (String JavaDoc) uiComponent.getAttributes().get("dropMask"));
165         String JavaDoc dragOptions =
166                 (String JavaDoc) uiComponent.getAttributes().get("dragOptions");
167         String JavaDoc hoverClass =
168                 (String JavaDoc) 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 JavaDoc("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 JavaDoc {
206         validateParameters(facesContext, uiComponent, null);
207         DOMContext domContext =
208                 DOMContext.getDOMContext(facesContext, uiComponent);
209         domContext.stepOver();
210
211     }
212
213     protected String JavaDoc appendStyle(String JavaDoc currentStyle, String JavaDoc additionalStyle) {
214         String JavaDoc 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 JavaDoc 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 JavaDoc dndType = getDndType(component);
241
242             if (panel.getDraggable() != null || panel.getDropTarget() != null) {
243
244                 Map JavaDoc requestMap = context.getExternalContext()
245                         .getRequestParameterValuesMap();
246                 String JavaDoc fieldName =
247                         getHiddenFieldName(context, component, STATUS);
248                 String JavaDoc status = getParamamterValue(
249                         (String JavaDoc[]) 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 JavaDoc targetID = getParamamterValue((String JavaDoc[]) requestMap
260                         .get(getHiddenFieldName(context, component, DROP)));
261
262                 Object JavaDoc targetDragValue = null;
263                 Object JavaDoc 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 JavaDoc 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 JavaDoc createHiddenField(DOMContext domContext,
318                                         FacesContext facesContext,
319                                         UIComponent uiComponent, String JavaDoc name) {
320         Element JavaDoc ele = domContext.createElement(HTML.INPUT_ELEM);
321         ele.setAttribute(HTML.TYPE_ATTR, "hidden");
322         String JavaDoc 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 JavaDoc getHiddenFieldName(FacesContext facesContext,
331                                         UIComponent uiComponent, String JavaDoc name) {
332         UIComponent form = findForm(uiComponent);
333         String JavaDoc formId = form.getClientId(facesContext);
334         String JavaDoc 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 JavaDoc getDndType(UIComponent uiComponent) {
343         String JavaDoc dndType = null;
344         String JavaDoc draggable =
345                 (String JavaDoc) uiComponent.getAttributes().get("draggable");
346         String JavaDoc droppable =
347                 (String JavaDoc) 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     /**
360      * Safri can return mutile values. The first one blank. This was a BIG
361      * problem to solve!
362      *
363      * @param sa
364      * @return
365      */

366     private String JavaDoc getParamamterValue(String JavaDoc[] sa) {
367
368         // bail if the sa array is null
369
if (sa == null) {
370             if (log.isTraceEnabled()) {
371                 log.trace("Null parameter value");
372             }
373             return null;
374         }
375
376         String JavaDoc result = null;
377         for (int i = 0; i < sa.length; i++) {
378             String JavaDoc 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