001    /*
002     * Apache License
003     * Version 2.0, January 2004
004     * http://www.apache.org/licenses/
005     *
006     * Copyright 2008-2010 by chenillekit.org
007     *
008     * Licensed under the Apache License, Version 2.0 (the "License");
009     * you may not use this file except in compliance with the License.
010     * You may obtain a copy of the License at
011     *
012     * http://www.apache.org/licenses/LICENSE-2.0
013     */
014    
015    package org.chenillekit.tapestry.core.base;
016    
017    import org.apache.tapestry5.MarkupWriter;
018    import org.apache.tapestry5.MarkupWriterListener;
019    import org.apache.tapestry5.annotations.Import;
020    import org.apache.tapestry5.corelib.base.AbstractField;
021    import org.apache.tapestry5.dom.Element;
022    
023    /**
024     * @version $Id: AbstractYuiField.java 668 2010-06-18 16:07:35Z homburgs $
025     */
026    @Import(library = {"${yahoo.yui}/yahoo-dom-event/yahoo-dom-event.js",
027                    "${yahoo.yui}/element/element${yahoo.yui.mode}.js"})
028    abstract public class AbstractYuiField extends AbstractField
029    {
030            private static final String YUI_CSS_CLASS = "yui-skin-sam";
031    
032            /**
033             * Tapestry render phase method.
034             * Called after component template is rendered.
035             *
036             * @param writer the markup writer
037             */
038            void afterRenderTemplate(final MarkupWriter writer)
039            {
040                    writer.addListener(new MarkupWriterListener()
041                    {
042                            public void elementDidStart(Element element)
043                            {
044                                    Element bodyElement = element.getDocument().find("html/body");
045                                    if (bodyElement == null)
046                                            return;
047    
048                                    String cssClassValue = bodyElement.getAttribute("class");
049                                    if (cssClassValue == null)
050                                            bodyElement.attribute("class", YUI_CSS_CLASS);
051                                    else
052                                    {
053                                            if (!cssClassValue.contains(YUI_CSS_CLASS))
054                                                    bodyElement.addClassName(YUI_CSS_CLASS);
055                                    }
056    
057                                    if (bodyElement.getAttribute("class") != null)
058                                            writer.removeListener(this);
059                            }
060    
061                            public void elementDidEnd(Element element)
062                            {
063                            }
064                    });
065            }
066    }