FileDocCategorySizeDatePackage
LogonDialog.javaAPI DocExample8456Sat Sep 12 03:01:00 BST 1998borland.samples.apps.chess.client

LogonDialog.java

/*
 * Copyright (c) 1997-1998 Borland International, Inc. All Rights Reserved.
 * 
 * This SOURCE CODE FILE, which has been provided by Borland as part
 * of a Borland product for use ONLY by licensed users of the product,
 * includes CONFIDENTIAL and PROPRIETARY information of Borland.  
 *
 * USE OF THIS SOFTWARE IS GOVERNED BY THE TERMS AND CONDITIONS 
 * OF THE LICENSE STATEMENT AND LIMITED WARRANTY FURNISHED WITH
 * THE PRODUCT.
 *
 * IN PARTICULAR, YOU WILL INDEMNIFY AND HOLD BORLAND, ITS RELATED
 * COMPANIES AND ITS SUPPLIERS, HARMLESS FROM AND AGAINST ANY CLAIMS
 * OR LIABILITIES ARISING OUT OF THE USE, REPRODUCTION, OR DISTRIBUTION
 * OF YOUR PROGRAMS, INCLUDING ANY CLAIMS OR LIABILITIES ARISING OUT OF
 * OR RESULTING FROM THE USE, MODIFICATION, OR DISTRIBUTION OF PROGRAMS
 * OR FILES CREATED FROM, BASED ON, AND/OR DERIVED FROM THIS SOURCE
 * CODE FILE.
 */
package borland.samples.apps.chess.client  ;

import java.awt.*;
import java.awt.event.*;
import borland.jbcl.layout.GridBagConstraints2;
import java.util.ResourceBundle;

/**Displays the logon dialog & initiates the logon message to the server
*/
public class LogonDialog extends Dialog  implements OkCancelDialog , KeyListener
{
  ResourceBundle res = ResourceBundle.getBundle("borland.samples.apps.chess.client.Res");
  Panel dlgPanel = new Panel();
  Button cancelButton = new Button();
  Button okButton = new Button();
  Panel client = new Panel();
  Panel buttonBar = new Panel();
  FlowLayout buttonBarFlowLayout = new FlowLayout();
  Panel buttonBarGrid = new Panel();
  GridLayout buttonBarGridLayout = new GridLayout();
  BorderLayout logonBorderLayout = new BorderLayout() ;
  GridBagLayout clientGridBagLayout = new GridBagLayout();
  Label statusLabel = new Label();
  Label nameLabel = new Label();
  Label passwordLabel = new Label();
  TextField password = new TextField();
  TextField name = new TextField();
  static String pass  = "";
  static String login = "";
  boolean loggingOn = false;
  ChessViewer dlgParent;

  public LogonDialog(ChessViewer theParent)  {
    super(theParent.f,"",false);
    setTitle(res.getString("LOG_ON"));
    dlgParent = theParent;
    try {
      enableEvents(AWTEvent.WINDOW_EVENT_MASK);
      dlgParent.statusLine.setText(res.getString("PLEASE_ENTER_YOUR"));
      jbInit();
      add(dlgPanel);
      dlgParent.setModal(true);
      Point y = dlgParent.listPanel.getLocation();
      Point x = dlgParent.ep.getLocation();
      pack();
      if (x.x <= 0) {
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension frameSize = getPreferredSize();
        if (frameSize.height > screenSize.height)
          frameSize.height = screenSize.height;
        if (frameSize.width > screenSize.width)
          frameSize.width = screenSize.width;
        setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
      }
      else
        setLocation(x.x,y.y);
    }
    catch (Exception e) {
      String message = e.toString() ;
      e.printStackTrace();
    }
  }

