You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tapestry.apache.org by pf...@apache.org on 2005/01/30 23:48:25 UTC

cvs commit: jakarta-tapestry/framework/src/java/org/apache/tapestry/link ButtonLinkRenderer.java

pferraro    2005/01/30 14:48:25

  Added:       framework/src/java/org/apache/tapestry/link
                        ButtonLinkRenderer.java
  Log:
  ILinkRenderer implementation that generates an html button.
  
  Revision  Changes    Path
  1.1                  jakarta-tapestry/framework/src/java/org/apache/tapestry/link/ButtonLinkRenderer.java
  
  Index: ButtonLinkRenderer.java
  ===================================================================
  // 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.tapestry.IMarkupWriter;
  import org.apache.tapestry.IRequestCycle;
  import org.apache.tapestry.Tapestry;
  import org.apache.tapestry.components.ILinkComponent;
  import org.apache.tapestry.engine.ILink;
  
  /**
   * An {@link ILinkRenderer} implementation that generates an HTML button.
   * This is particularly useful for implementing cancel buttons.
   * 
   * @author Paul Ferraro
   * @since 3.1
   */
  public class ButtonLinkRenderer implements ILinkRenderer
  {
      public static final ILinkRenderer SHARED_INSTANCE = new ButtonLinkRenderer();
  
      /**
       * @see org.apache.tapestry.link.ILinkRenderer#renderLink(org.apache.tapestry.IMarkupWriter,
       *      org.apache.tapestry.IRequestCycle, org.apache.tapestry.components.ILinkComponent)
       */
      public void renderLink(IMarkupWriter writer, IRequestCycle cycle, ILinkComponent component)
      {
          if (cycle.getAttribute(Tapestry.LINK_COMPONENT_ATTRIBUTE_NAME) != null)
          {
              String message = Tapestry.getMessage("AbstractLinkComponent.no-nesting");
              throw new ApplicationRuntimeException(message, component, null, null);
          }
  
          cycle.setAttribute(Tapestry.LINK_COMPONENT_ATTRIBUTE_NAME, component);
  
          ILink link = component.getLink(cycle);
  
          writer.begin("button");
          writer.attribute("type", "button");
  
          if (component.isDisabled())
          {
              writer.attribute("disabled", "disabled");
          }
  
          String url = link.getURL(component.getAnchor(), true);
          String target = component.getTarget();
          String onclick = (target == null) ? getScript(url) : getScript(url, target);
  
          writer.attribute("onclick", onclick);
  
          component.renderAdditionalAttributes(writer, cycle);
  
          IMarkupWriter wrappedWriter = writer.getNestedWriter();
  
          component.renderBody(wrappedWriter, cycle);
  
          wrappedWriter.close();
  
          writer.end();
  
          cycle.removeAttribute(Tapestry.LINK_COMPONENT_ATTRIBUTE_NAME);
      }
  
      /**
       * Generates the onclick event handler that opens the specified url in the current window.
       * @param url the url generated by this link
       * @return a JavaScript onclick event handler
       */
      protected String getScript(String url)
      {
          return "window.location='" + url + "'";
      }
  
      /**
       * Generates the onclick event handler that opens the specified url in the specified window or frame.
       * @param url the url generated by this link
       * @param target the name of the target window or frame
       * @return a JavaScript onclick event handler
       */
      protected String getScript(String url, String target)
      {
          return "window.open('" + url + "','" + target + "')";
      }
  }
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-dev-help@jakarta.apache.org


Re: cvs commit: jakarta-tapestry/framework/src/java/org/apache/tapestry/link ButtonLinkRenderer.java

Posted by Howard Lewis Ship <hl...@gmail.com>.
It would be nice if these new renderers could be collected inside a
HiveMind module, say tapestry.components, for easy reuse.


On 30 Jan 2005 22:48:25 -0000, pferraro@apache.org <pf...@apache.org> wrote:
> pferraro    2005/01/30 14:48:25
> 
>   Added:       framework/src/java/org/apache/tapestry/link
>                         ButtonLinkRenderer.java
>   Log:
>   ILinkRenderer implementation that generates an html button.
> 
>   Revision  Changes    Path
>   1.1                  jakarta-tapestry/framework/src/java/org/apache/tapestry/link/ButtonLinkRenderer.java
> 
>   Index: ButtonLinkRenderer.java
>   ===================================================================
>   // 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.tapestry.IMarkupWriter;
>   import org.apache.tapestry.IRequestCycle;
>   import org.apache.tapestry.Tapestry;
>   import org.apache.tapestry.components.ILinkComponent;
>   import org.apache.tapestry.engine.ILink;
> 
>   /**
>    * An {@link ILinkRenderer} implementation that generates an HTML button.
>    * This is particularly useful for implementing cancel buttons.
>    *
>    * @author Paul Ferraro
>    * @since 3.1
>    */
>   public class ButtonLinkRenderer implements ILinkRenderer
>   {
>       public static final ILinkRenderer SHARED_INSTANCE = new ButtonLinkRenderer();
> 
>       /**
>        * @see org.apache.tapestry.link.ILinkRenderer#renderLink(org.apache.tapestry.IMarkupWriter,
>        *      org.apache.tapestry.IRequestCycle, org.apache.tapestry.components.ILinkComponent)
>        */
>       public void renderLink(IMarkupWriter writer, IRequestCycle cycle, ILinkComponent component)
>       {
>           if (cycle.getAttribute(Tapestry.LINK_COMPONENT_ATTRIBUTE_NAME) != null)
>           {
>               String message = Tapestry.getMessage("AbstractLinkComponent.no-nesting");
>               throw new ApplicationRuntimeException(message, component, null, null);
>           }
> 
>           cycle.setAttribute(Tapestry.LINK_COMPONENT_ATTRIBUTE_NAME, component);
> 
>           ILink link = component.getLink(cycle);
> 
>           writer.begin("button");
>           writer.attribute("type", "button");
> 
>           if (component.isDisabled())
>           {
>               writer.attribute("disabled", "disabled");
>           }
> 
>           String url = link.getURL(component.getAnchor(), true);
>           String target = component.getTarget();
>           String onclick = (target == null) ? getScript(url) : getScript(url, target);
> 
>           writer.attribute("onclick", onclick);
> 
>           component.renderAdditionalAttributes(writer, cycle);
> 
>           IMarkupWriter wrappedWriter = writer.getNestedWriter();
> 
>           component.renderBody(wrappedWriter, cycle);
> 
>           wrappedWriter.close();
> 
>           writer.end();
> 
>           cycle.removeAttribute(Tapestry.LINK_COMPONENT_ATTRIBUTE_NAME);
>       }
> 
>       /**
>        * Generates the onclick event handler that opens the specified url in the current window.
>        * @param url the url generated by this link
>        * @return a JavaScript onclick event handler
>        */
>       protected String getScript(String url)
>       {
>           return "window.location='" + url + "'";
>       }
> 
>       /**
>        * Generates the onclick event handler that opens the specified url in the specified window or frame.
>        * @param url the url generated by this link
>        * @param target the name of the target window or frame
>        * @return a JavaScript onclick event handler
>        */
>       protected String getScript(String url, String target)
>       {
>           return "window.open('" + url + "','" + target + "')";
>       }
>   }
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tapestry-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tapestry-dev-help@jakarta.apache.org
> 
> 


-- 
Howard M. Lewis Ship
Independent J2EE / Open-Source Java Consultant
Creator, Jakarta Tapestry
Creator, Jakarta HiveMind

Professional Tapestry training, mentoring, support
and project work.  http://howardlewisship.com

---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-dev-help@jakarta.apache.org