Listing of Source terminal/TerminalChangeSession.java
package se.entra.phantom.server;

/**
 * This class handles the Terminal Change session of the panel CHGSESS.
 */
public class TerminalChangeSession extends VirtualComponentAdapter
{
  /**
   * The terminal application.
   */
  private TerminalApplication app;
  
  ///

  /**
   * Called to initialize the panel before it is displayed.
   *
   * <p>This function doesn't perform anything.
   */
  @Override
  public void onPanelCreate(VirtualPanel vp)
    {
    VirtualPanel main=vp.getPanelSession().getPanel(0);
    if ( main==null )
      return;
    VirtualPanelListener listener=main.getListener();
    if ( listener==null || !(listener instanceof TerminalApplication) )
      return;
    app=(TerminalApplication)listener;
    String s=app.allowedSessions;
    if ( s==null )
      return;
    if ( s.equals("-") )
      s="";
    HostSessionManager hsm=vp.getClientSession().getHostSessionManager();
    HostSession chs=vp.getCurrentHostSession();
    if ( s.indexOf('*')>=0 )
      s="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    else
      s=s.toUpperCase();
    for ( int ii=0, cc=0; ii<s.length(); ++ii )
      {
      int index=s.charAt(ii)-'A';
      HostSession hs=hsm.getSession(index);
      if ( hs==null )
        {
        if ( !HostSessionManager.isHostSessionValid(s.charAt(ii)) )
          continue;
        }
      String descr=HostSessionManager.getDescription(index);
      if ( descr==null )
        descr="-";
      String type=HostSessionManager.getStringType(index);
      String err=null;
      String how="Disconnected";
      if ( hs!=null )
        {
        err=hs.getLastFailure();
        type=hs.getStringType();
        if      ( hs.isConnected () ) how="Connected";
        else if ( hs.isConnecting() ) how="Connecting";
        }
      if ( err!=null ) err=" - "+err;
      else err="";
      String line=s.charAt(ii)+"\t"+type+"\t"+how+err+"\t"+descr;
      vp.insertLine("LIST",line,-1);
      if ( hs==chs )
        vp.setSelection("LIST",cc,true);
      ++cc;
      }
    }
    
  /**
   * Called to inform the listener that the panel is destroyed.
   * Here, we enable all menu items in the main terminal
   * panel.
   */
  @Override
  public void onPanelDestroy(VirtualPanel vp)
    {
    TerminalApplication ta=vp.getVirtualSessionManager().getTerminalApplication();
    if ( ta!=null )
      ta.enableTopMenuItems(true);
    }

  /**
   * Push buttons and menu items generates this event when selected.
   */
  @Override
  public void onAction(VirtualPanel vp,String controlID)
    {
    if      ( controlID.equals("CANCEL" ) )  close(vp);
    else if ( controlID.equals("OK"     ) )  performOK(vp);
    else System.out.println("#TerminalChangeSession, Command ID "+controlID+" not handled");
    }

  /**
   * Closes this panel.
   */
  private void close(VirtualPanel vp)
    {
    if ( vp.isModal() )
      vp.dismissPanel(0);
    else
      vp.destroy();
    }

  /**
   * Called when the close button of a panel is pressed,
   * but before any other processing has been done. If this
   * function returns false, the application close object
   * will be called (if defined), otherwise a faked keypress
   * of the Escape key is performed. If these two actions
   * failed (because there is no close object or no button or
   * menu item is connected to the Escape key), then the
   * function <code>onPanelClose</code> is called.
   *
   * @return  true  to cancel all default processing.
   */
  @Override
  public boolean onPanelClosing(VirtualPanel vp)
    {
    close(vp);
    return true;
    }
  
  /**
   * Performs the OK change.
   */
  public void performOK(VirtualPanel vp)
    {
    int line=vp.getNextSelection("LIST",-1);
    if ( line<0 )
      return;
    String s=vp.getCell("LIST",0,line);
    if ( s==null )
      return;
    int index=s.charAt(0)-'A';
    HostSessionManager hsm=vp.getClientSession().getHostSessionManager();
    if ( !hsm.initializeSession(index) )
      return;
    HostSession connectSession=hsm.getSession(index);
    if ( connectSession==null )
      return;
    HostSession chs=vp.getCurrentHostSession();
    if ( connectSession!=chs )
      hsm.setCurrentSession(index,false);

    VirtualSessionManager vsm=vp.getVirtualSessionManager();
    vsm.setLockState(true);
    vsm.commitChanges(true);
    
    connectSession.connect();
    if ( app!=null )
      {
      app.setTitle();
      app.updateConnectMenuItem();
      }

    vsm.setLockState(false);
    
    close(vp);

    vsm.setLockState(true);
    vsm.setLockState(false);
    }
}