Example usage for javax.swing.table TableModel getColumnCount

List of usage examples for javax.swing.table TableModel getColumnCount

Introduction

In this page you can find the example usage for javax.swing.table TableModel getColumnCount.

Prototype

public int getColumnCount();

Source Link

Document

Returns the number of columns in the model.

Usage

From source file:org.apache.metamodel.jdbc.JdbcDataContextTest.java

public void testMaxRowsRewrite() throws Exception {
    JdbcDataContext dc = new JdbcDataContext(getTestDbConnection(), new TableType[] { TableType.TABLE }, null);
    IQueryRewriter rewriter = new DefaultQueryRewriter(dc) {
        @Override/* w  ww . jav a 2  s  .  c  o m*/
        public String rewriteQuery(Query query) {
            return "SELECT COUNT(*) FROM PUBLIC.CUSTOMERS";
        }
    };
    dc = dc.setQueryRewriter(rewriter);
    TableModel tm = new DataSetTableModel(dc.executeQuery(new Query().selectCount()));
    assertEquals(1, tm.getRowCount());
    assertEquals(1, tm.getColumnCount());
    assertEquals("122", tm.getValueAt(0, 0).toString());
}

From source file:org.broad.igv.cbio.FilterGeneNetworkUI.java

/**
 * Export the current table to a tab-delimited file.
 * String exported should be the same as what user sees
 *
 * @param outFile//from  w  w w . j  a v a2s  .  c  o m
 * @throws IOException
 */
private void saveTable(File outFile) throws FileNotFoundException {
    PrintWriter writer = new PrintWriter(outFile);
    TableModel model = geneTable.getModel();
    String delimiter = "\t";

    //Write header
    String header = model.getColumnName(0);
    for (int col = 1; col < model.getColumnCount(); col++) {
        header += delimiter + model.getColumnName(col);
    }

    writer.println(header);

    for (int row = 0; row < model.getRowCount(); row++) {
        String rowStr = "" + model.getValueAt(row, 0);
        for (int col = 1; col < model.getColumnCount(); col++) {
            rowStr += delimiter + model.getValueAt(row, col);
        }

        writer.println(rowStr);
    }

    writer.flush();
    writer.close();
}

From source file:org.datacleaner.result.AnnotatedRowResultTest.java

private void performAssertions(AnnotatedRowsResult result) {
    assertEquals(2, result.getAnnotatedRowCount());
    assertEquals("[MockInputColumn[name=foo], MockInputColumn[name=bar]]", result.getInputColumns().toString());
    assertEquals("[MockInputColumn[name=foo]]", Arrays.toString(result.getHighlightedColumns()));
    assertNotNull(result.getSampleRows());
    TableModel tableModel = result.toTableModel();
    assertNotNull(tableModel);/*from   w  w  w .java2  s  .c om*/
    assertEquals(2, tableModel.getColumnCount());
    assertEquals("foo", tableModel.getColumnName(0));
    assertEquals("bar", tableModel.getColumnName(1));

    tableModel = result.toDistinctValuesTableModel(result.getHighlightedColumns()[0]);
    assertNotNull(tableModel);
    assertEquals(2, tableModel.getColumnCount());
    assertEquals("foo", tableModel.getColumnName(0));
    assertEquals("Count in dataset", tableModel.getColumnName(1));
}

From source file:org.eobjects.analyzer.result.AnnotatedRowResultTest.java

private void performAssertions(AnnotatedRowsResult result) {
    assertEquals(2, result.getAnnotatedRowCount());
    assertEquals("[MockInputColumn[name=foo], MockInputColumn[name=bar]]", result.getInputColumns().toString());
    assertEquals("[MockInputColumn[name=foo]]", Arrays.toString(result.getHighlightedColumns()));
    assertNotNull(result.getRows());/*from  w  ww .  j a v a2 s.c o  m*/
    TableModel tableModel = result.toTableModel();
    assertNotNull(tableModel);
    assertEquals(2, tableModel.getColumnCount());
    assertEquals("foo", tableModel.getColumnName(0));
    assertEquals("bar", tableModel.getColumnName(1));

    tableModel = result.toDistinctValuesTableModel(result.getHighlightedColumns()[0]);
    assertNotNull(tableModel);
    assertEquals(2, tableModel.getColumnCount());
    assertEquals("foo", tableModel.getColumnName(0));
    assertEquals("Count in dataset", tableModel.getColumnName(1));
}

From source file:org.owasp.jbrofuzz.fuzz.io.Save.java

/**
 * <p>Method for obtaining the CSV output, given a table.</p>
 * <p>No "\n" is written at the end of the final line.</p>
 * /* w  w w . j a va  2 s .  c om*/
 * @param The TableModel holding the data.
 * @return
 * <code>
 * 1,Plain Text,,
 * 2,URL UTF-8,graun,ge3dr
 * </code>
 * 
 * @author subere@uncon.org
 * @version 2.5
 * @since 2.5
 */
public static String getTableDataInCSVFormat(final TableModel inputTableModel) {

    final StringBuffer output = new StringBuffer();
    final int totalRows = inputTableModel.getRowCount();
    final int totalColumns = inputTableModel.getColumnCount();

    if (totalRows < 1) {
        return "";
    }

    for (int currentRow = 0; currentRow < totalRows; currentRow++) {

        for (int currentColumn = 0; currentColumn < totalColumns; currentColumn++) {
            output.append(inputTableModel.getValueAt(currentRow, currentColumn));
            // Append a ',' but not for the last value
            if (currentColumn != totalColumns - 1) {
                output.append(',');
            }
        }

        // Append a new line, but not for the last line
        if (currentRow != totalRows - 1) {
            output.append('\n');
        }

    }

    return output.toString();

}

From source file:org.owasp.jbrofuzz.fuzz.io.Save.java

/**
 * @author daemonmidi@gmail.com/*from  w  ww . j  av a 2s . c  o  m*/
 * @since version 2.5
 * @param inputTableModel
 * @return
 */
