/* * Smart GWT (GWT for SmartClient) * Copyright 2008 and beyond, Isomorphic Software, Inc. * * Smart GWT is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License version 3 * as published by the Free Software Foundation. Smart GWT is also * available under typical commercial license terms - see * http://smartclient.com/license * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. */ import com.smartgwt.client.data.DataSource; import com.smartgwt.client.data.OperationBinding; import com.smartgwt.client.data.fields.DataSourceTextField; import com.smartgwt.client.types.DSOperationType; 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.ListGridField; import com.smartgwt.client.widgets.grid.ListGridRecord; import com.google.gwt.core.client.EntryPoint; public class FormXmlEditAndSaveSample implements EntryPoint { public ListGridRecord createRecord(String countryCode, String countryName, String capital, String continent) { ListGridRecord record = new ListGridRecord(); record.setAttribute("continent", continent); record.setAttribute("countryName", countryName); record.setAttribute("countryCode", countryCode); record.setAttribute("capital", capital); return record; } public void onModuleLoad() { Canvas canvas = new Canvas(); DataSource countryDS = new DataSource(); countryDS.setClientOnly(true); DataSourceTextField countryCode = new DataSourceTextField("countryCode", "Code"); countryCode.setPrimaryKey(true); countryCode.setCanEdit(false); DataSourceTextField countryName = new DataSourceTextField("countryName", "Country"); DataSourceTextField capital = new DataSourceTextField("capital", "Capital"); countryDS.setFields(countryCode, countryName, capital); countryDS.setOperationBindings(); OperationBinding fetch = new OperationBinding(DSOperationType.FETCH, "data/dataIntegration/xml/responses/country_fetch.xml"); OperationBinding add = new OperationBinding(DSOperationType.ADD, "data/dataIntegration/xml/responses/country_add.xml"); OperationBinding update = new OperationBinding(DSOperationType.UPDATE, "data/dataIntegration/xml/responses/country_update.xml"); OperationBinding remove = new OperationBinding(DSOperationType.REMOVE, "data/dataIntegration/xml/responses/country_remove.xml"); countryDS.setOperationBindings(fetch, add, update, remove); final ListGrid countryGrid = new ListGrid(); countryGrid.setWidth(500); countryGrid.setHeight(224); countryGrid.setDataSource(countryDS); countryGrid.setEmptyCellValue("--"); ListGridField codeField = new ListGridField("countryCode"); ListGridField nameField = new ListGridField("countryName"); ListGridField capitalField = new ListGridField("capital"); ListGridField continentField = new ListGridField("continent", "Continent"); countryGrid.setFields(codeField, nameField, capitalField, continentField); countryGrid.setSortField(0); countryGrid.setDataPageSize(50); countryGrid.setAutoFetchData(true); canvas.addChild(countryGrid); final IButton addButton = new IButton("Add new Country"); addButton.setLeft(0); addButton.setTop(240); addButton.setWidth(150); addButton.addClickHandler(new ClickHandler() { public void onClick(ClickEvent event) { countryGrid.addData(createRecord("A1", "New Value", "New Value", "New Value")); addButton.disable(); } }); canvas.addChild(addButton); final IButton updateButton = new IButton("Update Country (US)"); updateButton.setLeft(175); updateButton.setTop(240); updateButton.setWidth(150); updateButton.addClickHandler(new ClickHandler() { public void onClick(ClickEvent event) { countryGrid.updateData(createRecord("US", "Edited Value", "Edited Value", "Edited Value")); updateButton.disable(); } }); canvas.addChild(updateButton); final IButton removeButton = new IButton("Remove Country (UK)"); removeButton.setLeft(350); removeButton.setTop(240); removeButton.setWidth(150); removeButton.addClickHandler(new ClickHandler() { public void onClick(ClickEvent event) { ListGridRecord record = new ListGridRecord(); record.setAttribute("countryCode", "UK"); countryGrid.removeData(record); removeButton.disable(); } }); canvas.addChild(removeButton); canvas.draw(); } }