⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jmtablemodel.java

📁 梦界家园程序开发基底框架
💻 JAVA
字号:
package jm.framework.gui.module.jmtable.model ;

import javax.swing.table.DefaultTableModel;

import jm.framework.gui.module.jmtable.JMTable;
import jm.util.JM2DArray;
import jm.util.JMVList;
import jm.util.JMVal;
import jm.util.JMVector;

/**
 * <p>Title: </p>
 *
 * <p>@author Spook</p>
 *
 * <p>@version 0.1.0</p>
 *
 * <p> </p> not attributable
 */
public class JMTableModel
    extends DefaultTableModel
{
    /**
	 *
	 */
	private static final long serialVersionUID = -6832470731923210887L;
	public static final String CLASS_NAME = "jmtableModel" ;
    public static final int CELL_ALL_EDIT = 999 ;
    private JM2DArray rowdata = null ;
    private JM2DArray otypes = null ;
    private JMVector<String> coldata = null ;
    private boolean[] etypes = null ;
    private JMTable owner = null;
    public JMTableModel ()
    {
        super () ;
    }

    public JMTableModel ( JMVector<String> ocoldata , JM2DArray orowdata )
    {
        this () ;
        rowdata = orowdata ;
        coldata = ocoldata ;
    }

//    public JMTableModel ( JMVector<JMVList> oRowData , JMVector<String> oColData )
//    {
//        this () ;
//        coldata = oColData ;
//    }

    public JMTableModel ( JM2DArray oRowData )
    {
        this () ;
        rowdata = oRowData ;
        coldata = getColData () ;
    }

    public JMTableModel ( JMVector<String> oColData )
    {
        this () ;
        coldata = oColData ;
    }

    public void setJMTable(JMTable table){
    	owner = table;
    }

    public void setCellEdit ( JMVList colsType )
    {
        if ( etypes == null ) etypes = new boolean[this.getColumnCount () ] ;
        for ( int i = 0 ; i < this.getColumnCount () ; i++ )
        {
            try
            {
                etypes[ i ] = colsType.get ( i ).getBooleanValue () ;
            }
            catch ( Exception e )
            {
                etypes[ i ] = false ;
            }
        }
    }

    public void setCellEdit ( JM2DArray colsType )
    {
        otypes = colsType ;
    }

    public void setCellEditers ()
    {
        try
        {
        	for(int i =0; i < this.getColumnCount();i++){
        		setCellEdit(i);
        	}
        }
        catch ( Exception e )
        {
        }
    }

    public void setCellEdit ( int colindex )
    {
        try
        {
            if ( etypes == null ) etypes = new boolean[this.getColumnCount () ] ;
            etypes[ colindex ] = true ;
        }
        catch ( Exception e )
        {
            etypes[ colindex ] = false ;
        }
    }

    public boolean addRow ( JMVector<JMVal> row )
    {
        try
        {
            if ( rowdata.addRow ( row ) )
            {
                this.fireTableDataChanged () ;
                return true ;
            }
            else
            {
                return false ;
            }
        }
        catch ( Exception ex )
        {
        	ex.printStackTrace();
            return false ;
        }
    }

    public boolean addColumn ( String colName , JMVList columnData )
    {
        try
        {
            if ( columnData.isSame () && !coldata.contains ( colName ) )
            {
                rowdata.addCol ( colName , columnData ) ;
                coldata.add ( colName ) ;
//                this.fireTableStructureChanged();
                this.fireTableDataChanged () ;
                return true ;

            }
            return false ;
        }
        catch ( Exception ex )
        {
        	ex.printStackTrace();
            return false ;
        }
    }

    public boolean isCellEditable ( int row , int col )
    {
        try
        {
            if ( otypes != null && otypes.rowCount () > 0 )
            {
                return otypes.getBooleanValue ( row , col ) ;
            }
            return etypes[ col ] ;
        }
        catch ( Exception ex )
        {
        	//ex.printStackTrace();
            return false ;
        }
    }



    public String getColumnName ( int columnIndex )
    {
        try
        {
            return "" + coldata.get ( columnIndex ) ;
        }
        catch ( Exception ex )
        {
        	ex.printStackTrace();
            return "" + columnIndex ;
        }

    }

    public String getColumnKeyName ( int columnIndex )
    {
        try
        {
            String col = "" ;
            if ( rowdata != null && rowdata.rowCount () != 0 )
            {
                col = "" + rowdata.getColName ( columnIndex ) ;
            }
            return col ;
        }
        catch ( Exception ex )
        {
        	ex.printStackTrace();
            return "" + columnIndex ;
        }
    }

    public boolean setRowData ( JM2DArray oRowData )
    {
        try
        {
            rowdata = oRowData ;
            if ( rowdata != null ){
                coldata = rowdata.getColNames();
            }
            return true ;
        }
        catch ( Exception ex )
        {
        	ex.printStackTrace();
            return false ;
        }
    }

    public JM2DArray getRowData ()
    {
        return rowdata ;
    }

    public boolean setColNames ( JMVector<String> oColData )
    {
        try
        {
            coldata = oColData ;
           int length = 0;
           int columnsWidth = 0;
            for (  int i=0;i<oColData.size();i++ )
            {
            	String s = coldata.get(i);
            	length = s.length()*7+15;
            	columnsWidth = columnsWidth + length;
//                owner.addColumn(new JMTableColumn(i));
                super.addColumn ( s ) ;
                owner.setColWidth(i, length);
            }
//            System.out.println(columnsWidth);
            owner.setColumnsWidth(columnsWidth);
            return true ;
        }
        catch ( Exception ex )
        {
//        	ex.printStackTrace();
            return false ;
        }
    }

    public JMVector<String> getColData ()
    {
        try
        {
            if ( coldata != null && coldata.size () > 0 )
            {
                return coldata ;
            }
            else
            {
                coldata = ( JMVector<String> ) rowdata.getColNames () ;
                return coldata ;
            }
        }
        catch ( Exception ex )
        {
//        	ex.printStackTrace();
            return null ;
        }
    }

    /**
     * Returns the number of columns in the model.
     *
     * @return the number of columns in the model
     * @todo Implement this javax.swing.table.TableModel method
     */
    public int getColumnCount ()
    {
        int colCount = 0 ;
        if ( coldata != null )
        {
            colCount = coldata.size () ;
        }
        else
        {
            if ( rowdata != null ) {
            	colCount = rowdata.colCount () ;
            }
        }
        return colCount ;
    }

    /**
     * Returns the number of rows in the model.
     *
     * @return the number of rows in the model
     * @todo Implement this javax.swing.table.TableModel method
     */
    public int getRowCount ()
    {
        int rowCount = 0 ;
        if ( rowdata != null ) rowCount = rowdata.rowCount () ;
        return rowCount ;
    }

    /**
     * Returns the value for the cell at <code>columnIndex</code> and
     * <code>rowIndex</code>.
     *
     * @param rowIndex the row whose value is to be queried
     * @param columnIndex the column whose value is to be queried
     * @return the value Object at the specified cell
     * @todo Implement this javax.swing.table.TableModel method
     */
    public Object getValueAt ( int rowIndex , int columnIndex )
    {
        try
        {
            return rowdata.getItemBody ( rowIndex , columnIndex ) ;
            //return rowdata.getStringValue( rowIndex , columnIndex ) ;
        }
        catch ( Exception ex )
        {
//        	ex.printStackTrace();
            return new JMVal ( "" ) ;
//            return new JMVal ( rowIndex + " : " + columnIndex ) ;
            //return rowIndex + " : " + columnIndex ;
        }
    }

    public void removeRow ( int rowIndex )
    {
        try
        {
            rowdata.delRow ( rowIndex ) ;
            this.fireTableDataChanged () ;
        }
        catch ( Exception ex )
        {
//        	ex.printStackTrace();
        }
    }

    public void setValueAt ( Object aValue ,
                             int rowIndex ,
                             int columnIndex )
    {
        try
        {
            if ( rowdata != null )
            {
                rowdata.replaceObject ( rowIndex , columnIndex , ( JMVal ) aValue ) ;
            }
            fireTableCellUpdated(rowIndex, columnIndex);
        }
        catch ( Exception ex )
        {
//ex.printStackTrace();
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -