📄 tablemodelutils.java
字号:
/* * Copyright 2004 original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package org.extremecomponents.table.core;import java.util.ArrayList;import java.util.Collection;import java.util.List;import org.apache.commons.lang.StringUtils;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.extremecomponents.table.bean.Column;import org.extremecomponents.table.callback.FilterRowsCallback;import org.extremecomponents.table.callback.SortRowsCallback;import org.extremecomponents.table.cell.Cell;import org.extremecomponents.table.limit.Limit;import org.extremecomponents.table.limit.Sort;/** * Helpful utilities directly related to the TableModel. * * @author Jeff Johnston */public final class TableModelUtils { private static Log logger = LogFactory.getLog(TableModelUtils.class); private TableModelUtils() { } public static Collection filterRows(BaseModel model, Collection rows) throws Exception { boolean filtered = model.getLimit().isFiltered(); boolean cleared = model.getLimit().isCleared(); if (!filtered || cleared) { return rows; } FilterRowsCallback filterRowsCallback = (FilterRowsCallback) model.getCallback(model.getTableHandler() .getTable().getFilterRowsCallback()); return filterRowsCallback.filterRows(model, rows); } public static Collection sortRows(BaseModel model, Collection rows) throws Exception { boolean sorted = model.getLimit().isSorted(); if (sorted) { SortRowsCallback sortRowsCallback = (SortRowsCallback) model.getCallback(model.getTableHandler().getTable() .getSortRowsCallback()); return sortRowsCallback.sortRows(model, rows); } return rows; } public static Collection currentRows(BaseModel model, Collection rows) { Limit limit = model.getLimit(); if (limit.getRowStart() >= rows.size()) { return rows; } Collection results = new ArrayList(); for (int i = limit.getRowStart(); i < limit.getRowEnd(); i++) { Object bean = ((List) rows).get(i); results.add(bean); } return results; } public static boolean isSorted(BaseModel model, String property) { Sort sort = model.getLimit().getSort(); if (property.equals(sort.getProperty())) { return true; } return false; } public static boolean isFirstPageEnabled(int page) { if (page == 1) { return false; } return true; } public static boolean isPrevPageEnabled(int page) { if (page - 1 < 1) { return false; } return true; } public static boolean isNextPageEnabled(int page, int totalPages) { if (page + 1 > totalPages) { return false; } return true; } public static boolean isLastPageEnabled(int page, int totalPages) { if (page == totalPages || totalPages == 0) { return false; } return true; } public static int getTotalPages(BaseModel model) { int currentRowsDisplayed = model.getLimit().getCurrentRowsDisplayed(); if (currentRowsDisplayed == 0) { currentRowsDisplayed = model.getLimit().getTotalRows(); } int totalRows = model.getLimit().getTotalRows(); int totalPages = 1; if (currentRowsDisplayed != 0) { totalPages = totalRows / currentRowsDisplayed; } if ((currentRowsDisplayed != 0) && ((totalRows % currentRowsDisplayed) > 0)) { totalPages++; } return totalPages; } /** * Determine whether or not this is a resource bundle key. It is a resource * bundle key if the value has a '.' character in it. * * @param value * The value that will be inspected to find out if resource key * @return True if this is a resource bundle key */ public static boolean isResourceBundleProperty(String value) { if (StringUtils.contains(value, ".")) { return true; } return false; } public static String getImage(BaseModel model, String imageName) { String imagePath = model.getTableHandler().getTable().getImagePath(); if (StringUtils.isNotBlank(imagePath)) { int index = imagePath.indexOf("*."); return imagePath.substring(0, index) + imageName + imagePath.substring(index + 1); } return null; } /** * Build up the cells in the row. Will either load up a known cell type, or * just instantiate a new Cell using the fully qualified package name. * * NOTE: Whoever calls this is responsible for cleaning up the cell. */ public static Cell buildCell(BaseModel model, Column column, Object value, Object propertyValue) { String cellName = column.getCell(); String className = model.getProperties().getProperty(TableProperties.CELL + cellName); return initCell(model, column, value, propertyValue, cellName, className); } /** * Build up the cells in the row. Will either load up a known cell type, or * just instantiate a new Cell using the fully qualified package name. * * NOTE: Whoever calls this is responsible for cleaning up the cell. */ public static Cell buildFilterCell(BaseModel model, Column column, Object value) { String cellName = column.getFilterCell(); String className = model.getProperties().getProperty(TableProperties.FILTER_CELL + cellName); return initCell(model, column, value, value, cellName, className); } /** * Build up the cells in the row. Will either load up a known cell type, or * just instantiate a new Cell using the fully qualified package name. * * NOTE: Whoever calls this is responsible for cleaning up the cell. */ public static Cell buildHeaderCell(BaseModel model, Column column, Object value) { String cellName = column.getHeaderCell(); String className = model.getProperties().getProperty(TableProperties.HEADER_CELL + cellName); return initCell(model, column, value, value, cellName, className); } private static Cell initCell(BaseModel model, Column column, Object value, Object propertyValue, String cellName, String cellClassName) { Cell cell = null; if (cellClassName == null) { cellClassName = cellName; } try { cell = model.getCachedCell(Class.forName(cellClassName)); column.setValue(value); column.setPropertyValue(propertyValue); cell.init(model, column); } catch (Exception e) { String msg = "Could not build the cell for column [" + column.getProperty() + "]. The class was not found or does not exist."; logger.error(msg, e); throw new RuntimeException(msg); } return cell; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -