Home » tapestry-src-5.0.19 » org.apache.tapestry5.corelib.components » [javadoc | source]

    1   // Copyright 2008 The Apache Software Foundation
    2   //
    3   // Licensed under the Apache License, Version 2.0 (the "License");
    4   // you may not use this file except in compliance with the License.
    5   // You may obtain a copy of the License at
    6   //
    7   //     http://www.apache.org/licenses/LICENSE-2.0
    8   //
    9   // Unless required by applicable law or agreed to in writing, software
   10   // distributed under the License is distributed on an "AS IS" BASIS,
   11   // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   12   // See the License for the specific language governing permissions and
   13   // limitations under the License.
   14   
   15   package org.apache.tapestry5.corelib.components;
   16   
   17   import org.apache.tapestry5;
   18   import org.apache.tapestry5.annotations.Parameter;
   19   import org.apache.tapestry5.annotations.SupportsInformalParameters;
   20   import org.apache.tapestry5.dom.Element;
   21   import org.apache.tapestry5.ioc.annotations.Inject;
   22   
   23   /**
   24    * Renders an arbitrary element including informal parameters.
   25    */
   26   @SupportsInformalParameters
   27   public class Any implements ClientElement
   28   {
   29       @Parameter(defaultPrefix = BindingConstants.LITERAL)
   30       private String element;
   31   
   32       /**
   33        * The desired client id, which defaults to the components id.
   34        */
   35       @Parameter(value = "prop:componentResources.id", defaultPrefix = BindingConstants.LITERAL)
   36       private String clientId;
   37   
   38       private Element anyElement;
   39   
   40       private String uniqueId;
   41   
   42       @Inject
   43       private ComponentResources resources;
   44   
   45       @Inject
   46       private RenderSupport renderSupport;
   47   
   48       String defaultElement()
   49       {
   50           return resources.getElementName("div");
   51       }
   52   
   53       void beginRender(MarkupWriter writer)
   54       {
   55           anyElement = writer.element(element);
   56   
   57           uniqueId = null;
   58   
   59           resources.renderInformalParameters(writer);
   60       }
   61   
   62       /**
   63        * Returns the client id.  This has side effects: this first time this is called (after the Any component renders
   64        * its start tag), a unique id is allocated (based on, and typically the same as, the clientId parameter, which
   65        * defaults to the component's id). The rendered element is updated, with its id attribute set to the unique client
   66        * id, which is then returned.
   67        *
   68        * @return unique client id for this component
   69        */
   70       public String getClientId()
   71       {
   72           if (uniqueId == null)
   73           {
   74               uniqueId = renderSupport.allocateClientId(clientId);
   75               anyElement.forceAttributes("id", uniqueId);
   76           }
   77   
   78           return uniqueId;
   79       }
   80   
   81       void afterRender(MarkupWriter writer)
   82       {
   83           writer.end(); // the element
   84       }
   85   
   86       void inject(RenderSupport support, ComponentResources resources, String element, String clientId)
   87       {
   88           this.renderSupport = support;
   89           this.resources = resources;
   90           this.element = element;
   91           this.clientId = clientId;
   92       }
   93   }

Home » tapestry-src-5.0.19 » org.apache.tapestry5.corelib.components » [javadoc | source]