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.BindingConstants;
018 import org.apache.tapestry5.ClientElement;
019 import org.apache.tapestry5.ComponentResources;
020 import org.apache.tapestry5.MarkupWriter;
021 import org.apache.tapestry5.MarkupWriterListener;
022 import org.apache.tapestry5.annotations.AfterRenderTemplate;
023 import org.apache.tapestry5.annotations.Environmental;
024 import org.apache.tapestry5.annotations.Parameter;
025 import org.apache.tapestry5.dom.Element;
026 import org.apache.tapestry5.ioc.annotations.Inject;
027 import org.apache.tapestry5.services.javascript.JavaScriptSupport;
028
029 /**
030 * @version $Id: AbstractYahooComponent.java 675 2010-07-30 14:49:17Z homburgs $
031 */
032 //@Import(library = {"${yahoo.yui}/yahoo-dom-event/yahoo-dom-event.js",
033 // "${yahoo.yui}/element/element${yahoo.yui.mode}.js"})
034 abstract public class AbstractYahooComponent implements ClientElement
035 {
036 private static final String YUI_CSS_CLASS = "yui-skin-sam";
037
038 /**
039 * The id used to generate a page-unique client-side identifier for the component. If a
040 * component renders multiple times, a suffix will be appended to the to id to ensure
041 * uniqueness. The uniqued value may be accessed via the
042 * {@link #getClientId() clientId property}.
043 */
044 @Parameter(value = "prop:componentResources.id", defaultPrefix = BindingConstants.LITERAL)
045 private String clientId;
046
047 /**
048 * dis-/enable the button.
049 */
050 @Parameter(value = "false")
051 private boolean disabled;
052
053 /**
054 * RenderSupport to get unique client side id.
055 */
056 @Environmental
057 private JavaScriptSupport javascriptSupport;
058
059 /**
060 * For blocks, messages, crete actionlink, trigger event.
061 */
062 @Inject
063 private ComponentResources resources;
064
065
066 private String assignedClientId;
067
068 /**
069 * Tapestry render phase method.
070 * Initialize temporary instance variables here.
071 */
072 void setupRender()
073 {
074 assignedClientId = javascriptSupport.allocateClientId(clientId);
075 }
076
077 /**
078 * Tapestry render phase method.
079 * Called after component template is rendered.
080 *
081 * @param writer the markup writer
082 */
083 @AfterRenderTemplate
084 void afterRenderTemplate(final MarkupWriter writer)
085 {
086 writer.addListener(new MarkupWriterListener()
087 {
088 public void elementDidStart(Element element)
089 {
090 Element bodyElement = element.getDocument().find("html/body");
091 if (bodyElement == null)
092 return;
093
094 String cssClassValue = bodyElement.getAttribute("class");
095 if (cssClassValue == null)
096 bodyElement.attribute("class", YUI_CSS_CLASS);
097 else
098 {
099 if (!cssClassValue.contains(YUI_CSS_CLASS))
100 bodyElement.addClassName(YUI_CSS_CLASS);
101 }
102
103 if (bodyElement.getAttribute("class") != null)
104 writer.removeListener(this);
105 }
106
107 public void elementDidEnd(Element element)
108 {
109 }
110 });
111 }
112
113 public final String getClientId()
114 {
115 return assignedClientId;
116 }
117
118 public boolean isDisabled()
119 {
120 return disabled;
121 }
122 }