001: /*
002: * Version: MPL 1.1/GPL 2.0/LGPL 2.1
003: *
004: * "The contents of this file are subject to the Mozilla Public License
005: * Version 1.1 (the "License"); you may not use this file except in
006: * compliance with the License. You may obtain a copy of the License at
007: * http://www.mozilla.org/MPL/
008: *
009: * Software distributed under the License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
011: * License for the specific language governing rights and limitations under
012: * the License.
013: *
014: * The Original Code is ICEfaces 1.5 open source software code, released
015: * November 5, 2006. The Initial Developer of the Original Code is ICEsoft
016: * Technologies Canada, Corp. Portions created by ICEsoft are Copyright (C)
017: * 2004-2006 ICEsoft Technologies Canada, Corp. All Rights Reserved.
018: *
019: * Contributor(s): _____________________.
020: *
021: * Alternatively, the contents of this file may be used under the terms of
022: * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"
023: * License), in which case the provisions of the LGPL License are
024: * applicable instead of those above. If you wish to allow use of your
025: * version of this file only under the terms of the LGPL License and not to
026: * allow others to use your version of this file under the MPL, indicate
027: * your decision by deleting the provisions above and replace them with
028: * the notice and other provisions required by the LGPL License. If you do
029: * not delete the provisions above, a recipient may use your version of
030: * this file under either the MPL or the LGPL License."
031: *
032: */
033:
034: package com.icesoft.faces.component.ext.renderkit;
035:
036: import com.icesoft.faces.component.dragdrop.DndEvent;
037: import com.icesoft.faces.component.dragdrop.DragEvent;
038: import com.icesoft.faces.component.dragdrop.DropEvent;
039: import com.icesoft.faces.component.ext.HtmlPanelGroup;
040: import com.icesoft.faces.context.DOMContext;
041: import com.icesoft.faces.context.effects.CurrentStyle;
042: import com.icesoft.faces.context.effects.DragDrop;
043: import com.icesoft.faces.context.effects.JavascriptContext;
044: import com.icesoft.faces.context.effects.LocalEffectEncoder;
045: import com.icesoft.faces.renderkit.dom_html_basic.HTML;
046: import com.icesoft.faces.utils.DnDCache;
047: import org.apache.commons.logging.Log;
048: import org.apache.commons.logging.LogFactory;
049: import org.w3c.dom.Element;
050:
051: import javax.faces.component.NamingContainer;
052: import javax.faces.component.UIComponent;
053: import javax.faces.component.UIViewRoot;
054: import javax.faces.context.FacesContext;
055: import javax.faces.el.MethodBinding;
056: import java.io.IOException;
057: import java.util.Map;
058:
059: public class GroupRenderer extends
060: com.icesoft.faces.renderkit.dom_html_basic.GroupRenderer {
061:
062: protected static final String STATUS = "status";
063:
064: protected static final String DROP = "dropID";
065: private static Log log = LogFactory.getLog(GroupRenderer.class);
066:
067: public void encodeBegin(FacesContext facesContext,
068: UIComponent uiComponent) throws IOException {
069: try {
070:
071: String viewID = facesContext.getViewRoot().getViewId();
072:
073: String style = ((HtmlPanelGroup) uiComponent).getStyle();
074: String styleClass = ((HtmlPanelGroup) uiComponent)
075: .getStyleClass();
076: String blockingFlag = (String) facesContext
077: .getExternalContext().getRequestMap().get(
078: "BlockingServlet");
079:
080: String dndType = getDndType(uiComponent);
081: DOMContext domContext = DOMContext.attachDOMContext(
082: facesContext, uiComponent);
083:
084: if (!domContext.isInitialized()) {
085: Element rootSpan = domContext
086: .createElement(HTML.DIV_ELEM);
087: domContext.setRootNode(rootSpan);
088: setRootElementId(facesContext, rootSpan, uiComponent);
089:
090: if (dndType != null) {
091: // Drag an drop needs some hidden fields
092: Element statusField = createHiddenField(domContext,
093: facesContext, uiComponent, STATUS);
094: rootSpan.appendChild(statusField);
095:
096: Element targetID = createHiddenField(domContext,
097: facesContext, uiComponent, DROP);
098: rootSpan.appendChild(targetID);
099: }
100:
101: }
102:
103: if (dndType != null) {
104: DnDCache.getInstance(facesContext, true).put(
105: uiComponent.getClientId(facesContext),
106: (HtmlPanelGroup) uiComponent, facesContext);
107: addJavascriptCalls(uiComponent, dndType, null,
108: facesContext);
109: }
110:
111: Element rootSpan = (Element) domContext.getRootNode();
112: if (styleClass != null) {
113: rootSpan.setAttribute("class", styleClass);
114: }
115: JavascriptContext.fireEffect(uiComponent, facesContext);
116:
117: LocalEffectEncoder.encodeLocalEffects(uiComponent,
118: rootSpan, facesContext);
119: String extraStyle = null;
120: String scrollWidth = (String) uiComponent.getAttributes()
121: .get("scrollWidth");
122: String scrollHeight = (String) uiComponent.getAttributes()
123: .get("scrollHeight");
124:
125: if (scrollHeight != null || scrollWidth != null) {
126: if (extraStyle == null) {
127: extraStyle = "";
128: }
129: if (scrollHeight == null) {
130: extraStyle += "width:" + scrollWidth
131: + ";overflow:auto;";
132: } else if (scrollWidth == null) {
133: extraStyle += "height:" + scrollHeight
134: + ";overflow:auto;";
135: } else {
136: extraStyle += "width:" + scrollWidth + ";height:"
137: + scrollHeight + ";overflow:auto;";
138: }
139: }
140:
141: CurrentStyle.apply(facesContext, uiComponent, null,
142: extraStyle);
143: domContext.streamWrite(facesContext, uiComponent,
144: domContext.getRootNode(), rootSpan);
145: domContext.stepInto(uiComponent);
146: // domContext.stepOver();
147: } catch (Exception e) {
148: e.printStackTrace();
149: }
150: }
151:
152: protected String addJavascriptCalls(UIComponent uiComponent,
153: String dndType, String handleId, FacesContext facesContext) {
154: String calls = "";
155:
156: boolean dragListener = uiComponent.getAttributes().get(
157: "dragListener") != null;
158: boolean dropListener = uiComponent.getAttributes().get(
159: "dropListener") != null;
160: String dragMask = DndEvent.parseMask((String) uiComponent
161: .getAttributes().get("dragMask"));
162: String dropMask = DndEvent.parseMask((String) uiComponent
163: .getAttributes().get("dropMask"));
164: String dragOptions = (String) uiComponent.getAttributes().get(
165: "dragOptions");
166: String hoverClass = (String) uiComponent.getAttributes().get(
167: "hoverclass");
168: if (!dragListener) {
169: if (dragMask == null) {
170: dragMask = DndEvent.MASK_ALL_BUT_DROPS;
171: }
172: }
173: if (!dropListener) {
174: dropMask = DndEvent.MASK_ALL;
175: }
176:
177: if ("DRAG".equalsIgnoreCase(dndType)) {
178:
179: calls += DragDrop.addDragable(uiComponent
180: .getClientId(facesContext), handleId, dragOptions,
181: dragMask, facesContext);
182:
183: } else if ("drop".equalsIgnoreCase(dndType)) {
184: calls += DragDrop.addDroptarget(uiComponent
185: .getClientId(facesContext), null, facesContext,
186: dropMask, hoverClass);
187: } else if ("dragdrop".equalsIgnoreCase(dndType)) {
188:
189: calls += DragDrop.addDragable(uiComponent
190: .getClientId(facesContext), handleId, dragOptions,
191: dragMask, facesContext);
192: calls += DragDrop.addDroptarget(uiComponent
193: .getClientId(facesContext), null, facesContext,
194: dropMask, hoverClass);
195: } else {
196: throw new IllegalArgumentException(
197: "Value ["
198: + dndType
199: + "] is not valid for dndType. Please use drag or drop");
200: }
201: return calls;
202: }
203:
204: public void encodeEnd(FacesContext facesContext,
205: UIComponent uiComponent) throws IOException {
206: validateParameters(facesContext, uiComponent, null);
207: DOMContext domContext = DOMContext.getDOMContext(facesContext,
208: uiComponent);
209: domContext.stepOver();
210:
211: }
212:
213: protected String appendStyle(String currentStyle,
214: String additionalStyle) {
215: String result = "";
216: if (!isBlank(currentStyle)) {
217: result = currentStyle;
218: }
219: if (!isBlank(additionalStyle)) {
220: result += additionalStyle;
221: }
222: if (isBlank(result)) {
223: return null;
224: }
225: return result;
226: }
227:
228: private static boolean isBlank(String s) {
229: return !(s != null && s.trim().length() > 0);
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
243: || panel.getDropTarget() != null) {
244:
245: Map requestMap = context.getExternalContext()
246: .getRequestParameterValuesMap();
247: String fieldName = getHiddenFieldName(context,
248: component, STATUS);
249: String status = getParamamterValue((String[]) requestMap
250: .get(fieldName));
251: if (status == null) {
252: if (log.isTraceEnabled()) {
253: log.trace("Drag Drop Status for ID ["
254: + panel.getClientId(context)
255: + "] Field Name [" + fieldName
256: + "] is null. Returning");
257: }
258: return;
259: }
260: String targetID = getParamamterValue((String[]) requestMap
261: .get(getHiddenFieldName(context, component,
262: DROP)));
263:
264: Object targetDragValue = null;
265: Object targetDropValue = null;
266:
267: if (targetID != null && targetID.length() > 0) {
268: DnDCache dndCache = DnDCache.getInstance(context,
269: false);
270: targetDragValue = dndCache.getDragValue(targetID);
271: targetDropValue = dndCache.getDropValue(targetID);
272:
273: }
274:
275: if (log.isTraceEnabled()) {
276: log.trace("Dnd Event Client ID ["
277: + component.getClientId(context)
278: + "] Target ID [" + targetID + "] Status ["
279: + status + "]");
280: }
281:
282: if (panel.getDragListener() == null
283: && panel.getDropListener() == null) {
284:
285: return;
286: }
287: int type = 0;
288: try {
289: type = Integer.parseInt(status);
290: } catch (NumberFormatException e) {
291: if (status != null || status.length() != 0)
292:
293: {
294: return;
295: }
296: }
297:
298: MethodBinding listener = panel.getDragListener();
299: if (listener != null) {
300:
301: DragEvent event = new DragEvent(component, type,
302: targetID, targetDragValue, targetDropValue);
303: panel.queueEvent(event);
304: }
305: listener = panel.getDropListener();
306: if (listener != null) {
307:
308: DropEvent event = new DropEvent(component, type,
309: targetID, targetDragValue, targetDropValue);
310: panel.queueEvent(event);
311: }
312: }
313: }
314: }
315:
316: protected Element createHiddenField(DOMContext domContext,
317: FacesContext facesContext, UIComponent uiComponent,
318: String name) {
319: Element ele = domContext.createElement(HTML.INPUT_ELEM);
320: ele.setAttribute(HTML.TYPE_ATTR, "hidden");
321: String n = getHiddenFieldName(facesContext, uiComponent, name);
322: ele.setAttribute(HTML.NAME_ATTR, n);
323: ele.setAttribute(HTML.ID_ATTR, n);
324: ele.setAttribute(HTML.VALUE_ATTR, "");
325: return ele;
326: }
327:
328: protected String getHiddenFieldName(FacesContext facesContext,
329: UIComponent uiComponent, String name) {
330: UIComponent form = findForm(uiComponent);
331: String formId = form.getClientId(facesContext);
332: String clientId = uiComponent.getClientId(facesContext);
333: return formId + NamingContainer.SEPARATOR_CHAR
334: + UIViewRoot.UNIQUE_ID_PREFIX + clientId + name;
335: }
336:
337: protected String getDndType(UIComponent uiComponent) {
338: String dndType = null;
339: String draggable = (String) uiComponent.getAttributes().get(
340: "draggable");
341: String droppable = (String) uiComponent.getAttributes().get(
342: "dropTarget");
343: if ("true".equalsIgnoreCase(draggable)
344: && "true".equalsIgnoreCase(droppable)) {
345: dndType = "dragdrop";
346: } else if ("true".equalsIgnoreCase(draggable)) {
347: dndType = "DRAG";
348: } else if ("true".equalsIgnoreCase(droppable)) {
349: dndType = "drop";
350: }
351: return dndType;
352: }
353:
354: /**
355: * Safri can return mutile values. The first one blank. This was a BIG
356: * problem to solve!
357: *
358: * @param sa
359: * @return
360: */
361: private String getParamamterValue(String[] sa) {
362:
363: // bail if the sa array is null
364: if (sa == null) {
365: if (log.isTraceEnabled()) {
366: log.trace("Null parameter value");
367: }
368: return null;
369: }
370:
371: String result = null;
372: for (int i = 0; i < sa.length; i++) {
373: String s = sa[i];
374: if (log.isTraceEnabled()) {
375: log.trace("getParameterValue Checking [" + s + "]");
376: }
377: if (s != null && s.trim().length() > 0) {
378: if (log.isTraceEnabled()) {
379: log.trace("getParameterValue result:" + s);
380: }
381: result = s;
382: }
383: }
384: if (log.isTraceEnabled()) {
385: log.trace("Length [" + sa.length + "] Result [" + result
386: + "]");
387: }
388: return result;
389: }
390:
391: }
|