Package net.xoetrope.xui.validation

Source Code of net.xoetrope.xui.validation.XBaseValidator

package net.xoetrope.xui.validation;

import java.awt.Color;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import net.xoetrope.xui.XTextHolder;
import net.xoetrope.xui.helper.MessageHelper;
import net.xoetrope.xml.XmlElement;
import net.xoetrope.xui.WidgetAdapter;
import net.xoetrope.xui.XProject;

/**
* <p>A basic implementation of the XValidator interface</p>
* <p>Copyright: Copyright (c) Xoetrope Ltd., 1998-2003<br>
* License:      see license.txt</p>
* $Revision: 2.6 $
*/
public abstract class XBaseValidator implements XValidator
{
  public static final int USE_NORMAL_STYLE = 0;
  public static final int USE_WARN_STYLE = 1;
  public static final int USE_FAIL_STYLE = 2;
 
  private static Color normalColor = Color.white;
  private static Color warnColor = new Color( 255, 255, 213 );
  private static Color failColor = new Color( 255, 230, 213 );
 
  /**
   * boolean indicating whether component requires input
   */
  protected boolean mandatory;

  /**
   * The unformatted message to be displayed
   */
  protected String message;

  /**
   * Used to store the current value contained in the component
   */
  protected String value;

  /**
   * The formatted message
   */
  protected String formattedMessage;

  /**
   * The name of the validation which is being handled by this validator
   */
  protected String validationName;

  /**
   * Container to be used for validation methods
   */
  protected Object container;
 
  /**
   * Method to be used for callback validations
   */
  protected Method validationMethod;
 
  /**
   * The event mask of the validation
   */
  protected int mask;
 
  /**
   * The level of the errors detected by the validation
   */
  protected int errorLevel = XValidator.LEVEL_IGNORE;
 
  /**
   * Indicates that the validation has started
   */
  protected boolean start = true;

  /**
   * The owner project and the context for this object
   */ 
  protected XProject currentProject;

  /**
   * @param project the owner project
   */
  public XBaseValidator( XProject project )
  {
    currentProject = project;
  }

  /**
   * Get the event mask. The mask helps filter the events for which the
   * validator is invoked
   * @return the mask
   */
  public int getMask()
  {
    return mask;
  }

  /**
   * Set the event mask. The mask helps filter the events for which the
   * validator is invoked
   * @param newMask The new mask which is to be used by the validator
   */
  public void setMask( int newMask )
  {
    mask = newMask;
  }

  /**
   * Set the Method of the XPage to be invoked when we are doing a funcion
   * validation
   * @param c The component being validated
   * @param m The method to be invoked
   */
  public void setValidationMethod( Method m, Object c )
  {
    container = c;
    validationMethod = m;
  }

  /**
   * Get the name of the validation
   * @return The name
   */
  public String getName()
  {
    return validationName;
  }

  /**
   * Set the name of the validation
   * @param name the new name
   */
  public void setName( String name )
  {
    validationName = name;
  }

  /**
   * Get the validation message which is formatted after the tokens have been
   * replaced
   * @return The formatted message.
   */
  public String getMessage()
  {
    return formattedMessage;
  }

  /**
   * Set the validation parameters
   * @param element the validator parameters
   */
  public void setup( XmlElement element )
  {
    message = element.getAttribute( "msg" );
    formattedMessage = message;
  }

  /**
   * Get the value as a string object
   * @return the value as string
   */
  public String getValueAsString()
  {
    return value;
  }

  /**
   * Get the error level of the validations
   * @return the error level
   */
  public int getLevel()
  {
    return errorLevel;
  }

  /**
   * Call the funcion to get the value we are validating.
   * @return The result of the function call
   */
  public Object invokeMethod()
  {
    if ( validationMethod != null ) {
      try {
        return validationMethod.invoke( container, (Object[])null );
      }
      catch ( IllegalAccessException ex ) {
        return null;
      }
      catch ( IllegalArgumentException ex ) {
        return null;
      }
      catch ( InvocationTargetException ex ) {
        return null;
      }
    }
    return "";
  }

  /**
   * Carry out a replacement on the validation message
   * @param lookup The text to find
   * @param replacement The text to replace the text with
   */
  protected void replaceToken( String lookup, String replacement)
  {
    if ( start )
      formattedMessage = message;
    formattedMessage = MessageHelper.replaceToken( formattedMessage, lookup, replacement );
    start = false;
  }

  /**
   * Replace any further tokens in the message
   */
  protected void replaceTokens()
  {
    if ( start )
      formattedMessage = message;
    formattedMessage = MessageHelper.replaceTokens( formattedMessage );
    start = false;
  }

  /**
   * Get the text value of the component.
   * @param c The component
   * @return The text components value
   */
  protected String getText( Object c )
  {
    return ( ( XTextHolder ) c ).getText();
  }

  /**
   * Replace the remaining tokens and throw the exception
   * @throws Exception Throw an exception so that it can be caught
   */
  protected void throwException() throws Exception
  {
    replaceTokens();
    start = true;
    throw new Exception( formattedMessage );
  }
 
  /**
   * Modify the evaluated component to indicate the error status
   * @param comp the validated component
   * @param errorStatus the error status
   */
  public void applyErrorStyle( Object comp, int errorStatus )
  {
    if ( comp instanceof XTextHolder ) {
      WidgetAdapter adapter = WidgetAdapter.getInstance();
      if ( errorStatus == USE_NORMAL_STYLE )
        adapter.setBackground( comp, normalColor );
      else if ( errorStatus == USE_WARN_STYLE )
        adapter.setBackground( comp, warnColor );
      else if ( errorStatus == USE_FAIL_STYLE )
        adapter.setBackground( comp, failColor );
    }
  }
 
  /**
   * Set the validation colors
   * @param validationColors the validation colors in the following order:
   * <ol>
   * <li>Normal - the style for normal or valid input</li>
   * <li>Warn - the style for values that are near the limit or values that may
   * fail secondary validation, but that will allow the application to proceed,
   * or non-mandatory validation failures</li>
   * <li>Fail - the style for failed inputs</li>
   * </ol>
   */
  public static void setValidationColors( Color[] validationColors )
  {
    normalColor = validationColors[ USE_NORMAL_STYLE ];
    warnColor = validationColors[ USE_WARN_STYLE ];
    failColor = validationColors[ USE_FAIL_STYLE ];
  }
}
TOP

Related Classes of net.xoetrope.xui.validation.XBaseValidator

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.