Example usage for javax.swing.table TableModel getValueAt

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

Introduction

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

Prototype

public Object getValueAt(int rowIndex, int columnIndex);

Source Link

Document

Returns the value for the cell at columnIndex and rowIndex.

Usage

From source file:pkgnew.line.PluginDialog.java

private void uninstallPluginActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_uninstallPluginActionPerformed
    List<Plugin> plugins = PluginManager.getInstance().getPlugins();
    TableModel model = TablePlugins.getModel();
    int rowCount = model.getRowCount();
    File[] files = ((File) new File("plugins/")).listFiles();

    for (int i = 0; i < rowCount; i++) {
        boolean isSelected = (boolean) model.getValueAt(i, 0);
        if (isSelected) {
            plugins.get(i).suspend();/*  w  w  w . ja  v a2s  . c o m*/

            for (File file : files) {
                System.out.println("comparing " + file.getName() + " and " + plugins.get(i).getName());
                if (file.getName().startsWith(plugins.get(i).getName())) {
                    file.delete();
                    plugins.remove(i);
                }
            }
        }
    }
    listPlugins();
}

From source file:pt.webdetails.cda.CdaQueryComponent.java

private IPentahoResultSet convertTableToResultSet(TableModel tableModel) {
    List<String> columnNames = new ArrayList<String>();
    for (int i = 0; i < tableModel.getColumnCount(); i++) {
        columnNames.add(tableModel.getColumnName(i));
    }//ww w. j a  v  a 2s. c  o m
    MemoryMetaData metadata = new MemoryMetaData(columnNames);

    MemoryResultSet resultSet = new MemoryResultSet();
    resultSet.setMetaData(metadata);
    for (int i = 0; i < tableModel.getRowCount(); i++) {
        Object row[] = new Object[tableModel.getColumnCount()];
        for (int j = 0; j < tableModel.getColumnCount(); j++) {
            row[j] = tableModel.getValueAt(i, j);
        }
        resultSet.addRow(row);
    }
    return resultSet;
}

From source file:pt.webdetails.cda.dataaccess.JoinCompoundDataAccess.java

private String getInjectorStepXmlString(String name, TableModel t) {
    StringBuilder xml = new StringBuilder("<step><name>");
    Class<?> columnClass;//from w  w  w. j av a 2s  .  c  o m
    xml.append(name).append("</name><type>Injector</type><copies>1</copies>");

    int maxRowsTypeSearch = getMaxTypeSearchRowCount(t);

    // If we have metadata information, put it here
    if (t.getColumnCount() > 0) {

        xml.append("<fields>");
        for (int i = 0; i < t.getColumnCount(); i++) {
            /* The proper way to get the column class is from t.getColumnClass().
             * However, this always returns Object when the column at hand is a
             * Calculated Column -- and we have no idea what to do with Objects.
             * Therefore, we try to infer the correct type from the getClass() of
             * the chosen column, first row, as that can't be worse than trying
             * to deal with Object.
             */
            columnClass = t.getColumnClass(i);
            if (columnClass.equals(Object.class) && t.getRowCount() > 0) {
                for (int j = 0; j < maxRowsTypeSearch; j++) {
                    if (t.getValueAt(j, i) != null) {
                        columnClass = t.getValueAt(j, i).getClass();
                        break;
                    }
                }
            }
            xml.append("<field>");
            xml.append("<name>").append(t.getColumnName(i)).append("</name>");
            xml.append("<type>").append(getKettleTypeFromColumnClass(columnClass)).append("</type>");
            xml.append("<length>-1</length><precision>-1</precision></field>");
        }
        xml.append("</fields>");

    }

    xml.append("</step>");
    return xml.toString();

}

From source file:pt.webdetails.cda.exporter.HtmlExporter.java

