home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / pc / java / enxle1f6 / src / games / battle / client / europaclient / loginwindow.java < prev    next >
Encoding:
Java Source  |  1996-08-14  |  7.4 KB  |  337 lines

  1. /*
  2.  * @(#)LoginWindow.java
  3.  */
  4.  
  5. package games.Battle.client.EuropaClient;
  6.  
  7. import java.awt.*;
  8.  
  9. import games.Battle.shared.comm.PlayerInfo;
  10.  
  11. /**
  12.  * This dialog window is responsible for collecting login/creation
  13.  * information about a player.
  14.  */
  15. public class LoginWindow extends Frame {
  16.  
  17.     /**
  18.      * The client applet.
  19.      */
  20.     EuropaClient client;
  21.  
  22.     /**
  23.      * The container for all the dialog elements.
  24.      */
  25.     Container info;
  26.  
  27.     /**
  28.      * The text field for the login id.
  29.      */
  30.     TextField login;
  31.  
  32.     /**
  33.      * The text field for the password.
  34.      */
  35.     TextField passwd;
  36.  
  37.     /**
  38.      * The text field for the name of the player.
  39.      */
  40.     TextField name;
  41.  
  42.     /**
  43.      * The text field for the e-mail address of the player.
  44.      */
  45.     TextField email;
  46.  
  47.     /**
  48.      * The panel for the buttons at the bottom of the dialog.
  49.      */
  50.     Panel buttons;
  51.  
  52.     /**
  53.      * The ok button.
  54.      */
  55.     Button ok;
  56.  
  57.     /** 
  58.      * Indicates which button has user focus.
  59.      */
  60.     int focus = 0;
  61.  
  62.     /**
  63.      * A referncing array for all the buttons.
  64.      */
  65.     Component[] fields;
  66.  
  67.     /**
  68.      * Construct a LoginWindow as a child of the given client applet.
  69.      * This constructor is used when first logging into the game.
  70.      * @param client the EuropaClient parent window
  71.      */
  72.     public LoginWindow(EuropaClient client) {
  73.         //super(null, "Authorization Required", true);
  74.         super("Authorization Required");
  75.         this.client = client;
  76.  
  77.         setupLogin();
  78.     }
  79.  
  80.     /**
  81.      * Construct a LoginWindow with the given PlayerInfo. This 
  82.      * version of the constructor is used to allow players
  83.      * to modify their login information.
  84.      * @param client the EuropaClient parent applet
  85.      * @param info the player info to initialize the dialog with
  86.      */
  87.     public LoginWindow(EuropaClient client, PlayerInfo info) {
  88.         //super(null, "Change Information", true);
  89.         super("Change Information");
  90.         this.client = client;
  91.  
  92.         setupChangeInfo(info);
  93.     }
  94.  
  95.     /**
  96.      * Set up the fields for a login dialog.
  97.      */
  98.     void setupLogin() {
  99.         // kludge: couldn't find a way to reset the size of the spacing for
  100.         // the borderlayout that we have by default.
  101.         setLayout(new BorderLayout(80, 10));
  102.  
  103.         if (info != null)
  104.             info.removeNotify();
  105.  
  106.         info = new Panel();
  107.         info.setLayout(new GridLayout(2, 2, 4, 4));
  108.         info.add(new Label("                login:", Label.RIGHT));
  109.         info.add(login = new TextField());
  110.         info.add(new Label("             password:", Label.RIGHT));
  111.         info.add(passwd = new TextField());
  112.         name = null;
  113.         email = null;
  114.         passwd.setEchoCharacter('*');
  115.         add("West", info);
  116.         add("North", new Panel());
  117.  
  118.         if (buttons != null)
  119.             buttons.removeNotify();
  120.  
  121.         buttons = new Panel();
  122.         Button cancel;
  123.         Button help;
  124.         buttons.setLayout(new FlowLayout(FlowLayout.CENTER, 20, 10));
  125.         buttons.add(ok = new Button("Login"));
  126.         buttons.add(cancel = new Button("Cancel"));
  127.         buttons.add(help = new Button("I'm new"));
  128.         add("South", buttons);
  129.  
  130.         if (fields != null) {
  131.             for (int i = 0; i < fields.length; i++)    
  132.                 fields[i].removeNotify();
  133.         }
  134.  
  135.         fields = new Component[5];
  136.         fields[0] = login;
  137.         fields[1] = passwd;
  138.         fields[2] = ok;
  139.         fields[3] = cancel;
  140.         fields[4] = help;
  141.         focus = 0;
  142.  
  143.         pack();
  144.  
  145.         login.requestFocus();
  146.     }
  147.  
  148.     /**
  149.      * Setup the dialog with new player components.
  150.      */
  151.     void setupPlayerInfo() {
  152.         // kludge: couldn't find a way to reset the size of the spacing for
  153.         // the borderlayout that we have by default.
  154.         setLayout(new BorderLayout(80, 10));
  155.  
  156.         if (info != null)
  157.             info.removeNotify();
  158.  
  159.         info = new Panel();
  160.         info.setLayout(new GridLayout(4, 2, 4, 4));
  161.         info.add(new Label("                login:", Label.RIGHT));
  162.         info.add(login = new TextField());
  163.         info.add(new Label("         new password:", Label.RIGHT));
  164.         info.add(passwd = new TextField());
  165.  
  166.         info.add(new Label("            real name:", Label.RIGHT));
  167.         info.add(name = new TextField());
  168.         info.add(new Label("    (optional) e-mail:", Label.RIGHT));
  169.         info.add(email = new TextField());
  170.  
  171.         passwd.setEchoCharacter('*');
  172.         add("West", info);
  173.         add("North", new Panel());
  174.  
  175.         if (buttons != null)
  176.             buttons.removeNotify();
  177.  
  178.         buttons = new Panel();
  179.         Button cancel;
  180.         Button help;
  181.         buttons.setLayout(new FlowLayout(FlowLayout.CENTER, 20, 10));
  182.         buttons.add(ok = new Button("Login"));
  183.         buttons.add(cancel = new Button("Cancel"));
  184.         buttons.add(help = new Button("Back"));
  185.         add("South", buttons);
  186.  
  187.         if (fields != null) {
  188.             for (int i = 0; i < fields.length; i++)    
  189.                 fields[i].removeNotify();
  190.         }
  191.  
  192.         fields = new Component[7];
  193.         fields[0] = login;
  194.         fields[1] = passwd;
  195.         fields[2] = name;
  196.         fields[3] = email;
  197.         fields[4] = ok;
  198.         fields[5] = cancel;
  199.         fields[6] = help;
  200.         focus = 0;
  201.  
  202.         pack();
  203.  
  204.         login.requestFocus();
  205.     }
  206.  
  207.     /**
  208.      * Set up the dialog with player change info components.
  209.      */
  210.     void setupChangeInfo(PlayerInfo id) {
  211.         // kludge: couldn't find a way to reset the size of the spacing for
  212.         // the borderlayout that we have by default.
  213.         setLayout(new BorderLayout(80, 10));
  214.  
  215.         if (info != null)
  216.             info.removeNotify();
  217.  
  218.         info = new Panel();
  219.         info.setLayout(new GridLayout(4, 2, 4, 4));
  220.         info.add(new Label("                login:", Label.RIGHT));
  221.         info.add(login = new TextField());
  222.         login.setEditable(false);
  223.         login.setText(id.getHandle());
  224.  
  225.         info.add(new Label("         new password:", Label.RIGHT));
  226.         info.add(passwd = new TextField());
  227.         passwd.setEchoCharacter('*');
  228.         passwd.setText(id.getPassword());
  229.  
  230.         info.add(new Label("            real name:", Label.RIGHT));
  231.         info.add(name = new TextField());
  232.         name.setText(id.getName());
  233.         info.add(new Label("    (optional) e-mail:", Label.RIGHT));
  234.         info.add(email = new TextField());
  235.         email.setText(id.getMail());
  236.  
  237.         add("West", info);
  238.         add("North", new Panel());
  239.  
  240.         if (buttons != null)
  241.             buttons.removeNotify();
  242.  
  243.         buttons = new Panel();
  244.         Button cancel;
  245.         Button help;
  246.         buttons.setLayout(new FlowLayout(FlowLayout.CENTER, 20, 10));
  247.         buttons.add(ok = new Button("Update"));
  248.         buttons.add(cancel = new Button("Don't Update"));
  249.         add("South", buttons);
  250.  
  251.         if (fields != null) {
  252.             for (int i = 0; i < fields.length; i++)    
  253.                 fields[i].removeNotify();
  254.         }
  255.  
  256.         fields = new Component[7];
  257.         fields[0] = login;
  258.         fields[1] = passwd;
  259.         fields[2] = name;
  260.         fields[3] = email;
  261.         fields[4] = ok;
  262.         fields[5] = cancel;
  263.         focus = 0;
  264.  
  265.         hide();
  266.         pack();
  267.         show();
  268.  
  269.         login.requestFocus();
  270.     }
  271.  
  272.     /**
  273.      * Process events in the dialog box.
  274.      */
  275.     public boolean handleEvent(Event e) {
  276.         boolean doneLogin = false;
  277.  
  278.         if (e.id == Event.WINDOW_DESTROY || e.arg == "Cancel") {
  279.             client.setPlayerInfo(null);
  280.             client.resume();
  281.             dispose();
  282.         }
  283.         else if (e.arg == "Don't Update") {
  284.             client.resume();
  285.             dispose();
  286.         }
  287.         else if (e.id == Event.KEY_RELEASE) {
  288.             if ((char)e.key == '\t' || (char)e.key == '\n') {
  289.                 focus++;
  290.                 focus %= fields.length;
  291.                 if (fields[focus] == ok && (char)e.key == '\n')
  292.                     doneLogin = true;
  293.                 fields[focus].requestFocus();
  294.             }
  295.         }
  296.         else if (e.arg == "Login" || e.arg == "Update") {
  297.             doneLogin = true;
  298.         }
  299.         else if (e.arg == "I'm new") {
  300.             setupPlayerInfo();
  301.         }
  302.         else if (e.arg == "Back") {
  303.             setupLogin();
  304.         }
  305.  
  306.         if (doneLogin) {
  307.             String realname;
  308.             String emailaddr;
  309.  
  310.             if (name != null)
  311.                 realname = name.getText();
  312.             else
  313.                 realname = "";
  314.  
  315.             if (email != null)
  316.                 emailaddr = email.getText();
  317.             else
  318.                 emailaddr = "";
  319.  
  320.             client.setPlayerInfo(new PlayerInfo(login.getText(), 
  321.                                                         realname, emailaddr,
  322.                                                         passwd.getText()));
  323.             client.resume();
  324.             dispose();
  325.         }
  326.         return super.handleEvent(e);
  327.     }
  328.  
  329.     /**
  330.      * Overload the layout method to pack() as well.
  331.      */
  332.     public synchronized void layout() {
  333.         pack();
  334.         super.layout();
  335.     }
  336. }
  337.