/* * 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.google.gwt.user.client.ui.Composite; import com.smartgwt.client.data.DataSource; import com.smartgwt.client.types.Alignment; 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.form.DynamicForm; import com.smartgwt.client.widgets.form.fields.SelectItem; import com.smartgwt.client.widgets.form.fields.events.ChangedEvent; import com.smartgwt.client.widgets.form.fields.events.ChangedHandler; import com.smartgwt.client.widgets.grid.ListGrid; import com.smartgwt.client.widgets.grid.events.RecordClickEvent; import com.smartgwt.client.widgets.grid.events.RecordClickHandler; import com.smartgwt.client.widgets.layout.HLayout; import com.smartgwt.client.widgets.layout.VLayout; import com.smartgwt.sample.showcase.client.data.CountryXmlDS; import com.smartgwt.sample.showcase.client.data.SupplyCategoryXmlDS; import com.google.gwt.core.client.EntryPoint; public class CompositeWidgetSample implements EntryPoint { public void onModuleLoad() { final DataSource countryDS = CountryXmlDS.getInstance(); final DataSource supplyItemDS = SupplyCategoryXmlDS.getInstance(); final CompoundEditor cEditor = new CompoundEditor(countryDS); SelectItem dsSelect = new SelectItem(); dsSelect.setName("datasource2"); dsSelect.setShowTitle(false); dsSelect.setValueMap("country", "supply"); dsSelect.addChangedHandler(new ChangedHandler() { public void onChanged(ChangedEvent event) { String ds = (String)event.getValue(); if(ds.equalsIgnoreCase("country")) { cEditor.setDatasource(countryDS); } else { cEditor.setDatasource(supplyItemDS); } } }); DynamicForm form = new DynamicForm(); form.setValue("datasource2", "Select a DataSource"); form.setItems(dsSelect); VLayout layout = new VLayout(15); layout.addMember(form); layout.addMember(cEditor); layout.draw(); } private static class CompoundEditor extends Composite { private DataSource datasource; private DynamicForm form; private ListGrid grid; private IButton saveButton; public CompoundEditor(DataSource datasource) { super(); this.datasource = datasource; this.form = new DynamicForm(); form.setDataSource(datasource); saveButton = new IButton("Save"); saveButton.setLayoutAlign(Alignment.CENTER); saveButton.addClickHandler(new ClickHandler() { public void onClick(ClickEvent event) { form.saveData(); } }); VLayout editorLayout = new VLayout(5); editorLayout.addMember(form); editorLayout.addMember(saveButton); grid = new ListGrid(); grid.setWidth(500); grid.setHeight(350); grid.setDataSource(datasource); grid.setShowResizeBar(true); grid.setAutoFetchData(true); grid.addRecordClickHandler(new RecordClickHandler() { public void onRecordClick(RecordClickEvent event) { form.clearErrors(true); form.editRecord(event.getRecord()); saveButton.enable(); } }); HLayout hLayout = new HLayout(); hLayout.setAutoWidth(); hLayout.addMember(grid); hLayout.addMember(editorLayout); // All composites must call initWidget() in their constructors. initWidget(hLayout); } public DataSource getDatasource() { return datasource; } public void setDatasource(DataSource datasource) { this.datasource = datasource; grid.setDataSource(datasource); form.setDataSource(datasource); saveButton.disable(); grid.fetchData(); } } }