tabletag.java
来自「displaytag-1.0修正版」· Java 代码 · 共 1,827 行 · 第 1/4 页
JAVA
1,827 行
this.previousRow = new Hashtable(10);
// variables to hold next row column values.
this.nextRow = new Hashtable(10);
// Put the page stuff there if it needs to be there...
if (this.properties.getAddPagingBannerTop())
{
// search result and navigation bar
writeSearchResultAndNavigation();
}
String css = this.properties.getCssTable();
if (StringUtils.isNotBlank(css))
{
this.addClass(css);
}
// open table
write(getOpenTag(), out);
// caption
if (this.caption != null)
{
write(this.caption, out);
}
// thead
if (this.properties.getShowHeader())
{
writeTableHeader();
}
if (this.footer != null)
{
write(TagConstants.TAG_TFOOTER_OPEN, out);
write(this.footer, out);
write(TagConstants.TAG_TFOOTER_CLOSE, out);
// reset footer
this.footer = null;
}
// open table body
write(TagConstants.TAG_TBODY_OPEN, out);
// write table body
writeTableBody();
// close table body
write(TagConstants.TAG_TBODY_CLOSE, out);
// close table
write(getCloseTag(), out);
writeTableFooter();
if (this.tableModel.getTableDecorator() != null)
{
this.tableModel.getTableDecorator().finish();
}
if (log.isDebugEnabled())
{
log.debug("[" + getUid() + "] getHTMLData end");
}
}
/**
* Generates the table header, including the first row of the table which displays the titles of the columns.
*/
private void writeTableHeader()
{
JspWriter out = this.pageContext.getOut();
if (log.isDebugEnabled())
{
log.debug("[" + getUid() + "] getTableHeader called");
}
// open thead
write(TagConstants.TAG_THEAD_OPEN, out);
// open tr
write(TagConstants.TAG_TR_OPEN, out);
// no columns?
if (this.tableModel.isEmpty())
{
write(TagConstants.TAG_TH_OPEN, out);
write(TagConstants.TAG_TH_CLOSE, out);
}
// iterator on columns for header
Iterator iterator = this.tableModel.getHeaderCellList().iterator();
while (iterator.hasNext())
{
// get the header cell
HeaderCell headerCell = (HeaderCell) iterator.next();
if (headerCell.getSortable())
{
String cssSortable = this.properties.getCssSortable();
headerCell.addHeaderClass(cssSortable);
}
// if sorted add styles
if (headerCell.isAlreadySorted())
{
// sorted css class
headerCell.addHeaderClass(this.properties.getCssSorted());
// sort order css class
headerCell.addHeaderClass(this.properties.getCssOrder(this.tableModel.isSortOrderAscending()));
}
// append th with html attributes
write(headerCell.getHeaderOpenTag(), out);
// title
String header = headerCell.getTitle();
// column is sortable, create link
if (headerCell.getSortable())
{
// creates the link for sorting
Anchor anchor = new Anchor(getSortingHref(headerCell), header);
// append to buffer
header = anchor.toString();
}
write(header, out);
write(headerCell.getHeaderCloseTag(), out);
}
// close tr
write(TagConstants.TAG_TR_CLOSE, out);
// close thead
write(TagConstants.TAG_THEAD_CLOSE, out);
if (log.isDebugEnabled())
{
log.debug("[" + getUid() + "] getTableHeader end");
}
}
/**
* Generates the link to be added to a column header for sorting.
* @param headerCell header cell the link should be added to
* @return Href for sorting
*/
private Href getSortingHref(HeaderCell headerCell)
{
// costruct Href from base href, preserving parameters
Href href = new Href(this.baseHref);
// add column number as link parameter
href.addParameter(encodeParameter(TableTagParameters.PARAMETER_SORT), headerCell.getColumnNumber());
boolean nowOrderAscending = !(headerCell.isAlreadySorted() && this.tableModel.isSortOrderAscending());
int sortOrderParam = nowOrderAscending ? SortOrderEnum.ASCENDING.getCode() : SortOrderEnum.DESCENDING.getCode();
href.addParameter(encodeParameter(TableTagParameters.PARAMETER_ORDER), sortOrderParam);
// If user want to sort the full table I need to reset the page number.
if (this.tableModel.isSortFullTable())
{
href.addParameter(encodeParameter(TableTagParameters.PARAMETER_PAGE), 1);
}
return href;
}
/**
* This takes a column value and grouping index as the argument. It then groups the column and returns the
* appropriate string back to the caller.
* @param value String
* @param group int
* @return String
*/
private String groupColumns(String value, int group)
{
if ((group == 1) && this.nextRow.size() > 0)
{
// we are at the begining of the next row so copy the contents from nextRow to the previousRow.
this.previousRow.clear();
this.previousRow.putAll(this.nextRow);
this.nextRow.clear();
}
if (!this.nextRow.containsKey(new Integer(group)))
{
// Key not found in the nextRow so adding this key now...
// remember all the old values.
this.nextRow.put(new Integer(group), value);
}
// Start comparing the value we received, along with the grouping index.
// if no matching value is found in the previous row then return the value.
// if a matching value is found then this value should not get printed out
// so return an empty String
if (this.previousRow.containsKey(new Integer(group)))
{
for (int j = 1; j <= group; j++)
{
if (!((String) this.previousRow.get(new Integer(j))).equals((this.nextRow.get(new Integer(j)))))
{
// no match found so return this value back to the caller.
return value;
}
}
}
// This is used, for when there is no data in the previous row,
// It gets used only the first time.
if (this.previousRow.size() == 0)
{
return value;
}
// There is corresponding value in the previous row
// this value doesn't need to be printed, return an empty String
return TagConstants.EMPTY_STRING;
}
/**
* Writes the table body content.
* @throws ObjectLookupException for errors in looking up properties in objects
* @throws DecoratorException for errors returned by decorators
*/
private void writeTableBody() throws ObjectLookupException, DecoratorException
{
JspWriter out = this.pageContext.getOut();
// Ok, start bouncing through our list (only the visible part)
RowIterator rowIterator = this.tableModel.getRowIterator(false);
// iterator on rows
while (rowIterator.hasNext())
{
Row row = rowIterator.next();
if (log.isDebugEnabled())
{
log.debug("[" + getUid() + "] rowIterator.next()=" + row);
}
if (this.tableModel.getTableDecorator() != null)
{
String stringStartRow = this.tableModel.getTableDecorator().startRow();
if (stringStartRow != null)
{
write(stringStartRow, out);
}
}
// open tr
write(row.getOpenTag(), out);
// iterator on columns
if (log.isDebugEnabled())
{
log.debug("[" + getUid() + "] creating ColumnIterator on " + this.tableModel.getHeaderCellList());
}
ColumnIterator columnIterator = row.getColumnIterator(this.tableModel.getHeaderCellList());
while (columnIterator.hasNext())
{
Column column = columnIterator.nextColumn();
// Get the value to be displayed for the column
write(column.getOpenTag(), out);
String value = column.getChoppedAndLinkedValue();
// check if column is grouped
if (column.getGroup() != -1)
{
value = this.groupColumns(value, column.getGroup());
}
// add column value
write(value, out);
write(column.getCloseTag(), out);
}
// no columns?
if (this.tableModel.isEmpty())
{
if (log.isDebugEnabled())
{
log.debug("[" + getUid() + "] table has no columns");
}
write(TagConstants.TAG_TD_OPEN, out);
write(row.getObject().toString(), out);
write(TagConstants.TAG_TD_CLOSE, out);
}
// close tr
write(row.getCloseTag(), out);
if (this.tableModel.getTableDecorator() != null)
{
String endRow = this.tableModel.getTableDecorator().finishRow();
if (endRow != null)
{
write(endRow, out);
}
}
}
if (this.tableModel.getRowListPage().size() == 0)
{
write(MessageFormat.format(properties.getEmptyListRowMessage(), new Object[]{new Integer(this.tableModel
.getNumberOfColumns())}), out);
}
}
/**
* Generates table footer with links for export commands.
*/
private void writeTableFooter()
{
// Put the page stuff there if it needs to be there...
if (this.properties.getAddPagingBannerBottom())
{
writeSearchResultAndNavigation();
}
// add export links (only if the table is not empty)
if (this.export && this.tableModel.getRowListPage().size() != 0)
{
writeExportLinks();
}
}
/**
* generates the search result and navigation bar.
*/
private void writeSearchResultAndNavigation()
{
if (this.pagesize != 0 && this.listHelper != null)
{
// create a new href
Href navigationHref = new Href(this.baseHref);
write(this.listHelper.getSearchResultsSummary());
write(this.listHelper.getPageNavigationBar(
navigationHref,
encodeParameter(TableTagParameters.PARAMETER_PAGE)));
}
}
/**
* Writes the formatted export links section.
*/
private void writeExportLinks()
{
// Figure out what formats they want to export, make up a little string
Href exportHref = new Href(this.baseHref);
StringBuffer buffer = new StringBuffer(200);
Iterator iterator = MediaTypeEnum.iterator();
while (iterator.hasNext())
{
MediaTypeEnum currentExportType = (MediaTypeEnum) iterator.next();
if (this.properties.getAddExport(currentExportType))
{
if (buffer.length() > 0)
{
buffer.append(this.properties.getExportBannerSeparator());
}
exportHref.addParameter(encodeParameter(TableTagParameters.PARAMETER_EXPORTTYPE), currentExportType
.getCode());
// export marker
exportHref.addParameter(TableTagParameters.PARAMETER_EXPORTING, "1");
Anchor anchor = new Anchor(exportHref, this.properties.getExportLabel(currentExportType));
buffer.append(anchor.toString());
}
}
String[] exportOptions = {buffer.toString()};
write(MessageFormat.format(this.properties.getExportBanner(), exportOptions));
}
/**
* Called by the setProperty tag to override some default behavior or text String.
* @param propertyName String property name
* @param propertyValue String property value
*/
public void setProperty(String propertyName, String propertyValue)
{
this.properties.setProperty(propertyName, propertyValue);
}
/**
* @see javax.servlet.jsp.tagext.Tag#release()
*/
public void release()
{
super.release();
// tag attributes
this.decoratorName = null;
this.defaultSortedColumn = -1;
this.defaultSortOrder = null;
this.export = false;
this.length = 0;
this.listAttribute = null;
this.name = null;
this.offset = 0;
this.pagesize = 0;
this.property = null;
this.requestUri = null;
this.dontAppendContext = false;
this.scope = null;
this.sortFullTable = null;
this.excludedParams = null;
this.filteredRows = null;
this.uid = null;
}
/**
* Returns the name.
* @return String
*/
public String getName()
{
return this.name;
}
/**
* encode a parameter name to be unique in the page using ParamEncoder.
* @param parameterName parameter name to encode
* @return String encoded parameter name
*/
private String encodeParameter(String parameterName)
{
// paramEncoder has been already instantiated?
if (this.paramEncoder == null)
{
// use the id attribute to get the unique identifier
this.paramEncoder = new ParamEncoder(getUid());
}
return this.paramEncoder.encodeParameterName(parameterName);
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?