package org.gwtoolbox.widget.client.table.datagrid.column;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import org.gwtoolbox.commons.collections.client.ComparableComparator;
import org.gwtoolbox.widget.client.data.DataType;
import org.gwtoolbox.widget.client.menu.Menu;
import org.gwtoolbox.widget.client.menu.MenuBuilder;
import org.gwtoolbox.widget.client.table.datagrid.OldDataGrid;
import org.gwtoolbox.widget.client.table.datagrid.FieldGroup;
/**
* @author Uri Boness
*/
public class FieldColumn<T> extends Column<T> {
private final String fieldName;
private boolean sortable;
private boolean groupable;
public FieldColumn(String name, String fieldName, DataType<T> dataType) {
this(name, fieldName, dataType, null);
}
public FieldColumn(String name, String fieldName, DataType<T> dataType, String width) {
this(name, fieldName, dataType, width, (CellRenderer<T>) null);
}
public FieldColumn(String name, String fieldName, DataType<T> dataType, String width, HasHorizontalAlignment.HorizontalAlignmentConstant alignment) {
this(name, fieldName, dataType, width, (CellRenderer<T>) null);
setHorizontalAlignment(alignment);
}
public FieldColumn(String name, String fieldName, DataType<T> dataType, String width, CellRenderer<T> cellRenderer) {
super(name, new FieldValueExtractor(fieldName), dataType, width, cellRenderer);
this.fieldName = fieldName;
}
public boolean isSortable() {
return sortable;
}
public void setSortable(boolean sortable) {
this.sortable = sortable;
}
public boolean isGroupable() {
return groupable;
}
public void setGroupable(boolean groupable) {
this.groupable = groupable;
}
public String getFieldName() {
return fieldName;
}
public MenuBuilder getMenuBuilder(final OldDataGrid grid) {
if (!sortable && !groupable) {
return null;
}
MenuBuilder builder = new MenuBuilder() {
public void build(Menu menu) {
if (sortable) {
menu.addItem("Sort Asc.", new Command() {
public void execute() {
grid.sort(fieldName, true);
}
});
menu.addItem("Sort Desc.", new Command() {
public void execute() {
grid.sort(fieldName, false);
}
});
}
if (groupable) {
if (sortable) {
menu.addSeparator();
}
menu.addItem("Group By This Field", new Command() {
public void execute() {
FieldGroup group = new FieldGroup(getName(), fieldName, new ComparableComparator());
grid.setGroupBy(group);
}
});
}
}
};
return builder;
}
}