001: /*
002: * GWT-Ext Widget Library
003: * Copyright(c) 2007-2008, GWT-Ext.
004: * licensing@gwt-ext.com
005: *
006: * http://www.gwt-ext.com/license
007: */
008: package com.gwtext.sample.showcase2.client.form;
009:
010: import com.gwtext.client.core.Connection;
011: import com.gwtext.client.core.EventObject;
012: import com.gwtext.client.core.Position;
013: import com.gwtext.client.data.*;
014: import com.gwtext.client.widgets.Button;
015: import com.gwtext.client.widgets.Panel;
016: import com.gwtext.client.widgets.event.ButtonListenerAdapter;
017: import com.gwtext.client.widgets.form.*;
018: import com.gwtext.sample.showcase2.client.SampleData;
019: import com.gwtext.sample.showcase2.client.ShowcasePanel;
020:
021: public class XmlFormSample extends ShowcasePanel {
022:
023: public String getSourceUrl() {
024: return "source/form/XmlFormSample.java.html";
025: }
026:
027: public String getXmlDataUrl() {
028: return "source/form/XmlFormSample.xml.html";
029: }
030:
031: public Panel getViewPanel() {
032: if (panel == null) {
033: panel = new Panel();
034:
035: //setup form data reader
036: RecordDef recordDef = new RecordDef(new FieldDef[] {
037: new StringFieldDef("first", "name/first"),
038: new StringFieldDef("last", "name/last"),
039: new StringFieldDef("company"),
040: new StringFieldDef("email"),
041: new StringFieldDef("state"),
042: new DateFieldDef("dob", "dob", "m/d/Y") });
043:
044: final XmlReader reader = new XmlReader("contact", recordDef);
045: reader.setSuccess("@success");
046:
047: //setup error reader to process from submit response from server
048: RecordDef errorRecordDef = new RecordDef(
049: new FieldDef[] { new StringFieldDef("id"),
050: new StringFieldDef("msg") });
051:
052: XmlReader errorReader = new XmlReader("field",
053: errorRecordDef);
054: errorReader.setSuccess("@success");
055:
056: final FormPanel formPanel = new FormPanel(Position.RIGHT);
057: formPanel.setFrame(true);
058: formPanel.setTitle("XML Form");
059: formPanel.setWidth(400);
060: formPanel.setLabelWidth(75);
061: //set reader and error reader
062: formPanel.setReader(reader);
063: formPanel.setErrorReader(errorReader);
064:
065: //add some fields
066: FieldSet fieldSet = new FieldSet("Contact Information");
067: fieldSet.add(new TextField("First Name", "first", 190));
068: fieldSet.add(new TextField("Last Name", "last", 190));
069: fieldSet.add(new TextField("Company", "company", 190));
070:
071: TextField email = new TextField("Email", "email", 190);
072: email.setVtype(VType.EMAIL);
073: fieldSet.add(email);
074:
075: //add a ComboBox field
076: Store store = new SimpleStore(new String[] { "abbr",
077: "state" }, SampleData.getStates());
078: store.load();
079:
080: ComboBox cb = new ComboBox();
081: cb.setFieldLabel("State");
082: cb.setHiddenName("state");
083: cb.setStore(store);
084: cb.setDisplayField("abbr");
085: cb.setTypeAhead(true);
086: cb.setMode(ComboBox.LOCAL);
087: cb.setTriggerAction(ComboBox.ALL);
088: cb.setEmptyText("Select a state...");
089: cb.setSelectOnFocus(true);
090: cb.setWidth(190);
091: fieldSet.add(cb);
092:
093: DateField dob = new DateField("Date of birth", "dob", 190);
094: dob.setAllowBlank(false);
095: fieldSet.add(dob);
096:
097: formPanel.add(fieldSet);
098:
099: final Button submitBtn = new Button("Submit",
100: new ButtonListenerAdapter() {
101: public void onClick(Button button, EventObject e) {
102: formPanel.getForm().submit(
103: "data/xml-errors.xml", null,
104: Connection.GET, "Saving Data...",
105: false);
106: }
107: });
108:
109: Button loadBtn = new Button("Load",
110: new ButtonListenerAdapter() {
111: public void onClick(Button button, EventObject e) {
112: formPanel.getForm().load(
113: "data/xml-form.xml", null,
114: Connection.GET, "Loading...");
115: submitBtn.enable();
116: }
117: });
118:
119: formPanel.addButton(loadBtn);
120: formPanel.addButton(submitBtn);
121: panel.add(formPanel);
122: }
123:
124: return panel;
125: }
126:
127: public String getIntro() {
128: return "<b>Loading/Saving a Form using XML</b>"
129: + "<p>"
130: + " This is a very simple example of using XML to load and submit Form data."
131: + "</p>"
132: + "<p>"
133: + " Click \"Load\" to load the dummy XML data from the server using an XmlReader."
134: + "</p>"
135: + "<p>"
136: + " After loading the form, you will be able to hit submit. The submit action will make a post to the server,"
137: + " and the dummy XML file on the server with test server-side validation failure messages will be sent back."
138: + " Those messages will be applied to the appropriate fields in the form."
139: + ""
140: + "</p>"
141: + "<p>"
142: + " Note: The built-in JSON support does not require any special readers for mapping. However, If you don't like the Form's built-in JSON format, you could also use a JsonReader for reading data into a form."
143: + "</p>";
144: }
145: }
|