Subversion Repositories eFlore/Archives.cel-v1

Rev

Rev 15 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
14 ddelon 1
/*
2
 * Copyright 2006 Google Inc.
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5
 * use this file except in compliance with the License. You may obtain a copy of
6
 * the License at
7
 *
8
 * http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13
 * License for the specific language governing permissions and limitations under
14
 * the License.
15
 */
16
package org.tela_botanica.client;
17
 
18
 
19
import com.google.gwt.json.client.JSONArray;
20
import com.google.gwt.json.client.JSONBoolean;
21
import com.google.gwt.json.client.JSONParser;
22
import com.google.gwt.json.client.JSONString;
23
import com.google.gwt.json.client.JSONValue;
24
import com.google.gwt.user.client.HTTPRequest;
25
import com.google.gwt.user.client.ResponseTextHandler;
26
import com.google.gwt.user.client.ui.ClickListener;
27
import com.google.gwt.user.client.ui.DialogBox;
28
import com.google.gwt.user.client.ui.Grid;
29
import com.google.gwt.user.client.ui.HTML;
30
import com.google.gwt.user.client.ui.KeyboardListener;
31
import com.google.gwt.user.client.ui.PasswordTextBox;
32
import com.google.gwt.user.client.ui.TextBox;
33
import com.google.gwt.user.client.ui.VerticalPanel;
34
import com.google.gwt.user.client.ui.Widget;
35
 
36
public class LoginDialog extends DialogBox {
37
 
38
 
39
  Mediator mediator=null;
40
 
41
  private String serviceBaseUrl = null;
42
  private TextBox login = new TextBox();
43
  private PasswordTextBox  password = new PasswordTextBox();
44
  private String user=null;
45
 
46
 
47
 
48
  public LoginDialog(final Mediator med) {
49
 
50
    setText("Connexion");
51
 
52
    mediator=med;
53
 
54
    mediator.registerLoginDialog(this);
55
 
56
    user=mediator.getUser();
57
 
58
    serviceBaseUrl = mediator.getServiceBaseUrl();
59
 
60
 
61
    VerticalPanel outer = new VerticalPanel();
62
 
63
    Grid inner = new Grid(3,2);
64
 
65
 
66
 
67
    HTML textLogin = new HTML("E-mail:");
68
 
69
 
70
    HTML textPassword = new HTML("Mot de passe:");
71
 
72
 
73
	HTML okButton=new HTML("Ok");
74
	okButton.setStyleName("html_button");
75
	okButton.addClickListener(
76
	    	new ClickListener() {
77
	    		public void onClick(Widget sender) {
78
					  loginFromService(login.getText(),password.getText());
79
			        hide();
80
	    		}
81
	     	}
82
	);
83
 
84
 
85
 
86
	HTML cancelButton=new HTML("Annuler");
87
	cancelButton.setStyleName("html_button");
88
	cancelButton.addClickListener(
89
	    	new ClickListener() {
90
	    		public void onClick(Widget sender) {
91
					        hide();
92
	    		}
93
	     	}
94
	);
95
 
96
 
97
	  login.addKeyboardListener( new KeyboardListener() {
98
 
99
		  public void onKeyDown(Widget arg0, char arg1, int arg2) {
100
 
101
 
102
			  if(arg1 == KEY_ENTER)
103
			    {
104
				  loginFromService(login.getText(),password.getText());
105
			        hide();
106
			    }
107
 
108
		  }
109
 
110
		  public void onKeyUp(Widget arg0, char arg1, int arg2) {
111
		  }
112
 
113
		  public void onKeyPress(Widget arg0, char arg1, int arg2) {
114
		  }
115
 
116
		  }
117
  );
118
 
119
 
120
 
121
	  password.addKeyboardListener( new KeyboardListener() {
122
 
123
		  public void onKeyDown(Widget arg0, char arg1, int arg2) {
124
 
125
 
126
			  if(arg1 == KEY_ENTER)
127
			    {
128
				  loginFromService(login.getText(),password.getText());
129
			        hide();
130
			    }
131
 
132
		  }
133
 
134
		  public void onKeyUp(Widget arg0, char arg1, int arg2) {
135
		  }
136
 
137
		  public void onKeyPress(Widget arg0, char arg1, int arg2) {
138
		  }
139
 
140
		  }
141
  );
142
 
143
	  inner.setWidget(0,0,textLogin);
144
	  inner.setWidget(0,1,login);
145
	  inner.setWidget(1,0,textPassword);
146
	  inner.setWidget(1,1,password);
147
	  inner.setWidget(2,0,okButton);
148
	  inner.setWidget(2,1,cancelButton);
149
 
150
	  inner.setCellPadding(10);
151
	  outer.add(inner);
152
 
153
	 setWidget(outer);
154
 
155
  }
156
 
157
 
158
	/**
159
	 *
160
	 */
161
 
162
	private void loginFromService(String login, String password) {
163
 
164
 
165
		HTTPRequest.asyncGet(serviceBaseUrl + "/User/" + login + "/" + password ,
166
				new ResponseTextHandler() {
167
 
168
					public void onCompletion(String str) {
169
 
170
 
171
						JSONValue jsonValue = JSONParser.parse(str);
172
						JSONArray jsonArray;
173
						if ((jsonArray = jsonValue.isArray()) != null) {
174
								user = ((JSONString) jsonArray.get(0)).stringValue();
175
								mediator.setConnected(((JSONBoolean) jsonArray.get(1)).booleanValue());
176
						}
177
 
178
						if (mediator.getConnected()) {
179
							mediator.onLogin(user);
180
						}
181
					}
182
				});
183
 
184
	}
185
 
186
 
187
  public boolean onKeyDownPreview(char key, int modifiers) {
188
	    // Use the popup's key preview hooks to close the dialog when either
189
	    //  escape is pressed.
190
	    switch (key) {
191
	      case KeyboardListener.KEY_ESCAPE:
192
	        hide();
193
	        break;
194
	    }
195
 
196
	    return true;
197
  }
198
 
199
 
200
 
201
}