TableurModel.java 5.72 KB
package osp.utils;

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

import javax.swing.table.AbstractTableModel;

import osp.SkyObject;

public class TableurModel extends AbstractTableModel {
	
	private static final long serialVersionUID = 1L;

	private int NB_COL=9; // Come from MainFrame
	/** Required columns in tab :
	 * "Selected - Num obj(proj) - Id obj(when imported) - IsRef - Priority -catalog from -  Affected Slit - Ra - Dec - 
	 *  others columns are optionnals 
	 * */
	private String[] colType = 
		{ "int", "String", "boolean", "String", "String", "String", "String", "String", "String" };
	private int valMaxCol=Integer.MAX_VALUE;

	private int[] colSize = {45, valMaxCol, 40, 55, 45, 40, valMaxCol, valMaxCol, valMaxCol};
    private String  	colString[] = {"num", "IdOb", "IsRef", "Priority", "Slit", " Cat", "RA", "DEC", " " };

	private  boolean[]   colEditable = { false, false, true, true, false,
	                                                  false, false, false, false};
	 
	public int num, id, ref,ra,dec, prior, slit, nbcat;
	
	private ArrayList<String> colList;
	 
		 
	private Object[][] donnees;
//	private OSPE_VisuPanel vPanel;
	
	/**
	 * Constructor
	 */
	public TableurModel() {
		attributeNumCol();
		Object[][] objectsData = new Object[0][NB_COL];
		fillTableModelPerso(objectsData);
	}



	/** Test Constructor 
	 * 
	 * @param visuPanel
	 * @param colString2
	 * @param colEditable2
	 * @param colType2
	 */
//	public TableurModel(OSPE_VisuPanel visuPanel, String[] colString2, boolean[] colEditable2,
	public TableurModel(String[] colString2, boolean[] colEditable2,String[] colType2) {
		// TODO Auto-generated constructor stub
		Object[][] objectsData = new Object[0][NB_COL];
		fillTableModelPerso(objectsData);
	}

	public void fillTableModelPerso(Object[][] data) {
		donnees = new Object[data.length][NB_COL];

		for (int i = 0; i < data.length; i++) {
			for (int j = 0; j < NB_COL; j++) {
								
				donnees[i][j] = data[i][j];
			}
		}
	}

	@Override
	public boolean isCellEditable(int row, int col) {
		return colEditable[col];
	}

	@Override
	public int getColumnCount() {
		return (NB_COL);
	}

	@Override
	public Object getValueAt(int parm1, int parm2) {
		return donnees[parm1][parm2];
	}

	@Override
	public int getRowCount() {
		return donnees.length;
	}

	@Override
	public String getColumnName(int col) {
		return colString[col];
	}

	public Object[][] getDonnees() {
		return donnees;
	}

	@Override
	public void setValueAt(Object value, int row, int col) {
		if (value != null) {
			if (value.getClass() != getColumnClass(col))
				donnees[row][col] = null;
			else
				donnees[row][col] = value;
		}
		fireTableDataChanged();
	} 

	public void clear() {
		for (int i = 0; i < colString.length; i++)
			donnees[i] = null;
		fireTableDataChanged();
	}

	@Override
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public Class getColumnClass(int columnIndex) {
		Object valueAt = getValueAt(0, columnIndex);
		if (valueAt == null)
			return String.class;
		else 
			return valueAt.getClass();
	}
	/**  Not used for the moment
	 * Get the column type  
	 * @param col : column number. 
	 * @return column type 
	 */
	public Object getTypecol(int col) {
		return colSize[col];
	}

	/**  Not used for the moment
	 * Get the column size  
	 * @param col : column number. 
	 * @return column size 
	 */
	public int getSizecol(int col) {
		return colSize[col];
	}
	
	/** 
	 *  Not used for the moment - data are filled in visu. 
	 *  == Ne sert pas pour le moment.
	 *  Les donnees sont remplies ds visu - getObjectProp()
	 * ce qui n'est pas top. A revoir. 
	 * @param objectlist
	 */	
	  public void setnewData(List<SkyObject> objectlist)
	    {
	        Object[][]              data = new Object[][]{};
	        int                     nb_Object;
	        
	        
	        if (objectlist != null)
	        {
	            //priority = projectControler.getImageControler().getCurrentImage().getPriorityChoose();
	            nb_Object = objectlist.size();
	            data = new Object[nb_Object][NB_COL]; 
	            for (int i = 0; i < objectlist.size() && nb_Object > 0; i++)
	            {
	            		SkyObject object = objectlist.get(i);
	            		//SkyObject skObj= image.getObject(i);
//	                if (priority == 0 || objectModel.getPriority() == priority)
//	                {
	                    for (int j = 0; j < NB_COL; j++) {
	                        if (j == 0)	
	                            data[i][j] = object.getNum();
	                        else if (j == 1)
	                            data[i][j] = object.isRef();
	                        else if (j == 2)
	                            data[i][j] = object.getObjWorldCenter().getRA();
	                        else if (j == 3)
	                            data[i][j] = object.getObjWorldCenter().getDec();
	                        else if (j == 4)
	                            data[i][j] = object.getSrc();
	                        else if (j == 5)
	                            data[i][j] = String.valueOf(object.getPriority());
	                    //}
	                }
	            }
	        }
	        this.donnees = data;
	        fireTableDataChanged();
	    }
	    
		
	    private void attributeNumCol(){
			 
			/** Set the value column number **/
	    	
		for (int i=0;i<NB_COL;i++)
		{
		  num=0;
		  id=1;
		  nbcat=2;
		  ref=3;
		  prior=4;
		  slit=5;
		  ra=6;
		  dec=7;
		}
		  }

	    /**
	     * Adjust colon size  
	     * @return Table colon size
	     */
		public int[] getColSize() {
			// TODO Auto-generated method stub
			return colSize;
		} 
	    
	    
}