/src/main/java/org/primefaces/component/behavior/ajax/AjaxBehaviorHandler.java

http://primefaces.googlecode.com/ · Java · 218 lines · 175 code · 27 blank · 16 comment · 39 complexity · 5f0d7f5fdf4809b9c95c0b98a0029a66 MD5 · raw file

  1. /*
  2. * Copyright 2009-2012 Prime Teknoloji.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.primefaces.component.behavior.ajax;
  17. import java.beans.BeanDescriptor;
  18. import java.beans.BeanInfo;
  19. import java.io.IOException;
  20. import java.util.ArrayList;
  21. import java.util.Collection;
  22. import java.util.Collections;
  23. import java.util.List;
  24. import java.util.Map;
  25. import javax.el.MethodExpression;
  26. import javax.faces.application.Application;
  27. import javax.faces.component.UIComponent;
  28. import javax.faces.component.behavior.ClientBehaviorHolder;
  29. import javax.faces.context.FacesContext;
  30. import javax.faces.event.AjaxBehaviorEvent;
  31. import javax.faces.view.AttachedObjectHandler;
  32. import javax.faces.view.AttachedObjectTarget;
  33. import javax.faces.view.BehaviorHolderAttachedObjectHandler;
  34. import javax.faces.view.BehaviorHolderAttachedObjectTarget;
  35. import javax.faces.view.facelets.BehaviorConfig;
  36. import javax.faces.view.facelets.ComponentHandler;
  37. import javax.faces.view.facelets.FaceletContext;
  38. import javax.faces.view.facelets.TagAttribute;
  39. import javax.faces.view.facelets.TagException;
  40. import javax.faces.view.facelets.TagHandler;
  41. public class AjaxBehaviorHandler extends TagHandler implements BehaviorHolderAttachedObjectHandler {
  42. private final TagAttribute event;
  43. private final TagAttribute process;
  44. private final TagAttribute update;
  45. private final TagAttribute onstart;
  46. private final TagAttribute onerror;
  47. private final TagAttribute onsuccess;
  48. private final TagAttribute oncomplete;
  49. private final TagAttribute disabled;
  50. private final TagAttribute immediate;
  51. private final TagAttribute listener;
  52. private final TagAttribute global;
  53. private final TagAttribute async;
  54. private final TagAttribute partialSubmit;
  55. public AjaxBehaviorHandler(BehaviorConfig config) {
  56. super(config);
  57. this.event = this.getAttribute("event");
  58. this.process = this.getAttribute("process");
  59. this.update = this.getAttribute("update");
  60. this.onstart = this.getAttribute("onstart");
  61. this.onerror = this.getAttribute("onerror");
  62. this.onsuccess = this.getAttribute("onsuccess");
  63. this.oncomplete = this.getAttribute("oncomplete");
  64. this.disabled = this.getAttribute("disabled");
  65. this.immediate = this.getAttribute("immediate");
  66. this.listener = this.getAttribute("listener");
  67. this.global = this.getAttribute("global");
  68. this.async = this.getAttribute("async");
  69. this.partialSubmit = this.getAttribute("partialSubmit");
  70. }
  71. public void apply(FaceletContext ctx, UIComponent parent) throws IOException {
  72. if(!ComponentHandler.isNew(parent)) {
  73. return;
  74. }
  75. String eventName = getEventName();
  76. if(UIComponent.isCompositeComponent(parent)) {
  77. boolean tagApplied = false;
  78. if(parent instanceof ClientBehaviorHolder) {
  79. applyAttachedObject(ctx, parent, eventName);
  80. tagApplied = true;
  81. }
  82. BeanInfo componentBeanInfo = (BeanInfo) parent.getAttributes().get(UIComponent.BEANINFO_KEY);
  83. if(null == componentBeanInfo) {
  84. throw new TagException(tag, "Composite component does not have BeanInfo attribute");
  85. }
  86. BeanDescriptor componentDescriptor = componentBeanInfo.getBeanDescriptor();
  87. if(null == componentDescriptor) {
  88. throw new TagException(tag, "Composite component BeanInfo does not have BeanDescriptor");
  89. }
  90. List<AttachedObjectTarget> targetList = (List<AttachedObjectTarget>)componentDescriptor.getValue(AttachedObjectTarget.ATTACHED_OBJECT_TARGETS_KEY);
  91. if(null == targetList && !tagApplied) {
  92. throw new TagException(tag, "Composite component does not support behavior events");
  93. }
  94. boolean supportedEvent = false;
  95. for(AttachedObjectTarget target : targetList) {
  96. if(target instanceof BehaviorHolderAttachedObjectTarget) {
  97. BehaviorHolderAttachedObjectTarget behaviorTarget = (BehaviorHolderAttachedObjectTarget) target;
  98. if((null != eventName && eventName.equals(behaviorTarget.getName()))
  99. || (null == eventName && behaviorTarget.isDefaultEvent())) {
  100. supportedEvent = true;
  101. break;
  102. }
  103. }
  104. }
  105. if(supportedEvent) {
  106. getAttachedObjectHandlers(parent).add(this);
  107. }
  108. else {
  109. if(!tagApplied) {
  110. throw new TagException(tag, "Composite component does not support event " + eventName);
  111. }
  112. }
  113. }
  114. else if(parent instanceof ClientBehaviorHolder) {
  115. applyAttachedObject(ctx, parent, eventName);
  116. }
  117. else {
  118. throw new TagException(this.tag, "Unable to attach <p:ajax> to non-ClientBehaviorHolder parent");
  119. }
  120. }
  121. public String getEventName() {
  122. return (this.event != null) ? this.event.getValue() : null;
  123. }
  124. public void applyAttachedObject(FaceletContext context, UIComponent component, String eventName) {
  125. ClientBehaviorHolder holder = (ClientBehaviorHolder) component;
  126. if(null == eventName) {
  127. eventName = holder.getDefaultEventName();
  128. if (null == eventName) {
  129. throw new TagException(this.tag, "Event attribute could not be determined: " + eventName);
  130. }
  131. } else {
  132. Collection<String> eventNames = holder.getEventNames();
  133. if (!eventNames.contains(eventName)) {
  134. throw new TagException(this.tag, "Event:" + eventName + " is not supported.");
  135. }
  136. }
  137. AjaxBehavior ajaxBehavior = createAjaxBehavior(context, eventName);
  138. holder.addClientBehavior(eventName, ajaxBehavior);
  139. }
  140. // Construct our AjaxBehavior from tag parameters.
  141. private AjaxBehavior createAjaxBehavior(FaceletContext ctx, String eventName) {
  142. Application application = ctx.getFacesContext().getApplication();
  143. AjaxBehavior behavior = (AjaxBehavior)application.createBehavior(AjaxBehavior.BEHAVIOR_ID);
  144. setBehaviorAttribute(ctx, behavior, this.process, String.class);
  145. setBehaviorAttribute(ctx, behavior, this.update, String.class);
  146. setBehaviorAttribute(ctx, behavior, this.onstart, String.class);
  147. setBehaviorAttribute(ctx, behavior, this.onerror, String.class);
  148. setBehaviorAttribute(ctx, behavior, this.onsuccess, String.class);
  149. setBehaviorAttribute(ctx, behavior, this.oncomplete, String.class);
  150. setBehaviorAttribute(ctx, behavior, this.disabled, Boolean.class);
  151. setBehaviorAttribute(ctx, behavior, this.immediate, Boolean.class);
  152. setBehaviorAttribute(ctx, behavior, this.global, Boolean.class);
  153. setBehaviorAttribute(ctx, behavior, this.async, Boolean.class);
  154. setBehaviorAttribute(ctx, behavior, this.partialSubmit, Boolean.class);
  155. setBehaviorAttribute(ctx, behavior, this.listener, MethodExpression.class);
  156. if(listener != null) {
  157. behavior.addAjaxBehaviorListener(new AjaxBehaviorListenerImpl(
  158. this.listener.getMethodExpression(ctx, Object.class, new Class[] {}) ,
  159. this.listener.getMethodExpression(ctx, Object.class, new Class[] {AjaxBehaviorEvent.class})));
  160. }
  161. return behavior;
  162. }
  163. public String getFor() {
  164. return null;
  165. }
  166. public void applyAttachedObject(FacesContext context, UIComponent parent) {
  167. FaceletContext ctx = (FaceletContext) context.getAttributes().get(FaceletContext.FACELET_CONTEXT_KEY);
  168. applyAttachedObject(ctx, parent, getEventName());
  169. }
  170. private void setBehaviorAttribute(FaceletContext ctx, AjaxBehavior behavior, TagAttribute attr, Class type) {
  171. if(attr != null) {
  172. behavior.setValueExpression(attr.getLocalName(), attr.getValueExpression(ctx, type));
  173. }
  174. }
  175. public List<AttachedObjectHandler> getAttachedObjectHandlers(UIComponent component) {
  176. return getAttachedObjectHandlers(component, true);
  177. }
  178. public List<AttachedObjectHandler> getAttachedObjectHandlers(UIComponent component, boolean create) {
  179. Map<String, Object> attrs = component.getAttributes();
  180. List<AttachedObjectHandler> result = (List<AttachedObjectHandler>) attrs.get("javax.faces.RetargetableHandlers");
  181. if (result == null) {
  182. if (create) {
  183. result = new ArrayList<AttachedObjectHandler>();
  184. attrs.put("javax.faces.RetargetableHandlers", result);
  185. } else {
  186. result = Collections.EMPTY_LIST;
  187. }
  188. }
  189. return result;
  190. }
  191. }