All Downloads are FREE. Search and download functionalities are using the official Maven repository.

weka.gui.beans.InteractiveTableModel Maven / Gradle / Ivy

Go to download

The Waikato Environment for Knowledge Analysis (WEKA), a machine learning workbench. This version represents the developer version, the "bleeding edge" of development, you could say. New functionality gets added to this version.

There is a newer version: 3.9.6
Show newest version
/*
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 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 General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see .
 */

/*
 *    InteractiveTableModel.java
 *    Copyright (C) 2011-2013 University of Waikato, Hamilton, New Zealand
 */

package weka.gui.beans;

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

import javax.swing.table.AbstractTableModel;

/**
 * Table model that automatically adds a new row to the table on pressing enter
 * in the last cell of a row.
 * 
 * @author Mark Hall (mhall{[at]}pentaho{[dot]}com)
 * @version $Revision: 47640 $
 */
public class InteractiveTableModel extends AbstractTableModel {

  /**
   * For serialization
   */
  private static final long serialVersionUID = -5113873323690309667L;

  /** Index of the hidden column */
  public int m_hidden_index;

  /** The names of the columns */
  protected String[] m_columnNames;

  /** Holds the data */
  protected List> m_dataVector;

  /**
   * Constructor
   * 
   * @param columnNames the names of the columns
   */
  public InteractiveTableModel(String[] columnNames) {
    m_columnNames = columnNames;
    m_dataVector = new ArrayList>();
    m_hidden_index = columnNames.length - 1;
  }

  @Override
  public String getColumnName(int column) {
    return m_columnNames[column];
  }

  @Override
  public boolean isCellEditable(int row, int column) {
    if (column == m_hidden_index) {
      return false;
    }
    return true;
  }

  @Override
  public Class getColumnClass(int column) {
    return String.class;
  }

  @Override
  public Object getValueAt(int row, int column) {
    if (column >= m_columnNames.length) {
      return new Object();
    }

    List rowData = m_dataVector.get(row);
    return rowData.get(column);
  }

  @Override
  public void setValueAt(Object value, int row, int column) {
    if (column >= m_columnNames.length) {
      System.err.println("Invalid index");
    }

    List rowData = m_dataVector.get(row);
    rowData.set(column, value.toString());
    fireTableCellUpdated(row, column);
  }

  @Override
  public int getRowCount() {
    return m_dataVector.size();
  }

  @Override
  public int getColumnCount() {
    return m_columnNames.length;
  }

  /**
   * Returns true if the model has an empty row
   * 
   * @return true if the model has an empty row
   */
  public boolean hasEmptyRow() {
    if (m_dataVector.size() == 0) {
      return false;
    }

    List dataRow = m_dataVector.get(m_dataVector.size() - 1);
    for (String s : dataRow) {
      if (s.length() != 0) {
        return false;
      }
    }

    return true;
  }

  /**
   * Adds an empty row to the model
   */
  public void addEmptyRow() {
    ArrayList empty = new ArrayList();
    for (int i = 0; i < m_columnNames.length; i++) {
      empty.add("");
    }
    m_dataVector.add(empty);
    fireTableRowsInserted(m_dataVector.size() - 1, m_dataVector.size() - 1);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy