Subversion Repositories eFlore/Archives.cel-v1

Rev

Rev 15 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

/*
 * Copyright 2006 Google 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.
 */
package org.tela_botanica.client;


import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONBoolean;
import com.google.gwt.json.client.JSONParser;
import com.google.gwt.json.client.JSONString;
import com.google.gwt.json.client.JSONValue;
import com.google.gwt.user.client.HTTPRequest;
import com.google.gwt.user.client.ResponseTextHandler;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.KeyboardListener;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class LoginDialog extends DialogBox {


  Mediator mediator=null;

  private String serviceBaseUrl = null;
  private TextBox login = new TextBox();
  private PasswordTextBox  password = new PasswordTextBox();
  private String user=null;
  
  
        
  public LoginDialog(final Mediator med) {
          
    setText("Connexion");

    mediator=med;

    mediator.registerLoginDialog(this);

    user=mediator.getUser();
  
    serviceBaseUrl = mediator.getServiceBaseUrl();

    
    VerticalPanel outer = new VerticalPanel();

    Grid inner = new Grid(3,2);
    
    
   
    HTML textLogin = new HTML("E-mail:");
    
    
    HTML textPassword = new HTML("Mot de passe:");
 
        
        HTML okButton=new HTML("Ok");
        okButton.setStyleName("html_button");
        okButton.addClickListener(
                new ClickListener() {
                        public void onClick(Widget sender) {
                                          loginFromService(login.getText(),password.getText());
                                hide();
                        }
                }
        );              

        
        
        HTML cancelButton=new HTML("Annuler");
        cancelButton.setStyleName("html_button");
        cancelButton.addClickListener(
                new ClickListener() {
                        public void onClick(Widget sender) {
                                                hide();
                        }
                }
        );              

        
          login.addKeyboardListener( new KeyboardListener() {

                  public void onKeyDown(Widget arg0, char arg1, int arg2) {
                          
                          
                          if(arg1 == KEY_ENTER)
                            {
                                  loginFromService(login.getText(),password.getText());
                                hide();
                            }

                  }
                  
                  public void onKeyUp(Widget arg0, char arg1, int arg2) {
                  }

                  public void onKeyPress(Widget arg0, char arg1, int arg2) {
                  }
                  
                  }
  );
        

                
          password.addKeyboardListener( new KeyboardListener() {

                  public void onKeyDown(Widget arg0, char arg1, int arg2) {
                          
                          
                          if(arg1 == KEY_ENTER)
                            {
                                  loginFromService(login.getText(),password.getText());
                                hide();
                            }

                  }
                  
                  public void onKeyUp(Widget arg0, char arg1, int arg2) {
                  }

                  public void onKeyPress(Widget arg0, char arg1, int arg2) {
                  }
                  
                  }
  );
        
          inner.setWidget(0,0,textLogin);
          inner.setWidget(0,1,login);
          inner.setWidget(1,0,textPassword);
          inner.setWidget(1,1,password);
          inner.setWidget(2,0,okButton);
          inner.setWidget(2,1,cancelButton);
         
          inner.setCellPadding(10);
          outer.add(inner);
          
         setWidget(outer);
            
  }
  
  
        /**
         * 
         */
        
        private void loginFromService(String login, String password) {


                HTTPRequest.asyncGet(serviceBaseUrl + "/User/" + login + "/" + password ,
                                new ResponseTextHandler() {

                                        public void onCompletion(String str) {
                                        
                                                
                                                JSONValue jsonValue = JSONParser.parse(str);
                                                JSONArray jsonArray;
                                                if ((jsonArray = jsonValue.isArray()) != null) {
                                                                user = ((JSONString) jsonArray.get(0)).stringValue();
                                                                mediator.setConnected(((JSONBoolean) jsonArray.get(1)).booleanValue());
                                                }
                                        
                                                if (mediator.getConnected()) {
                                                        mediator.onLogin(user);
                                                }
                                        }
                                });

        }
  
  
  public boolean onKeyDownPreview(char key, int modifiers) {
            // Use the popup's key preview hooks to close the dialog when either
            //  escape is pressed.
            switch (key) {
              case KeyboardListener.KEY_ESCAPE:
                hide();
                break;
            }

            return true;
  }

  

}