src/login/LoginDemo.java
author Matthias Mann
Thu Jun 23 22:43:25 2011 +0200 (11 months ago)
changeset 253 25120c47f789
parent 235 e042714142cb
child 260 42064e048ff4
permissions -rw-r--r--
refactored utility methods into TestUtils
     1 /*
     2  * Copyright (c) 2008-2011, 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.FPSCounter;
    36 import de.matthiasmann.twl.GUI;
    37 import de.matthiasmann.twl.Label;
    38 import de.matthiasmann.twl.Timer;
    39 import de.matthiasmann.twl.Widget;
    40 import de.matthiasmann.twl.renderer.lwjgl.LWJGLRenderer;
    41 import de.matthiasmann.twl.theme.ThemeManager;
    42 import org.lwjgl.opengl.Display;
    43 import org.lwjgl.opengl.DisplayMode;
    44 import org.lwjgl.opengl.GL11;
    45 import test.TestUtils;
    46 
    47 /**
    48  * A simple login panel
    49  * 
    50  * @author Matthias Mann
    51  */
    52 public class LoginDemo extends Widget {
    53     
    54     public static void main(String[] args) {
    55         try {
    56             Display.setDisplayMode(new DisplayMode(800, 600));
    57             Display.create();
    58             Display.setTitle("TWL Login Panel Demo");
    59             Display.setVSyncEnabled(true);
    60 
    61             LoginDemo demo = new LoginDemo();
    62             
    63             LWJGLRenderer renderer = new LWJGLRenderer();
    64             GUI gui = new GUI(demo, renderer);
    65 
    66             ThemeManager theme = ThemeManager.createThemeManager(
    67                     LoginDemo.class.getResource("login.xml"), renderer);
    68             gui.applyTheme(theme);
    69 
    70             while(!Display.isCloseRequested() && !demo.quit) {
    71                 GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
    72 
    73                 gui.update();
    74                 Display.update();
    75             }
    76 
    77             gui.destroy();
    78             theme.destroy();
    79         } catch (Exception ex) {
    80             TestUtils.showErrMsg(ex);
    81         }
    82         Display.destroy();
    83     }
    84 
    85     final FPSCounter fpsCounter;
    86     final DialogLayout loginPanel;
    87     final EditField efName;
    88     final EditField efPassword;
    89     final Button btnLogin;
    90     
    91     boolean quit;
    92 
    93     public LoginDemo() {
    94         fpsCounter = new FPSCounter();
    95         
    96         loginPanel = new DialogLayout();
    97         loginPanel.setTheme("login-panel");
    98         
    99         efName = new EditField();
   100         
   101         efPassword = new EditField();
   102         efPassword.setPasswordMasking(true);
   103         
   104         Label lName = new Label("Name");
   105         lName.setLabelFor(efName);
   106         
   107         Label lPassword = new Label("Password");
   108         lPassword.setLabelFor(efPassword);
   109         
   110         btnLogin = new Button("LOGIN");
   111         btnLogin.addCallback(new Runnable() {
   112             public void run() {
   113                 emulateLogin();
   114             }
   115         });
   116         
   117         DialogLayout.Group hLabels = loginPanel.createParallelGroup(lName, lPassword);
   118         DialogLayout.Group hFields = loginPanel.createParallelGroup(efName, efPassword);
   119         DialogLayout.Group hBtn = loginPanel.createSequentialGroup()
   120                 .addGap()   // right align the button by using a variable gap
   121                 .addWidget(btnLogin);
   122         
   123         loginPanel.setHorizontalGroup(loginPanel.createParallelGroup()
   124                 .addGroup(loginPanel.createSequentialGroup(hLabels, hFields))
   125                 .addGroup(hBtn));
   126         loginPanel.setVerticalGroup(loginPanel.createSequentialGroup()
   127                 .addGroup(loginPanel.createParallelGroup(lName, efName))
   128                 .addGroup(loginPanel.createParallelGroup(lPassword, efPassword))
   129                 .addWidget(btnLogin));
   130         
   131         add(fpsCounter);
   132         add(loginPanel);
   133     }
   134 
   135     @Override
   136     protected void layout() {
   137         // fpsCounter is bottom right
   138         fpsCounter.adjustSize();
   139         fpsCounter.setPosition(
   140                 getInnerRight() - fpsCounter.getWidth(),
   141                 getInnerBottom() - fpsCounter.getHeight());
   142         
   143         // login panel is centered
   144         loginPanel.adjustSize();
   145         loginPanel.setPosition(
   146                 getInnerX() + (getInnerWidth() - loginPanel.getWidth())/2,
   147                 getInnerY() + (getInnerHeight() - loginPanel.getHeight())/2);
   148     }
   149     
   150     void emulateLogin() {
   151         GUI gui = getGUI();
   152         if(gui != null) {
   153             // step 1: disable all controls
   154             efName.setEnabled(false);
   155             efPassword.setEnabled(false);
   156             btnLogin.setEnabled(false);
   157             
   158             // step 2: start a timer to simulate the process of talking to a remote server
   159             Timer timer = gui.createTimer();
   160             timer.setCallback(new Runnable() {
   161                 public void run() {
   162                     // once the timer fired re-enable the controls and clear the password
   163                     efName.setEnabled(true);
   164                     efPassword.setEnabled(true);
   165                     efPassword.setText("");
   166                     btnLogin.setEnabled(true);
   167                 }
   168             });
   169             timer.setDelay(2500);
   170             timer.start();
   171             
   172             /* NOTE: in a real app you would need to keep a reference to the timer object
   173              * to cancel it if the user closes the dialog which uses the timer.
   174              * @see Widget#beforeRemoveFromGUI(de.matthiasmann.twl.GUI)
   175              */
   176         }
   177     }
   178 }