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.components;
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.annotations.Environmental;
022 import org.apache.tapestry5.annotations.Parameter;
023 import org.apache.tapestry5.annotations.SupportsInformalParameters;
024 import org.apache.tapestry5.ioc.annotations.Inject;
025 import org.apache.tapestry5.services.javascript.JavaScriptSupport;
026
027 /**
028 * Helper component that will render a variable element type.
029 * Similar to the Any component in Tapestry3.
030 *
031 * @version $Id: Element.java 674 2010-07-29 12:47:25Z homburgs $
032 * @deprecated use Any component from tapestry 5
033 */
034 @SupportsInformalParameters
035 public class Element implements ClientElement
036 {
037 @Inject
038 private ComponentResources resources;
039
040 @Environmental
041 private JavaScriptSupport javascriptSupport;
042
043 /**
044 * The id used to generate a page-unique client-side identifier for the component. If a component renders multiple
045 * times, a suffix will be appended to the to id to ensure uniqueness.
046 */
047 @Parameter(value = "prop:componentResources.id", defaultPrefix = BindingConstants.LITERAL)
048 private String clientId;
049
050 /**
051 * The element to render. If not null, then the component will render the indicated element around
052 * its body. The default is derived from the component template.
053 */
054 @Parameter(value = "prop:componentResources.elementName", defaultPrefix = "literal")
055 private String elementName;
056
057 private String assignedClientId;
058
059 void setupRender()
060 {
061 assignedClientId = javascriptSupport.allocateClientId(clientId);
062 }
063
064 void beginRender(MarkupWriter writer)
065 {
066 writer.element(elementName, "id", getClientId());
067 resources.renderInformalParameters(writer);
068 }
069
070 void afterRender(MarkupWriter writer)
071 {
072 writer.end();
073 }
074
075 /**
076 * Returns a unique id for the element. This value will be unique for any given rendering of a
077 * page. This value is intended for use as the id attribute of the client-side element, and will
078 * be used with any DHTML/Ajax related JavaScript.
079 */
080 public String getClientId()
081 {
082 return assignedClientId;
083 }
084 }