List of usage examples for javax.swing.table TableColumn getModelIndex
public int getModelIndex()
From source file:edu.ku.brc.ui.UIHelper.java
/** * Calculates and sets the each column to it preferred size. NOTE: This * method also sets the table height to 10 rows. * //from w ww. j av a 2 s. c o m * @param table the table to fix up * @param numRowsHeight the number of rows to make the table height (or null not to set it) */ public static void calcColumnWidths(final JTable table, final Integer numRowsHeight, final Integer maxWidth) { if (table != null) { JTableHeader header = table.getTableHeader(); TableCellRenderer defaultHeaderRenderer = null; if (header != null) { defaultHeaderRenderer = header.getDefaultRenderer(); } TableColumnModel columns = table.getColumnModel(); TableModel data = table.getModel(); int margin = columns.getColumnMargin(); // only JDK1.3 int rowCount = data.getRowCount(); int totalWidth = 0; for (int i = columns.getColumnCount() - 1; i >= 0; --i) { TableColumn column = columns.getColumn(i); int columnIndex = column.getModelIndex(); int width = -1; TableCellRenderer h = column.getHeaderRenderer(); if (h == null) h = defaultHeaderRenderer; if (h != null) // Not explicitly impossible { Component c = h.getTableCellRendererComponent(table, column.getHeaderValue(), false, false, -1, i); width = c.getPreferredSize().width; } for (int row = rowCount - 1; row >= 0; --row) { TableCellRenderer r = table.getCellRenderer(row, i); Component c = r.getTableCellRendererComponent(table, data.getValueAt(row, columnIndex), false, false, row, i); width = Math.max(width, c.getPreferredSize().width + 10); // adding an arbitray 10 pixels to make it look nicer if (maxWidth != null) { width = Math.min(width, maxWidth); } } if (width >= 0) { column.setPreferredWidth(width + margin); // <1.3: without margin } else { // ??? } totalWidth += column.getPreferredWidth(); } // If you like; This does not make sense for two many columns! Dimension size = table.getPreferredScrollableViewportSize(); //if (totalWidth > size.width) { if (numRowsHeight != null) { size.height = Math.min(size.height, table.getRowHeight() * numRowsHeight); } size.width = totalWidth; table.setPreferredScrollableViewportSize(size); } } }
From source file:org.isatools.isacreator.api.utils.SpreadsheetUtils.java
/** * Gets the freetext terms (which ideally should be ontology terms) in a Spreadsheet object * * @param spreadsheet @see Spreadsheet/* w ww. ja v a 2 s. c om*/ * @return Map<Column Name, Set<Column Values>> */ public static Map<String, Set<String>> getFreetextInSpreadsheet(Spreadsheet spreadsheet) { Enumeration<TableColumn> columns = spreadsheet.getTable().getColumnModel().getColumns(); Map<String, Set<String>> columnToFreeText = new HashMap<String, Set<String>>(); while (columns.hasMoreElements()) { TableColumn tc = columns.nextElement(); if (spreadsheet.getTableReferenceObject() .getClassType(tc.getHeaderValue().toString().trim()) == DataTypes.ONTOLOGY_TERM) { int colIndex = Utils.convertModelIndexToView(spreadsheet.getTable(), tc.getModelIndex()); for (int row = 0; row < spreadsheet.getTable().getRowCount(); row++) { String columnValue = (spreadsheet.getTable().getValueAt(row, colIndex) == null) ? "" : spreadsheet.getTable().getValueAt(row, colIndex).toString(); if (columnValue != null && !columnValue.trim().equals("") && !columnValue.contains(":")) { if (!columnToFreeText.containsKey(tc.getHeaderValue().toString())) { columnToFreeText.put(tc.getHeaderValue().toString(), new HashSet<String>()); } columnToFreeText.get(tc.getHeaderValue().toString()).add(columnValue); } } } } return columnToFreeText; }
From source file:org.isatools.isacreator.api.utils.SpreadsheetUtils.java
public static void replaceFreeTextWithOntologyTerms(Spreadsheet spreadsheet, Map<String, OntologyTerm> annotations) { Enumeration<TableColumn> columns = spreadsheet.getTable().getColumnModel().getColumns(); while (columns.hasMoreElements()) { TableColumn tc = columns.nextElement(); if (spreadsheet.getTableReferenceObject() .getClassType(tc.getHeaderValue().toString().trim()) == DataTypes.ONTOLOGY_TERM) { int colIndex = Utils.convertModelIndexToView(spreadsheet.getTable(), tc.getModelIndex()); for (int row = 0; row < spreadsheet.getTable().getRowCount(); row++) { String columnValue = (spreadsheet.getTable().getValueAt(row, colIndex) == null) ? "" : spreadsheet.getTable().getValueAt(row, colIndex).toString(); if (annotations.containsKey(columnValue)) { spreadsheet.getTable().setValueAt(annotations.get(columnValue).getShortForm(), row, colIndex);/*from ww w .j ava 2s. com*/ } } } } }
From source file:org.isatools.isacreator.api.utils.SpreadsheetUtils.java
/** * Method returns a Set of all the files defined in a spreadsheet. These locations are used to zip up the data files * in the ISArchive for submission to the index. * * @return Set of files defined in the spreadsheet *///ww w.jav a 2s.c o m public static Set<String> getFilesDefinedInTable(Spreadsheet spreadsheet) { Enumeration<TableColumn> columns = spreadsheet.getTable().getColumnModel().getColumns(); Set<String> files = new HashSet<String>(); while (columns.hasMoreElements()) { TableColumn tc = columns.nextElement(); if (spreadsheet.getTableReferenceObject().acceptsFileLocations(tc.getHeaderValue().toString())) { int colIndex = Utils.convertModelIndexToView(spreadsheet.getTable(), tc.getModelIndex()); for (int row = 0; row < spreadsheet.getTable().getRowCount(); row++) { String s = (spreadsheet.getTable().getValueAt(row, colIndex) == null) ? "" : spreadsheet.getTable().getValueAt(row, colIndex).toString(); if (s != null && !s.trim().equals("")) { files.add(s); } } } } return files; }
From source file:org.isatools.isacreator.api.utils.SpreadsheetUtils.java
/** * Given a spreadsheet and a set of values to find, will return a set of those * values found within the spreadsheet object * * @param spreadsheet - Spreadsheet to interrogate * @param values - Set of values to be searched for. * @param fieldFocus - target search on specific field types, e.g. Protocol REF, Characteristics and so forth. * @return Set<String> containing the values that were found. *//*from ww w. ja v a 2s .c o m*/ public static Set<String> findValueInSheet(Spreadsheet spreadsheet, Set<String> values, Set<String> fieldFocus) { Enumeration<TableColumn> columns = spreadsheet.getTable().getColumnModel().getColumns(); Set<String> foundValues = new HashSet<String>(); while (columns.hasMoreElements()) { TableColumn tc = columns.nextElement(); boolean headerContained = fieldFocus.isEmpty() || fieldFocus.contains(tc.getHeaderValue().toString()); if (headerContained) { int colIndex = Utils.convertModelIndexToView(spreadsheet.getTable(), tc.getModelIndex()); for (int row = 0; row < spreadsheet.getTable().getRowCount(); row++) { String columnValue = (spreadsheet.getTable().getValueAt(row, colIndex) == null) ? "" : spreadsheet.getTable().getValueAt(row, colIndex).toString(); if (values.contains(columnValue)) { foundValues.add(columnValue); } // end early if possible if (foundValues.size() == values.size()) { return foundValues; } } } } return foundValues; }
From source file:org.isatools.isacreator.api.utils.SpreadsheetUtils.java
public static Set<String> findValuesForColumnInSpreadsheet(Spreadsheet spreadsheet, String type) { Enumeration<TableColumn> columns = spreadsheet.getTable().getColumnModel().getColumns(); Set<String> values = new HashSet<String>(); while (columns.hasMoreElements()) { TableColumn tc = columns.nextElement(); boolean isTargetFieldType = tc.getHeaderValue().toString().contains(type); if (isTargetFieldType) { int colIndex = Utils.convertModelIndexToView(spreadsheet.getTable(), tc.getModelIndex()); for (int row = 0; row < spreadsheet.getTable().getRowCount(); row++) { String columnValue = (spreadsheet.getTable().getValueAt(row, colIndex) == null) ? "" : spreadsheet.getTable().getValueAt(row, colIndex).toString(); values.add(columnValue); }/*from w w w . ja v a 2 s.c o m*/ } } return values; }
From source file:org.isatools.isacreator.gui.formelements.SubForm.java
protected void removeColumn(int curColDelete) { if ((curColDelete == -1) || (curColDelete == 0)) { return;//from ww w .j ava 2 s . c om } if (defaultTableModel.getColumnCount() == 2 && curColDelete == (defaultTableModel.getColumnCount() - 1)) { clearColumn(curColDelete); return; } else { clearColumn(curColDelete); } if (fieldType == FieldTypes.ASSAY && (dataEntryForm != null) && !uneditableRecords.contains(curColDelete)) { clearColumn(curColDelete); return; } DefaultTableModel model = (DefaultTableModel) scrollTable.getModel(); // get the column. because 1 was added on previously to take account of the first column, we need to remove // it this time since the column indexes are now coming from the table. TableColumn col = scrollTable.getColumnModel().getColumn(curColDelete - 1); int columnModelIndex = col.getModelIndex(); Vector data = model.getDataVector(); Vector<String> colIds = new Vector<String>(); for (int i = 0; i < model.getColumnCount(); i++) { colIds.addElement(model.getColumnName(i)); } scrollTable.removeColumn(col); colIds.removeElementAt(columnModelIndex); // remove any data present in the column on deletion for (Object aData : data) { Vector row = (Vector) aData; row.removeElementAt(columnModelIndex); } model.setDataVector(data, colIds); // decrease each column index after deleted column by 1 so that indexes can be kept intact. Enumeration columnEnumeration = scrollTable.getColumnModel().getColumns(); while (columnEnumeration.hasMoreElements()) { TableColumn c = (TableColumn) columnEnumeration.nextElement(); if (c.getModelIndex() >= columnModelIndex) { c.setModelIndex(c.getModelIndex() - 1); } } if (fieldType == FieldTypes.ASSAY && uneditableRecords.contains(defaultTableModel.getColumnCount() - 1)) { uneditableRecords.remove(defaultTableModel.getColumnCount() - 1); } // update the model model.fireTableStructureChanged(); updateTables(); }
From source file:org.isatools.isacreator.spreadsheet.Spreadsheet.java
/** * This method checks through the spreadsheet to determine whether or not all the required fields defined in the configuration * have been filled in. If they have not been filled in, an ErrorLocator is logged and returned in a List of ErrorLocator objects! * * @return returns a List (@see List) of ErrorLocator (@see ErrorLocator) objects * @see org.isatools.isacreator.spreadsheet.model.TableReferenceObject * @see org.isatools.isacreator.archiveoutput.ArchiveOutputError *//*from w ww . j a v a 2 s . c om*/ public List<ArchiveOutputError> checkForCompleteness() { Enumeration<TableColumn> columns = table.getColumnModel().getColumns(); List<ArchiveOutputError> archiveOutputErrors = new ArrayList<ArchiveOutputError>(); boolean lastTermRequired = false; while (columns.hasMoreElements()) { TableColumn tc = columns.nextElement(); int columnViewIndex = Utils.convertModelIndexToView(table, tc.getModelIndex()); if (tableReferenceObject.isRequired(tc.getHeaderValue().toString()) || (tc.getHeaderValue().toString().equals("Unit") && lastTermRequired)) { lastTermRequired = SpreadsheetUtils .isFactorParameterOrCharacteristic(tc.getHeaderValue().toString()); for (int row = 0; row < spreadsheetModel.getRowCount(); row++) { Object cellObj = table.getValueAt(row, columnViewIndex); String value = (cellObj == null) ? "" : cellObj.toString().trim(); if (value.equals("")) { // a required value has not been filled! therefore report the index of the row and column as well as the calling] // location and message! archiveOutputErrors.add(new ArchiveOutputError( "Data missing for " + tc.getHeaderValue().toString() + " at record " + row, assayDataEntryEnvironment, tc.getHeaderValue().toString(), row, columnViewIndex)); } } } } return archiveOutputErrors; }
From source file:org.isatools.isacreator.spreadsheet.Spreadsheet.java
/** * Method will replace any absolute file paths to relative ones to match with their new location inside the ISArchive * * @param toSwitch -> to switch to relative, use Spreadsheet.SWITCH_RELATIVE, to switch back to absolute, use Spreadsheet.SWITCH_ABSOLUTE */// w w w. j ava 2 s .c o m public void changeFilesToRelativeOrAbsolute(int toSwitch) { Enumeration<TableColumn> columns = table.getColumnModel().getColumns(); if (absRelFileMappings == null) { absRelFileMappings = new HashMap<String, String>(); } while (columns.hasMoreElements()) { TableColumn tc = columns.nextElement(); if (tableReferenceObject.acceptsFileLocations(tc.getHeaderValue().toString())) { int colIndex = tc.getModelIndex(); for (int row = 0; row < spreadsheetModel.getRowCount(); row++) { String s = (spreadsheetModel.getValueAt(row, colIndex) == null) ? "" : spreadsheetModel.getValueAt(row, colIndex).toString(); if (s != null && !s.trim().equals("")) { switch (toSwitch) { case SWITCH_RELATIVE: if (!s.startsWith("ftp") && !s.startsWith("http")) { String newFileName = s.substring(s.lastIndexOf(File.separator) + 1); absRelFileMappings.put(newFileName, s); spreadsheetModel.doSetValueAt(newFileName, row, colIndex); } break; case SWITCH_ABSOLUTE: if (!s.startsWith("ftp") && !s.startsWith("ftps")) { String absFileName = absRelFileMappings.get(s); if (absFileName != null) { spreadsheetModel.doSetValueAt(absFileName, row, colIndex); } } break; } } } } } if (toSwitch == SWITCH_ABSOLUTE) { absRelFileMappings = null; } }
From source file:org.isatools.isacreator.spreadsheet.Spreadsheet.java
private Set<Integer> getRequiredFieldIndices() { Set<Integer> indices = new HashSet<Integer>(); Enumeration<TableColumn> columns = table.getColumnModel().getColumns(); boolean lastTermRequired = false; while (columns.hasMoreElements()) { TableColumn tc = columns.nextElement(); if (!tc.getHeaderValue().toString().equalsIgnoreCase(TableReferenceObject.ROW_NO_TEXT)) { int columnViewIndex = Utils.convertModelIndexToView(table, tc.getModelIndex()); if (tableReferenceObject.isRequired(tc.getHeaderValue().toString())) { if (tc.getHeaderValue().toString().equals("Unit") && lastTermRequired) { indices.add(columnViewIndex); } else { indices.add(columnViewIndex); lastTermRequired = SpreadsheetUtils .isFactorParameterOrCharacteristic(tc.getHeaderValue().toString()); }// w w w.j av a2s . c o m } } } return indices; }