  private void jbInit() {
    dlgPanel.setLayout(logonBorderLayout);

    buttonBar.setLayout(buttonBarFlowLayout);
    buttonBarGrid.setLayout(buttonBarGridLayout);
    buttonBarGridLayout.setColumns(2);
    buttonBarGridLayout.setHgap(6);
    okButton.setLabel(res.getString("LOG_ON"));
    okButton.addActionListener(new OkButtonListener(this));
    okButton.addKeyListener(this);
    buttonBarGrid.add(okButton );
    cancelButton.setLabel(res.getString("CANCEL"));
    cancelButton.addActionListener(new CancelButtonListener(this));
    cancelButton.addKeyListener(this);
    buttonBarGrid.add(cancelButton);
    buttonBar.add(buttonBarGrid);
    dlgPanel.add(buttonBar,BorderLayout.SOUTH);

    client.setLayout(clientGridBagLayout);
    statusLabel.setAlignment(Label.CENTER);
    statusLabel.setText(res.getString("WELCOME_TO_THE"));
    client.add(statusLabel,new GridBagConstraints2(0, 0, 2, 1, 0.0, 0.0
            ,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(4, 4, 4, 4), 0, 0));
    nameLabel.setText(res.getString("YOUR_NAME"));
    client.add(nameLabel,new GridBagConstraints2(0, 1, 1, 1, 0.0, 0.0
            ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(4, 20, 4, 0), 0, 0));
    name.setText(login);
    name.addKeyListener(this);
    name.selectAll();
    client.add(name,new GridBagConstraints2(1, 1, 1, 1, 1.0, 0.0
            ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(4, 0, 4, 20), 20, 0));

    passwordLabel.setText(res.getString("PASSWORD"));
    client.add(passwordLabel,new GridBagConstraints2(0, 2, 1, 1, 0.0, 0.0
            ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(4, 20, 4, 0), 0, 0));
    password.setText(pass);
    password.addKeyListener(this);
    password.selectAll();
    password.setEchoChar('*');     //NORES
    client.add(password,new GridBagConstraints2(1, 2, 1, 1, 1.0, 0.0
            ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(4, 0, 4, 20), 20, 0));
    dlgPanel.add(client,BorderLayout.CENTER);
  }

  /** Dispose dialog if system close is activated .
  */
  protected void processWindowEvent(WindowEvent e)  {

    if (e.getID() == WindowEvent.WINDOW_CLOSING)
      cancel(null);
    else
      super.processWindowEvent(e);
  }

  public void setVisible(boolean isVisible) {
    super.setVisible(isVisible);
    if (isVisible)
      if (login != null && login.length() > 0)
        okButton.requestFocus();
      else
        name.requestFocus();
  }

  public void badLogon() {
    loggingOn = false;
  }

  public void goodLogon() {
    dispose();
    dlgParent.setModal(false);
    dlgParent.pDialog = null;
  }

  /**OkButtonListener invokes this OkCancelDialog interface method
  */
  public void ok(ActionEvent evt) {
    if (!loggingOn) {
    try {
      loggingOn = true;
      login = name.getText();
      if (login != null)
        login = login.trim();
      dlgParent.myName  = login;
      pass = password.getText() ;
      if (pass != null)
        pass = pass.trim();
      if (login == null ) {
        name.requestFocus();
        statusLabel.setText(res.getString("INEED_A_NAME"));
        loggingOn = false;
        return ;
      }
      if (login.length() < 2)   {
        statusLabel.setText(res.getString("PLEASE_CHOOSE_A"));
        name.requestFocus();
        name.selectAll();
        loggingOn = false;
        return ;
      }
      if (pass == null  || pass.indexOf('[') != -1 ) {
        password.requestFocus();
        statusLabel.setText(res.getString("INEED_A_PASSWORD"));
        pass = "";
        loggingOn = false;
        return ;
      }
      if (pass.length() < 2)   {
        statusLabel.setText(res.getString("PLEASE_CHOOSE_PASSWORD"));
        password.requestFocus();
        password.selectAll();
        loggingOn = false;
        return;
      }
      dlgParent.statusLine.setText(res.getString("LOGGING_ON_"));
      dlgParent.sendMsg("Name",login + "[" + pass) ;
      password.requestFocus();
    }
    catch (Exception e) {
      String message = e.toString() ;
      //System.out.println("logonDlg err " + e + String.valueOf(i));
      e.printStackTrace();
    }
    }
  }

  /**CancelButtonListener invokes this OkCancelDialog interface method
  */
  public void cancel(ActionEvent evt) {
    try {
      dispose();
      dlgParent.setModal(false);
      dlgParent.statusLine.setText(res.getString("FEEL_FREE_TO_USE_THE"));
      dlgParent.pDialog = null;
    }
    catch (Exception e) {
      String message = e.toString() ;
      //System.out.println("logonDlg err " + e + String.valueOf(i));
      e.printStackTrace();
    }
  }

  public void keyTyped(KeyEvent parm1) {
    //TODO: implement this  java.awt.event.KeyListener method;
  }

  public void keyPressed(KeyEvent parm1) {
    //TODO: implement this  java.awt.event.KeyListener method;
  }

  public void keyReleased(KeyEvent e) {
    if (e.getKeyCode() == KeyEvent.VK_ENTER)
       if (e.getSource() == cancelButton)
         cancel(null);
       else
         ok(null);
     else
     if (e.getKeyCode() == KeyEvent.VK_ESCAPE)
        cancel(null);
  }
}