net.kamhon.ieagle.swing.table.KTableModel.java Source code

Java tutorial

Introduction

Here is the source code for net.kamhon.ieagle.swing.table.KTableModel.java

Source

/*
 * Copyright 2012 Eng Kam Hon (kamhon@gmail.com)
 * 
 * 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 net.kamhon.ieagle.swing.table;

import java.util.ArrayList;
import java.util.List;

import javax.swing.table.AbstractTableModel;

import net.kamhon.ieagle.exception.DataException;
import net.kamhon.ieagle.util.CollectionUtil;

import org.apache.commons.beanutils.NestedNullException;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class KTableModel<T> extends AbstractTableModel {
    private static final long serialVersionUID = 1L;

    private static final Log log = LogFactory.getLog(KTableModel.class);

    @SuppressWarnings("unused")
    private KTable<T> table;
    private List<T> datas = new ArrayList<T>();
    private Object[][] dataIn2DArray;

    private List<KTableColumn> columns;
    private boolean hasRowCheckBox;

    public KTableModel(KTable<T> table, List<KTableColumn> columns) {
        this.table = table;
        this.columns = columns;

        for (KTableColumn column : columns) {
            if (column instanceof KTableRowCheckBoxColumn) {
                hasRowCheckBox = true;
            }
        }
    }

    @Override
    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
        log.debug("Method Name = " + "setValueAt()");
        log.debug("aValue = " + aValue);
        log.debug("rowIndex = " + rowIndex);
        log.debug("columnIndex = " + columnIndex);

        /*KTableColumn column = columns.get(columnIndex);
        if (column instanceof KTableRowCheckBoxColumn) {
           int[] selectedRows = table.getSelectedColumns();
           boolean isSelected = false;
           for (int selectedRow : selectedRows) {
          if (columnIndex == selectedRow) {
             isSelected = true;
             break;
          }
           }
            
           if (aValue.equals(true)) {
          if (!isSelected)
             table.changeSelection(rowIndex, columnIndex, true, true);
           } else {
          if (isSelected)
             table.changeSelection(rowIndex, columnIndex, true, true);
           }
            
        }*/

        dataIn2DArray[rowIndex][columnIndex] = aValue;
    }

    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        log.debug("columnIndex = " + columnIndex);

        KTableColumn column = columns.get(columnIndex);
        if (column instanceof KTableCheckBoxColumn) {
            return true;
        } else if (column instanceof KTableTextColumn) {
            return true;
        }

        return super.isCellEditable(rowIndex, columnIndex);
    }

    @Override
    public int getRowCount() {
        if (CollectionUtil.isNotEmpty(datas)) {
            // log.debug("Method Name = " + "getRowCount() - " + datas.size());
            return datas.size();
        }

        return 0;
    }

    @Override
    public int getColumnCount() {
        if (CollectionUtil.isNotEmpty(columns)) {
            // log.debug("Method Name = " + "getColumnCount() - " + columns.size());
            return columns.size();
        }
        return 0;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        if (rowIndex >= getRowCount()) {
            return new Object();
        }
        if (dataIn2DArray != null) {
            KTableCustomColumnValue customCol = getCustomColumnValue(columnIndex);
            if (customCol != null) {
                return customCol.getValueAt(dataIn2DArray[rowIndex][columnIndex], rowIndex, columnIndex);
            }
            return dataIn2DArray[rowIndex][columnIndex];
        }
        return new Object();
    }

    private KTableCustomColumnValue getCustomColumnValue(int columnIndex) {
        if (CollectionUtil.isEmpty(columns) || columnIndex >= columns.size())
            return null;

        KTableColumn column = columns.get(columnIndex);
        return column.getCustomColumnValue();
    }

    public void setData(List<T> datas) {
        this.datas = datas;
        // this.dataIn2DArray = CollectionUtil.to2DArray(datas, getColumnNames(), true);
        this.dataIn2DArray = generateDataIn2DArray();
        fireTableDataChanged();
    }

    private Object[][] generateDataIn2DArray() {
        if (CollectionUtil.isEmpty(datas)) {
            return new Object[0][0];
        }

        List<Object[]> result = new ArrayList<Object[]>();
        for (Object object : datas) {
            List<Object> record = new ArrayList<Object>();

            for (KTableColumn column : columns) {
                try {
                    Object propValue = null;
                    if (StringUtils.isNotBlank(column.getColumnName()))
                        propValue = PropertyUtils.getProperty(object, column.getColumnName());

                    if (column instanceof KTableRowCheckBoxColumn) {
                        record.add(false);
                    } else if (column instanceof KTableCheckBoxColumn) {
                        if (propValue instanceof Boolean) {
                            record.add(propValue);
                        } else {
                            record.add(false);
                        }
                    } else {
                        record.add(propValue == null ? "" : propValue);
                    }
                } catch (NestedNullException ex) {
                    // if nested bean referenced is null
                    if (column instanceof KTableCheckBoxColumn) {
                        record.add(false);
                    } else {
                        record.add("");
                    }
                } catch (Exception ex) {
                    throw new DataException(ex);
                }
            }

            log.debug("record = " + CollectionUtil.toLog(record));

            result.add(record.toArray(new Object[0]));
        }

        return result.toArray(new Object[0][0]);
    }

    public void setColumns(List<KTableColumn> columns) {
        this.columns = columns;
    }

    public List<KTableColumn> getColumns() {
        return columns;
    }

    public boolean isHasRowCheckBox() {
        return hasRowCheckBox;
    }

    /**
     * 
     * @param rowIndex
     *            rowIndex in table(the rowIndex user see it at the time).
     * @return
     */
    public T getRecord(int rowIndex) {
        log.debug("rowIndex = " + rowIndex);
        return datas.get(rowIndex);
    }
}