Skip to content

Instantly share code, notes, and snippets.

@slugmandrew
Last active June 9, 2017 12:09
Show Gist options
  • Save slugmandrew/e3d50e959b7c2b0ce07d3d2e986656d0 to your computer and use it in GitHub Desktop.
Save slugmandrew/e3d50e959b7c2b0ce07d3d2e986656d0 to your computer and use it in GitHub Desktop.
Custom Widget of CellTable, dataprovider, pager etc with ability to hide/show columns
package com.utilitiessavings.usavappv7.client.application.us;
import com.allen_sauer.gwt.log.client.Log;
import com.google.common.collect.Maps;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.SimplePager;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.view.client.ListDataProvider;
import org.gwtbootstrap3.client.ui.gwt.CellTable;
import org.gwtbootstrap3.extras.select.client.ui.MultipleSelect;
import org.gwtbootstrap3.extras.select.client.ui.Option;
import java.util.List;
import java.util.Map;
/**
* Created by Drew on 07/06/2017.
*/
public class SuperTable<T> extends Composite
{
@UiField
MultipleSelect columnSelect;
@UiField
Panel emptyPanel, tablePanel;
@UiField
CellTable<T> table;
@UiField
SimplePager pager;
private ListDataProvider<T> dataProvider = new ListDataProvider<>();
private Map<String, Column<T, ?>> columnsMap = Maps.newHashMap();
interface Binder extends UiBinder<HTMLPanel, SuperTable>
{
}
private static Binder uiBinder = GWT.create(Binder.class);
public SuperTable()
{
initWidget(uiBinder.createAndBindUi(this));
columnSelect.addValueChangeHandler(new ValueChangeHandler<List<String>>()
{
@Override
public void onValueChange(ValueChangeEvent<List<String>> event)
{
Log.info("SuperTable -> onColumnSelect: " + event.getValue());
setSelectedColumns(columnSelect.getValue().toArray(new String[0]));
}
});
}
public void setColumnsMap(Map<String, Column<T, ?>> columnsMap)
{
this.columnsMap = columnsMap;
initTable();
}
void initTable()
{
// link the dataprovider to the table
dataProvider.addDataDisplay(table);
// loop the columns, creating an option for each in the
for(String name : columnsMap.keySet())
{
Option option = new Option();
option.setText(name);
option.setSelected(true);
// pass it in to the select
columnSelect.add(option);
}
setSelectedColumns(columnSelect.getValue().toArray(new String[0]));
}
public void setData(List<T> data)
{
dataProvider.setList(data);
pager.setDisplay(table);
// hide or show panels
if(data.isEmpty())
{
emptyPanel.setVisible(true);
tablePanel.setVisible(false);
}
else
{
emptyPanel.setVisible(false);
tablePanel.setVisible(true);
}
}
private void setSelectedColumns(String[] values)
{
int columnCount = table.getColumnCount();
// remove all columns
for(int i = 0; i < columnCount; i++)
{
Log.info("QuotesView -> setSelectedColumns: removing column 0");
table.removeColumn(0);
}
// loop the selected values and add them
for(String columnName : values)
{
table.addColumn(columnsMap.get(columnName), columnName);
}
}
}
<!DOCTYPE ui:UiBinder SYSTEM "http://dl.google.com/gwt/DTD/xhtml.ent">
<ui:UiBinder xmlns:ui="urn:ui:com.google.gwt.uibinder"
xmlns:b="urn:import:org.gwtbootstrap3.client.ui"
xmlns:b.gwt="urn:import:org.gwtbootstrap3.client.ui.gwt"
xmlns:b.html="urn:import:org.gwtbootstrap3.client.ui.html"
xmlns:b.select="urn:import:org.gwtbootstrap3.extras.select.client.ui"
xmlns:g="urn:import:com.google.gwt.user.client.ui"
xmlns:cellview="urn:import:com.google.gwt.user.cellview.client">
<g:HTMLPanel>
<b:Panel>
<b:PanelHeader addStyleNames="clearfix">
<b:Row>
<b:Column size="XS_12 SM_6 MD_4">
<!--TODO this is a filter based on the user profile-->
<!--<b:InputGroup>-->
<!--<b:InputGroupAddon>Belonging to</b:InputGroupAddon>-->
<!--<b:ValueListBox ui:field="scopeListBox"/>-->
<!--</b:InputGroup>-->
</b:Column>
<b:Column size="XS_12 SM_6 MD_4" offset="MD_4" pull="RIGHT">
<b.html:Div pull="RIGHT">
<b:InputGroup>
<b.select:MultipleSelect ui:field="columnSelect" title="Select" countSelectedTextFormat="2"/>
<b:InputGroupAddon icon="COLUMNS">Columns</b:InputGroupAddon>
</b:InputGroup>
</b.html:Div>
</b:Column>
</b:Row>
</b:PanelHeader>
<g:HTMLPanel ui:field="emptyPanel">
<b:Heading size="H3" text="List of contracts is empty" alignment="CENTER" color="#888" paddingBottom="10"/>
</g:HTMLPanel>
<g:ScrollPanel width="100%" ui:field="tablePanel">
<b.gwt:CellTable ui:field="table"/>
</g:ScrollPanel>
<cellview:SimplePager ui:field="pager" location="CENTER"/>
</b:Panel>
</g:HTMLPanel>
</ui:UiBinder>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment