PDA

View Full Version : Add TextField or ComboBox depending on server answer



Saladir
6 Aug 2009, 5:34 AM
Hi,

i want to show a TextField or a ComboBox depending on a configuration on the Server. Therefor i created a service, that tells the client the configuration param. But nothing is show. I am a newbie to GWT, pls can somebody help me? Here is my Code:


package gui.client.widgets;

import gui.client.AppEvents;
import gui.client.BackclickGUI;
import gui.client.ServerRequestsAsync;
import gui.client.model.Mandator;
import com.extjs.gxt.ui.client.Registry;
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.KeyListener;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.mvc.Dispatcher;
import com.extjs.gxt.ui.client.util.IconHelper;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.Status;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.ComboBox;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
import com.extjs.gxt.ui.client.widget.toolbar.FillToolItem;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.rpc.AsyncCallback;

public class LoginDialog extends Dialog {

private TextField<String> userName;
private TextField<String> password;
private TextField<String> mandatorText = new TextField<String>();
private ComboBox<Mandator> mandatorCombo = null;

private Button reset;
private Button login;
private Status status;
private ServerRequestsAsync service;

public LoginDialog() {

FormLayout layout = new FormLayout();
layout.setLabelWidth(95);
layout.setDefaultWidth(195);
setLayout(layout);
setButtonAlign(HorizontalAlignment.LEFT);

setButtons("");
setClosable(false);
setDraggable(false);
setResizable(false);
setIcon(IconHelper.createStyle("user"));
setHeading("Login");
setModal(true);
setBodyBorder(true);
setBodyStyle("padding:12px;background:none");
setWidth(341);

final KeyListener keyListener = new KeyListener() {
public void componentKeyUp(ComponentEvent event) {
validate();
}

};

userName = new TextField<String>();
userName.setMinLength(3);
userName.setFieldLabel("Username");
userName.addKeyListener(keyListener);
add(userName);


password = new TextField<String>();
password.setMinLength(6);
password.setPassword(true);
password.setFieldLabel("Password");
password.addKeyListener(keyListener);
add(password);


service = (ServerRequestsAsync)Registry.get(SomeGUI.SERVICE);

service.getInitParameter("mandatorSelect", new AsyncCallback<String>() {
public void onFailure(Throwable caught) {
Dispatcher.forwardEvent(AppEvents.Error, caught);
}
public void onSuccess(String result) {
TextField<String> f = new TextField<String>();
f.setFieldLabel("Label");
add(f);
}
});
setFocusWidget(userName);
validate();
}

@Override
protected void createButtons() {
super.createButtons();
status = new Status();
status.setBusy("Please wait");
status.hide();
status.setAutoWidth(true);
getButtonBar().add(status);
getButtonBar().add(new FillToolItem());
getButtonBar().setMinButtonWidth(90);

reset = new Button("Reset");
reset.disable();
reset.addSelectionListener(new SelectionListener<ButtonEvent>() {
public void componentSelected(ButtonEvent ce) {
userName.reset();
password.reset();
validate();
userName.focus();
}

});

login = new Button("Login");
login.disable();
login.addSelectionListener(new SelectionListener<ButtonEvent>() {
public void componentSelected(ButtonEvent ce) {
onSubmit();
}
});

addButton(reset);
addButton(login);

}

private void onSubmit() {
status.show();
getButtonBar().disable();
Timer t = new Timer() {

@Override
public void run() {
LoginDialog.this.hide();
}

};
t.schedule(2000);
}

private boolean hasValue(TextField<String> field) {
return field.getValue() != null && field.getValue().length() > 0;
}

private void validate() {
login.setEnabled(hasValue(userName) && hasValue(password) && password.getValue().length() > 3);
}

}
I use AsyncCallBack. The answer is correct, but my TextField or ComboBox is not show.

regards

aumaster
6 Aug 2009, 4:04 PM
The solution is simple:

just call "doLayout()" after adding your components...