Package syn3d.ui

Source Code of syn3d.ui.SceneGraphDataTableModel

/* ========================
* JSynoptic : a free Synoptic editor
* ========================
*
* Project Info:  http://jsynoptic.sourceforge.net/index.html
*
* This program is free software; you can redistribute it and/or modify it under the terms
* of the GNU Lesser General Public License as published by the Free Software Foundation;
* either version 2.1 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with this
* program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA.
*
* (C) Copyright 2001-2005, by :
*     Corporate:
*         Astrium SAS
*     Individual:
*         Ronan Ogor
*
* $Id: SceneGraphDataTableModel.java,v 1.6 2005/11/15 14:27:21 ogor Exp $
*
* Changes
* -------
* 30 juin 2005 : Creation date (RO);
*
*/
package syn3d.ui;

import javax.swing.table.AbstractTableModel;


import simtools.data.DataInfo;
import simtools.data.DataSource;
import syn3d.data.SceneGraphData;
import syn3d.data.SceneGraphDoubleData;
import syn3d.data.SceneGraphFloatData;
import simtools.ui.MenuResourceBundle;
import simtools.ui.SourceTree;


/**
* This abstract class manages a data table used in a user interface with a SourceTree
* Extended classes have to implement the method getName() and to set the attributes
* data & dataCopy as non abstract objects.
* @author ogor
*/
public abstract class SceneGraphDataTableModel extends AbstractTableModel{

  protected static MenuResourceBundle resources=SceneGraphModel.resources;
  private final SourceTree dstree;
  protected SceneGraphData dataCopy;
 
 
  public SceneGraphDataTableModel(SourceTree sourceTree, SceneGraphData dcopy){
    super();
    dstree = sourceTree;
    dataCopy = dcopy;
  }
 
  /* (non-Javadoc)
   * @see javax.swing.table.TableModel#getColumnCount()
   */
  public int getColumnCount() {
    return 4;
  }

  /* (non-Javadoc)
   * @see javax.swing.table.TableModel#getRowCount()
   */
  public int getRowCount() {
    return dataCopy.length();
  }
 
  public String getColumnName(int columnIndex){
    return ""+columnIndex;
  }
 
  public Class getColumnClass(int columnIndex){
    if(columnIndex==2){
      return Boolean.class;
    }
    return super.getColumnClass(columnIndex);
  }

  /* (non-Javadoc)
   * @see javax.swing.table.TableModel#getValueAt(int, int)
   */
  public Object getValueAt(int rowIndex, int columnIndex) {
    switch(columnIndex){
      case 0:
        return getName(rowIndex);
      case 1:
        return getValue(rowIndex);
      case 2:
        return Boolean.valueOf(dataCopy.getDataSource(rowIndex)!=null);
      case 3:
        DataSource ds=dataCopy.getDataSource(rowIndex);
        if(ds==null){
          return "";
        }
        return DataInfo.getLabel(ds);
      default:
        throw new IllegalArgumentException();
    }
  }
 
  public abstract String getName(int index);
 
  public boolean isCellEditable(int row, int col) {
    if(col==0 || col==3){
      return false;
    }
    if(col==2){
      boolean edit=getValueAt(row,col).equals(Boolean.TRUE);
      if(!edit){
        edit= dstree.getSelectedSourceOrCollection() instanceof DataSource;
      }
      return dataCopy.isEditable() && edit;
    }
    return dataCopy.isEditable() && (dataCopy.getDataSource(row)==null);
  }
 
  public void setValueAt(Object aValue, int row, int column) {
    if(column==1){
      setValue(row, aValue);
    }
    else if(column==2){
      if(aValue.equals(Boolean.TRUE)){
        Object o= dstree.getSelectedSourceOrCollection();
        if (o instanceof DataSource){
          dataCopy.setDataSource(row, (DataSource)o);
          fireTableCellUpdated(row,3);
        }
      }
      else{
        dataCopy.setDataSource(row, null);
        fireTableCellUpdated(row,3);
      }
    }
    else{
      throw new IllegalArgumentException();
    }
  }


  /**
   * A default implementation to handle SceneGraphFloatData
   * @param index=rowIndex
   * @param aValue=object displayed in the table
   */
  public void setValue(int index, Object aValue){
    if(dataCopy instanceof SceneGraphFloatData){
      SceneGraphFloatData df=(SceneGraphFloatData)dataCopy;
      df.setValue(index, Float.parseFloat((String)aValue));
    }
    else if(dataCopy instanceof SceneGraphDoubleData){
      SceneGraphDoubleData dd =(SceneGraphDoubleData)dataCopy;
      dd.setValue(index, Double.parseDouble((String)aValue));
     
    }
  }
   
  /**
   * A default implementation to handle SceneGraphFloatData
   * @param index=rowIndex
   * @return object to be displayed in the table
   */
  public Object getValue(int index){
    if(dataCopy instanceof SceneGraphFloatData){
      SceneGraphFloatData df=(SceneGraphFloatData)dataCopy;
      return new Float(df.getValue(index));
    }
    else if(dataCopy instanceof SceneGraphDoubleData){
      SceneGraphDoubleData dd =(SceneGraphDoubleData)dataCopy;
      return new Double(dd.getValue(index));
     
    }
    return null;
  }
   
 
}
TOP

Related Classes of syn3d.ui.SceneGraphDataTableModel

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.