List of usage examples for javax.swing.table TableModel getRowCount
public int getRowCount();
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 . j a va 2s. 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; }/* w w w . java 2s. c o m*/ } return false; }
From source file:pt.webdetails.cda.tests.KettleStringArrayParamIT.java
public void testKettleStringArray() throws Exception { final CdaSettings cdaSettings = parseSettingsFile("sample-kettle-ParamArray.cda"); final QueryOptions queryOptions = new QueryOptions(); queryOptions.setDataAccessId("1"); queryOptions.setParameter("countries", "Portugal;Germany"); queryOptions.setParameter("Costumers", "307;369"); logger.info("Doing query"); TableModel tm = doQuery(cdaSettings, queryOptions); assertEquals(2, tm.getRowCount()); assertEquals("307", tm.getValueAt(0, 0).toString()); assertEquals("Der Hund Imports", tm.getValueAt(0, 1)); }
From source file:pt.webdetails.cda.utils.kettle.SortableTableModel.java
public SortableTableModel(TableModel base) { this.base = base; sortedIndices = new Integer[base.getRowCount()]; for (int i = 0; i < sortedIndices.length; i++) { sortedIndices[i] = i;/* w w w.j a v a 2s . c om*/ } }
From source file:pt.webdetails.cda.utils.kettle.SortTableModel.java
public TableModel defaultSort(TableModel unsorted, List<String> sortBy) throws SortException { if (unsorted == null || unsorted.getRowCount() == 0) { return unsorted; } else {/*from w w w.j a va 2s .c o m*/ TableModel output = null; inputCallables.clear(); try { String sort = getSortXmlStep(unsorted, sortBy); DynamicTransMetaConfig transMetaConfig = new DynamicTransMetaConfig(Type.EMPTY, "JoinCompoundData", null, null); DynamicTransConfig transConfig = new DynamicTransConfig(); transConfig.addConfigEntry(EntryType.STEP, "input", "<step><name>input</name><type>Injector</type><copies>1</copies></step>"); transConfig.addConfigEntry(EntryType.STEP, "sort", sort); transConfig.addConfigEntry(EntryType.HOP, "input", "sort"); TableModelInput input = new TableModelInput(); transConfig.addInput("input", input); inputCallables.add(input.getCallableRowProducer(unsorted, true)); RowMetaToTableModel outputListener = new RowMetaToTableModel(false, true, false); transConfig.addOutput("sort", outputListener); DynamicTransformation trans = new DynamicTransformation(transConfig, transMetaConfig); trans.executeCheckedSuccess(null, null, this); logger.info(trans.getReadWriteThroughput()); output = outputListener.getRowsWritten(); return output; } catch (Exception e) { throw new SortException("Exception during sorting ", e); } } }
From source file:pt.webdetails.cda.utils.TableModelUtils.java
public static TableModel postProcessTableModel(final DataAccess dataAccess, final QueryOptions queryOptions, final TableModel rawTableModel) throws SortException, InvalidOutputIndexException { if (rawTableModel == null) { throw new IllegalArgumentException("Cannot process null table."); }/*ww w . j a va2 s. c o m*/ // We will: // 1. Evaluate Calculated columns // 2. Show only the output columns we want; // 3. Sort // 4. Pagination TableModel table; // 1 Evaluate Calculated columns table = evaluateCalculatedColumns(dataAccess, rawTableModel); // 2. Show only the output columns we want, filter rows List<Integer> outputIndexes = getOutputIndexes(dataAccess, queryOptions, table); DataTableFilter rowFilter = getRowFilter(queryOptions, outputIndexes); //mdx and denormalizedMdx queries with an empty result set can return different metadata (less columns), //in this cases, the output indexes will be ignored boolean useOutputIndexes = true; if (table.getRowCount() == 0 && (dataAccess.getType().equals("mdx") || dataAccess.getType().equals("denormalizedMdx"))) { useOutputIndexes = false; logger.warn("Mdx query returned empty result set, output indexes will be ignored."); } table = useOutputIndexes ? filterTable(table, outputIndexes, rowFilter) : filterTable(table, new ArrayList<Integer>(), rowFilter); // 3. Sort if (!queryOptions.getSortBy().isEmpty()) { // no action table = (new SortTableModel()).doSort(table, queryOptions.getSortBy()); } // Create a metadata-aware table model final Class<?>[] colTypes = new Class[table.getColumnCount()]; final String[] colNames = new String[table.getColumnCount()]; for (int i = 0; i < table.getColumnCount(); i++) { colTypes[i] = table.getColumnClass(i); colNames[i] = table.getColumnName(i); } final int rowCount = table.getRowCount(); MetadataTableModel result = new MetadataTableModel(colNames, colTypes, rowCount); result.setMetadata("totalRows", rowCount); for (int r = 0; r < rowCount; r++) { for (int j = 0; j < table.getColumnCount(); j++) { result.setValueAt(table.getValueAt(r, j), r, j); } } // 4. Pagination return paginateTableModel(result, queryOptions); }
From source file:pt.webdetails.cda.utils.TableModelUtils.java
/** * @param table/* w ww.ja v a 2 s .c o m*/ * @param outputIndexes * @param rowFilter (optional) * @return * @throws InvalidOutputIndexException */ private static TableModel filterTable(final TableModel table, List<Integer> outputIndexes, final DataTableFilter rowFilter) throws InvalidOutputIndexException { int columnCount = outputIndexes.size(); if (columnCount == 0 && rowFilter != null) { //still have to go through the motions if we need to filter rows for (int i = 0; i < table.getColumnCount(); i++) { outputIndexes.add(i); } columnCount = outputIndexes.size(); } if (columnCount != 0) { //logger.info(Collections.max(outputIndexes)+" "+table.getColumnCount()); if ((Collections.max(outputIndexes) > table.getColumnCount() - 1)) { String errorMessage = String.format( "Output index higher than number of columns in tableModel. %s > %s", Collections.max(outputIndexes), table.getColumnCount()); logger.error(errorMessage); if (table.getColumnCount() > 0) { throw new InvalidOutputIndexException(errorMessage, null); } else { logger.warn( "Unable to validate output indexes because table metadata is empty. Returning table."); return table; } } final int rowCount = table.getRowCount(); logger.debug(rowCount == 0 ? "No data found" : "Found " + rowCount + " rows"); final Class<?>[] colTypes = new Class[columnCount]; final String[] colNames = new String[columnCount]; //just set the number of rows/columns final TypedTableModel typedTableModel = new TypedTableModel(colNames, colTypes, rowCount); for (int rowIn = 0, rowOut = 0; rowIn < rowCount; rowIn++, rowOut++) { //filter rows if (rowFilter != null && !rowFilter.rowContainsSearchTerms(table, rowIn)) { rowOut--; continue; } //filter columns for (int j = 0; j < outputIndexes.size(); j++) { final int outputIndex = outputIndexes.get(j); typedTableModel.setValueAt(table.getValueAt(rowIn, outputIndex), rowOut, j); } } //since we set the calculated table model to infer types, they will be available after rows are evaluated for (int i = 0; i < outputIndexes.size(); i++) { final int outputIndex = outputIndexes.get(i); typedTableModel.setColumnName(i, table.getColumnName(outputIndex)); typedTableModel.setColumnType(i, table.getColumnClass(outputIndex)); } return typedTableModel; } return table; }
From source file:pt.webdetails.cda.utils.TableModelUtils.java
public static TableModel copyTableModel(final DataAccess dataAccess, final TableModel t) { // We're removing the ::table-by-index:: cols // Build an array of column indexes whose name is different from ::table-by-index::.* ArrayList<String> namedColumns = new ArrayList<String>(); ArrayList<Class<?>> namedColumnsClasses = new ArrayList<Class<?>>(); for (int i = 0; i < t.getColumnCount(); i++) { String colName = t.getColumnName(i); if (!colName.startsWith("::table-by-index::") && !colName.startsWith("::column::")) { namedColumns.add(colName);/*from ww w.ja v a2 s. c o m*/ namedColumnsClasses.add(t.getColumnClass(i)); } } final int count = namedColumns.size(); final Class<?>[] colTypes = namedColumnsClasses.toArray(new Class[] {}); final String[] colNames = namedColumns.toArray(new String[] {}); for (int i = 0; i < count; i++) { colTypes[i] = t.getColumnClass(i); final ColumnDefinition col = dataAccess.getColumnDefinition(i); colNames[i] = col != null ? col.getName() : t.getColumnName(i); } final int rowCount = t.getRowCount(); logger.debug(rowCount == 0 ? "No data found" : "Found " + rowCount + " rows"); //if the first row has no values, the class will be Object, however, next rows can have values, we evaluate those for (int i = 0; i < colTypes.length; i++) { if (colTypes[i] == Object.class) { for (int k = 0; k < t.getRowCount(); k++) { if (t.getValueAt(k, i) != null) { colTypes[i] = t.getValueAt(k, i).getClass(); break; } } } } final TypedTableModel typedTableModel = new TypedTableModel(colNames, colTypes, rowCount); for (int r = 0; r < rowCount; r++) { for (int c = 0; c < count; c++) { typedTableModel.setValueAt(t.getValueAt(r, c), r, c); } } return typedTableModel; }
From source file:pt.webdetails.cda.utils.TableModelUtils.java
/** * Method to append a tablemodel into another. We'll make no guarantees about the types * * @param tableModelA TableModel to be modified * @param tableModelB Contents to be appended # *//* www . ja v a2s. c om*/ public static TableModel appendTableModel(final TableModel tableModelA, final TableModel tableModelB) { // We will believe the data is correct - no type checking int colCountA = tableModelA.getColumnCount(), colCountB = tableModelB.getColumnCount(); boolean usingA = colCountA > colCountB; int colCount = usingA ? colCountA : colCountB; TableModel referenceTable = (usingA ? tableModelA : tableModelB); final Class<?>[] colTypes = new Class[colCount]; final String[] colNames = new String[colCount]; for (int i = 0; i < referenceTable.getColumnCount(); i++) { colTypes[i] = referenceTable.getColumnClass(i); colNames[i] = referenceTable.getColumnName(i); } int rowCount = tableModelA.getRowCount() + tableModelB.getRowCount(); // Table A final TypedTableModel typedTableModel = new TypedTableModel(colNames, colTypes, rowCount); for (int r = 0; r < tableModelA.getRowCount(); r++) { for (int c = 0; c < colTypes.length; c++) { typedTableModel.setValueAt(tableModelA.getValueAt(r, c), r, c); } } // Table B int rowCountOffset = tableModelA.getRowCount(); for (int r = 0; r < tableModelB.getRowCount(); r++) { for (int c = 0; c < colTypes.length; c++) { typedTableModel.setValueAt(tableModelB.getValueAt(r, c), r + rowCountOffset, c); } } return typedTableModel; }
From source file:uk.ac.babraham.SeqMonk.Filters.GeneSetFilter.GeneSetDisplay.java
public void actionPerformed(ActionEvent ae) { /* if (ae.getActionCommand().equals("plot")) { /*from w w w . jav a2 s .c om*/ drawScatterPlot(); } */ if (ae.getActionCommand().equals("save_image")) { ImageSaver.saveImage(scatterPlotPanel); } else if (ae.getActionCommand().equals("swap_plot")) { if (storesQuantitated()) { plotPanel.remove(scatterPlotPanel); if (scatterPlotPanel instanceof GeneSetScatterPlotPanel) { scatterPlotPanel = new ZScoreScatterPlotPanel(fromStore, toStore, probes, currentSelectedProbeList, dotSizeSlider.getValue(), zScoreLookupTable); plotPanel.add(scatterPlotPanel, BorderLayout.CENTER); swapPlotButton.setText("Display standard scatterplot"); } else if (scatterPlotPanel instanceof ZScoreScatterPlotPanel) { scatterPlotPanel = new GeneSetScatterPlotPanel(fromStore, toStore, startingProbeList, currentSelectedProbeList, true, dotSizeSlider.getValue(), customRegressionValues, simpleRegression); plotPanel.add(scatterPlotPanel, BorderLayout.CENTER); swapPlotButton.setText("Display z-score plot"); } } } else if (ae.getActionCommand().equals("close")) { /* if(currentSelectedProbeList != null){ currentSelectedProbeList[0].delete(); //currentSelectedProbeList = null; } */ this.dispose(); } else if (ae.getActionCommand().equals("select_all")) { if (selectAllButton.isSelected()) { for (int i = 0; i < tableModel.selected.length; i++) { tableModel.selected[i] = true; tableModel.fireTableCellUpdated(i, 0); } selectAllButton.setText("deselect all"); } else { for (int i = 0; i < tableModel.selected.length; i++) { tableModel.selected[i] = false; tableModel.fireTableCellUpdated(i, 0); } selectAllButton.setText("select all"); } } else if (ae.getActionCommand().equals("save_selected_probelists")) { boolean[] selectedListsBoolean = tableModel.selected; if (selectedListsBoolean.length != filterResultsPVals.length) { System.err.println("not adding up here"); } else { ArrayList<MappedGeneSetTTestValue> selectedListsArrayList = new ArrayList<MappedGeneSetTTestValue>(); for (int i = 0; i < selectedListsBoolean.length; i++) { if (selectedListsBoolean[i] == true) { selectedListsArrayList.add(filterResultsPVals[i]); } } MappedGeneSetTTestValue[] selectedLists = selectedListsArrayList .toArray(new MappedGeneSetTTestValue[0]); if (selectedLists.length == 0) { JOptionPane.showMessageDialog(SeqMonkApplication.getInstance(), "No probe lists were selected", "No probe lists selected", JOptionPane.INFORMATION_MESSAGE); return; } saveProbeLists(selectedLists); if (currentSelectedProbeList != null) { currentSelectedProbeList[0].delete(); currentSelectedProbeList = null; } } } else if (ae.getActionCommand().equals("save_table")) { JFileChooser chooser = new JFileChooser(SeqMonkPreferences.getInstance().getSaveLocation()); chooser.setMultiSelectionEnabled(false); chooser.setFileFilter(new FileFilter() { public String getDescription() { return "Text files"; } public boolean accept(File f) { if (f.isDirectory() || f.getName().toLowerCase().endsWith(".txt")) { return true; } else { return false; } } }); int result = chooser.showSaveDialog(this); if (result == JFileChooser.CANCEL_OPTION) return; File file = chooser.getSelectedFile(); if (!file.getPath().toLowerCase().endsWith(".txt")) { file = new File(file.getPath() + ".txt"); } SeqMonkPreferences.getInstance().setLastUsedSaveLocation(file); // Check if we're stepping on anyone's toes... if (file.exists()) { int answer = JOptionPane.showOptionDialog(this, file.getName() + " exists. Do you want to overwrite the existing file?", "Overwrite file?", 0, JOptionPane.QUESTION_MESSAGE, null, new String[] { "Overwrite and Save", "Cancel" }, "Overwrite and Save"); if (answer > 0) { return; } } try { PrintWriter p = new PrintWriter(new FileWriter(file)); TableModel model = table.getModel(); int rowCount = model.getRowCount(); int colCount = model.getColumnCount(); // Do the headers first StringBuffer b = new StringBuffer(); for (int c = 1; c < colCount; c++) { b.append(model.getColumnName(c)); if (c + 1 != colCount) { b.append("\t"); } } p.println(b); for (int r = 0; r < rowCount; r++) { b = new StringBuffer(); for (int c = 1; c < colCount; c++) { b.append(model.getValueAt(r, c)); if (c + 1 != colCount) { b.append("\t"); } } p.println(b); } p.close(); } catch (FileNotFoundException e) { new CrashReporter(e); } catch (IOException e) { new CrashReporter(e); } } else { throw new IllegalArgumentException("Unknown command " + ae.getActionCommand()); } }