Package org.strecks.web.tag

Source Code of org.strecks.web.tag.FieldLabelTag

/*
* Copyright 2005-2006 the original author or authors.
*
* 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 requiredSuffix 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.strecks.web.tag;

import java.util.Iterator;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.struts.Globals;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.taglib.TagUtils;

/**
* Tags which shows which fields are in error. Allows error styles to be added to the field label in
* the event of an error in form submission. Also allows for localized field labels.
*
* @author Phil Zoio
*/
public class FieldLabelTag extends TagSupport
{

  private static final long serialVersionUID = -2706602151601105905L;

  /**
   * The name of the property for which error messages should be returned
   */
  protected String property = null;

  /**
   * The message resource key for errors prefix.
   */
  protected String errorPrefix = null;

  /**
   * The message resource key for errors suffix.
   */
  protected String errorSuffix = null;

  /**
   * The label for the field
   */
  protected String label = null;

  /**
   * The label for the field
   */
  protected String labelKey = null;

  /**
   * Whether the field is requiredSuffix
   */
  protected boolean required = false;

  /**
   * The label for the field
   */
  protected String requiredPrefix = null;
 
  /**
   * The label for the field
   */
  protected String requiredSuffix = null;

  // ------------------------------------------------------- Public Methods

  /**
   * Render the specified error messages if there are any.
   *
   * @exception JspException
   *                if a JSP exception has occurred
   */
  public int doStartTag() throws JspException
  {

    String toWrite = getResultString();
    TagUtils.getInstance().write(pageContext, toWrite);
    return (EVAL_BODY_INCLUDE);

  }

  String getResultString() throws JspException
  {

    // Were any error messages specified?
    ActionMessages errors = null;
    try
    {
      errors = TagUtils.getInstance().getActionMessages(pageContext, Globals.ERROR_KEY);
    }
    catch (JspException e)
    {
      TagUtils.getInstance().saveException(pageContext, e);
      throw e;
    }

    boolean errorPrefixPresent = TagUtils.getInstance().present(pageContext, null, null, getErrorPrefix());

    boolean errorSuffixPresent = TagUtils.getInstance().present(pageContext, null, null, getErrorSuffix());

    boolean requiredPrefixPresent = TagUtils.getInstance().present(pageContext, null, null, getRequiredPrefix());
   
    boolean requiredSuffixPresent = TagUtils.getInstance().present(pageContext, null, null, getRequiredSuffix());

    // Render the error messages appropriately
    StringBuffer results = new StringBuffer();

    Iterator reports = errors.get(property);

    if (reports != null && reports.hasNext())
    {

      String message = null;
      if (errorPrefixPresent)
      {
        message = TagUtils.getInstance().message(pageContext, Globals.MESSAGES_KEY, Globals.LOCALE_KEY,
            getErrorPrefix());
        results.append(message);
      }
     
      if (required && requiredPrefixPresent)
      {
        message = TagUtils.getInstance().message(pageContext, Globals.MESSAGES_KEY, Globals.LOCALE_KEY,
            getRequiredPrefix());
        results.append(message);
      }

      addLabel(results);

      if (required && requiredSuffixPresent)
      {
        message = TagUtils.getInstance().message(pageContext, Globals.MESSAGES_KEY, Globals.LOCALE_KEY,
            getRequiredSuffix());
        results.append(message);
      }

      if (errorSuffixPresent)
      {
        message = TagUtils.getInstance().message(pageContext, Globals.MESSAGES_KEY, Globals.LOCALE_KEY,
            getErrorSuffix());
        results.append(message);
      }
    }
    else
    {
      if (required && requiredPrefixPresent)
      {
        String message = TagUtils.getInstance().message(pageContext, Globals.MESSAGES_KEY, Globals.LOCALE_KEY,
            getRequiredPrefix());
        results.append(message);
      }
     
      addLabel(results);

      if (required && requiredSuffixPresent)
      {
        String message = TagUtils.getInstance().message(pageContext, Globals.MESSAGES_KEY, Globals.LOCALE_KEY,
            getRequiredSuffix());
        results.append(message);
      }
    }

    String toWrite = results.toString();
    return toWrite;
  }

  private void addLabel(StringBuffer results) throws JspException
  {
    String message;
    if (getLabelKey() != null)
    {
      message = TagUtils.getInstance().message(pageContext, Globals.MESSAGES_KEY, Globals.LOCALE_KEY,
          getLabelKey());
      results.append(message);
    }
    else if (getLabel() != null)
    {
      results.append(getLabel());
    }
    else
    {
      throw new JspException("FieldLabel tag must specify a non-null 'label' or 'labelKey' property");
    }
  }

  /**
   * Release any acquired resources.
   */
  public void release()
  {
    super.release();
    property = null;
    label = null;
    labelKey = null;
    errorPrefix = null;
    errorSuffix = null;
    required = false;
    requiredPrefix = null;
    requiredSuffix = null;
  }

  /* ****** getters and setters ******* */

  public void setProperty(String property)
  {
    this.property = property;
  }

  public String getErrorPrefix()
  {
    return errorPrefix == null ? "field.error.prefix" : errorPrefix;
  }

  public void setErrorPrefix(String prefix)
  {
    this.errorPrefix = prefix;
  }

  public String getErrorSuffix()
  {
    return errorSuffix == null ? "field.error.suffix" : errorSuffix;
  }

  public void setErrorSuffix(String suffix)
  {
    this.errorSuffix = suffix;
  }

  public void setLabel(String label)
  {
    this.label = label;
  }

  public String getLabel()
  {
    return label;
  }

  public String getProperty()
  {
    return property;
  }

  public String getLabelKey()
  {
    return labelKey;
  }

  public void setLabelKey(String labelKey)
  {
    this.labelKey = labelKey;
  }

  public boolean isRequired()
  {
    return required;
  }

  public void setRequired(boolean mandatory)
  {
    this.required = mandatory;
  }

  public String getRequiredSuffix()
  {
    return requiredSuffix == null ? "field.required.suffix" : requiredSuffix;
  }
 
  public String getRequiredPrefix()
  {
    return requiredPrefix == null ? "field.required.prefix" : requiredPrefix;
  }


  public void setRequiredSuffix(String required)
  {
    this.requiredSuffix = required;
  }

 
  public void setRequiredPrefix(String requiredPrefix)
  {
    this.requiredPrefix = requiredPrefix;
  }

}
TOP

Related Classes of org.strecks.web.tag.FieldLabelTag

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.