1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
| package com.example.FameuxBinome.client;
import com.example.FameuxBinome.shared.FieldVerifier;
/**
* Entry point classes define <code>onModuleLoad()</code>.
*/
public class FameuxBinome implements EntryPoint {
/**
* The message displayed to the user when the server cannot be reached or
* returns an error.
*/
private static final String SERVER_ERROR = "An error occurred while "
+ "attempting to contact the server. Please check your network "
+ "connection and try again.";
/**
* Create a remote service proxy to talk to the server-side Greeting service.
*/
private final GreetingServiceAsync greetingService = GWT
.create(GreetingService.class);
/**
* This is the entry point method.
*/
public void onModuleLoad() {
final Label errorLabel = new Label();
// Use RootPanel.get() to get the entire body element
RootPanel.get("errorLabelContainer").add(errorLabel);
final Button btnEnvoyer = new Button("envoyer");
RootPanel.get("nameFieldContainer").add(btnEnvoyer, 247, 231);
Button button = new Button("annuler");
RootPanel.get("annuler").add(button, 335, 231);
button.setSize("62px", "30px");
final TextBox txtbxUserLogin = new TextBox();
txtbxUserLogin.setText("User login");
RootPanel.get("userlog").add(txtbxUserLogin, 159, 56);
txtbxUserLogin.setSize("150px", "23px");
final PasswordTextBox txtbxUserPassword = new PasswordTextBox();
txtbxUserPassword.setText("user password");
RootPanel.get("userpass").add(txtbxUserPassword, 159, 106);
txtbxUserPassword.setSize("150px", "23px");
Label lblLogin = new Label("Login");
RootPanel.get("log").add(lblLogin, 42, 73);
Label lblPassword = new Label("Password");
RootPanel.get("pass").add(lblPassword, 42, 123);
// Create the popup dialog box
final DialogBox dialogBox = new DialogBox();
dialogBox.setText("Remote Procedure Call");
dialogBox.setAnimationEnabled(true);
final Button closeButton = new Button("Close");
// We can set the id of a widget by accessing its Element
closeButton.getElement().setId("closeButton");
final Label textToServerLabel = new Label();
final HTML serverResponseLabel = new HTML();
VerticalPanel dialogVPanel = new VerticalPanel();
dialogVPanel.addStyleName("dialogVPanel");
dialogVPanel.add(new HTML("<b>Sending name to the server:</b>"));
dialogVPanel.add(textToServerLabel);
dialogVPanel.add(new HTML("<br><b>Server replies:</b>"));
dialogVPanel.add(serverResponseLabel);
dialogVPanel.setHorizontalAlignment(VerticalPanel.ALIGN_RIGHT);
dialogVPanel.add(closeButton);
dialogBox.setWidget(dialogVPanel);
// Add a handler to close the DialogBox
closeButton.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
dialogBox.hide();
btnEnvoyer.setEnabled(true);
btnEnvoyer.setFocus(true);
}
});
// Create a handler for the sendButton and nameField
class MyHandler implements ClickHandler, KeyUpHandler {
/**
* Fired when the user clicks on the sendButton.
*/
public void onClick(ClickEvent event) {
sendNameToServer();
}
/**
* Fired when the user types in the nameField.
*/
public void onKeyUp(KeyUpEvent event) {
if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
sendNameToServer();
}
}
/**
* Send the name from the nameField to the server and wait for a response.
*/
private void sendNameToServer() {
// First, we validate the input.
errorLabel.setText("");
String textToServerLog = txtbxUserLogin.getText();
String textToServerPass = txtbxUserPassword.getText();
if (!FieldVerifier.isValidName(textToServerLog)|| !FieldVerifier.isValidName(textToServerPass) ) {
errorLabel.setText("Please enter at least four characters");
return;
}
// Then, we send the input to the server.
btnEnvoyer.setEnabled(false);
textToServerLabel.setText(textToServerLog);
serverResponseLabel.setText("");
greetingService.greetServer(textToServerLog,textToServerPass,
new AsyncCallback<String>() {
public void onFailure(Throwable caught) {
// Show the RPC error message to the user
dialogBox
.setText("Remote Procedure Call - Failure");
serverResponseLabel
.addStyleName("serverResponseLabelError");
serverResponseLabel.setHTML(SERVER_ERROR);
dialogBox.center();
closeButton.setFocus(true);
}
public void onSuccess(String result) {
dialogBox.setText("Remote Procedure Call");
serverResponseLabel
.removeStyleName("serverResponseLabelError");
serverResponseLabel.setHTML(result);
dialogBox.center();
closeButton.setFocus(true);
}
});
}
}
// Add a handler to send the name to the server
MyHandler handler = new MyHandler();
btnEnvoyer.addClickHandler(handler);
}
} |
Partager