package com.dotj.test;
import com.dotj.grid.interfaces.*;
import com.dotj.io.DataSet;
/**
* Sample listener class for processing a grid sublist. For processing grid sublists, this class must implement the
* IGridListener2 interface in order to make use of the doSublist and getColumnSummaryTotal callback events.
*
* @author dotJ Software
*/
public class MyGridSublistListener implements IGridListener2 {
// Number of rows per page.
public static final int PAGE_SIZE = 10;
private boolean isPost = false;
private int sortColumn = 1;
private int sortType = DataSet.SORT_NONE;
private int pageCount = 1;
private int rowCount = 0;
private int threadTotal = 0;
/**
* Is this request a POST request?
* @return true if a POST request, otherwise false.
*/
public boolean isPost() {
return isPost;
}
/**
* Tell this object that the request is a POST request.
* @param post true if a POST request, otherwise false.
*/
public void setPost(boolean post) {
isPost = post;
}
/**
* Set the thread total. This will be used in the getColumnSummaryTotal callback event.
* @param total Total value for the thread column.
*/
public void setThreadTotal(int total) {
threadTotal = total;
}
/**
* Set the sort column. This was pulled from dotJ in the Struts ProcessSublistaction class.
*
* This will be used in the doCustomSort callback event.
* @param sortColumn The column that the grid is sorted on.
*/
public void setSortColumn(int sortColumn) {
this.sortColumn = sortColumn;
}
/**
* Set the sort type. This was pulled from dotJ in the Struts ProcessSublistaction class.
*
* This will be used in the doCustomSort callback event.
* @param sortType The type of sort (ascending or descending) that the grid is sorted on.
*/
public void setSortType(int sortType) {
this.sortType = sortType;
}
/**
* Set the row count. This was pulled from dotJ in the Struts ProcessSublistaction class.
*
* This will be used in the doSublist callback event.
* @param rowCount The *total* number of rows in the grid, not the number of rows in the ResultSet. dotJ cannot
* compute this accurately since it does not have visibility to every row in the grid.
*/
public void setRowCount(int rowCount) {
this.rowCount = rowCount;
}
/**
* Set the page count. This was pulled from dotJ in the Struts ProcessSublistaction class.
*
* This will be used in the doSublist callback event.
* @param pageCount The number of pages in the grid. dotJ cannot compute this accurately since it does not have
* visibility to every row in the grid.
*/
public void setPageCount(int pageCount) {
this.pageCount = pageCount;
}
//**************************************************************************************************************
//*
//* BEGIN OF DOTJ CALLBACK EVENTS.
//*
//**************************************************************************************************************
/**
* The doCustomSort method is used to tell the grid whether or not to physically sort the grid when building it. For
* example, if you are sorting your grid using a SQL ORDER BY clause, then you are in control of the sorting and can
* disable dotJ's internal sorting algorithm by returning a value of true.
*
* @param sortContext The context of the sort. This includes the column number being sorted and the sort type (ascending or descending).
* @return true if you are doing your own custom sorting, otherwise false.
*/
public boolean doCustomSort(ISortContext sortContext) {
// Tell the grid we are sorting upon the first display of the grid. From there on, dotJ will keep track.
if (isPost == false) {
sortContext.setSortColumnNumber(sortColumn);
sortContext.setSortType(sortType);
}
return true;
}
// No custom implementation since we will not alter the contents or look and feel of any cell in the grid.
public void rowBegin(IGridCommandEventArgs eventArgs, IRowContext rowContext) {
}
// No custom implementation since we will not alter the contents or look and feel of any cell in the grid.
public void rowEnd(IGridCommandEventArgs eventArgs, IRowContext rowContext) {
}
// No custom implementation since we will not alter the contents or look and feel of any cell in the grid.
public void columnBegin(IRowContext rowContext, IColumnContext columnContext, IColumnTag tag) {
}
// No custom implementation since we will not alter the contents or look and feel of any cell in the grid.
public void columnEnd(IRowContext rowContext, IColumnContext columnContext, IColumnTag tag) {
}
/**
* The doSublist callback event. This gets called by dotJ at the start of the grid tag processing. Put custom logic
* in this method when performing grid sublist processing.
*
* @param sublistContext The sublist context. This contains all the pertinent paging information and also allows
* you to set custom paging values.
* @return true if doing sublist processing, otherwise false.
* @since 2.1
*/
public boolean doSublist(ISublistContext sublistContext) {
boolean doSublist = true;
// Since we're doing our own sublist processing, tell dotJ about the following two paging values. This is necessary
// since dotJ cannot determine these since it does not have visibility to the entire set of rows.
if (doSublist) {
sublistContext.setRowCount(rowCount);
sublistContext.setPageCount(pageCount);
sublistContext.setProcessVisibleRowsOnly(true);
}
return doSublist;
}
/**
* Get the summary total value for the specified column. dotJ will make this callback if and when it needs to display
* a summarized value at the bottom of the grid. This occurs if the summary attribute is set on the
* <dotj:datacolumn> tag.
*
* @param controlBreakType The type of control break used on the <dotj:columns> tag.
* @param columnContext The column as declared in the JSP.
* @return The summarized total for the column.
* @since 2.1
*/
public String getColumnSummaryTotal(String controlBreakType, IColumnTag columnContext) {
if (columnContext.getSummary() != null) {
if (columnContext.getSummary().equals("{rowcount}")) { // Can also use com.dotj.base.Tokens.TOKEN_ROWCOUNT
return "" + rowCount;
} else if (columnContext.getSummary().equals("{sum}")) { // Can also use com.dotj.base.Tokens.TOKEN_SUM
return "" + threadTotal;
}
}
return " ";
}
}