@Override
public void export(OutputStream out, TableModel tableModel) throws ExporterException {
    final Document document = DocumentHelper.createDocument();
    Element table = null;/*from w w  w  . java2 s  .  c o m*/

    if (fullHtml) {
        final Element html = document.addElement("html");
        final Element head = html.addElement("head");
        head.addElement("title").addText(title);
        table = html.addElement("body").addElement("table");
    } else {
        table = document.addElement("table");
    }

    final int columnCount = tableModel.getColumnCount();

    //table headers
    final Element headerRow = table.addElement("tr");
    for (int i = 0; i < columnCount; i++) {
        String colName = tableModel.getColumnName(i);
        headerRow.addElement("th").addText(colName);
    }

    //table body
    for (int i = 0; i < tableModel.getRowCount(); i++) {
        Element row = table.addElement("tr");

        for (int j = 0; j < columnCount; j++) {
            Element tableCell = row.addElement("td");
            Object value = tableModel.getValueAt(i, j);
            tableCell.setText(valueToText(value));

            if (value instanceof Date) {
                tableCell.setText(format.format(value));
            } else if (value != null) {
                // numbers can be safely converted via toString, as they use a well-defined format there
                tableCell.setText(value.toString());
            }

        }
    }

    try {
        document.setXMLEncoding("UTF-8");

        OutputFormat outFormat = new OutputFormat();
        outFormat.setOmitEncoding(true);
        outFormat.setSuppressDeclaration(true);//otherwise msexcel/oocalc may not recognize content
        outFormat.setNewlines(true);
        outFormat.setIndentSize(columnCount);
        final Writer writer = new BufferedWriter(new OutputStreamWriter(out));
        XMLWriter xmlWriter = new XMLWriter(writer, outFormat);
        xmlWriter.write(document);
        xmlWriter.flush();
    } catch (IOException e) {
        throw new ExporterException("IO Exception converting to utf-8", e);
    }
}

From source file:pt.webdetails.cda.exporter.JsonExporter.java

public JSONObject getTableAsJson(TableModel tableModel, Integer rowLimit)
        throws JSONException, ExporterException {
    JSONObject json = new JSONObject();

    // Generate metadata
    final JSONArray metadataArray = new JSONArray();

    final int columnCount = tableModel.getColumnCount();
    int rowCount = tableModel.getRowCount();

    if (rowLimit != null) {
        rowCount = Math.min(rowCount, rowLimit);
    }/*www  .ja v  a2s .co  m*/

    boolean[] isColumnDouble = new boolean[columnCount];
    for (int i = 0; i < columnCount; i++) {
        JSONObject info = new JSONObject();
        info.put("colIndex", i);
        info.put("colName", tableModel.getColumnName(i));
        Class<?> columnClass = tableModel.getColumnClass(i);
        isColumnDouble[i] = (columnClass.isAssignableFrom(Double.class));
        info.put("colType", getColType(columnClass));
        metadataArray.put(info);
    }
    json.put("metadata", metadataArray);

    if (tableModel instanceof MetadataTableModel) {
        json.put("queryInfo", ((MetadataTableModel) tableModel).getAllMetadata());
    }
    final JSONArray valuesArray = new JSONArray();

    for (int rowIdx = 0; rowIdx < rowCount; rowIdx++) {
        final JSONArray rowArray = new JSONArray();
        for (int colIdx = 0; colIdx < columnCount; colIdx++) {
            Object value = tableModel.getValueAt(rowIdx, colIdx);
            try {
                if (value != null && isColumnDouble[colIdx] && ((Double) value).isInfinite()) {
                    value = null;
                    //value = Double.POSITIVE_INFINITY == (Double) value ? "Infinity" : "-Infinity";//workaround for JSON
                    // issue with Infinity
                }
            } catch (ClassCastException e) {
            } //just because it says Double doesn't mean we don't get oranges
            rowArray.put(value);
        }
        valuesArray.put(rowArray);
    }
    json.put("resultset", valuesArray);
    return json;
}

From source file:pt.webdetails.cda.exporter.XmlExporter.java