public static JSONArray getTableDataInJSON(final TableModel inputTableModel) {
    JSONArray tableData = new JSONArray();
    final int totalRows = inputTableModel.getRowCount() - 1;
    final int totalColumns = inputTableModel.getColumnCount() - 1;
    if (totalRows < 1) {
        return new JSONArray();
    }

    for (int currentRow = 0; currentRow < totalRows; currentRow++) {

        for (int currentColumn = 0; currentColumn < totalColumns - 1; currentColumn++) {
            String name = inputTableModel.getColumnName(currentColumn);
            String value = inputTableModel.getValueAt(currentColumn, currentRow).toString();
            String cellString = "{\"" + name + "\":\"" + value + "\"}";
            JSONObject cell;
            try {
                cell = new JSONObject(cellString);
                tableData.put(cell);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }
    return tableData;
}

From source file:org.pentaho.reporting.engine.classic.core.cache.CachingDataFactory.java

/**
 * Prints a table model to standard output.
 *
 * @param mod// ww w . j  a  v  a 2s .co  m
 *          the model.
 */
public static void printTableModelContents(final TableModel mod) {
    if (mod == null) {
        throw new NullPointerException();
    }

    logger.debug("Tablemodel contains " + mod.getRowCount() + " rows."); //$NON-NLS-1$ //$NON-NLS-2$
    for (int i = 0; i < mod.getColumnCount(); i++) {
        logger.debug("Column: " + i + " Name = " + mod.getColumnName(i) + "; DataType = " //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
                + mod.getColumnClass(i));
    }

    logger.debug("Checking the data inside"); //$NON-NLS-1$
    for (int rows = 0; rows < mod.getRowCount(); rows++) {
        for (int i = 0; i < mod.getColumnCount(); i++) {
            final Object value = mod.getValueAt(rows, i);
            logger.debug("ValueAt (" + rows + ", " + i + ") is " + value); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
        }
    }
}

From source file:org.pentaho.reporting.engine.classic.core.function.formula.MultiValueQueryFunction.java

private Object performQuery(final ReportFormulaContext context, final String query, final String columnName,
        final int queryTimeout, final int queryLimit) throws EvaluationException {

    try {//ww w .  jav  a2s  .  com
        final DataFactory dataFactory = context.getRuntime().getDataFactory();
        final TableModel tableModel = dataFactory.queryData(query,
                new QueryDataRowWrapper(context.getDataRow(), queryLimit, queryTimeout));
        if (tableModel == null) {
            return null;
        }
        final int columnCount = tableModel.getColumnCount();
        if (tableModel.getRowCount() == 0 || columnCount == 0) {
            return null;
        }

        for (int column = 0; column < columnCount; column++) {
            if (columnName == null || columnName.equals(tableModel.getColumnName(column))) {
                final ArrayList<Object> values = new ArrayList<Object>();
                int rowCount = tableModel.getRowCount();
                if (queryLimit > 0) {
                    rowCount = Math.min(queryLimit, tableModel.getRowCount());
                }
                for (int row = 0; row < rowCount; row++) {
                    values.add(tableModel.getValueAt(row, column));
                }
                return values.toArray();
            }
        }
        throw EvaluationException.getInstance(LibFormulaErrorValue.ERROR_UNEXPECTED_VALUE);
    } catch (EvaluationException e) {
        throw e;
    } catch (Exception e) {
        logger.warn("SingleValueQueryFunction: Failed to perform query", e);
        throw EvaluationException.getInstance(LibFormulaErrorValue.ERROR_UNEXPECTED_VALUE);
    }
}

From source file:org.pentaho.reporting.engine.classic.core.function.formula.SingleValueQueryFunction.java

private Object performQuery(final ReportFormulaContext context, final String query, final String column,
        final int queryTimeout) throws EvaluationException {

    try {/*  w  ww.  j a  v a2s. c o m*/
        final DataFactory dataFactory = context.getRuntime().getDataFactory();
        final TableModel tableModel = dataFactory.queryData(query,
                new QueryDataRowWrapper(context.getDataRow(), 1, queryTimeout));
        if (tableModel == null) {
            return null;
        }

        final int columnCount = tableModel.getColumnCount();
        if (tableModel.getRowCount() == 0 || columnCount == 0) {
            return null;
        }
        if (column == null) {
            return tableModel.getValueAt(0, 0);
        }
        for (int i = 0; i < columnCount; i++) {
            if (column.equals(tableModel.getColumnName(i))) {
                return tableModel.getValueAt(0, i);
            }
        }
        throw EvaluationException.getInstance(LibFormulaErrorValue.ERROR_UNEXPECTED_VALUE);
    } catch (EvaluationException e) {
        throw e;
    } catch (Exception e) {
        SingleValueQueryFunction.logger.warn("SingleValueQueryFunction: Failed to perform query", e);
        throw EvaluationException.getInstance(LibFormulaErrorValue.ERROR_UNEXPECTED_VALUE);
    }
}

From source file:org.pentaho.reporting.engine.classic.core.function.sys.SingleValueQueryFunction.java

/**
 * Performs the query by collecting the data from the datarow and executing the query.
 *
 * @return the computed value./* w  w  w.  j a va 2  s.  com*/
 */
private Object performQuery() {
    if (query == null) {
        return null;
    }
    try {
        final DataFactory dataFactory = getRuntime().getDataFactory();
        final String[] fields = getField();
        final int length = fields.length;
        final ParameterMapping[] mappings = new ParameterMapping[length];
        for (int i = 0; i < length; i++) {
            mappings[i] = new ParameterMapping(fields[i], fields[i]);
        }

        final QueryParametersDataRow params = new QueryParametersDataRow(getDataRow(), mappings);
        final TableModel tableModel = dataFactory.queryData(query,
                new QueryDataRowWrapper(params, 1, queryTimeout));
        if (tableModel == null) {
            return null;
        }
        final int columnCount = tableModel.getColumnCount();
        if (tableModel.getRowCount() == 0 || columnCount == 0) {
            return null;
        }
        if (resultColumn == null) {
            return tableModel.getValueAt(0, 0);
        }
        for (int i = 0; i < columnCount; i++) {
            if (resultColumn.equals(tableModel.getColumnName(i))) {
                return tableModel.getValueAt(0, i);
            }
        }
        // do nothing ..
    } catch (Exception e) {
        logger.warn("SingleValueQueryFunction: Failed to perform query", e);
    }
    return null;
}