home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-07-03 | 7.0 KB | 221 lines |
- package borland.samples.apps.chess.client ;
-
- import java.awt.*;
- import java.awt.event.*;
- import borland.jbcl.layout.GridBagConstraints2;
-
- /**Displays the logon dialog & initiates the logon message to the server
- */
- public class LogonDialog extends Dialog implements OkCancelDialog , KeyListener
- {
- 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,CVS.LOG_ON,false);
- dlgParent = theParent;
- try {
- enableEvents(AWTEvent.WINDOW_EVENT_MASK);
- dlgParent.statusLine.setText(CVS.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();
- }
- }
-
- public void jbInit() {
- dlgPanel.setLayout(logonBorderLayout);
-
- buttonBar.setLayout(buttonBarFlowLayout);
- buttonBarGrid.setLayout(buttonBarGridLayout);
- buttonBarGridLayout.setColumns(2);
- buttonBarGridLayout.setHgap(6);
- okButton.setLabel(CVS.LOG_ON);
- okButton.addActionListener(new OkButtonListener(this));
- okButton.addKeyListener(this);
- buttonBarGrid.add(okButton );
- cancelButton.setLabel(CVS.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(CVS.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(CVS.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(CVS.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 show() {
- super.show();
- 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(CVS.INEED_A_NAME);
- loggingOn = false;
- return ;
- }
- if (login.length() < 2) {
- statusLabel.setText(CVS.PLEASE_CHOOSE_A);
- name.requestFocus();
- name.selectAll();
- loggingOn = false;
- return ;
- }
- if (pass == null || pass.indexOf('[') != -1 ) {
- password.requestFocus();
- statusLabel.setText(CVS.INEED_A_PASSWORD);
- pass = "";
- loggingOn = false;
- return ;
- }
- if (pass.length() < 2) {
- statusLabel.setText(CVS.PLEASE_CHOOSE_PASSWORD);
- password.requestFocus();
- password.selectAll();
- loggingOn = false;
- return;
- }
- dlgParent.statusLine.setText(CVS.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(CVS.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);
- }
- }
-
-