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

import java.util.Vector;

/**
 * This class identifies output text/static text controls for the Gui-on-the-fly, from unused GofHostFields.
 * @author J. Bergström
 */
public class GofOutputTextIdentifier extends GofControlIdentifierAdapter implements PhantomControlType
{
  // ------------------
  // INSTANCE VARIABLES
  // ------------------

  /** 
   * Flag indicating that static text should be used for all controls.
   */
  private boolean useStaticText;

  /**
   * Flag indicating if trailing punctuation marks should be removed.
   */
  private boolean delTrailingPunctMark;

  /**
   * Indicates the way the output text's/static texts's layout will be created.
   * Valid values are:
   * <pre>
   *    DEFAULT
   *    FONT
   *    COLOR
   *    FONTANDCOLOR
   * </pre>
   */
  private String layout;

  /**
   * Flag indicating that the text has been changed, necessitates the use of static text.
   */
  private boolean hasTextBeenChanged;

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

  /**
   * Loads setting for the controll from the server ini-file.
   * <p>
   * There is one setting for this class that makes it possible to remove trailing punctuation 
   * marks. These punctuation marks are either dots (.) or colons (:). The setting looks like 
   * this:
   * <pre>
   *    deltrailingpunctmark=1
   * </pre>
   * Remember that if the text is changed, the control that will be created will always be a 
   * static text, not output text.
   * <p>
   * It is also possible to specify that the controls created by this class should always be 
   * static texts. To do this, set the alwaysusestatictext setting to 1, otherwise to 0. In 
   * the example below, static texts will always be used.
   * <pre>
   *    alwaysusestatictext=1
   * </pre>
   * There is also a setting that affects the look of the output texts/static texts. This is 
   * the outputtextlayout setting. Valid values for this setting are:
   * <pre>
   *    outputtextlayout=DEFAULT
   *    outputtextlayout=FONT
   *    outputtextlayout=COLOR
   *    outputtextlayout=FONTANDCOLOR
   * </pre>
   * DEFAULT means that no settings are taken from the template panel, default values are used. 
   * <p>
   * FONT means that the font is taken from template panel, from an output text with the id=O, 
   * id=OR, id=ONL or id=ONR (depending on the host fields justification, and if the host field 
   * is numeric or not). If this control cannot be found, or if it is not an output text control, 
   * default values will be used.
   * <p>
   * COLOR means that the color is taken from template panel, from an output text with the id=O, 
   * id=OR, id=ONL or id=ONR (depending on the host fields justification, and if the host field 
   * is numeric or not). If this control cannot be found, or if it is not an output text control, 
   * default values will be used.
   * <p>
   * FONTANDCOLOR means that the font and color are taken from template panel, from an output 
   * text with the id=O, id=OR, id=ONL or id=ONR (depending on the host fields justification, 
   * and if the host field is numeric or not). If this control cannot be found, or if it is not 
   * an output text control, default values will be used.
   * <p>
   * Any other value will be treated as DEFAULT.
   * <p>
   * Remember that default color for an output text is black, and for a static text it is dark blue. 
   * This means that if the color for the output text with id=O is set to default, output texts and 
   * static texts will receive different colors on the created panel.
   * @param confFile The server ini file.
   */
  @Override
  public void getControlSettings( IniFile confFile, String subsection )
  {
    String setting;
    setting = confFile.getData( subsection, "alwaysusestatictext" );
    if( setting.equals( "1" ) )
      useStaticText = true;
    else
      useStaticText = false;

    setting = confFile.getData( subsection, "deltrailingpunctmark" );
    if( setting.equals( "1" ) )
      delTrailingPunctMark = true;
    else
      delTrailingPunctMark = false;

    layout = confFile.getData( subsection, "outputtextlayout" );
    if( layout == null )
      layout = "DEFAULT";
  }

  /**
   * Identifies all the output text controls from the <code>GofHostFields</code>.
   * <p>
   * Every unused GofHostField that is protected, and not empty, will result in either a 
   * output text or a static text. The type of control that will be created depends on 
   * the settings for this class in the server.ini file.
   * <p>
   * If the setting <i>alwaysusestatictext</i> has been set to 1 in the configuration file, all
   * identified output texts will be created as static texts. Otherwise only those whose
   * text's that has to be changed, will created as static texts.
   * @param gofRuntime        The GuiOnTheFlyRuntime instance.
   * @param areaIdentifier    The areaIdentifier in which the controls 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.
   * @param offsetX           The offset in columns for popup window.
   * @param offsetY           The offset in lines for popup window.
   */
  @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( );

