Last active
November 24, 2017 06:14
-
-
Save branflake2267/7993084 to your computer and use it in GitHub Desktop.
GXT large grid for testing, has RPC proxy simulation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.sencha.gxt.text.client.larggrid; | |
import java.util.ArrayList; | |
import java.util.List; | |
import com.google.gwt.user.client.rpc.AsyncCallback; | |
import com.google.gwt.user.client.ui.HasHorizontalAlignment; | |
import com.google.gwt.user.client.ui.RootPanel; | |
import com.sencha.gxt.core.client.ValueProvider; | |
import com.sencha.gxt.data.client.loader.RpcProxy; | |
import com.sencha.gxt.data.shared.ListStore; | |
import com.sencha.gxt.data.shared.ModelKeyProvider; | |
import com.sencha.gxt.data.shared.loader.FilterPagingLoadConfig; | |
import com.sencha.gxt.data.shared.loader.FilterPagingLoadConfigBean; | |
import com.sencha.gxt.data.shared.loader.LoadResultListStoreBinding; | |
import com.sencha.gxt.data.shared.loader.PagingLoadResult; | |
import com.sencha.gxt.data.shared.loader.PagingLoader; | |
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.container.Viewport; | |
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; | |
import com.sencha.gxt.widget.core.client.grid.filters.GridFilters; | |
import com.sencha.gxt.widget.core.client.grid.filters.StringFilter; | |
import com.sencha.gxt.widget.core.client.toolbar.PagingToolBar; | |
public class LargeGrid { | |
private static final int COLUMNS_SIZE = 32; | |
private static final int TOTAL_LENGTH = 2000; | |
public class Data { | |
private String key; | |
private String[] values; | |
public Data(String key, String[] values) { | |
this.key = key; | |
this.values = values; | |
} | |
public String getKey() { | |
return key; | |
} | |
public void setKey(String key) { | |
this.key = key; | |
} | |
public String getValue(int index) { | |
return values[index]; | |
} | |
public void setValue(int index, String value) { | |
this.values[index] = value; | |
} | |
} | |
private PagingLoader<FilterPagingLoadConfig, PagingLoadResult<Data>> pagingLoader; | |
public LargeGrid() { | |
Viewport vp = new Viewport(); | |
RootPanel.get().add(vp); | |
VerticalLayoutContainer vlc = new VerticalLayoutContainer(); | |
vp.add(vlc); | |
vlc.add(createGrid(), new VerticalLayoutData(1, 1)); | |
vlc.add(createPagingToolBar()); | |
} | |
private PagingToolBar createPagingToolBar() { | |
PagingToolBar toolBar = new PagingToolBar(50); | |
toolBar.bind(pagingLoader); | |
return toolBar; | |
} | |
public class ValueProviderExt implements ValueProvider<Data, String> { | |
private int index; | |
public ValueProviderExt(int index) { | |
this.index = index; | |
} | |
@Override | |
public String getValue(Data data) { | |
return data.getValue(index); | |
} | |
@Override | |
public void setValue(Data object, String value) { | |
} | |
@Override | |
public String getPath() { | |
return "path" + index; | |
} | |
} | |
public class StringFilterExt extends StringFilter<Data> { | |
public StringFilterExt(int index) { | |
super(new ValueProviderExt(index)); | |
} | |
} | |
public Grid<Data> createGrid() { | |
ColumnModel<Data> cm = new ColumnModel<Data>(getColumns()); | |
ListStore<Data> listStore = getStore(); | |
pagingLoader = getLoader(listStore); | |
final Grid<Data> grid = new Grid<Data>(listStore, cm) { | |
@Override | |
protected void onAfterFirstAttach() { | |
super.onAfterFirstAttach(); | |
pagingLoader.load(); | |
} | |
}; | |
grid.setLoader(pagingLoader); | |
addFiltersToGrid(grid); | |
return grid; | |
} | |
private void addFiltersToGrid(Grid<Data> grid) { | |
GridFilters<Data> filters = new GridFilters<Data>(); | |
for (int i=0; i < COLUMNS_SIZE; i++) { | |
filters.addFilter(new StringFilterExt(i)); | |
} | |
filters.initPlugin(grid); | |
filters.setAutoReload(true); | |
filters.setLocal(true); | |
} | |
private PagingLoader<FilterPagingLoadConfig, PagingLoadResult<Data>> getLoader(ListStore<Data> store) { | |
// Data Provider | |
RpcProxy<FilterPagingLoadConfig, PagingLoadResult<Data>> proxy = new RpcProxy<FilterPagingLoadConfig, PagingLoadResult<Data>>() { | |
@Override | |
public void load(FilterPagingLoadConfig loadConfig, AsyncCallback<PagingLoadResult<Data>> callback) { | |
getDatas(loadConfig, callback); | |
} | |
}; | |
// Paging Loader | |
final PagingLoader<FilterPagingLoadConfig, PagingLoadResult<Data>> remoteLoader = new PagingLoader<FilterPagingLoadConfig, PagingLoadResult<Data>>(proxy) { | |
@Override | |
protected FilterPagingLoadConfig newLoadConfig() { | |
return new FilterPagingLoadConfigBean(); | |
} | |
}; | |
remoteLoader.setRemoteSort(true); | |
remoteLoader.addLoadHandler(new LoadResultListStoreBinding<FilterPagingLoadConfig, Data, PagingLoadResult<Data>>(store)); | |
return remoteLoader; | |
} | |
private List<ColumnConfig<Data, ?>> getColumns() { | |
List<ColumnConfig<Data, ?>> columns = new ArrayList<ColumnConfig<Data, ?>>(); | |
for (int c = 0; c < COLUMNS_SIZE; c++) { | |
String header = "col" + c; | |
ColumnConfig<Data, String> col = new ColumnConfig<LargeGrid.Data, String>(new ValueProviderExt(c), 50, header); | |
col.setAlignment(HasHorizontalAlignment.ALIGN_CENTER); | |
columns.add(col); | |
} | |
return columns; | |
} | |
private ListStore<Data> getStore() { | |
ListStore<Data> store = new ListStore<Data>(new ModelKeyProvider<Data>() { | |
@Override | |
public String getKey(Data item) { | |
return item.getKey(); | |
} | |
}); | |
return store; | |
} | |
private void getDatas(FilterPagingLoadConfig loadConfig, AsyncCallback<PagingLoadResult<Data>> callback) { | |
final int offset = loadConfig.getOffset(); | |
int limit = loadConfig.getLimit(); | |
System.out.println("getDatas: offset=" + offset + " limit=" + limit); | |
final List<Data> datas = new ArrayList<Data>(); | |
for (int i = offset; i < offset + limit; i++) { | |
datas.add(getData(i)); | |
} | |
PagingLoadResult<Data> result = new PagingLoadResult<LargeGrid.Data>() { | |
@Override | |
public List<Data> getData() { | |
return datas; | |
} | |
@Override | |
public void setTotalLength(int totalLength) { | |
} | |
@Override | |
public void setOffset(int offset) { | |
} | |
@Override | |
public int getTotalLength() { | |
return TOTAL_LENGTH; | |
} | |
@Override | |
public int getOffset() { | |
return offset; | |
} | |
}; | |
callback.onSuccess(result); | |
} | |
private Data getData(int row) { | |
String key = "key" + row; | |
String[] values = new String[COLUMNS_SIZE]; | |
for (int col = 0; col < COLUMNS_SIZE; col++) { | |
values[col] = "" + col + "," + row; | |
} | |
Data data = new Data(key, values); | |
return data; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment