package org.drools.guvnor.client;
/*
 * Copyright 2005 JBoss Inc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */



import org.drools.guvnor.client.common.FormStylePopup;
import org.drools.guvnor.client.common.GenericCallback;
import org.drools.guvnor.client.common.LoadingPopup;
import org.drools.guvnor.client.rpc.RepositoryServiceFactory;
import org.drools.guvnor.client.messages.Constants;

import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.ui.*;
import com.google.gwt.core.client.GWT;

/**
 * Used for logging in, obviously !
 *
 * @author Michael Neale
 */
public class LoginWidget {

    Constants messages = GWT.create(Constants.class);

	private Command loggedInEvent;
	private String userNameLoggedIn;

	public void show() {
		final FormStylePopup pop = new FormStylePopup("images/login.gif", messages.Login());

		final TextBox userName = new TextBox();
		pop.addAttribute(messages.UserName(), userName);

		final PasswordTextBox password = new PasswordTextBox();
		pop.addAttribute(messages.Password(), password);

        KeyboardListener kl = new KeyboardListenerAdapter() {
            @Override
            public void onKeyUp(Widget sender, char keyCode, int modifiers) {
                if (keyCode == KeyboardListener.KEY_ENTER) {
                    doLogin(userName, password, pop);
                }
            }
        };

        userName.addKeyboardListener(kl);
        password.addKeyboardListener(kl);
		Button b = new Button(messages.OK());
		b.addClickListener(new ClickListener() {
			public void onClick(Widget arg0) {
                doLogin(userName, password, pop);
			}
		});
		pop.addAttribute("", b);
		pop.show();

        pop.setAfterShow(new Command() {
            public void execute() {
                userName.setFocus(true);
            }
        });
	}

    private void doLogin(final TextBox userName, PasswordTextBox password, final FormStylePopup pop) {
        LoadingPopup.showMessage(messages.Authenticating());
        RepositoryServiceFactory.login( userName.getText(), password.getText(), new GenericCallback() {
            public void onSuccess(Object o) {
                userNameLoggedIn = userName.getText();
                LoadingPopup.close();
                Boolean success = (Boolean) o;
                if (!success.booleanValue()) {
                    com.google.gwt.user.client.Window.alert(messages.IncorrectUsernameOrPassword());
                } else {
                    loggedInEvent.execute();
                    pop.hide();
                }
            }
        });
    }


    /**
     * Return the name that was entered.
     */
    public String getUserName() {
        return userNameLoggedIn;
    }

    /**
     * This is required for a callback after a successful login.
     */
    public void setLoggedInEvent(Command loggedInEvent) {
        this.loggedInEvent = loggedInEvent;
    }


}
@@ -986,16 +986,160 @@
 t.ui.*;%0A
+import com.gwtext.client.core.EventObject;%0Aimport com.gwtext.client.widgets.event.KeyListener;%0Aimport com.gwtext.client.widgets.form.TextField;%0A
 import c
@@ -1506,35 +1506,37 @@
 );%0A%0A%09%09final Text
-Box
+Field
  userName = new 
@@ -1535,28 +1535,40 @@
 e = new Text
-Box(
+Field(%22username%22
 );%0A%09%09pop.add
@@ -1618,31 +1618,25 @@
 %09%09final 
-PasswordTextBox
+TextField
  passwor
@@ -1647,24 +1647,59 @@
 new 
-PasswordTextBox(
+TextField(%22password%22 );%0A%09%09password.setPassword(true
 );%0A%09
@@ -1756,29 +1756,24 @@
 %0A        Key
-board
 Listener kl 
@@ -1781,29 +1781,24 @@
  new Key
-board
 Listener
 Adapter(
@@ -1793,15 +1793,8 @@
 ener
-Adapter
 () %7B
@@ -1808,125 +1808,64 @@
     
-  @Override%0A            public void onKeyUp(Widget sender, char keyCode, int modifiers) %7B%0A                
+%09public void onKey(int key, EventObject e) %7B%0A%09%09%09%09
 if (key
-Code
  == 
@@ -1958,32 +1958,37 @@
               %7D%0A
+%09%09%09%09%0A
             %7D%0A  
@@ -2013,24 +2013,36 @@
 userName.add
+KeyListener(
 KeyboardList
@@ -2041,25 +2041,36 @@
 oardListener
-(
+.KEY_ENTER, 
 kl);%0A       
@@ -2082,16 +2082,28 @@
 word.add
+KeyListener(
 Keyboard
@@ -2114,14 +2114,26 @@
 ener
-(
+.KEY_ENTER, 
 kl);%0A
+%0A
 %09%09Bu
@@ -2168,16 +2168,17 @@
 .OK());%0A
+%0A
 %09%09b.addC
@@ -2311,16 +2311,19 @@
 %7D%0A%09%09%7D);%0A
+%09%09%0A
 %09%09pop.ad
@@ -2345,21 +2345,14 @@
 b);%0A
-%09%09pop.show();
+    //
 %0A%0A  
@@ -2455,12 +2455,9 @@
 ame.
-setF
+f
 ocus
@@ -2461,18 +2461,56 @@
 cus(true
-);
+,100);                    %0A            %09
 %0A       
@@ -2528,16 +2528,33 @@
     %7D);%0A
+%09%09pop.show();%0A%09%09%0A
 %09%7D%0A%0A    
@@ -2584,19 +2584,21 @@
 nal Text
-Box
+Field
  userNam
@@ -2604,23 +2604,17 @@
 me, 
-PasswordTextBox
+TextField
  pas

Set focus on 'user name' in the login screen