FileDocCategorySizeDatePackage
WebTable.javaAPI DocExample11673Mon Jul 23 13:26:34 BST 2007org.apache.struts2.components.table

WebTable

public class WebTable extends org.apache.struts2.components.GenericUIBean

Fields Summary
private static final Log
LOG
public static final String
TEMPLATE
protected String
sortOrder
protected String
modelName
protected TableModel
model
protected WebTableColumn[]
columns
protected boolean
sortableAttr
protected int
sortColumn
protected int
curRow
Constructors Summary
public WebTable(com.opensymphony.xwork2.util.ValueStack stack, HttpServletRequest request, HttpServletResponse response)


           
        super(stack, request, response);
    
Methods Summary
public booleanend(java.io.Writer writer, java.lang.String body)

        if (sortableAttr && model instanceof SortableTableModel) {
            LOG.debug("we are looking for " + getSortColumnLinkName());

            String sortColumn = request.getParameter(getSortColumnLinkName());
            String sortOrder = request.getParameter(getSortOrderLinkName());

            try {
                if ((sortColumn != null) || (sortOrder != null)) {
                    if (sortColumn != null) {
                        try {
                            this.sortColumn = Integer.parseInt(sortColumn);
                        } catch (Exception ex) {
                            if (LOG.isDebugEnabled()) {
                                LOG.debug("coudn't convert column, take default");
                            }
                        }
                    }

                    if (sortOrder != null) {
                        this.sortOrder = sortOrder;
                    }
                } else {
                    LOG.debug("no sorting info in the request");
                }

                if (this.sortColumn >= 0) {
                    LOG.debug("we have the sortColumn " + Integer.toString(this.sortColumn));
                    LOG.debug("we have the sortOrder " + this.sortOrder);

                    try {
                        ((SortableTableModel) model).sort(this.sortColumn, this.sortOrder);
                    } catch (Exception ex) {
                        if (LOG.isDebugEnabled()) {
                            LOG.debug("couldn't sort the data");
                        }
                    }

                    LOG.debug("we just sorted the data");
                }
            } catch (Exception e) {
                throw new StrutsException("Error with WebTable: " + toString(e), e);
            }
        }

        return super.end(writer, body);
    
protected voidevaluateExtraParams()

        if (modelName != null) {
            modelName = findString(modelName);

            Object obj = stack.findValue(this.modelName);

            if (obj instanceof TableModel) {
                setModel((TableModel) obj);
            }
        }

        super.evaluateExtraParams();
    
public WebTableColumngetColumn(int index)

        try {
            return (columns[index]);
        } catch (Exception E) {
            //blank
        }

        return null;
    
public intgetColumnCount()

        return (columns.length);
    
public voidgetColumnDisplayName(int column)

        columns[column].getDisplayName();
    
public org.apache.struts2.components.table.renderer.CellRenderergetColumnRenderer(int column)

        return columns[column].getRenderer();
    
public WebTableColumn[]getColumns()

        return columns;
    
protected java.lang.StringgetDefaultTemplate()

        return TEMPLATE;
    
public java.lang.String[]getFormattedRow(int row)

        ArrayList data = new ArrayList(getNumberOfVisibleColumns());

        for (int i = 0; i < getColumnCount(); ++i) {
            if (columns[i].isVisible()) {
                data.add(columns[i].getRenderer().renderCell(this, model.getValueAt(row, i), row, i));
            }
        }

        return (String[]) data.toArray(new String[0]);
    
public javax.swing.table.TableModelgetModel()

        return (model);
    
public java.lang.StringgetModelName()

        return modelName;
    
protected intgetNumberOfVisibleColumns()

        int count = 0;

        for (int i = 0; i < columns.length; ++i) {
            if (!columns[i].isHidden()) {
                ++count;
            }
        }

        return count;
    
public java.lang.ObjectgetRawData(int row, int column)

        return model.getValueAt(row, column);
    
public java.util.IteratorgetRawDataRowIterator()

        return new WebTableRowIterator(this, WebTableRowIterator.RAW_DATA);
    
public java.lang.Object[]getRow(int row)

        ArrayList data = new ArrayList(getNumberOfVisibleColumns());

        for (int i = 0; i < getColumnCount(); ++i) {
            if (columns[i].isVisible()) {
                data.add(model.getValueAt(row, i));
            }
        }

        return data.toArray(new Object[0]);
    
public intgetRowCount()

        return model.getRowCount();
    
public java.util.IteratorgetRowIterator()

        return new WebTableRowIterator(this);
    
public intgetSortColumn()

        if (model instanceof SortableTableModel) {
            return ((SortableTableModel) model).getSortedColumnNumber();
        }

        return -1;
    
public java.lang.StringgetSortColumnLinkName()

        return "WEBTABLE_" + modelName + "_SORT_COLUMN";
    
public java.lang.StringgetSortOrder()

        if ((model instanceof SortableTableModel) && (getSortColumn() >= 0)) {
            return ((SortableTableModel) model).getSortedDirection(getSortColumn());
        }

        return SortableTableModel.NONE;
    
public java.lang.StringgetSortOrderLinkName()

        return "WEBTABLE_" + modelName + "_SORT_ORDER";
    
public booleanisColumnHidden(int column)

        return columns[column].isHidden();
    
public booleanisSortable()

        return sortableAttr;
    
public voidsetColumnDisplayName(int column, java.lang.String displayName)

        columns[column].setDisplayName(displayName);
    
public voidsetColumnHidden(int column, boolean hide)

        columns[column].setHidden(hide);
    
public voidsetColumnRenderer(int column, org.apache.struts2.components.table.renderer.CellRenderer renderer)

        columns[column].setRenderer(renderer);
    
public voidsetModel(javax.swing.table.TableModel model)

        this.model = model;
        columns = new WebTableColumn[this.model.getColumnCount()];

        for (int i = 0; i < columns.length; ++i) {
            columns[i] = new WebTableColumn(this.model.getColumnName(i), i);
        }

        if ((sortableAttr) && !(this.model instanceof SortableTableModel)) {
            this.model = new SortFilterModel(this.model);
        }
    
public voidsetModelName(java.lang.String modelName)
The name of model to use

s.tagattribute
required="true" type="String"

        this.modelName = modelName;
    
public voidsetSortColumn(int sortColumn)

        this.sortColumn = sortColumn;
    
public voidsetSortOrder(java.lang.String sortOrder)

        if (sortOrder.equals(SortableTableModel.NONE)) {
            this.sortOrder = SortableTableModel.NONE;
        } else if (sortOrder.equals(SortableTableModel.DESC)) {
            this.sortOrder = SortableTableModel.DESC;
        } else if (sortOrder.equals(SortableTableModel.ASC)) {
            this.sortOrder = SortableTableModel.ASC;
        } else {
            this.sortOrder = SortableTableModel.NONE;
        }
    
public voidsetSortable(boolean sortable)

        sortableAttr = sortable;

        if ((sortableAttr) && (model != null) && !(model instanceof SortableTableModel)) {
            model = new SortFilterModel(model);
        }