src/login/LoginDemo.java
author Matthias Mann
Sat Feb 11 13:27:12 2012 +0100 (11 days ago)
changeset 262 f896aec65113
parent 253 25120c47f789
permissions -rw-r--r--
allow mouse input for scrolling, don't update text area model when nothing has changed
     1 /*
     2  * Copyright (c) 2008-2012, Matthias Mann
     3  *
     4  * All rights reserved.
     5  *
     6  * Redistribution and use in source and binary forms, with or without
     7  * modification, are permitted provided that the following conditions are met:
     8  *
     9  *     * Redistributions of source code must retain the above copyright notice,
    10  *       this list of conditions and the following disclaimer.
    11  *     * Redistributions in binary form must reproduce the above copyright
    12  *       notice, this list of conditions and the following disclaimer in the
    13  *       documentation and/or other materials provided with the distribution.
    14  *     * Neither the name of Matthias Mann nor the names of its contributors may
    15  *       be used to endorse or promote products derived from this software
    16  *       without specific prior written permission.
    17  *
    18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
    22  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
    23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
    24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
    25  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
    26  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
    27  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
    28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    29  */
    30 package login;
    31 
    32 import de.matthiasmann.twl.Button;
    33 import de.matthiasmann.twl.DialogLayout;
    34 import de.matthiasmann.twl.EditField;
    35 import de.matthiasmann.twl.EditField.Callback;
    36 import de.matthiasmann.twl.Event;
    37 import de.matthiasmann.twl.FPSCounter;
    38 import de.matthiasmann.twl.GUI;
    39 import de.matthiasmann.twl.Label;
    40 import de.matthiasmann.twl.Timer;
    41 import de.matthiasmann.twl.Widget;
    42 import de.matthiasmann.twl.renderer.lwjgl.LWJGLRenderer;
    43 import de.matthiasmann.twl.theme.ThemeManager;
    44 import org.lwjgl.opengl.Display;
    45 import org.lwjgl.opengl.DisplayMode;
    46 import org.lwjgl.opengl.GL11;
    47 import test.TestUtils;
    48 
    49 /**
    50  * A simple login panel
    51  * 
    52  * @author Matthias Mann
    53  */
    54 public class LoginDemo extends Widget {
    55     
    56     public static void main(String[] args) {
    57         try {
    58             Display.setDisplayMode(new DisplayMode(800, 600));
    59             Display.create();
    60             Display.setTitle("TWL Login Panel Demo");
    61             Display.setVSyncEnabled(true);
    62 
    63             LoginDemo demo = new LoginDemo();
    64             
    65             LWJGLRenderer renderer = new LWJGLRenderer();
    66             GUI gui = new GUI(demo, renderer);
    67 
    68             demo.efName.requestKeyboardFocus();
    69             
    70             ThemeManager theme = ThemeManager.createThemeManager(
    71                     LoginDemo.class.getResource("login.xml"), renderer);
    72             gui.applyTheme(theme);
    73 
    74             while(!Display.isCloseRequested() && !demo.quit) {
    75                 GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
    76 
    77                 gui.update();
    78                 Display.update();
    79             }
    80 
    81             gui.destroy();
    82             theme.destroy();
    83         } catch (Exception ex) {
    84             TestUtils.showErrMsg(ex);
    85         }
    86         Display.destroy();
    87     }
    88 
    89     final FPSCounter fpsCounter;
    90     final DialogLayout loginPanel;
    91     final EditField efName;
    92     final EditField efPassword;
    93     final Button btnLogin;
    94     
    95     boolean quit;
    96 
    97     public LoginDemo() {
    98         fpsCounter = new FPSCounter();
    99         
   100         loginPanel = new DialogLayout();
   101         loginPanel.setTheme("login-panel");
   102         
   103         efName = new EditField();
   104         efName.addCallback(new Callback() {
   105             public void callback(int key) {
   106                 if(key == Event.KEY_RETURN) {
   107                     efPassword.requestKeyboardFocus();
   108                 }
   109             }
   110         });
   111         
   112         efPassword = new EditField();
   113         efPassword.setPasswordMasking(true);
   114         efPassword.addCallback(new Callback() {
   115             public void callback(int key) {
   116                 if(key == Event.KEY_RETURN) {
   117                     emulateLogin();
   118                 }
   119             }
   120         });
   121         
   122         Label lName = new Label("Name");
   123         lName.setLabelFor(efName);
   124         
   125         Label lPassword = new Label("Password");
   126         lPassword.setLabelFor(efPassword);
   127         
   128         btnLogin = new Button("LOGIN");
   129         btnLogin.addCallback(new Runnable() {
   130             public void run() {
   131                 emulateLogin();
   132             }
   133         });
   134         
   135         DialogLayout.Group hLabels = loginPanel.createParallelGroup(lName, lPassword);
   136         DialogLayout.Group hFields = loginPanel.createParallelGroup(efName, efPassword);
   137         DialogLayout.Group hBtn = loginPanel.createSequentialGroup()
   138                 .addGap()   // right align the button by using a variable gap
   139                 .addWidget(btnLogin);
   140         
   141         loginPanel.setHorizontalGroup(loginPanel.createParallelGroup()
   142                 .addGroup(loginPanel.createSequentialGroup(hLabels, hFields))
   143                 .addGroup(hBtn));
   144         loginPanel.setVerticalGroup(loginPanel.createSequentialGroup()
   145                 .addGroup(loginPanel.createParallelGroup(lName, efName))
   146                 .addGroup(loginPanel.createParallelGroup(lPassword, efPassword))
   147                 .addWidget(btnLogin));
   148         
   149         add(fpsCounter);
   150         add(loginPanel);
   151     }
   152 
   153     @Override
   154     protected void layout() {
   155         // fpsCounter is bottom right
   156         fpsCounter.adjustSize();
   157         fpsCounter.setPosition(
   158                 getInnerRight() - fpsCounter.getWidth(),
   159                 getInnerBottom() - fpsCounter.getHeight());
   160         
   161         // login panel is centered
   162         loginPanel.adjustSize();
   163         loginPanel.setPosition(
   164                 getInnerX() + (getInnerWidth() - loginPanel.getWidth())/2,
   165                 getInnerY() + (getInnerHeight() - loginPanel.getHeight())/2);
   166     }
   167     
   168     void emulateLogin() {
   169         GUI gui = getGUI();
   170         if(gui != null) {
   171             // step 1: disable all controls
   172             efName.setEnabled(false);
   173             efPassword.setEnabled(false);
   174             btnLogin.setEnabled(false);
   175             
   176             // step 2: get name & password from UI
   177             String name = efName.getText();
   178             String pasword = efPassword.getText();
   179             System.out.println("Name: " + name + " with a " + pasword.length() + " character password");
   180             
   181             // step 3: start a timer to simulate the process of talking to a remote server
   182             Timer timer = gui.createTimer();
   183             timer.setCallback(new Runnable() {
   184                 public void run() {
   185                     // once the timer fired re-enable the controls and clear the password
   186                     efName.setEnabled(true);
   187                     efPassword.setEnabled(true);
   188                     efPassword.setText("");
   189                     efPassword.requestKeyboardFocus();
   190                     btnLogin.setEnabled(true);
   191                 }
   192             });
   193             timer.setDelay(2500);
   194             timer.start();
   195             
   196             /* NOTE: in a real app you would need to keep a reference to the timer object
   197              * to cancel it if the user closes the dialog which uses the timer.
   198              * @see Widget#beforeRemoveFromGUI(de.matthiasmann.twl.GUI)
   199              */
   200         }
   201     }
   202 }