249 lines
7.0 KiB
Java
249 lines
7.0 KiB
Java
package org.gcube.portlets.widgets.ckandatapublisherwidget.client.ui;
|
|
|
|
import java.util.Comparator;
|
|
import java.util.List;
|
|
|
|
import org.gcube.portlets.widgets.ckandatapublisherwidget.shared.ResourceBeanWrapper;
|
|
|
|
import com.github.gwtbootstrap.client.ui.Button;
|
|
import com.github.gwtbootstrap.client.ui.CellTable;
|
|
import com.google.gwt.cell.client.Cell.Context;
|
|
import com.google.gwt.cell.client.CheckboxCell;
|
|
import com.google.gwt.cell.client.EditTextCell;
|
|
import com.google.gwt.core.client.GWT;
|
|
import com.google.gwt.dom.client.Element;
|
|
import com.google.gwt.dom.client.NativeEvent;
|
|
import com.google.gwt.dom.client.Style.Unit;
|
|
import com.google.gwt.event.dom.client.ClickEvent;
|
|
import com.google.gwt.event.dom.client.ClickHandler;
|
|
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
|
|
import com.google.gwt.user.cellview.client.Column;
|
|
import com.google.gwt.user.cellview.client.ColumnSortEvent.ListHandler;
|
|
import com.google.gwt.user.cellview.client.TextColumn;
|
|
import com.google.gwt.user.client.ui.Composite;
|
|
import com.google.gwt.user.client.ui.VerticalPanel;
|
|
import com.google.gwt.view.client.ListDataProvider;
|
|
import com.google.gwt.view.client.Range;
|
|
|
|
/**
|
|
* The resources table class.
|
|
* @author Costantino Perciante at ISTI-CNR (costantino.perciante@isti.cnr.it)
|
|
*/
|
|
public class ResourcesTable extends Composite{
|
|
|
|
// the data provider
|
|
protected ListDataProvider<ResourceBeanWrapper> dataProvider = new ListDataProvider<ResourceBeanWrapper>();
|
|
|
|
// the table that will be displayed
|
|
private CellTable<ResourceBeanWrapper> table = new CellTable<ResourceBeanWrapper>();
|
|
|
|
// save original list
|
|
private List<ResourceBeanWrapper> originalResources;
|
|
|
|
// main panel
|
|
private VerticalPanel mainPanel = new VerticalPanel();
|
|
|
|
// button labels
|
|
private static final String SELECT_ALL_LABEL = "Select All";
|
|
private static final String DESELECT_ALL_LABEL = "Deselect All";
|
|
|
|
// maintain the selection status
|
|
private boolean selectedAll = true;
|
|
final Button selectAllButton = new Button(DESELECT_ALL_LABEL);
|
|
|
|
// is read only?
|
|
private boolean readOnly = false;
|
|
|
|
public ResourcesTable(List<ResourceBeanWrapper> resources){
|
|
|
|
super();
|
|
initWidget(mainPanel);
|
|
|
|
// set panel width
|
|
mainPanel.setWidth("100%");
|
|
|
|
// save original resources
|
|
this.originalResources = resources;
|
|
|
|
// add data to the provider
|
|
dataProvider.setList(resources);
|
|
dataProvider.addDataDisplay(table);
|
|
|
|
// style of the table
|
|
table.setStriped(true);
|
|
table.setWidth("100%", false);
|
|
table.addStyleName("table-style");
|
|
table.setBordered(true);
|
|
|
|
// visible rows
|
|
table.setVisibleRange(new Range(0, originalResources.size()));
|
|
table.setRowCount(originalResources.size(), true);
|
|
|
|
// Add a checked column to add the resource.
|
|
Column<ResourceBeanWrapper, Boolean> chosenColumn = new Column<ResourceBeanWrapper, Boolean>(new CheckboxCell(true,false)) {
|
|
|
|
@Override
|
|
public Boolean getValue(ResourceBeanWrapper object) {
|
|
return object.isToBeAdded();
|
|
}
|
|
|
|
@Override
|
|
public void onBrowserEvent(Context context, final Element parent, final ResourceBeanWrapper res, NativeEvent event) {
|
|
event.preventDefault();
|
|
res.setToBeAdded(!res.isToBeAdded());
|
|
}
|
|
|
|
@Override
|
|
public void render(Context context, ResourceBeanWrapper object,
|
|
SafeHtmlBuilder sb) {
|
|
|
|
if(readOnly){
|
|
|
|
String checked = object.isToBeAdded() ? "checked" : "unchecked";
|
|
sb.appendHtmlConstant("<input type='checkbox'" ).
|
|
appendHtmlConstant("tabindex='-1' ").
|
|
appendEscaped(checked).appendEscaped(" disabled ").appendHtmlConstant("/>");
|
|
|
|
}else{
|
|
|
|
super.render(context, object, sb);
|
|
|
|
}
|
|
|
|
}
|
|
};
|
|
table.addColumn(chosenColumn, "Select");
|
|
|
|
// Add a text column to show the name (and make it sortable) TODO : make it editable
|
|
Column<ResourceBeanWrapper, String> nameColumn = new Column<ResourceBeanWrapper, String>(new EditTextCell()) {
|
|
|
|
@Override
|
|
public String getValue(ResourceBeanWrapper object) {
|
|
return object.getName();
|
|
}
|
|
|
|
@Override
|
|
public void onBrowserEvent(Context context, Element elem,
|
|
ResourceBeanWrapper object, NativeEvent event) {
|
|
// TODO Auto-generated method stub
|
|
//super.onBrowserEvent(context, elem, object, event);
|
|
}
|
|
|
|
};
|
|
|
|
ListHandler<ResourceBeanWrapper> nameColHandler = new ListHandler<ResourceBeanWrapper>(dataProvider.getList());
|
|
|
|
nameColHandler.setComparator(nameColumn, new Comparator<ResourceBeanWrapper>() {
|
|
|
|
public int compare(ResourceBeanWrapper o1, ResourceBeanWrapper o2) {
|
|
|
|
return o1.getName().compareTo(o2.getName());
|
|
|
|
}
|
|
});
|
|
nameColumn.setSortable(true);
|
|
nameColumn.setDefaultSortAscending(false);
|
|
table.addColumnSortHandler(nameColHandler);
|
|
table.addColumn(nameColumn, "Name");
|
|
|
|
// Add a date column to show the url
|
|
TextColumn<ResourceBeanWrapper> urlColumn = new TextColumn<ResourceBeanWrapper>() {
|
|
@Override
|
|
public String getValue(ResourceBeanWrapper object) {
|
|
return object.getUrl();
|
|
}
|
|
};
|
|
table.addColumn(urlColumn, "Url");
|
|
|
|
// Add a date column to show the description (and make it sortable)
|
|
Column<ResourceBeanWrapper, String> descColumn = new Column<ResourceBeanWrapper, String>(new EditTextCell()) {
|
|
@Override
|
|
public String getValue(ResourceBeanWrapper object) {
|
|
return object.getDescription();
|
|
}
|
|
|
|
@Override
|
|
public void onBrowserEvent(Context context, Element elem,
|
|
ResourceBeanWrapper object, NativeEvent event) {
|
|
event.preventDefault();
|
|
|
|
if(!readOnly){
|
|
super.onBrowserEvent(context, elem, object, event);
|
|
|
|
GWT.log("Element is " + elem);
|
|
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void render(Context context, ResourceBeanWrapper object,
|
|
SafeHtmlBuilder sb) {
|
|
// TODO Auto-generated method stub
|
|
super.render(context, object, sb);
|
|
GWT.log("Element is " + sb);
|
|
}
|
|
};
|
|
|
|
table.addColumn(descColumn, "Description (Editable)");
|
|
|
|
// sort by columnName
|
|
table.getColumnSortList().push(nameColumn);
|
|
|
|
// set width column chosen
|
|
table.setColumnWidth(chosenColumn, 5, Unit.PCT);
|
|
table.setColumnWidth(nameColumn, 20, Unit.PCT);
|
|
table.setColumnWidth(urlColumn, 45, Unit.PCT);
|
|
table.setColumnWidth(descColumn, 30, Unit.PCT);
|
|
|
|
// add a select all button
|
|
|
|
selectAllButton.getElement().getStyle().setMarginTop(15, Unit.PX);
|
|
selectAllButton.getElement().getStyle().setMarginBottom(15, Unit.PX);
|
|
|
|
// add handler
|
|
selectAllButton.addClickHandler(new ClickHandler() {
|
|
|
|
@Override
|
|
public void onClick(ClickEvent event) {
|
|
selectedAll = !selectedAll;
|
|
checkAllResources(selectedAll);
|
|
}
|
|
});
|
|
|
|
// add the button
|
|
mainPanel.add(selectAllButton);
|
|
|
|
// add the table
|
|
mainPanel.add(table);
|
|
}
|
|
|
|
/**
|
|
* Check all resources
|
|
*/
|
|
public void checkAllResources(boolean value){
|
|
|
|
if(value)
|
|
selectAllButton.setText(DESELECT_ALL_LABEL);
|
|
else
|
|
selectAllButton.setText(SELECT_ALL_LABEL);
|
|
|
|
for(ResourceBeanWrapper resource: originalResources)
|
|
resource.setToBeAdded(value);
|
|
|
|
// refresh data
|
|
dataProvider.refresh();
|
|
}
|
|
|
|
/**
|
|
* Freeze table content and select/deselect all button
|
|
*/
|
|
public void freezeTable() {
|
|
|
|
selectAllButton.setEnabled(false);
|
|
readOnly = true;
|
|
|
|
// redraw the table
|
|
table.redraw();
|
|
}
|
|
}
|