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

import java.awt.Rectangle;

/**
 * This class handles the Terminal Application main panel TERMINAL.
 */
public class TerminalApplication extends VirtualComponentAdapter
{
  /**
   * Terminal sessions allowed to change to (null when no change
   * of session allowed).
   */
  protected String allowedSessions;
  
  /**
   * The panel.
   */
  protected VirtualPanel panel;
  
  ///

  /**
   * Called to initialize the panel before it is displayed.
   *
   * <p>This function doesn't perform anything.
   */
  @Override
  public void onPanelCreate(VirtualPanel vp)
    {
    panel=vp;
    updateConnectMenuItem();
    setTitle();

    // Check if change session is allowed.
    boolean chg=vp.getVirtualSessionManager().isEditor();
    HostSession hs=panel.getCurrentHostSession();
    if ( hs!=null )
      {
      if ( chg )
        allowedSessions="*";
      else
        {
        allowedSessions=ClientSessionManager.getServerIni().getData("host",hs.getSessionID()+".allowedSessions");
        if ( allowedSessions==null )
          allowedSessions=ClientSessionManager.getServerIni().getData("host","defaultAllowedSessions");
        if ( allowedSessions!=null && allowedSessions.length()>0 )
          {
          chg=true;
          if ( allowedSessions.indexOf('*')>=0 )
            allowedSessions="*";
          else
            allowedSessions=allowedSessions.toUpperCase();
          }
        }
      }
    vp.setEnabled("CHGSESS",chg);
    
    // Check if change panel session is available.
    if ( vp.setVisible("NEXT",false) )
      {
      VirtualInterface tb=vp.getControlFromID("TOOLBAR");
      if ( tb!=null )
        {
        tb.setText("1,D,"); // Delete first button
        tb.setText("0,F,"); // Refresh bar
        }
      }
/* Will be in soon future...
    VirtualSessionManager vsm=vp.getVirtualSessionManager();
    if ( !vsm.hasApplicationPanel() )
      {
      // Hide menu item "Next panel session", and if this fails, there
      // is no such item and in this case the toolbar shouldn't be modified.
      if ( vp.setVisible("NEXT",false) )
        {
        VirtualInterface tb=vp.getControlFromID("TOOLBAR");
        if ( tb!=null )
          {
          tb.setText("1,D,"); // Delete first button
          tb.setText("0,F,"); // Refresh bar
          }
        }
      }
    else
      {
      // Disable the NEXT menu item if we're the only session.
      System.out.println("PanelSessionCount = "+vsm.getSessionCount());
      //if ( vsm.getSessionCount()<=1 )
        //vp.setEnabled("NEXT",false);
      }
*/
    }

  /**
   * Sets the title bar in the panel.
   */
  public void setTitle()
    {
    if ( panel==null )
      return;

    // Change the "?" to the current session and its real name.
    String text=panel.getPanelData().getTitle();
    StringBuilder buf=new StringBuilder(128);
    if ( VirtualTextPrompt.formatTextPrompt(panel,text,buf) )
      text=buf.toString();
    HostSessionManager hsm=panel.getClientSession().getHostSessionManager();
    HostSession hs=hsm.getCurrentSession();
    if ( text.endsWith("?") )
      {
      text=text.substring(0,text.length()-1);
      if ( hs==null )
        text+="<none>";
      else
        {
        // Add ID [ - Description].
        int index=hs.getIndex();
        text+=""+(char)('A'+index);
        String descr=HostSessionManager.getDescription(index);
        if ( descr!=null && descr.length()>0 )
          text+=" - "+descr;
        }
      panel.setText(text);
      }
    }

  /**
   * Push buttons and menu items generates this event when selected.
   */
  @Override
  public void onAction(VirtualPanel vp,String controlID)
    {
    // Don't allow actions when pop-up panels are displayed.
    int index1=vp.getIndex()+1;
    if ( vp.getPanelSession().getPanelCount()!=index1 )
      return;
    
    // Check our action ID's.
    if      ( controlID.equals("EXIT"   ) )  performExit(vp);
    else if ( controlID.equals("PROPS"  ) )  performProperties(vp);
    else if ( controlID.equals("CHGSESS") )  performChangeSession(vp);
    else if ( controlID.equals("CUT"    ) )  performCut(vp);
    else if ( controlID.equals("COPY"   ) )  performCopy(vp);
    else if ( controlID.equals("PASTE"  ) )  performPaste(vp);
    else if ( controlID.equals("APPEND" ) )  performAppend(vp);
    else if ( controlID.equals("SELALL" ) )  performSelectAll(vp);
    else if ( controlID.equals("CONNECT") )  performConnect(vp);
    else if ( controlID.equals("PRINT"  ) )  performPrint(vp);
    else if ( controlID.equals("PRTWIN" ) )  performPrintWindow(vp);
    else if ( controlID.equals("ABOUT"  ) )  performAbout(vp);
    else if ( controlID.equals("NEXT"   ) )  vp.getVirtualSessionManager().selectNextSession();
    else System.out.println("#TerminalApplication, Command ID "+controlID+" not handled");
    
    // Check if a pop-up was displayed, and if so, disable all top-menu items.
    if ( vp.getPanelSession().getPanelCount()>index1 )
      enableTopMenuItems(false);
    }
  
  /**
   * Enables or disables all top menu items in the main terminal window.
   */
  public void enableTopMenuItems(boolean enable)
    {
    if ( panel!=null )
      {
      VirtualMenu m=panel.getMenu();
      if ( m!=null )
        for ( int ii=0; ii<m.getPullDownMenuCount(); ++ii )
          m.getPullDownMenu(ii).setEnabled(enable);
      }
    }

  /**
   * Menu items generates this event when selected.
   */
  @Override
  public void onMenuAction(VirtualPanel vp,VirtualMenuItem menuItem)
    {
    onAction(vp,menuItem.getID());
    }

  /**
   * Performs application exit.
   */
  public void performExit(VirtualPanel vp)
    {
    ClientSessionInterface clientSession=vp.getClientSession();
    if ( (clientSession instanceof ClientSession)
      && ((ClientSession)clientSession).isClientTerminalDisplayedProgrammatically() )
      {
      ((ClientSession)clientSession).removeTerminal(true);
      vp.getVirtualSessionManager().doPanelAsyncHostUpdate();
      return;
      }

    if ( !vp.getVirtualSessionManager().isParallel )
      {
      String language=vp.getClientSession().getClientConnectionData().language;
      String head=ClientSessionManager.getTextID(language,"ZL0019");
      String txt =ClientSessionManager.getTextID(language,"ZL0020");
      if ( vp.getVirtualSessionManager().message(6,3,txt,head)!=4 )
        return;
      }
    vp.getClientSession().exitApplication();
    }
  
  /**
   * Creates a dialog box for the application with a listener.
   */
  public VirtualPanel createPanel(VirtualSessionManager vsm,String panelID,VirtualComponentListener listener)
    {
    if ( vsm.isMultipleTerminalEnabled )
      return vsm.getCurrentSession().createTerminalPanel(panelID,listener);
    else
      return vsm.getCurrentTerminalSession().createTerminalPanel(panelID,listener);
    }
  
  /**
   * Performs change of host session.
   */
  public void performChangeSession(VirtualPanel vp)
    {
    // For Editor, create the panel as modal and change session is always allowed.
    VirtualSessionManager vsm=vp.getVirtualSessionManager();
    if ( vsm.isEditor() )
      vsm.createModalPanel("CHGSESS",new TerminalChangeSession());
    else
    if ( allowedSessions!=null && allowedSessions.length()>0 )
      createPanel(vsm,"CHGSESS",new TerminalChangeSession());
    }
  
  /**
   * Performs clipboard cut.
   */
  public void performCut(VirtualPanel vp)
    {
    // Get the marked rectangle.
    VirtualSessionManager vsm=vp.getVirtualSessionManager();
    TerminalFunctions tf=vsm.getTerminalFunctions();
    Rectangle r=tf.getClientTerminalMark();
    if ( r==null )
      return;
    
    // Build the clipboard.
    String s=tf.performTerminalCopy(r.x,r.y,r.width,r.height);
    if ( s!=null )
      {
      vsm.setClientClipboard(s);
      if ( tf.performTerminalClear(r.x,r.y,r.width,r.height) )
        tf.removeClientTerminalMark();    
      }
    }
  
  /**
   * Performs clipboard copy.
   */
  public void performCopy(VirtualPanel vp)
    {
    // Get the marked rectangle.
    VirtualSessionManager vsm=vp.getVirtualSessionManager();
    TerminalFunctions tf=vsm.getTerminalFunctions();
    Rectangle r=tf.getClientTerminalMark();
    if ( r==null )
      return;
    
    // Build the clipboard.
    String s=tf.performTerminalCopy(r.x,r.y,r.width,r.height);
    if ( s!=null )
      {
      vsm.setClientClipboard(s);
      tf.removeClientTerminalMark();    
      }
    }
  
  /**
   * Performs clipboard append.
   */
  public void performAppend(VirtualPanel vp)
    {
    VirtualSessionManager vsm=vp.getVirtualSessionManager();
    TerminalFunctions tf=vsm.getTerminalFunctions();

    // Get the marked rectangle.
    Rectangle r=tf.getClientTerminalMark();
    if ( r==null )
      return;
    
    // Get the current clipboard contents.
    String s=vsm.getClientClipboard();
    if ( s!=null )
      {
      // Build the clipboard.
      String s2=tf.performTerminalCopy(r.x,r.y,r.width,r.height);
      if ( s2!=null )
        {
        vsm.setClientClipboard(s+s2);
        tf.removeClientTerminalMark();
        }
      }
    }
  
