PDA

View Full Version : z



hiinsu
25 Jun 2010, 9:52 AM
Hi Sven,

WebSite which I'm working on has lots and lots of forms.
so, I drafted a number of methods, classes, etc...
it seems to work.. but I wanted to know the drawbacks..

i included drafted code below. to give you the idea of what i'm having

below code should generate a form panel
had adds form fields defined in json feed

please reply me with your opinion/comments



// sample json feed
__gwt_jsonp__.I4.onSuccess({
id : 4,
method : "executed procedure",
model:[
{ type:"combo", label:"combobox field", url:"testdata/comboboitem.json"},
{ type:"text", label:"text field", url:null},
{ type:"label", label:"Label field", url:null},
],
result:{
"combobox-field" : "This is where the value goes",
"text-field" : "variable name in json is simply label replace all space to - and to lower case",
"label-field" : "label field for items that should not be editable"
},
error : null
});

public class JSONData extends JavaScriptObject {

protected JSONData() {
}

public final int getId() {
return (int) new JSONSObject(this).get("id").isNumber().doubleValue();
}

public final String getMethod(){
return return new JSONObject(this).get("method").isString().toString.replaceAll("\"","");
}

public final JSONObject getResult() {
return new JSONObject(this).get("result").isObject();
}

public final JSONObject getError() {
return new JSONObject(this).get("error").isObject();
}
}

public class FormHelper {

private final static String LOADING_TEXT = "";

public static FormPanel createForm(String url, int labelWidth){
FormLayout layout = new FormLayout();
if(labelWidth>0){layout.setLabelWidth(labelWidth);}
layout.setLabelAlign(FormPanel.LabelAlign.RIGHT);

FormPanel form = new FormPanel();
form.setScrollMode(Style.Scroll.AUTO);
form.setLayout(layout);
form.setStyleAttribute("background-color", "#DFE8F6");
form.setBodyBorder(false);
form.getHeader().setBorders(false);
form.setCollapsible(false);
form.setId(url);
addTools(form, url);
return form;
}

public static TextField<String> textField(String id, String label){
TextField<String> field = new TextField<String>();
field.setId(id);
field.setName(id);
field.setFieldLabel(label);
field.setLabelStyle("font-weight:bold;");
return field;
}

public static LabelField labelField(String id, String label){
LabelField field = new LabelField(LOADING_TEXT);
field.setId(id);
field.setName(id);
field.setFieldLabel(label);
field.setLabelStyle("font-weight:bold;");
return field;
}

public static ComboBox<ModelData> comboBoxField(String id, String label, String url){
ModelType type = new ModelType();
type.setRoot("result");
type.addField("item", "item");

ScriptTagProxy<ListLoadResult<ModelData>> proxy =
new ScriptTagProxy<ListLoadResult<ModelData>>(url);
JsonLoadResultReader<ListLoadResult<ModelData>> reader =
new JsonLoadResultReader<ListLoadResult<ModelData>>(type);
BaseListLoader<ListLoadResult<ModelData>> loader =
new BaseListLoader<ListLoadResult<ModelData>>(proxy, reader);
ListStore<ModelData> store = new ListStore<ModelData>(loader);

ComboBox<ModelData> combo = new ComboBox<ModelData>();
combo.setId(id);
combo.setName(id);
combo.setFieldLabel(label);
combo.setLabelStyle("font-weight:bold;");
combo.setForceSelection(true);
combo.setTriggerAction(TriggerAction.ALL);
combo.setAllowBlank(false);
combo.setAutoValidate(true);
combo.setDisplayField("item");
combo.setStore(store);

return combo;
}

//etc...

public static void load(final FormPanel form, String url){
JsonpRequestBuilder jsonp = new JsonpRequestBuilder();
jsonp.requestObject(GWT.getHostPageBaseURL() + url, new AsyncCallback<JSONData>() {
@Override
public void onFailure(Throwable caught) {
System.out.println("load failed : " + form.getHeading());
}

@Override
public void onSuccess(JSONData data) {
//TODO Error handling
setTitle(form, data.getMethod());
addField(form, data.getModel());
show(form, data.getResult());
}
});
}

// etc...
}