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

import java.util.Vector;

/**
 * The identification of the area represented by this class is also very simple. It just 
 * takes a start line and an end line from the server.ini file, and finds all the 
 * GofHostFields that are between these two lines.
 * @author J. Bergström
 */
public class GofButtonAreaIdentifier extends GofHostAreaIdentifierAdapter
{
  // ------------------
  // INSTANCE VARIABLES
  // ------------------

  private int hostY;
  //private int hostX;

  /**
   * The start line setting from the ini-file.
   */
  private int startLine = 0;

  /**
   * The end line setting from the ini-file.
   */
  private int endLine = 0;

  /**
   * The min screen height for identifying a pushbutton area.
   */
  private int minHeight = 0;

  /**
   * The layout to use for this area.
   */
  private int layout = 0;

  /**
   * The start line for the current screen. For popup windows this might differ from 
   * the setting in the ini-file, because it will calculated from the popup area.
   */
  private int curStartLine;

  /**
   * The end line for the current screen. For popup windows this might differ from 
   * the setting in the ini-file, because it will calculated from the popup area.
   */
  private int curEndLine;

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

  /**
   * Loads setting for the controll from the server ini-file.
   * @param confFile   The server ini file.
   * @param subsection The section containing the Gui-on-the-fly settings to be used.
   */
  @Override
  public void getAreaSettings( IniFile confFile, String subsection )
  {
    String setting;

    setting = confFile.getData( subsection, "buttonarea_startline" );
    if( setting != null && setting.equals( "" ) == false )
    {
      try
      {
        startLine = Integer.valueOf( setting ).intValue( );
      }
      catch( NumberFormatException e )
      {
        startLine = 0;
      }
    }

    setting = confFile.getData( subsection, "buttonarea_endline" );
    if( setting != null && setting.equals( "" ) == false )
    {
      try
      {
        endLine = Integer.valueOf( setting ).intValue( );
      }
      catch( NumberFormatException e )
      {
        endLine = 0;
      }
    }

    setting = confFile.getData( subsection, "buttonarea_minheight" );
    if( setting != null && setting.equals( "" ) == false )
    {
      try
      {
        minHeight = Integer.valueOf( setting ).intValue( );
      }
      catch( NumberFormatException e )
      {
        minHeight = 0;
      }
    }

    setting = confFile.getData( subsection, "buttonarea_layout" );
    if( setting != null && setting.equals( "" ) == false )
    {
      try
      {
        layout = Integer.valueOf( setting ).intValue( );
      }
      catch( NumberFormatException e )
      {
        layout = 0;
      }
    }
  }

  /**
   * A method that identifies which of the Gui-on-the-fly host fields that belong to the area.
   * @param gofHostFields A <code>Vector</code> containing all the GofHostFields in the part of the 
   *                      screen area to be analyzed.
   * @param x             The start column of the area to be analyzed.
   * @param y             The start line of the area to be analyzed.
   * @param w             The width of the area to be analyzed.
   * @param h             The height of the area to be analyzed.
   * @return A <code>Vector</code> containing all the unused Gui-on-the-fly host fields.
   */
  @Override
  public Vector<GofHostField> identifyArea( Vector<GofHostField> gofHostFields, int x, int y, int w, int h )
  {
    hostY = y;

    areaGofHostFields = new Vector<GofHostField>( );
    phantomControls = new Vector<PhantomControl>( );

    Vector<GofHostField> unusedGofHostFields;
    if( h < minHeight )
      unusedGofHostFields = gofHostFields;
    else
    {
      unusedGofHostFields = new Vector<GofHostField>();

      setCurStartAndEnd( y, h );

      for( int i = 0, s = gofHostFields.size( ); i < s; i++ )
      {
        GofHostField ghf = gofHostFields.elementAt( i );
        int ghfy = ghf.getY( );
        if( ghfy >= curStartLine && ghfy <= curEndLine )
          areaGofHostFields.addElement( ghf );
        else
          unusedGofHostFields.addElement( ghf );
      }
    }

    return unusedGofHostFields;
  }

  /**
   * Sets the start line and end line for the current screen's pushbutton area.
   * @param y The current screens start line.
   * @param h The current screens height.
   */
  private void setCurStartAndEnd( int y, int h )
  {
    if( startLine < 0 )
      curStartLine = ( y + h ) + startLine;
    else if( startLine > 0 )
      curStartLine = y + startLine;
    else
      curStartLine = y + h;

    if( endLine < 0 )
      curEndLine = ( y + h ) + endLine;
    else if( startLine > 0 )
      curEndLine = y + endLine;
    else
      curEndLine = y + h;
  }

  /**
   * This method should layout the controls belonging to this area according to the layout rule 
   * specified in the configuration file.
   */
  @Override
  public void layout( )
  {
    switch( layout )
    {
      case FLOW_LAYOUT:
        int minHGap = 10;
        int minVGap = 5;
        int yStart = GofControlIdentifier.GOF_MARGINY + ( curStartLine - hostY ) * GofControlIdentifier.GOF_STEPY;

        int curX = GofControlIdentifier.GOF_MARGINX;
        int curY = yStart + minVGap;
        for( int i = 0, s = phantomControls.size( ); i < s; i++ )
        {
          PhantomControl pc = phantomControls.elementAt( i );
          pc.controlBase.x = curX;
          pc.controlBase.y = curY;
          curX = curX + pc.controlBase.cx + minHGap;
        }
        break;
    }
  }
}