public void export(final OutputStream out, final TableModel tableModel) throws ExporterException {
    final Document document = DocumentHelper.createDocument();

    // Generate metadata
    final Element root = document.addElement("CdaExport");
    final Element metadata = root.addElement("MetaData");

    final int columnCount = tableModel.getColumnCount();
    final int rowCount = tableModel.getRowCount();

    for (int i = 0; i < columnCount; i++) {
        final Element columnInfo = metadata.addElement("ColumnMetaData");
        columnInfo.addAttribute("index", (String.valueOf(i)));
        columnInfo.addAttribute("type", getColType(tableModel.getColumnClass(i)));
        columnInfo.addAttribute("name", tableModel.getColumnName(i));

    }//  ww  w . j a v  a  2 s. co m

    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ", Locale.US);
    final Element resultSet = root.addElement("ResultSet");
    for (int rowIdx = 0; rowIdx < rowCount; rowIdx++) {

        final Element row = resultSet.addElement("Row");

        for (int colIdx = 0; colIdx < columnCount; colIdx++) {
            final Element col = row.addElement("Col");

            final Object value = tableModel.getValueAt(rowIdx, colIdx);
            if (value instanceof Date) {
                col.setText(format.format(value));
            } else if (value != null) {
                // numbers can be safely converted via toString, as they use a well-defined format there
                col.setText(value.toString());
            } else {
                col.addAttribute("isNull", "true");
            }

        }
    }

    try {
        final Writer writer = new BufferedWriter(new OutputStreamWriter(out));

        document.setXMLEncoding("UTF-8");
        document.write(writer);
        writer.flush();
    } catch (IOException e) {
        throw new ExporterException("IO Exception converting to utf-8", e);
    }
}

From source file:pt.webdetails.cda.filetests.CacheKeysTest.java

@Test
public void testParam() throws Exception {

    final CdaSettings cdaSettings = parseSettingsFile("sample-securityParam.cda");

    QueryOptions queryOptions = new QueryOptions();

    queryOptions.setDataAccessId("junitDataAccess");
    TableModel tableModel = cdaSettings.getDataAccess(queryOptions.getDataAccessId()).doQuery(queryOptions);
    assertEquals(1, tableModel.getRowCount());
    assertEquals(1, tableModel.getColumnCount());
    String result = (String) tableModel.getValueAt(0, 0);
    assertEquals("thisIsAGoodValue", result);
}

From source file:pt.webdetails.cda.test.util.CdaTestHelper.java

@SafeVarargs
public static <T> boolean columnContains(TableModel table, int colIdx, T... values) {
    HashSet<T> set = new HashSet<T>();
    set.addAll(Arrays.asList(values));
    for (int i = 0; i < table.getRowCount(); i++) {
        set.remove(table.getValueAt(i, colIdx));
        if (set.isEmpty()) {
            return true;
        }//from   w w  w.  ja  v  a2  s .c o  m
    }
    return false;
}

From source file:pt.webdetails.cda.test.util.CdaTestHelper.java

public static <T> boolean columnContains(TableModel table, int colIdx, T value) {
    for (int i = 0; i < table.getRowCount(); i++) {
        if (value.equals(table.getValueAt(i, colIdx))) {
            return true;
        }//from   w  w w  .ja  va2s. c  o m
    }
    return false;
}

From source file:pt.webdetails.cda.tests.CdaPropertyLookupParserIT.java

public void testxPathQuery() throws Exception {

    final CdaSettings cdaSettings = parseSettingsFile("xPath_CDA_15.cda");

    final QueryOptions queryOptions = new QueryOptions();
    queryOptions.setDataAccessId("xPath_CDA_15");

    queryOptions.setParameter("theme", "Engagement");
    queryOptions.setParameter("level", "2");

    logger.info("Doing query");
    TableModel tm = doQuery(cdaSettings, queryOptions);

    assertEquals("Commentaire pour le theme Engagement et le niveau 2", tm.getValueAt(0, 0));
}