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.context.effects;
035:
036: import org.apache.commons.logging.Log;
037: import org.apache.commons.logging.LogFactory;
038: import org.w3c.dom.Element;
039:
040: import javax.faces.component.UIComponent;
041: import javax.faces.context.FacesContext;
042: import java.util.Map;
043:
044: /**
045: * Encode an effect call to an javascript event
046: */
047: public class LocalEffectEncoder {
048:
049: private static final String[] EVENTS = { "click", "dblclick",
050: "mousedown", "mouseup", "mousemove", "mouseover",
051: "mouseout", "change", "reset", "submit", "keypress",
052: "keydown", "keyup" };
053:
054: private static String[] ATTRIBUTES = new String[EVENTS.length];
055: private static final String ATTRIBUTE_PREFIX = "on";
056: private static String[] EFFECTS = new String[EVENTS.length];
057: private static final String EFFECT_SUFFIX = "effect";
058:
059: static {
060: for (int index = 0; index < EVENTS.length; index++) {
061: ATTRIBUTES[index] = ATTRIBUTE_PREFIX + EVENTS[index];
062: EFFECTS[index] = ATTRIBUTES[index] + EFFECT_SUFFIX;
063: }
064: }
065:
066: private static final Log log = LogFactory
067: .getLog(LocalEffectEncoder.class);
068:
069: public static void encodeLocalEffects(UIComponent comp,
070: Element rootNode, FacesContext facesContext) {
071: Map atts = comp.getAttributes();
072: try {
073: for (int i = 0; i < EVENTS.length; i++) {
074: Effect fx = (Effect) atts.get(EFFECTS[i]);
075: if (fx == null) {
076: // in some cases the value binding can be null on the initial render
077: // but contain an effect later. This makes a place holder for that effect
078: // once it arrives.
079: if (comp.getValueBinding(EFFECTS[i]) != null) {
080: fx = new BlankEffect();
081: }
082: }
083:
084: if (fx != null) {
085: String value = JavascriptContext.applyEffect(fx,
086: comp.getClientId(facesContext),
087: facesContext);
088:
089: String original = (String) atts.get(ATTRIBUTES[i]);
090: if (original == null) {
091: original = "";
092: }
093:
094: rootNode.setAttribute(ATTRIBUTES[i], value
095: + original);
096: }
097: }
098: } catch (Exception e) {
099:
100: if (log.isErrorEnabled()) {
101: log.error(e.getMessage(), e);
102: }
103: }
104: }
105:
106: public static void encodeLocalEffect(String id, Effect fx,
107: String event, FacesContext facesContext) {
108: String value = JavascriptContext.applyEffect(fx, id,
109: facesContext);
110: //TODO: refactor so that is dosen't clobber an existing effect
111: String js = "$('" + id + "').on" + event + "=function(){"
112: + value + "};";
113: JavascriptContext.addJavascriptCall(facesContext, js);
114: }
115: }
|