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

import java.util.StringTokenizer;
import java.util.Vector;

/**
 * This class identifies title for the Gui-on-the-fly, from unused GofHostFields.
 * If a title can be identified, the panel will get a title equal to the identified 
 * title.
 * @author J. Bergström
 */
public class GofTitleAreaTitleIdentifier extends GofControlIdentifierAdapter implements PhantomControlType
{
  // ------------------
  // INSTANCE VARIABLES
  // ------------------

  /**
   * The first line where to search for the title.
   */
  private int minLine;

  /**
   * The last line where to search for the title.
   */
  private int maxLine;

  /**
   * The first column where to search for the start of title.
   */
  private int minStartCol;
  
  /**
   * The last column where to search for the start of title.
   */
  private int maxStartCol;

  /**
   * The minimum length for the title.
   */
  private int minLen;

  /**
   * The maximum length of the title.
   */
  private int maxLen;

  /**
   * Title location only restricted by position. Valid values are:
   * <pre>
   *    L     Must start at the left edg.
   *    C     Must cover the center of the line.
   *    R     Must end at the right edge.
   * </pre>
   */
  private String pos;

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

  /**
   * Loads setting for the controll from the server ini-file.
   * <p>
   * The setting for title identifier allows two different ways to specify the location 
   * of the title on the host screen. It can either be specified as combination of the 
   * line it starts on, the line it ends on, the first column that it can start on, the 
   * last column it can start on, a minimum length and a maximum length. Or it can be 
   * specified as a combination of the line it starts on, the line it ends on and a 
   * location on the line. 
   * <p>
   * The location can either be left, center or right. Left means that host field must 
   * start at first column, center means that it must cross the center of the line and 
   * right means that it must end at the last column on the line.
   * <p>
   * The setting, that is named title_title consists of a comma separated list consisting 
   * of the start line, the end line, the first allowed start column, the last allowed start 
   * column, the minimum width, the maximum width, and the position on the line. Values not 
   * needed must be marked with a hyphen ('-').
   * <p>
   * An example of the first variant could be:
   * <pre>
   *    title_title=0,0,20,30,20,40,-
   * </pre>
   * In this example, the title must be on the first line, must start somewhere between 
   * column 20 and 30, and must have a width of between 20 and 40 characters.
   * <p>
   * And an example of the second variant could be:
   * <pre>
   *    title_title=0,0,-,-,-,-,C
   * </pre>
   * In this example the title must cross the center of the first line.
   * <p>
   * Normally the title would just be on a single line, so the first two values would be 
   * he same.
   * @param confFile   The configuration file.
   * @param subsection The name of the section to search in.
   */
  @Override
  public void getControlSettings( IniFile confFile, String subsection )
  {
    String setting = confFile.getData( subsection, "title_title" );
    if( setting != null )
      parseSetting( setting );
  }

  /**
   * Identifies the title from unused <code>GofHostFields</code>.
   * <p>
   * The identification of the title is quite simple. The title is supposed to be on a 
   * specific location on the screen. The location is specified in the configuration 
   * file. If a protected host field is found at the specified location, it will be 
   * accepted as the screen's title.
   * @param gofRuntime        The GuiOnTheFlyRuntime instance.
   * @param areaIdentifier    The areaIdentifier in which the entry fields should be identified..
   * @param phantomHostScreen The Phantom host screen corresponding to the host screen.
   * @param hostScreen        The host screen that we are trying to build a GOF panel for.
   * @param newPanel          The newly created Gui-on-the-fly runtime panel.
   */
  @Override
  public void identifyCtrls( GuiOnTheFlyRuntime gofRuntime,
                             GofHostAreaIdentifier areaIdentifier, 
                             PhantomHostScreen phantomHostScreen, 
                             HostScreen hostScreen,
                             PhantomPanelData templPanel, 
                             PhantomPanelData newPanel,
                             int offsetX,
                             int offsetY )
  {
    this.templPanel = templPanel;

    Vector<GofHostField> gofHostFields = areaIdentifier.getAreasGofHostFields( );

    String title = null;

    if( pos == null )
    {
      for( int i = 0, s = gofHostFields.size( ); i < s; i++ )
      {
        GofHostField gofHostField = gofHostFields.elementAt( i );
        int y = gofHostField.getY( );

        if( gofHostField.hasBeenProcessed == false && gofHostField.isProtected( ) == true && y >= minLine && y <= maxLine )
        {
          int x = gofHostField.getX( );
          int cx = gofHostField.getCx( );

          if( x >= minStartCol && x <= maxStartCol && cx >= minLen && cx <= maxLen )
          {
            gofHostField.hasBeenProcessed = true;
            if( title == null )
              title = gofHostField.getText( ).trim( );
            else
              title = title + " " + gofHostField.getText( ).trim( );
          }
        }
      }
    }
    else
    {
      // Just test the position on the line(s).

      int width = hostScreen.getWidth( );
      int mid = width / 2;

      for( int i = 0, s = gofHostFields.size( ); i < s; i++ )
      {
        GofHostField gofHostField = gofHostFields.elementAt( i );
        int y = gofHostField.getY( );

        if( gofHostField.hasBeenProcessed == false && gofHostField.isProtected( ) == true && y >= minLine && y <= maxLine )
        {
          int x = gofHostField.getX( );
          int cx = gofHostField.getCx( );

          if( pos.equals( "L" ) )
          {
            if( x == 0 )
            {
              gofHostField.hasBeenProcessed = true;
              if( title == null )
                title = gofHostField.getText( ).trim( );
              else
                title = title + " " + gofHostField.getText( ).trim( );
            }
          }
          else if( pos.equals( "C" ) )
          {
            if( x < mid && mid < x + cx )
            {
              gofHostField.hasBeenProcessed = true;
              if( title == null )
                title = gofHostField.getText( ).trim( );
              else
                title = title + " " + gofHostField.getText( ).trim( );
            }
          }
          else if( pos.equals( "R" ) )
          {
            if( x + cx == width )
            {
              gofHostField.hasBeenProcessed = true;
              if( title == null )
                title = gofHostField.getText( ).trim( );
              else
                title = title + " " + gofHostField.getText( ).trim( );
            }
          }
        }
      }
    }

    if( title != null )
      newPanel.setTitle( title );
  }

  /**
   * Parse the settings from the configuration file.
   */
  private void parseSetting( String setting )
  {
    int i = 0;

    StringTokenizer st = new StringTokenizer( setting, ", " );
    while( st.hasMoreTokens( ) )
    {
      String s = st.nextToken( ).trim( );
      int val;
      try
      {
        val = Integer.valueOf( s ).intValue( );
      }
      catch( NumberFormatException e )
      {
        val = 0;
      }
      switch( i )
      {
        case 0:
          minLine = val;
          break;
        case 1:
          maxLine = val;
          break;
        case 2:
          minStartCol = val;
          break;
        case 3:
          maxStartCol = val;
          break;
        case 4:
          minLen = val;
          break;
        case 5:
          maxLen = val;
          break;
        case 6:
          if( s.equals( "L" ) || s.equals( "C" ) || s.equals( "R" ) )
            pos = s;
          else
            pos = null;
      }
      i++;
    }
  }

}