Search in sources :

Example 1 with PackableTableModel

use of jgnash.ui.register.table.PackableTableModel in project jgnash by ccavanaugh.

the class JTableUtils method packTable.

public static void packTable(final JTable table) {
    if (!(table.getModel() instanceof PackableTableModel)) {
        packGenericTable(table);
        return;
    }
    PackableTableModel model = (PackableTableModel) table.getModel();
    /*
         * Get width of printable portion of the page to control column widths
         */
    int tableWidth = table.getWidth();
    // calculated optimal widths
    int[] widths = new int[model.getColumnCount()];
    // total of calculated widths
    int tWidth = 0;
    for (int i = 0; i < model.getColumnCount(); i++) {
        TableColumn col = table.getColumnModel().getColumn(i);
        // Get the column header width
        TableCellRenderer renderer = col.getHeaderRenderer();
        if (renderer == null) {
            renderer = table.getTableHeader().getDefaultRenderer();
        }
        Component comp = renderer.getTableCellRendererComponent(table, col.getHeaderValue(), false, false, 0, 0);
        int width = comp.getMinimumSize().width;
        // Find the largest width in the column
        int rows = table.getRowCount();
        for (int r = 0; r < rows; r++) {
            renderer = table.getCellRenderer(r, i);
            comp = renderer.getTableCellRendererComponent(table, table.getValueAt(r, i), false, false, r, i);
            width = Math.max(width, comp.getMinimumSize().width);
        }
        widths[i] = width;
        if (i != model.getColumnCount() - 1) {
            widths[i] += 4;
        } else {
            widths[i] += MIN_WIDTH_PADDING;
        }
        tWidth += widths[i];
    }
    int[] optimizedWidths = widths.clone();
    if (tWidth > tableWidth) {
        // calculated width is wider than the page... need to compress columns
        // integer widths to calc percentage widths
        // create a clone so return array is not modified
        int[] columnWeights = model.getPreferredColumnWeights().clone();
        // total fixed width of columns
        int fixedWidth = 0;
        for (int i = 0; i < optimizedWidths.length; i++) {
            if (columnWeights[i] == 0) {
                fixedWidth += optimizedWidths[i];
            }
        }
        // remaining non fixed width that must be compressed
        int diff = tableWidth - fixedWidth;
        // used to calculate percentages
        int totalWeight = 0;
        for (int columnWeight : columnWeights) {
            totalWeight += columnWeight;
        }
        int i = 0;
        while (i < columnWeights.length) {
            if (columnWeights[i] > 0) {
                int adj = (int) ((float) columnWeights[i] / (float) totalWeight * diff);
                if (optimizedWidths[i] > adj) {
                    // only change if necessary
                    optimizedWidths[i] = adj;
                } else {
                    // available difference is reduced
                    diff -= optimizedWidths[i];
                    // adjust the weighting
                    totalWeight -= columnWeights[i];
                    // reset widths
                    optimizedWidths = widths.clone();
                    // do not try to adjust width again
                    columnWeights[i] = 0;
                    // restart the loop from the beginning
                    i = -1;
                }
            }
            i++;
        }
    }
    for (int i = 0; i < model.getColumnCount(); i++) {
        TableColumn col = table.getColumnModel().getColumn(i);
        col.setPreferredWidth(optimizedWidths[i]);
    }
}
Also used : TableCellRenderer(javax.swing.table.TableCellRenderer) PackableTableModel(jgnash.ui.register.table.PackableTableModel) Component(java.awt.Component) TableColumn(javax.swing.table.TableColumn)

Aggregations

Component (java.awt.Component)1 TableCellRenderer (javax.swing.table.TableCellRenderer)1 TableColumn (javax.swing.table.TableColumn)1 PackableTableModel (jgnash.ui.register.table.PackableTableModel)1