Hi everybody,
could you please have a look at my code and let me know if I'm following the
right path. I think the idea is pretty good but I'm not sure if this is also
good for a big web application.
The idea is the following:
If the user selects a menu entry to view a grid, I first get the data structure
of the grid and after that I actually create a new DataSource which is used
for the grid.
Are there other / better ways to do something like that?
I'm greatful for every advice.
Thanks
Phillip aka Lippo
could you please have a look at my code and let me know if I'm following the
right path. I think the idea is pretty good but I'm not sure if this is also
good for a big web application.
The idea is the following:
If the user selects a menu entry to view a grid, I first get the data structure
of the grid and after that I actually create a new DataSource which is used
for the grid.
Are there other / better ways to do something like that?
I'm greatful for every advice.
Thanks
Phillip aka Lippo
Code:
package de.first.client.Grids;
import java.util.HashMap;
import com.google.gwt.event.shared.HasHandlers;
import com.smartgwt.client.data.DSCallback;
import com.smartgwt.client.data.DSRequest;
import com.smartgwt.client.data.DSResponse;
import com.smartgwt.client.data.RestDataSource;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.events.CellDoubleClickEvent;
import com.smartgwt.client.widgets.grid.events.CellDoubleClickHandler;
import de.first.client.SelectedData;
import de.first.client.DocFlow;
import de.first.client.PanelFactory;
import de.first.client.WorkPanel;
import de.first.client.data.DynamicDataSource;
import de.first.client.forms.SysUserEdit;
public class SystemGroupGrid extends WorkPanel implements HasHandlers {
HashMap<Integer, SelectedData> ht = new HashMap<Integer, SelectedData>();
ListGrid sysGroupGrid;
private RestDataSource restDS;
Canvas topCanvas = new Canvas();
//Canvas canvas = new Canvas();
public static class Factory implements PanelFactory {
private String id;
public Canvas create() {
final SystemGroupGrid panel = new SystemGroupGrid();
id = panel.getID();
return panel;
}
public String getID() {
return id;
}
public String getDescription() {
return "";
}
}
public Canvas getViewPanel() {
Canvas canvas = new Canvas();
// ====================================================================
// Get grid structure by first REST request
// ====================================================================
RestDataSource dataStructureDS;
dataStructureDS = new RestDataSource();
dataStructureDS.setDataURL("data/dataIntegration/xml/responses/SysUser_rest.php");
HashMap<String, String> params = new HashMap<String, String>();
params.put("_tablename", "GridLayout");
params.put("_sourcetable", "SysGroups");
DSRequest dsrequest = new DSRequest();
dsrequest.setParams(params);
dataStructureDS.setRequestProperties(dsrequest);
// ====================================================================
// Start request
// ====================================================================
dataStructureDS.fetchData(null, new DSCallback(){
// ====================================================================
// Wait for response
// ====================================================================
public void execute(DSResponse response, Object rawData, DSRequest request) {
// ====================================================================
// Create structure for the grid
// ====================================================================
restDS = new RestDataSource();
restDS.setDataURL("data/dataIntegration/xml/responses/SysUser_rest.php");
HashMap<String, String> params = new HashMap<String, String>();
params.put("_tablename", "SysGroups");
DSRequest dsrequest = new DSRequest();
dsrequest.setParams(params);
restDS.setRequestProperties(dsrequest);
restDS.setFields(DynamicDataSource.createDataSource(response, new RestDataSource()).getFields());
System.out.println("Fields have been set.");
sysGroupGrid.setCanEdit(true);
sysGroupGrid.setWidth(500);
sysGroupGrid.setPadding(10);
sysGroupGrid.setHeight(300);
sysGroupGrid.setShowFilterEditor(true);
sysGroupGrid.setFilterOnKeypress(false);
sysGroupGrid.setDataSource(restDS);
//sysGroupGrid.setAutoFetchData(true);
//sysUserGrid.setLoadDataOnDemand(true);
//sysUserGrid.setCanEdit(true);
//sysUserGrid.setShowAllRecords(true);
//sysUserGrid.setShowHeader(false);
sysGroupGrid.setAutoSaveEdits(true);
sysGroupGrid.setBorder("0px");
sysGroupGrid.setLoadingDataMessage("Loading Data");
sysGroupGrid.setEmptyMessage("No Groups have been found.");
sysGroupGrid.fetchData();
sysGroupGrid.addCellDoubleClickHandler(new CellDoubleClickHandler(){
public void onCellDoubleClick(CellDoubleClickEvent event) {
SelectedData s = new SelectedData(event.getRecord().getAttribute("login"), event.getRecord().getAttribute("id"), event.getRecord().getAttribute("login"), "silk/user.png", new SysUserEdit.Factory());
if (ht.containsKey(Integer.valueOf( event.getRecord().getAttribute("id") ).intValue())) {
DocFlow.showSelectedDataPanel(ht.get(Integer.valueOf( event.getRecord().getAttribute("id") ).intValue()));
} else {
ht.put(Integer.valueOf( event.getRecord().getAttribute("id")), s);
DocFlow.showSelectedDataPanel(ht.get(Integer.valueOf( event.getRecord().getAttribute("id") ).intValue()));
}
}
});
}
});
sysGroupGrid = new ListGrid();
sysGroupGrid.setCanEdit(true);
sysGroupGrid.setWidth(500);
sysGroupGrid.setPadding(10);
sysGroupGrid.setHeight(300);
sysGroupGrid.setBorder("0px");
sysGroupGrid.setLoadingDataMessage("Loading Data");
sysGroupGrid.setEmptyMessage("No Groups have been found.");
IButton editButton = new IButton("New");
editButton.setTop(350);
editButton.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
sysGroupGrid.startEditingNew();
}
});
IButton saveButton = new IButton("Save");
saveButton.setTop(350);
saveButton.setLeft(110);
saveButton.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
sysGroupGrid.saveAllEdits();
}
});
IButton discardButton = new IButton("Reset");
discardButton.setTop(350);
discardButton.setLeft(220);
discardButton.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
sysGroupGrid.discardAllEdits();
}
});
canvas.addChild(sysGroupGrid);
canvas.addChild(editButton);
canvas.addChild(saveButton);
canvas.addChild(discardButton);
return canvas;
}
}
Comment