001 //
002 // Copyright 2010 GOT5 (Gang Of Tapestry 5)
003 //
004 // Licensed under the Apache License, Version 2.0 (the "License");
005 // you may not use this file except in compliance with the License.
006 // You may obtain a copy of the License at
007 //
008 // http://www.apache.org/licenses/LICENSE-2.0
009 //
010 // Unless required by applicable law or agreed to in writing, software
011 // distributed under the License is distributed on an "AS IS" BASIS,
012 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 // See the License for the specific language governing permissions and
014 // limitations under the License.
015 //
016
017 package org.apache.tapestry5.beanvalidator;
018
019 import org.apache.tapestry5.annotations.SetupRender;
020 import org.apache.tapestry5.corelib.components.Form;
021 import org.apache.tapestry5.corelib.components.FormFragment;
022 import org.apache.tapestry5.model.MutableComponentModel;
023 import org.apache.tapestry5.plastic.MethodAdvice;
024 import org.apache.tapestry5.plastic.MethodInvocation;
025 import org.apache.tapestry5.plastic.PlasticClass;
026 import org.apache.tapestry5.plastic.PlasticMethod;
027 import org.apache.tapestry5.services.TransformConstants;
028 import org.apache.tapestry5.services.javascript.JavaScriptSupport;
029 import org.apache.tapestry5.services.transform.ComponentClassTransformWorker2;
030 import org.apache.tapestry5.services.transform.TransformationSupport;
031
032 /**
033 * Respectfully borrowed from the tapestry5-jquery project
034 * https://github.com/got5/tapestry5-jquery/blob/master/src/main/java/org/got5/tapestry5/jquery/services/FormResourcesInclusionWorker.java
035 */
036 public class FormResourcesInclusionWorker implements ComponentClassTransformWorker2
037 {
038 private final JavaScriptSupport javaScriptSupport;
039
040 public FormResourcesInclusionWorker(JavaScriptSupport javaScriptSupport)
041 {
042 this.javaScriptSupport = javaScriptSupport;
043 }
044
045 public void transform(PlasticClass plasticClass, TransformationSupport support, MutableComponentModel model)
046 {
047 if (model.getComponentClassName().equals(Form.class.getName()) ||
048 model.getComponentClassName().equals(FormFragment.class.getName()))
049 {
050 PlasticMethod setupRender = plasticClass.introduceMethod(TransformConstants.SETUP_RENDER_DESCRIPTION);
051
052 setupRender.addAdvice(new MethodAdvice()
053 {
054 public void advise(MethodInvocation invocation)
055 {
056 javaScriptSupport.importStack(BeanValidatorStack.STACK_ID);
057
058 invocation.proceed();
059 }
060 });
061
062 model.addRenderPhase(SetupRender.class);
063 }
064 }
065 }