Java JTable Row checkRow(DefaultTableModel table, List data, int row)

Here you can find the source of checkRow(DefaultTableModel table, List data, int row)

Description

Checks if data could be written on the table starting from row.

License

LGPL

Parameter

Parameter Description
table a parameter
data a list of data containing the train id, train locations and their timing.
row starting row.

Return

true if we can schedule the train from the starting row, false otherwise.

Declaration

private static boolean checkRow(DefaultTableModel table, List<String[]> data, int row) 

Method Source Code

//package com.java2s;
//License from project: LGPL 

import java.util.List;

import javax.swing.table.DefaultTableModel;

public class Main {
    /**/*from  w w  w.  j a  v a2s.  co  m*/
     * Checks if data could be written on the table starting from
     * <code>row</code>.
     * 
     * @param table
     * @param data
     *            a list of data containing the train id, train locations and
     *            their timing.
     * @param row
     *            starting row.
     * @return <code>true</code> if we can schedule the train from the starting
     *         row, <code>false</code> otherwise.
     */
    private static boolean checkRow(DefaultTableModel table, List<String[]> data, int row) {
        String[] locations = data.get(1);
        String[] time = data.get(2);

        int rows = table.getRowCount();
        int cols = table.getColumnCount();
        int counter = 0;
        for (int i = 0; i < locations.length; i++) {
            if (i - 1 >= 0) {
                counter += Integer.parseInt(time[i - 1].trim());
            }
            for (int j = 0; j < Integer.valueOf(time[i].trim()); j++) {
                int column = table.findColumn(locations[i].trim());
                String val = null;
                if ((counter + row + j < rows) && (column < cols)) {
                    val = (String) table.getValueAt(counter + row + j, column);
                }
                // Check for interlocking
                if ((j == (Integer.valueOf(time[i].trim()) - 1)) && (counter + row + j + 1 < rows)) {
                    // check if at this time the next location where I will be
                    // it's ocuppied by someone else and at the next unit time
                    // that
                    // resource will ocupy this position
                    if (i + 1 < locations.length) {
                        int column1 = table.findColumn(locations[i + 1].trim());
                        String val1 = (String) table.getValueAt(counter + row + j, column1);
                        // if occupied by val1
                        if (val1 != null) {
                            // check next row at this column if is occupied by
                            // val1
                            String val2 = (String) table.getValueAt(counter + row + j + 1, column);
                            if (val1.equals(val2)) {
                                // interlocking
                                val = new String("interlocking");
                            }
                        }
                    }
                }

                if (val != null) {
                    // we found a position occupied so it's no good
                    return false;
                }
            }
        }
        return true;
    }
}

Related

  1. clearAllRows(JTable table)
  2. contains(int row, TableModel model)
  3. copyRows(JTable table)
  4. createDBXMLFile(JTable table, String docField, String rowField, Connection con, boolean reuseConnection, String driver, String dsn, String user, String password, String tableName, String whereString, String orderByString, String groupByString, boolean convertCharsToEntites)