Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  // Copyright 2004, 2005 The Apache Software Foundation
  //
  // Licensed under the Apache License, Version 2.0 (the "License");
  // you may not use this file except in compliance with the License.
  // You may obtain a copy of the License at
  //
  //     http://www.apache.org/licenses/LICENSE-2.0
  //
  // Unless required by applicable law or agreed to in writing, software
 // distributed under the License is distributed on an "AS IS" BASIS,
 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
 package org.apache.tapestry.link;
 
 import  org.apache.hivemind.ApplicationRuntimeException;
 import  org.apache.hivemind.HiveMind;
 
 import java.util.List;
 import java.util.Map;

Default implementation of org.apache.tapestry.link.ILinkRenderer, which does nothing special. Can be used as a base class to provide additional handling.

Since:
3.0
 
 
 public class DefaultLinkRenderer implements ILinkRenderer
 {

    
A shared instance used as a default for any link that doesn't explicitly override.
 
 
     public static final ILinkRenderer SHARED_INSTANCE = new DefaultLinkRenderer();
 
     public void renderLink(IMarkupWriter writerIRequestCycle cycleILinkComponent linkComponent)
     {
         IMarkupWriter wrappedWriter = null;
         
         if (cycle.getAttribute(.) != null)
             throw new ApplicationRuntimeException(LinkMessages.noNesting(), linkComponentnullnull);
         
         cycle.setAttribute(.linkComponent);
         
         boolean hasBody = getHasBody();
 
         boolean disabled = linkComponent.isDisabled() || cycle.isRewinding();
 
         if (!disabled)
         {
             if (hasBody)
                 writer.begin(getElement());
             else 
                 writer.beginEmpty(getElement());
             
             linkComponent.renderAdditionalAttributes(writercycle);
             
             writer.attribute(getUrlAttribute(), constructURL(linkComponentcycle));
             
             String target = linkComponent.getTarget();
             
             if (HiveMind.isNonBlank(target))
                 writer.attribute(getTargetAttribute(), target);
             
             if (DirectLink.class.isInstance(linkComponent)) {
                 DirectLink direct = (DirectLink)linkComponent;
                 
                 renderAsyncParams(writercycledirect);
             }
             
             beforeBodyRender(writercyclelinkComponent);
             
             // Allow the wrapped components a chance to render.
             // Along the way, they may interact with this component
             // and cause the name variable to get set.
             
             wrappedWriter = writer.getNestedWriter();
         } else 
             wrappedWriter = writer;
         
         if (hasBody
             linkComponent.renderBody(wrappedWritercycle);
         
         if (!disabled) {
             
             afterBodyRender(writercyclelinkComponent);
                         
             if (hasBody) {
                 wrappedWriter.close();
                
                // Close the <element> tag
                
                writer.end();
            } else 
                writer.closeTag();
        }
        
    }

    
Converts the EngineServiceLink into a URI or URL. This implementation gets the scheme and anchor from the component (both of which may be null), and invokes ILink.getURL(String, String, int, String, boolean).
    protected String constructURL(ILinkComponent componentIRequestCycle cycle)
    {
        ILink link = component.getLink(cycle);
        
        String scheme = component.getScheme();
        Integer port = component.getPort();
        int portI = (port == null) ? 0 : port.intValue();
        String anchor = component.getAnchor();
        
        return link.getURL(schemenullportIanchortrue);
    }

    
Invoked after the href attribute has been written but before the body of the link is rendered (but only if the link is not disabled).

This implementation does nothing.

Parameters:
writer Markup writer.
cycle Current request cycle.
link The link component being rendered.
    protected void beforeBodyRender(IMarkupWriter writerIRequestCycle cycleILinkComponent link)
    {
    }

    
Invoked after the body of the link is rendered, but before ILinkComponent.renderAdditionalAttributes(IMarkupWriter, IRequestCycle)is invoked (but only if the link is not disabled).

This implementation does nothing.

Parameters:
writer Markup writer.
cycle Current request cycle.
link The link component being rendered.
    protected void afterBodyRender(IMarkupWriter writerIRequestCycle cycleILinkComponent link)
    {
    }

    
For DirectLink components only, manages writing out event handlers for link if any of the dynamic (async/json/etc) parameters are set on the component.

Will try to write the logic into the onClick attribute of the link if not bound, otherwise it will render it using the DirectLink.getScript() script.

Parameters:
writer The writer to render attributes into.
cycle The current request cycle.
link The component link being rendered for.
    protected void renderAsyncParams(IMarkupWriter writerIRequestCycle cycleDirectLink link)
    {
        List comps = link.getUpdateComponents();
        
        if (!link.isAsync() && !link.isJson() 
                && (comps == null
                || comps.size() <= 0))
            return;
        
        if (!link.isParameterBound("onclick") && !link.isParameterBound("onClick")) {
            writer.attribute("onclick"
                    "return tapestry.linkOnClick(this.href,'" + link.getClientId() + "', " + link.isJson() + ")");
            return;
        }
        
        PageRenderSupport prs = TapestryUtils.getPageRenderSupport(cyclelink);
        
        if (prs == null)
            return;
        
        Map parms = new HashMap();
        
        parms.put("component"link);
        parms.put("json", Boolean.valueOf(link.isJson()));
        parms.put("key", ScriptUtils.functionHash("onclick" + link.hashCode()));
        
        // execute script template
        
        link.getScript().execute(linkcycleprsparms);
    }
    
    

Since:
3.0 *
    protected String getElement()
    {
        return "a";
    }
    protected String getUrlAttribute()
    {
        return "href";
    }
    protected String getTargetAttribute()
    {
        return "target";
    }
    protected boolean getHasBody()
    {
        return true;
    }
New to GrepCode? Check out our FAQ X