  /**
   * Performs clipboard paste.
   */
  public void performPaste(VirtualPanel vp)
    {
    VirtualSessionManager vsm=vp.getVirtualSessionManager();
    TerminalFunctions tf=vsm.getTerminalFunctions();
    tf.performPaste(vsm.getClientClipboard());
    }

  /**
   * Performs clipboard select all.
   */
  public void performSelectAll(VirtualPanel vp)
    {
    vp.getVirtualSessionManager().getTerminalFunctions().setClientTerminalMarkSelectAll();
    }
  
  /**
   * Performs the properties panel handling.
   */
  public void performProperties(VirtualPanel vp)
    {
    VirtualSessionManager vsm=vp.getVirtualSessionManager();
    if ( vsm.isEditor() )
      vsm.createModalPanel("PROPS",new TerminalApplicationProperties());
    else
      createPanel(vsm,"PROPS",new TerminalApplicationProperties());
    }
  
  /**
   * Displays the "Edit properties" dialog box used when performing
   * Cut/Copy operations.
   * 
   * @return  true  to continue the operation or false to abort.
   */
  public boolean displayEditProperties(VirtualSessionManager vsm)
    {
    int rc;
    if ( vsm.isEditor() )
      rc=vsm.createModalPanel("CUTCOPY",new TerminalCutCopyParsing());
    else
      {
      VirtualPanel vp=createPanel(vsm,"CUTCOPY",new TerminalCutCopyParsing());
      if ( vp==null )
        return false;
      rc=vp.processPanel();
      vp.destroy();
      }
    return (rc==0);
    }
  
  /**
   * Updates the states for the Connect/Disconnect menu item.
   */
  public void updateConnectMenuItem()
    {
    if ( panel==null )
      return;
    
    HostSession hs=panel.getCurrentHostSession();
    boolean connect=!(hs!=null && (hs.isConnecting() || hs.isConnected() || hs.isReconnecting()));

    String text=panel.getRuntime().getTextID(connect? "TERM0051": "TERM0052");
    if ( text==null )
      text=connect? "~Connect": "~Disconnect";
                                             
    panel.setText("CONNECT",text);
    panel.setEnabled("CONNECT",(hs!=null));
    }

  /**
   * Called when a session has had a failure.
   * 
   * @return  true  if handled, otherwise false (disposes session).
   */
  public boolean onHostSessionFailure(Throwable exception)
    {
    if ( panel==null )
      return false;
    
    updateConnectMenuItem();
    ((ClientSession)(panel.getClientSession())).onHostPrinterSessionFailure(exception);

    panel.getVirtualSessionManager().setLockState(false);
    return true;
    }
  
  /**
   * Notifies the session of a connect state change.
   */
  public void onHostConnectChange(boolean connected)
    {
    updateConnectMenuItem();
    
    if ( panel==null )
      return;

    panel.getVirtualSessionManager().setLockState(false);
    }

  /**
   * Performs connect/disconnect for the current host session.
   */
  public void performConnect(VirtualPanel vp)
    {
    HostSession hs=panel.getCurrentHostSession();
    if ( hs==null )
      return;
    
    VirtualSessionManager vsm=panel.getVirtualSessionManager();
    vsm.setLockState(true);
    vsm.commitChanges(true);
    
    if ( hs.isConnecting() || hs.isConnected() || hs.isReconnecting() )
      hs.disconnect();
    else
      hs.connect();

    updateConnectMenuItem();
    vsm.setLockState(false);
    }
  
  /**
   * Performs a print screen with a printer device font
   * in order to print it out nicely.
   */
  public void performPrint(VirtualPanel vp)
    {
    HostSession hs=vp.getCurrentHostSession();
    if ( hs!=null )
      hs.printSession();
    }

  /**
   * Performs a print window of the terminal screen.
   * This printout results in a bitmap print of the
   * entire terminal window.
   */
  public void performPrintWindow(VirtualPanel vp)
    {
    vp.getVirtualSessionManager().getTerminalFunctions().performPrintWindow();
    }

  /**
   * Performs a print window of the terminal screen.
   * This printout results in a bitmap print of the
   * entire terminal window.
   */
  public void performAbout(VirtualPanel vp)
    {
    vp=createPanel(vp.getVirtualSessionManager(),"ABOUT",new TerminalAbout());
    if ( vp==null )
      return;

    vp.processPanel();
    vp.destroy();
    }
}