    for( int i = 0, s = gofHostFields.size( ); i < s; i++ )
    {
      GofHostField gofHostField = gofHostFields.elementAt( i );
      if( gofHostField.hasBeenProcessed == false && gofHostField.isProtected( ) == true && gofHostField.isEmpty( ) == false )
      {
        hasTextBeenChanged = false;

        int x = gofHostField.getX( );
        int y = gofHostField.getY( );
        int cx = gofHostField.getCx( );
        String text = gofHostField.getText( );
        if( delTrailingPunctMark == true )
          text = delTrailingPunctMark( text );

        if( text.trim( ).equals( "" ) == false )
        {
          // Create the PhantomHostField.
          PhantomHostField phf = new PhantomHostField( phantomHostScreen, hostScreen, x, y, cx );
          
          int font = -1;
          int foregroundColor=-1;

          HostField hf = gofHostField.getHostField( );

          // Check the right justification of the field.
          boolean isRJ = ( hf.justification == HostField.HFJ_RightZero || hf.justification == HostField.HFJ_RightBlank );

          PhantomControl templateControl;
          if( hf.type == HostField.HFT_NumericOnly || 
              hf.type == HostField.HFT_DigitsOnly || 
              hf.type == HostField.HFT_SignedNumeric || 
              hf.type == HostField.HFT_ImpliedDecimal || 
              hf.type == HostField.HFT_NumericShift )
          {
            if( isRJ == true )
              templateControl = templPanel.getControlFromID( "ONR" );
            else
              templateControl = templPanel.getControlFromID( "ONL" );
          }
          else if( isRJ == true )
            templateControl = templPanel.getControlFromID( "OR" );
          else
            templateControl = templPanel.getControlFromID( "O" );

          if( layout.equals( "FONT" ) )
          {
            if( templateControl != null && templateControl.controlBase.type == CTRLTYPE_OUT )
              font = ( ( PhantomCOutputText )templateControl ).font;
          }
          else if( layout.equals( "COLOR" ) )
          {
            if( templateControl != null && templateControl.controlBase.type == CTRLTYPE_OUT )
              foregroundColor = ( ( PhantomCOutputText )templateControl ).getForegroundColor();
          }
          else if( layout.equals( "FONTANDCOLOR" ) )
          {
            if( templateControl != null && templateControl.controlBase.type == CTRLTYPE_OUT )
            {
              font = ( ( PhantomCOutputText )templateControl ).font;
              foregroundColor = ( ( PhantomCOutputText )templateControl ).getForegroundColor();
            }
          }

          PhantomControl pc;
          PhantomControlBase bc;
          if( useStaticText == false && hasTextBeenChanged == false )
          {
            // Create the base control.
            bc = new PhantomControlBase( GOF_MARGINX + ( x - offsetX ) * GOF_STEPX,
                                         GOF_MARGINY + ( y - offsetY ) * GOF_STEPY,
                                         cx * GOF_GUIUNITX,
                                         GOF_GUIUNITY,
                                         CTRLTYPE_OUT );
            // Create the output text.
            PhantomCOutputText po = new PhantomCOutputText( newPanel, bc, isRJ, phf );
            po.font = font;
            po.setForegroundColor(foregroundColor);
            pc = po;
          }
          else
          {
            // Create the base control.
            bc = new PhantomControlBase( GOF_MARGINX + ( x - offsetX ) * GOF_STEPX,
                                         GOF_MARGINY + ( y - offsetY ) * GOF_STEPY,
                                         cx * GOF_GUIUNITX,
                                         GOF_GUIUNITY,
                                         CTRLTYPE_TEXT );
            // Create the static text.
            PhantomCStaticText ps = new PhantomCStaticText( newPanel, bc, isRJ, text );
            ps.font = font;
            ps.setForegroundColor(foregroundColor);
            pc = ps;
          }

          // Add it in the panel.
          newPanel.addControl( pc );
          areaIdentifier.addControl( pc );
          gofHostField.hasBeenProcessed = true;
        }
      }
    }
  }

  /**
   * Deletes the trailing punctuation marks.
   * @param text The text to delete trailing punctuation marks from.
   * @return The resulting text.
   */
  private String delTrailingPunctMark( String text )
  {
    if( text.trim( ).equals( "" ) == true )
      return "";

    int p = text.length( ) - 1;
    char c = text.charAt( p );
    while( c == '.' || c == ':' || c == ' ' )
    {
      hasTextBeenChanged = true;
      text = text.substring( 0, p );
      p = text.length( ) - 1;
      if( p < 0 )
        break;
      c = text.charAt( p );
    }
    return text;
  }
}