package org.gwtoolbox.sample.widget.client.form;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.*;
import org.gwtoolbox.ioc.core.client.annotation.Component;
import org.gwtoolbox.ioc.core.client.annotation.Order;
import org.gwtoolbox.sample.widget.client.SamplePanel;
import org.gwtoolbox.widget.client.button.SimpleButton;
import org.gwtoolbox.widget.client.data.Record;
import org.gwtoolbox.widget.client.form.Form;
import org.gwtoolbox.widget.client.form.SubmitHandler;
import org.gwtoolbox.widget.client.form.field.LongTextField;
import org.gwtoolbox.widget.client.form.field.ShortTextField;
import org.gwtoolbox.widget.client.panel.LayoutUtils;
import java.util.Map;
import static org.gwtoolbox.widget.client.form.validation.Validators.notEmpty;
import static org.gwtoolbox.widget.client.form.validation.Validators.notNull;
import static org.gwtoolbox.widget.client.panel.LayoutUtils.addGap;
/**
* @author Uri Boness
*/
@Component
@Order(1)
@FormSample
public class HtmlForm extends Composite implements SamplePanel {
public HtmlForm() {
ShortTextField toField = new ShortTextField("to", "To:");
toField.setValidators(notNull(), notEmpty());
ShortTextField subjectField = new ShortTextField("subject", "Subject:");
subjectField.setValidators(notNull(), notEmpty());
LongTextField bodyField = new LongTextField("body", "Body:");
bodyField.setValidators(notNull(), notEmpty());
final Form form = new Form();
form.addFields(toField, subjectField, bodyField);
EmailFormRenderer renderer = GWT.create(EmailFormRenderer.class);
Widget formPanel = renderer.render(form);
formPanel.setWidth("300px");
SimpleButton validateButton = new SimpleButton("Validate", new ClickHandler() {
public void onClick(ClickEvent event) {
form.submit(new SubmitHandler.Adapter() {
public void success(Record record) {
StringBuilder sb = new StringBuilder();
for (Map.Entry<String, Object> entry : record.getAllValues().entrySet()) {
sb.append(entry.getKey()).append(" = ").append(entry.getValue()).append("\n");
}
Window.alert(sb.toString());
}
});
}
});
SimpleButton sendButton = new SimpleButton("Send", new ClickHandler() {
public void onClick(ClickEvent event) {
form.submit(new SubmitHandler.Adapter() {
public void success(Record record) {
StringBuilder sb = new StringBuilder();
for (Map.Entry<String, Object> entry : record.getAllValues().entrySet()) {
sb.append(entry.getKey()).append(" = ").append(entry.getValue()).append("\n");
}
Window.alert(sb.toString());
}
});
}
});
SimpleButton clearButton = new SimpleButton("Clear", new ClickHandler() {
public void onClick(ClickEvent event) {
form.reset();
}
});
HorizontalPanel buttons = new HorizontalPanel();
buttons.add(validateButton);
addGap(buttons, "10px");
buttons.add(sendButton);
addGap(buttons, "10px");
buttons.add(clearButton);
VerticalPanel vp = new VerticalPanel();
vp.add(formPanel);
LayoutUtils.addGap(vp, "10px");
vp.add(buttons);
vp.setCellHorizontalAlignment(buttons, VerticalPanel.ALIGN_CENTER);
SimplePanel sp = new SimplePanel();
sp.setWidth("320px");
sp.setWidget(vp);
DOM.setStyleAttribute(sp.getElement(), "padding", "10px");
DOM.setStyleAttribute(sp.getElement(), "backgroundColor", "#cfe4fb");
SimplePanel main = new SimplePanel();
main.setWidget(sp);
initWidget(main);
}
public String getName() {
return "Html Form";
}
public Widget getContentWidget() {
return this;
}
public void reset() {
}
}