Listing of Source ../source/GOF/GofHostFieldIdentifierAdapter.java
package se.entra.phantom.server;

import java.util.Vector;

/**
 * This is an adapter class that must be used as a base class for classes implementing
 * the GofHostFieldIdentifier interface.
 * <p>
 * By extending from this class, instead of directly implementing the interface, makes
 * it possible to extend the interface with new methods in the future, without forcing
 * a recompilation of user written GofHostFieldIdentifier classes. Default versions of 
 * these new methods can instead be implemented in this adapter class.
 * @author J. Bergström
 */
public abstract class GofHostFieldIdentifierAdapter implements GofHostFieldIdentifier
{
  // ------------------
  // INSTANCE VARIABLES
  // ------------------

  /**
   * A vector holding all the identified GofHostFields.
   */
  protected Vector<GofHostField> gofHostFields;

  /**
   * Flag indicating if host fields that are partly inside the area to identify
   * should be included or not.
   */
  private boolean doIdentifyPartialFields;

  // -----------
  // CONSTRUCTOR
  // -----------

  /**
   * The constructor is a default constructor without any parameters. This makes it possible 
   * to load this class dynamically. The constructor doesn't do anything more than create the instance.
   */
  public GofHostFieldIdentifierAdapter( )
  {
  }

  // ----------------
  // INSTANCE METHODS
  // ----------------

  /**
   * This method must create the GofHostFields from the HostScreen's HostFields. It is up this 
   * method to decide if a HostField should be split up to several GofHostFields. The newly 
   * created GofHostFields must be stored internally in the class, since the method GetHostFields 
   * must be able to return them.
   * <p>
   * This adapter class only implements an empty method. Classes extending this class <b>must</b>
   * extend this method.
   * @param screen The host screen to process.
   */
  @Override
  public void identifyHostFields( HostScreen screen )
  {
  }

  /**
   * This method must store a flag indicating what should be done with GofHostFields that are partially 
   * inside, partially outside the area that should be identified. This flag is only meaningful when a 
   * part of the whole screen will be identified.
   * @param flag The value of the flag.
   */
  @Override
  public void doIdentifyPartialFields( boolean flag )
  {
    doIdentifyPartialFields = flag;
  }

  /**
   * This methods queries if partial fields are identified when GofHostFields that are partially 
   * inside, partially outside the area that should be identified. The returned value is only meaningful 
   * when a part of the whole screen will be identified.
   * @return <code>true</code> if partial fields are identified, <code>false</code> otherwise.
   */
  @Override
  public boolean isPartialFieldsIdentified( )
  {
    return doIdentifyPartialFields;
  }

  /** 
   * This method is a method that returns all the GofHostFields on the screen. It takes a start column, 
   * start line, width and height as parameters. The parameters determine the actual area of the host field 
   * that should be identified, so only GofhostFields inside this area will be returned.
   * <p>
   * When analyzing if a GofHosField is inside the area, it will take into account the value of the flag 
   * <code>doIdentifyPartialFields</code>. If this flag is <code>false</code>, only GofHostFields completely 
   * inside the area will be included, otherwise GofHostFields partially inside the area will also be included.
   * @param x The start column.
   * @param y The start row.
   * @param w The width.
   * @param h The height.
   * @return The logical host fields.
   */
  @Override
  public Vector<GofHostField> getHostFields( int x, int y, int w, int h )
  {
    Vector<GofHostField> inclHostFields = new Vector<GofHostField>( );

    for( int i = 0, s = gofHostFields.size( ); i < s; i++ )
    {
      GofHostField gofHf = gofHostFields.elementAt( i );
      int gx = gofHf.getX( );
      int gy = gofHf.getY( );
      int gcx = gofHf.getCx( );
      if( gx >= x && ( gx + gcx - 1 ) < ( x + w ) && gy >= y && gy < ( y + h ) )
        inclHostFields.addElement( gofHf );
      else if( doIdentifyPartialFields == true )
      {
        if( gy >= y && gy < ( y + h ) )
        {
          if( gx < x && ( gx + gcx ) < ( x + w ) )
          {
            // Starts outside area, but ends inside.
            inclHostFields.addElement( gofHf );
          }
          else if( gx >= x && ( gx + gcx ) >= ( x + w ) )
          {
            // Starts inside area, but ends outside.
            inclHostFields.addElement( gofHf );
          }
          else if( gx < x && ( gx + gcx ) >= ( x + w ) )
          {
            // Starts to the left of area, and ends to the right of area, but crosses over the area.
            inclHostFields.addElement( gofHf );
          }
        }
      }
    }
    return inclHostFields;
  }

  /** 
   * This is the method to get the GofHostFields belonging to a popup-window. It takes a start column, start line, 
   * width and height for the popup window as parameters. It first saves the value of the doIdentifyPartialFields 
   * flag, sets it to false, since no partial fields should exist in a popup window. It the calls the <code>getHostFields</code> 
   * method which retrieves the GofHostFields, and finally restores the doIdentifyPartialFields flag before returning 
   * the <code>Vector</code> with the included GofHostFields.
   * @param x The start column.
   * @param y The start row.
   * @param w The width.
   * @param h The height.
   * @return The logical host fields.
   */
  @Override
  public Vector<GofHostField> getPopupWindowHostFields( int x, int y, int w, int h )
  {
    boolean oldDoIdentifyPartialFields = doIdentifyPartialFields;
    doIdentifyPartialFields = false;

    Vector<GofHostField> inclHostFields = getHostFields( x, y, w, h );

    doIdentifyPartialFields = oldDoIdentifyPartialFields;

    return inclHostFields;
  }
}