@Override public int getRowHeight() { if (myRowHeightIsComputing) { return super.getRowHeight(); } if (myRowHeight < 0) { try { myRowHeightIsComputing = true; for (int row = 0; row < getRowCount(); row++) { for (int column = 0; column < getColumnCount(); column++) { final TableCellRenderer renderer = getCellRenderer(row, column); if (renderer != null) { final Object value = getValueAt(row, column); final Component component = renderer.getTableCellRendererComponent(this, value, true, true, row, column); if (component != null) { final Dimension size = component.getPreferredSize(); myRowHeight = Math.max(size.height, myRowHeight); } } } } } finally { myRowHeightIsComputing = false; } } if (myMinRowHeight == null) { myMinRowHeight = getFontMetrics(UIManager.getFont("Label.font")).getHeight(); } return Math.max(myRowHeight, myMinRowHeight); }
public void updateColumnSizes() { final JTableHeader header = getTableHeader(); final TableCellRenderer defaultRenderer = header == null ? null : header.getDefaultRenderer(); final RowSorter<? extends TableModel> sorter = getRowSorter(); final List<? extends RowSorter.SortKey> current = sorter == null ? null : sorter.getSortKeys(); ColumnInfo[] columns = getListTableModel().getColumnInfos(); for (int i = 0; i < columns.length; i++) { final ColumnInfo columnInfo = columns[i]; final TableColumn column = getColumnModel().getColumn(i); // hack to get sort arrow included into the renderer component if (sorter != null && columnInfo.isSortable()) { sorter.setSortKeys( Collections.singletonList(new RowSorter.SortKey(i, SortOrder.ASCENDING))); } final Component headerComponent = defaultRenderer == null ? null : defaultRenderer.getTableCellRendererComponent( this, column.getHeaderValue(), false, false, 0, 0); if (sorter != null && columnInfo.isSortable()) { sorter.setSortKeys(current); } final Dimension headerSize = headerComponent == null ? new Dimension(0, 0) : headerComponent.getPreferredSize(); final String maxStringValue; final String preferredValue; if (columnInfo.getWidth(this) > 0) { int width = columnInfo.getWidth(this); column.setMaxWidth(width); column.setPreferredWidth(width); column.setMinWidth(width); } else if ((maxStringValue = columnInfo.getMaxStringValue()) != null) { int width = getFontMetrics(getFont()).stringWidth(maxStringValue) + columnInfo.getAdditionalWidth(); width = Math.max(width, headerSize.width); column.setPreferredWidth(width); column.setMaxWidth(width); } else if ((preferredValue = columnInfo.getPreferredStringValue()) != null) { int width = getFontMetrics(getFont()).stringWidth(preferredValue) + columnInfo.getAdditionalWidth(); width = Math.max(width, headerSize.width); column.setPreferredWidth(width); } } }
private boolean setColumnPreferredSize() { boolean sizeCalculated = false; Font tableFont = UIManager.getFont("Table.font"); for (int i = 0; i < getColumnCount(); i++) { TableColumn column = getColumnModel().getColumn(i); if (i == GraphTableModel.ROOT_COLUMN) { // thin stripe, or root name, or nothing setRootColumnSize(column); } else if (i == GraphTableModel.COMMIT_COLUMN) { // let commit message occupy as much as possible column.setPreferredWidth(Short.MAX_VALUE); } else if (i == GraphTableModel.AUTHOR_COLUMN) { // detect author with the longest name // to avoid querying the last row (it would lead to full graph loading) int maxRowsToCheck = Math.min(MAX_ROWS_TO_CALC_WIDTH, getRowCount() - MAX_ROWS_TO_CALC_OFFSET); if (maxRowsToCheck < 0) { // but if the log is small, check all of them maxRowsToCheck = getRowCount(); } int maxWidth = 0; for (int row = 0; row < maxRowsToCheck; row++) { String value = getModel().getValueAt(row, i).toString(); maxWidth = Math.max(getFontMetrics(tableFont).stringWidth(value), maxWidth); if (!value.isEmpty()) sizeCalculated = true; } column.setMinWidth( Math.min(maxWidth + UIUtil.DEFAULT_HGAP, MAX_DEFAULT_AUTHOR_COLUMN_WIDTH)); column.setWidth(column.getMinWidth()); } else if (i == GraphTableModel.DATE_COLUMN) { // all dates have nearly equal sizes column.setMinWidth( getFontMetrics(tableFont) .stringWidth("mm" + DateFormatUtil.formatDateTime(new Date()))); column.setWidth(column.getMinWidth()); } } return sizeCalculated; }
public void setColumnWidths() { // See // "http://java.sun.com/docs/books/tutorial/uiswing/components/table.html#custom" int n = getModel().getColumnCount(); for (int j = 0; j < n; ++j) { TableColumn column = getColumnModel().getColumn(j); TableCellRenderer headerRenderer = column.getHeaderRenderer(); if (headerRenderer == null) headerRenderer = getTableHeader().getDefaultRenderer(); // the new 1.3 way Component columnComponent = headerRenderer.getTableCellRendererComponent( this, column.getHeaderValue(), false, false, -1, j); Component cellComponent = getDefaultRenderer(getColumnClass(j)) .getTableCellRendererComponent(this, getModel().getValueAt(0, j), false, false, 0, j); int wantWidth = Math.max( columnComponent.getPreferredSize().width + 10, // fudge factor ... seems to always be too small otherwise (on x86 Linux) cellComponent.getPreferredSize().width + 10 // fudge factor ... seems to always be too small otherwise (on Mac OS X) ); column.setPreferredWidth(wantWidth); } }
private int getHeaderHeight() { if ((header == null) || (header.getTable() == null)) { return 0; } int height = 0; boolean accomodatedDefault = false; TableColumnModel columnModel = header.getColumnModel(); for (int column = 0; column < columnModel.getColumnCount(); column++) { TableColumn aColumn = columnModel.getColumn(column); boolean isDefault = (aColumn.getHeaderRenderer() == null); if (!isDefault || !accomodatedDefault) { Component comp = getHeaderRenderer(column); int rendererHeight = comp.getPreferredSize().height; height = Math.max(height, rendererHeight); // Configuring the header renderer to calculate its preferred size // is expensive. Optimise this by assuming the default renderer // always has the same height as the first non-zero height that // it returns for a non-null/non-empty value. if (isDefault && rendererHeight > 0) { Object headerValue = aColumn.getHeaderValue(); if (headerValue != null) { headerValue = headerValue.toString(); if (headerValue != null && !headerValue.equals("")) { accomodatedDefault = true; } } } } } return height + 2; }
// This method from http://www.exampledepot.com/egs/javax.swing.table/PackCol.html public int packColumn(JTable table, int vColIndex, int margin) { DefaultTableColumnModel colModel = (DefaultTableColumnModel) table.getColumnModel(); TableColumn col = colModel.getColumn(vColIndex); int width = 0; // Get width of column header TableCellRenderer renderer = col.getHeaderRenderer(); if (renderer == null) { renderer = table.getTableHeader().getDefaultRenderer(); } Component comp = renderer.getTableCellRendererComponent(table, col.getHeaderValue(), false, false, 0, 0); width = comp.getPreferredSize().width; // Get maximum width of column data for (int r = 0; r < table.getRowCount(); r++) { renderer = table.getCellRenderer(r, vColIndex); comp = renderer.getTableCellRendererComponent( table, table.getValueAt(r, vColIndex), false, false, r, vColIndex); width = Math.max(width, comp.getPreferredSize().width); } // Add margin width += 2 * margin; // Set the width col.setPreferredWidth(width); return width; }
private int calculateMaxRootWidth() { int width = 0; for (VirtualFile file : myLogDataHolder.getRoots()) { Font tableFont = UIManager.getFont("Table.font"); width = Math.max(getFontMetrics(tableFont).stringWidth(file.getName() + " "), width); } return width; }
private int calcMaxContentColumnWidth(int columnIndex, int maxRowsToCheck) { int maxWidth = 0; for (int row = 0; row < maxRowsToCheck && row < getRowCount(); row++) { TableCellRenderer renderer = getCellRenderer(row, columnIndex); Component comp = prepareRenderer(renderer, row, columnIndex); maxWidth = Math.max(comp.getPreferredSize().width, maxWidth); } return maxWidth + UIUtil.DEFAULT_HGAP; }
private void scrollToRow(Integer row, Integer delta) { Rectangle startRect = myTable.getCellRect(row, 0, true); myTable.scrollRectToVisible( new Rectangle( startRect.x, Math.max(startRect.y - delta, 0), startRect.width, myTable.getVisibleRect().height)); }
/* * Adjust the width of the specified column in the table */ public void adjustColumn(final int column) { TableColumn tableColumn = table.getColumnModel().getColumn(column); if (!tableColumn.getResizable()) return; int columnHeaderWidth = getColumnHeaderWidth(column); int columnDataWidth = getColumnDataWidth(column); int preferredWidth = Math.max(columnHeaderWidth, columnDataWidth); updateTableColumn(column, preferredWidth); }
@Override public Component getTableCellEditorComponent( JTable table, Object value, boolean isSelected, int row, int column) { if (value == null) { return sldSlider; } if (value instanceof Integer) { int v = (Integer) value; sldSlider.setValue(Math.min(Math.max(v, min), max)); } else { sldSlider.setValue(max); } return sldSlider; }
public void updateColumnSizes() { final JTableHeader header = getTableHeader(); final TableCellRenderer defaultRenderer = header == null ? null : header.getDefaultRenderer(); ColumnInfo[] columns = getListTableModel().getColumnInfos(); for (int i = 0; i < columns.length; i++) { final ColumnInfo columnInfo = columns[i]; final TableColumn column = getColumnModel().getColumn(i); final Component headerComponent = defaultRenderer == null ? null : defaultRenderer.getTableCellRendererComponent( this, column.getHeaderValue(), false, false, 0, 0); final Dimension headerSize = headerComponent == null ? new Dimension(0, 0) : headerComponent.getPreferredSize(); final String maxStringValue; final String preferredValue; if (columnInfo.getWidth(this) > 0) { int width = columnInfo.getWidth(this); column.setMaxWidth(width); column.setPreferredWidth(width); column.setMinWidth(width); } else if ((maxStringValue = columnInfo.getMaxStringValue()) != null) { int width = getFontMetrics(getFont()).stringWidth(maxStringValue) + columnInfo.getAdditionalWidth(); width = Math.max(width, headerSize.width); column.setPreferredWidth(width); column.setMaxWidth(width); } else if ((preferredValue = columnInfo.getPreferredStringValue()) != null) { int width = getFontMetrics(getFont()).stringWidth(preferredValue) + columnInfo.getAdditionalWidth(); width = Math.max(width, headerSize.width); column.setPreferredWidth(width); } } }
/* * Calculate the width based on the widest cell renderer for the * given column. */ private int getColumnDataWidth(int column) { if (!isColumnDataIncluded) return 0; int preferredWidth = 0; int maxWidth = table.getColumnModel().getColumn(column).getMaxWidth(); for (int row = 0; row < table.getRowCount(); row++) { preferredWidth = Math.max(preferredWidth, getCellDataWidth(row, column)); // We've exceeded the maximum width, no need to check other rows if (preferredWidth >= maxWidth) break; } return preferredWidth; }
/* * Update the TableColumn with the newly calculated width */ private void updateTableColumn(int column, int width) { final TableColumn tableColumn = table.getColumnModel().getColumn(column); if (!tableColumn.getResizable()) return; width += spacing; // Don't shrink the column width if (isOnlyAdjustLarger) { width = Math.max(width, tableColumn.getPreferredWidth()); } columnSizes.put(tableColumn, new Integer(tableColumn.getWidth())); table.getTableHeader().setResizingColumn(tableColumn); tableColumn.setWidth(width); }
private void setMarked(int[] rows, final boolean marked) { if (rows == null || rows.length == 0) { return; } int firstRow = Integer.MAX_VALUE; int lastRow = Integer.MIN_VALUE; final Boolean newValue = marked ? Boolean.TRUE : Boolean.FALSE; for (final int row : rows) { final T element = myElements.get(row); final Boolean prevValue = myMarkedMap.put(element, newValue); if (!newValue.equals(prevValue)) { notifyElementMarked(element, newValue.booleanValue()); } firstRow = Math.min(firstRow, row); lastRow = Math.max(lastRow, row); } fireTableRowsUpdated(firstRow, lastRow); }
/** * Calculates statistical values for a data array. * * @param data the data array * @return the max, min, mean, SD, SE and non-NaN data count */ private double[] getStatistics(double[] data) { double max = -Double.MAX_VALUE; double min = Double.MAX_VALUE; double sum = 0.0; double squareSum = 0.0; int count = 0; for (int i = 0; i < data.length; i++) { if (Double.isNaN(data[i])) { continue; } count++; max = Math.max(max, data[i]); min = Math.min(min, data[i]); sum += data[i]; squareSum += data[i] * data[i]; } double mean = sum / count; double sd = count < 2 ? Double.NaN : Math.sqrt((squareSum - count * mean * mean) / (count - 1)); if (max == -Double.MAX_VALUE) max = Double.NaN; if (min == Double.MAX_VALUE) min = Double.NaN; return new double[] {max, min, mean, sd, sd / Math.sqrt(count), count}; }
public void updateColumnSizes() { final JTableHeader header = getTableHeader(); final TableCellRenderer defaultRenderer = header == null ? null : header.getDefaultRenderer(); final RowSorter<? extends TableModel> sorter = getRowSorter(); final List<? extends RowSorter.SortKey> current = sorter == null ? null : sorter.getSortKeys(); ColumnInfo[] columns = getListTableModel().getColumnInfos(); int[] sizeMode = new int[columns.length]; int[] headers = new int[columns.length]; int[] widths = new int[columns.length]; int allColumnWidth = 0; int varCount = 0; // calculate for (int i = 0; i < columns.length; i++) { final ColumnInfo columnInfo = columns[i]; final TableColumn column = getColumnModel().getColumn(i); // hack to get sort arrow included into the renderer component if (sorter != null && columnInfo.isSortable()) { sorter.setSortKeys( Collections.singletonList(new RowSorter.SortKey(i, SortOrder.ASCENDING))); } final Component headerComponent = defaultRenderer == null ? null : defaultRenderer.getTableCellRendererComponent( this, column.getHeaderValue(), false, false, 0, 0); if (sorter != null && columnInfo.isSortable()) { sorter.setSortKeys(current); } if (headerComponent != null) { headers[i] = headerComponent.getPreferredSize().width; } final String maxStringValue; final String preferredValue; if (columnInfo.getWidth(this) > 0) { sizeMode[i] = 1; int width = columnInfo.getWidth(this); widths[i] = width; } else if ((maxStringValue = columnInfo.getMaxStringValue()) != null) { sizeMode[i] = 2; widths[i] = getFontMetrics(getFont()).stringWidth(maxStringValue) + columnInfo.getAdditionalWidth(); varCount++; } else if ((preferredValue = columnInfo.getPreferredStringValue()) != null) { sizeMode[i] = 3; widths[i] = getFontMetrics(getFont()).stringWidth(preferredValue) + columnInfo.getAdditionalWidth(); varCount++; } allColumnWidth += widths[i]; } // apply: distribute available space between resizable columns // and make sure that header will fit as well int viewWidth = getParent() != null ? getParent().getWidth() : getWidth(); double gold = 0.5 * (3 - Math.sqrt(5)); int addendum = varCount == 0 || viewWidth < allColumnWidth ? 0 : (int) ((allColumnWidth < gold * viewWidth ? gold * viewWidth : allColumnWidth < (1 - gold) * viewWidth ? (1 - gold) * viewWidth : viewWidth) - allColumnWidth) / varCount; for (int i = 0; i < columns.length; i++) { TableColumn column = getColumnModel().getColumn(i); int width = widths[i]; if (sizeMode[i] == 1) { column.setMaxWidth(width); column.setPreferredWidth(width); column.setMinWidth(width); } else if (sizeMode[i] == 2) { width = Math.max(width + addendum, headers[i]); column.setPreferredWidth(width); column.setMaxWidth(width); } else if (sizeMode[i] == 3) { width = Math.max(width + addendum, headers[i]); column.setPreferredWidth(width); } } }
/** Returns the number of rows. */ public int getRowCount() { if (memory != null) return Math.max(memory.getMemorySize() - startAddress, 0); else return 0; }
public void setMinRowHeight(int i) { setRowHeight(Math.max(i, getRowHeight())); }
private int clamp(int val, int min, int max) { return Math.max(Math.min(val, max), min); }