allow mouse input for scrolling, don't update text area model when nothing has changed
2 * Copyright (c) 2008-2012, Matthias Mann
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
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.
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.
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;
50 * A simple login panel
52 * @author Matthias Mann
54 public class LoginDemo extends Widget {
56 public static void main(String[] args) {
58 Display.setDisplayMode(new DisplayMode(800, 600));
60 Display.setTitle("TWL Login Panel Demo");
61 Display.setVSyncEnabled(true);
63 LoginDemo demo = new LoginDemo();
65 LWJGLRenderer renderer = new LWJGLRenderer();
66 GUI gui = new GUI(demo, renderer);
68 demo.efName.requestKeyboardFocus();
70 ThemeManager theme = ThemeManager.createThemeManager(
71 LoginDemo.class.getResource("login.xml"), renderer);
72 gui.applyTheme(theme);
74 while(!Display.isCloseRequested() && !demo.quit) {
75 GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
83 } catch (Exception ex) {
84 TestUtils.showErrMsg(ex);
89 final FPSCounter fpsCounter;
90 final DialogLayout loginPanel;
91 final EditField efName;
92 final EditField efPassword;
93 final Button btnLogin;
98 fpsCounter = new FPSCounter();
100 loginPanel = new DialogLayout();
101 loginPanel.setTheme("login-panel");
103 efName = new EditField();
104 efName.addCallback(new Callback() {
105 public void callback(int key) {
106 if(key == Event.KEY_RETURN) {
107 efPassword.requestKeyboardFocus();
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) {
122 Label lName = new Label("Name");
123 lName.setLabelFor(efName);
125 Label lPassword = new Label("Password");
126 lPassword.setLabelFor(efPassword);
128 btnLogin = new Button("LOGIN");
129 btnLogin.addCallback(new Runnable() {
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);
141 loginPanel.setHorizontalGroup(loginPanel.createParallelGroup()
142 .addGroup(loginPanel.createSequentialGroup(hLabels, hFields))
144 loginPanel.setVerticalGroup(loginPanel.createSequentialGroup()
145 .addGroup(loginPanel.createParallelGroup(lName, efName))
146 .addGroup(loginPanel.createParallelGroup(lPassword, efPassword))
147 .addWidget(btnLogin));
154 protected void layout() {
155 // fpsCounter is bottom right
156 fpsCounter.adjustSize();
157 fpsCounter.setPosition(
158 getInnerRight() - fpsCounter.getWidth(),
159 getInnerBottom() - fpsCounter.getHeight());
161 // login panel is centered
162 loginPanel.adjustSize();
163 loginPanel.setPosition(
164 getInnerX() + (getInnerWidth() - loginPanel.getWidth())/2,
165 getInnerY() + (getInnerHeight() - loginPanel.getHeight())/2);
168 void emulateLogin() {
171 // step 1: disable all controls
172 efName.setEnabled(false);
173 efPassword.setEnabled(false);
174 btnLogin.setEnabled(false);
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");
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() {
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);
193 timer.setDelay(2500);
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)