package org.gcube.portlets.user.td.unionwizardwidget.client.grid; import java.util.ArrayList; import java.util.List; import org.gcube.portlets.user.td.gwtservice.client.rpc.TDGWTServiceAsync; import org.gcube.portlets.user.td.gwtservice.shared.tr.ColumnData; import org.gcube.portlets.user.td.widgetcommonevent.shared.TRId; import com.allen_sauer.gwt.log.client.Log; import com.google.gwt.core.client.GWT; import com.google.gwt.core.client.Scheduler; import com.google.gwt.core.client.Scheduler.ScheduledCommand; import com.google.gwt.event.logical.shared.HasSelectionHandlers; import com.google.gwt.event.logical.shared.SelectionHandler; import com.google.gwt.event.shared.HandlerRegistration; import com.google.gwt.user.client.rpc.AsyncCallback; import com.sencha.gxt.core.client.IdentityValueProvider; import com.sencha.gxt.core.client.Style.SelectionMode; import com.sencha.gxt.core.client.dom.ScrollSupport.ScrollMode; import com.sencha.gxt.core.client.util.Margins; import com.sencha.gxt.data.client.loader.RpcProxy; import com.sencha.gxt.data.shared.ListStore; import com.sencha.gxt.data.shared.loader.ListLoadConfig; import com.sencha.gxt.data.shared.loader.ListLoadResult; import com.sencha.gxt.data.shared.loader.ListLoadResultBean; import com.sencha.gxt.data.shared.loader.ListLoader; import com.sencha.gxt.data.shared.loader.LoadResultListStoreBinding; import com.sencha.gxt.widget.core.client.ContentPanel; import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer; import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer.VerticalLayoutData; import com.sencha.gxt.widget.core.client.grid.CheckBoxSelectionModel; import com.sencha.gxt.widget.core.client.grid.ColumnConfig; import com.sencha.gxt.widget.core.client.grid.ColumnModel; import com.sencha.gxt.widget.core.client.grid.Grid; public class ColumnDataGridPanel extends ContentPanel implements HasSelectionHandlers { protected static final int GRIDHEIGHT = 360; protected static final ColumnDataProperties props = GWT .create(ColumnDataProperties.class); protected final CheckBoxSelectionModel sm; protected final Grid grid; protected TRId trId; public ColumnDataGridPanel(TRId trId) { Log.debug("ColumnDataGridPanel"); if(trId==null){ Log.error("ColumnDataGridPanel: TRId is null"); } Log.debug("ColumnDataGridPanel: "+trId.toString()); this.trId=trId; setHeaderVisible(false); //setHeadingText("Columns"); ColumnConfig labelCol = new ColumnConfig( props.label(), 120,"Column"); IdentityValueProvider identity = new IdentityValueProvider(); sm = new CheckBoxSelectionModel(identity); List> l = new ArrayList>(); l.add(labelCol); ColumnModel cm = new ColumnModel(l); ListStore store = new ListStore(props.id()); RpcProxy> proxy = new RpcProxy>() { public void load(ListLoadConfig loadConfig, final AsyncCallback> callback) { loadData(loadConfig, callback); } }; final ListLoader> loader = new ListLoader>( proxy); loader.setRemoteSort(false); loader.addLoadHandler(new LoadResultListStoreBinding>( store) { }); grid = new Grid(store, cm) { @Override protected void onAfterFirstAttach() { super.onAfterFirstAttach(); Scheduler.get().scheduleDeferred(new ScheduledCommand() { public void execute() { loader.load(); } }); } }; sm.setSelectionMode(SelectionMode.SINGLE); grid.setLoader(loader); grid.setSelectionModel(sm); grid.setHeight(GRIDHEIGHT); grid.getView().setStripeRows(true); grid.getView().setColumnLines(true); grid.getView().setAutoFill(true); grid.setBorders(false); grid.setLoadMask(true); grid.setColumnReordering(true); VerticalLayoutContainer con = new VerticalLayoutContainer(); con.setScrollMode(ScrollMode.AUTO); con.add(grid, new VerticalLayoutData(-1, -1, new Margins(0))); setWidget(con); } public Grid getGrid() { return grid; } protected void loadData(ListLoadConfig loadConfig, final AsyncCallback> callback) { TDGWTServiceAsync.INSTANCE .getColumns(trId, new AsyncCallback>() { public void onFailure(Throwable caught) { Log.error("No load columns: " + caught.getLocalizedMessage()); callback.onFailure(caught); } public void onSuccess(ArrayList result) { Log.trace("loaded " + result.size() + " columns"); callback.onSuccess(new ListLoadResultBean( result)); sm.selectAll(); forceLayout(); } }); } public ColumnData getSelectedItem() { return grid.getSelectionModel().getSelectedItem(); } public HandlerRegistration addSelectionHandler( SelectionHandler handler) { return grid.getSelectionModel().addSelectionHandler(handler); } }