Example usage for javax.swing.table TableModel getRowCount

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

Introduction

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

Prototype

public int getRowCount();

Source Link

Document

Returns the number of rows in the model.

Usage

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>
 * /*from   w ww. jav  a  2 s.co m*/
 * @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// ww w . j  av  a 2 s.com
 * @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/*from   www.  ja  v  a  2  s.  com*/
 *          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.crosstab.CrosstabTestUtil.java

private static CrosstabSpecification fillCrosstabSpec(final TableModel model, final CrosstabSpecification spec)
        throws ReportProcessingException {
    final TableModelDataRow dr = new TableModelDataRow(model);

    Object rowKey = dr.get("Rows");
    spec.startRow();//from w  w w.j  a v  a2 s  .c o m
    for (int i = 0; i < model.getRowCount(); i += 1) {
        dr.setCurrentRow(i);
        Object row = dr.get("Rows");
        if (ObjectUtilities.equal(row, rowKey) == false) {
            DebugLog.log("R: " + rowKey + " -> " + row);
            spec.endRow();
            spec.startRow();
            rowKey = row;
        }
        spec.add(dr);
    }
    spec.endRow();
    spec.endCrosstab();
    return spec;
}

From source file:org.pentaho.reporting.engine.classic.core.crosstab.DataRowPaddingIT.java

private static CrosstabSpecification buildCS(final TableModel data) throws ReportProcessingException {
    final ProcessingContext prc = new DefaultProcessingContext();
    final GlobalMasterRow gmr = GlobalMasterRow.createReportRow(prc, new DefaultDataSchemaDefinition(),
            new ParameterDataRow());
    gmr.requireStructuralProcessing();//w  ww .  j av  a  2s  .  c o m
    MasterDataRow masterDataRow = gmr.deriveWithQueryData(data);
    final CrosstabSpecification crosstabSpecification = new SortedMergeCrosstabSpecification(
            new ReportStateKey(), new String[] { "Product", "Time" }, new String[] { "Region" });

    int advanceCount = 0;
    logger.debug("Building Crosstab: Cursor: " + String.valueOf(masterDataRow.getReportDataRow().getCursor()));
    crosstabSpecification.startRow();
    crosstabSpecification.add(masterDataRow.getGlobalView());
    Object grpVal = masterDataRow.getGlobalView().get("Region");
    while (masterDataRow.isAdvanceable()) {
        final MasterDataRow nextdata = masterDataRow.advance();
        final Object nextGrpVal = nextdata.getGlobalView().get("Region");
        if (ObjectUtilities.equal(grpVal, nextGrpVal) == false) {
            crosstabSpecification.endRow();
            crosstabSpecification.startRow();
        }

        crosstabSpecification.add(nextdata.getGlobalView());
        logger.debug("Prepare Advance Count: " + nextdata.getReportDataRow().getCursor());
        advanceCount += 1;
        masterDataRow = nextdata;
        grpVal = nextGrpVal;
    }
    crosstabSpecification.endRow();
    crosstabSpecification.endCrosstab();
    if (advanceCount != (data.getRowCount() - 1)) {
        throw new IllegalStateException("Expected 6 but got " + advanceCount);
    }
    return crosstabSpecification;
}

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 {/*w w  w.  jav  a 2  s.c o m*/
        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 {//from   w  w w  .  java 2 s  .c  om
        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.//www .j ava  2s  .c o m
 */
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;
}

From source file:org.pentaho.reporting.engine.classic.core.sorting.SortingDataFactory.java

protected TableModel postProcess(final String query, final DataRow parameters, final TableModel tableModel) {
    if (tableModel == null) {
        logger.debug("No data, therefore no sorting.");
        return null;
    }/*from www .  ja v  a  2  s.  com*/
    if (tableModel.getRowCount() == 1 || tableModel.getColumnCount() == 0) {
        logger.debug("Empty data, therefore no sorting.");
        return tableModel;
    }
    Object o = parameters.get(DataFactory.QUERY_SORT);
    if ((o instanceof List<?>) == false) {
        logger.debug("Sort constraints are not in list format.");
        return tableModel;
    }
    List<SortConstraint> sort = validate((List<?>) o);
    List<SortConstraint> resolvedConstraints = resolveColumnAliases(tableModel, sort);
    if (resolvedConstraints.isEmpty()) {
        logger.debug("Resolved sort constraints are empty.");
        return tableModel;
    }

    return sort(tableModel, resolvedConstraints);
}

From source file:org.pentaho.reporting.engine.classic.extensions.modules.mailer.MailProcessor.java

public static void performBursting(final MailDefinition definition, final Session session)
        throws MessagingException, ReportProcessingException, ContentIOException {
    if (session == null) {
        throw new NullPointerException();
    }//from  ww w .jav a2 s .c om

    // process parameters - validate!
    final ReportParameterValues parameterValues = definition.getParameterValues();
    final DefaultParameterContext parameterContext = new DefaultParameterContext(definition.getDataFactory(),
            parameterValues, ClassicEngineBoot.getInstance().getGlobalConfig(),
            definition.getResourceBundleFactory(), definition.getResourceManager(), definition.getContextKey(),
            definition.getReportEnvironment());

    try {
        final ReportParameterDefinition parameterDefinition = definition.getParameterDefinition();
        final ReportParameterValidator reportParameterValidator = parameterDefinition.getValidator();
        final ValidationResult validationResult = reportParameterValidator.validate(new ValidationResult(),
                parameterDefinition, parameterContext);
        if (validationResult.isEmpty() == false) {
            throw new ReportParameterValidationException(
                    "The parameters provided for this report are not valid.", validationResult);
        }
    } finally {
        parameterContext.close();
    }

    // definition: Single mail or multi-mail
    final TableModel burstingData;
    final DataFactory dataFactory = definition.getDataFactory();
    if (definition.getBurstQuery() != null
            && dataFactory.isQueryExecutable(definition.getBurstQuery(), parameterValues)) {
        burstingData = wrapWithParameters(dataFactory.queryData(definition.getBurstQuery(), parameterValues),
                parameterValues);
    } else {
        burstingData = wrapWithParameters(new DefaultTableModel(1, 0), parameterValues);
    }

    if (burstingData.getRowCount() > 0) {
        // final Transport transport = session.getTransport();
        // transport.connect();
        for (int i = 0; i < burstingData.getRowCount(); i++) {
            final DataRow parameterDataRow = createReportParameterDataRow(burstingData, i);
            final MimeMessage message = createReport(definition, session, parameterDataRow);

            parameterContext.setParameterValues(parameterDataRow);

            final MailHeader[] headers = definition.getHeaders();
            for (int j = 0; j < headers.length; j++) {
                final MailHeader header = headers[j];
                message.addHeader(header.getName(), header.getValue(parameterContext));
            }

            processRecipients(definition, message, dataFactory, parameterDataRow);

            // transport.sendMessage(message, message.getAllRecipients());
        }
        // transport.close();
    }
}