From 78dac7e498c52eb253cf25842034c096660b1d78 Mon Sep 17 00:00:00 2001 From: SiboVG Date: Wed, 25 Oct 2023 01:23:56 +0200 Subject: [PATCH] Compute optimal column width if not in preference --- .../preset/ComponentPresetChooserDialog.java | 14 +++--- .../net/sf/openrocket/gui/util/GUIUtil.java | 43 ++++++++++++++----- .../gui/util/TableUIPreferences.java | 17 +++++++- 3 files changed, 57 insertions(+), 17 deletions(-) diff --git a/swing/src/net/sf/openrocket/gui/dialogs/preset/ComponentPresetChooserDialog.java b/swing/src/net/sf/openrocket/gui/dialogs/preset/ComponentPresetChooserDialog.java index b06123c93..e991f7c26 100644 --- a/swing/src/net/sf/openrocket/gui/dialogs/preset/ComponentPresetChooserDialog.java +++ b/swing/src/net/sf/openrocket/gui/dialogs/preset/ComponentPresetChooserDialog.java @@ -156,10 +156,10 @@ public class ComponentPresetChooserDialog extends JDialog { // need to create componentSelectionTable before filter checkboxes, // but add to panel after componentSelectionTable = new ComponentPresetTable(presetType, presets, displayedColumnKeys); - GUIUtil.setAutomaticColumnTableWidths(componentSelectionTable, 20); + + // Make the first column (the favorite column) as small as possible int w = componentSelectionTable.getRowHeight() + 4; XTableColumnModel tm = componentSelectionTable.getXColumnModel(); - //TableColumn tc = componentSelectionTable.getColumnModel().getColumn(0); TableColumn tc = tm.getColumn(0); tc.setPreferredWidth(w); tc.setMaxWidth(w); @@ -168,7 +168,14 @@ public class ComponentPresetChooserDialog extends JDialog { // The normal left/right and tab/shift-tab key action traverses each cell/column of the table instead of going to the next row. TableRowTraversalPolicy.setTableRowTraversalPolicy(componentSelectionTable); + // Add checkboxes (legacy, match fore diameter, ...) panel.add(getFilterCheckboxes(tm, legacyColumnIndex), "wrap para"); + + // Load the table UI settings from the preferences + boolean legacySelected = showLegacyCheckBox.isSelected(); + TableUIPreferences.loadTableUIPreferences(componentSelectionTable, TABLE_ID + component.getComponentName(), + preferences.getTablePreferences()); + showLegacyCheckBox.setSelected(legacySelected); // Restore legacy state (may change during UI preference loading) JScrollPane scrollpane = new JScrollPane(); scrollpane.setViewportView(componentSelectionTable); @@ -219,9 +226,6 @@ public class ComponentPresetChooserDialog extends JDialog { this.setLocationByPlatform(true); GUIUtil.rememberWindowPosition(this); - TableUIPreferences.loadTableUIPreferences(componentSelectionTable, TABLE_ID + component.getComponentName(), - preferences.getTablePreferences()); - updateFilters(); } diff --git a/swing/src/net/sf/openrocket/gui/util/GUIUtil.java b/swing/src/net/sf/openrocket/gui/util/GUIUtil.java index 18aa91732..5a3541888 100644 --- a/swing/src/net/sf/openrocket/gui/util/GUIUtil.java +++ b/swing/src/net/sf/openrocket/gui/util/GUIUtil.java @@ -58,6 +58,7 @@ import javax.swing.event.ChangeListener; import javax.swing.table.AbstractTableModel; import javax.swing.table.DefaultTableColumnModel; import javax.swing.table.DefaultTableModel; +import javax.swing.table.TableCellRenderer; import javax.swing.table.TableColumn; import javax.swing.table.TableColumnModel; import javax.swing.table.TableModel; @@ -447,26 +448,46 @@ public class GUIUtil { window.setLocation(position); } } - - - public static void setAutomaticColumnTableWidths(JTable table, int max) { + + /** + * Computes the optimal column widths for the specified table, based on the content in all rows for that column, + * and optionally also the column header content. + * @param table the table + * @param max the maximum width for a column + * @param includeHeaderWidth whether to include the width of the column header + * @return an array of column widths + */ + public static int[] computeOptimalColumnWidths(JTable table, int max, boolean includeHeaderWidth) { int columns = table.getColumnCount(); - int widths[] = new int[columns]; + int[] widths = new int[columns]; Arrays.fill(widths, 1); - + + // Consider the width required by the header text if includeHeaderWidth is true + if (includeHeaderWidth) { + TableCellRenderer headerRenderer = table.getTableHeader().getDefaultRenderer(); + for (int col = 0; col < columns; col++) { + TableColumn column = table.getColumnModel().getColumn(col); + Component headerComp = headerRenderer.getTableCellRendererComponent(table, column.getHeaderValue(), false, false, 0, col); + widths[col] = headerComp.getPreferredSize().width; + } + } + + // Compare header width to the width required by the cell data for (int row = 0; row < table.getRowCount(); row++) { for (int col = 0; col < columns; col++) { Object value = table.getValueAt(row, col); - //System.out.println("row=" + row + " col=" + col + " : " + value); - widths[col] = Math.max(widths[col], value == null ? 0 : value.toString().length()); + TableCellRenderer cellRenderer = table.getCellRenderer(row, col); + Component cellComp = cellRenderer.getTableCellRendererComponent(table, value, false, false, row, col); + int cellWidth = cellComp.getPreferredSize().width; + widths[col] = Math.max(widths[col], cellWidth); } } - - + for (int col = 0; col < columns; col++) { - //System.err.println("Setting column " + col + " to width " + widths[col]); - table.getColumnModel().getColumn(col).setPreferredWidth(Math.min(widths[col], max) * 100); + widths[col] = Math.min(widths[col], max * 100); // Adjusting for your max value and scaling } + + return widths; } public static Window getWindowAncestor(Component c) { diff --git a/swing/src/net/sf/openrocket/gui/util/TableUIPreferences.java b/swing/src/net/sf/openrocket/gui/util/TableUIPreferences.java index d5887d4bb..2f5830df5 100644 --- a/swing/src/net/sf/openrocket/gui/util/TableUIPreferences.java +++ b/swing/src/net/sf/openrocket/gui/util/TableUIPreferences.java @@ -69,10 +69,25 @@ public class TableUIPreferences { } } + // Check if any column width is missing from preferences + boolean computeOptimalWidths = false; + for (int i = 0; i < table.getColumnCount() && !computeOptimalWidths; i++) { + TableColumn column = table.getColumnModel().getColumn(i); + if (preferences.get(tableName + TABLE_COLUMN_WIDTH_PREFIX + column.getIdentifier(), null) == null) { + computeOptimalWidths = true; + } + } + + // If any column width is missing, compute optimal widths for all columns + int[] optimalWidths = null; + if (computeOptimalWidths) { + optimalWidths = GUIUtil.computeOptimalColumnWidths(table, 20, true); + } + // Restore column widths for (int i = 0; i < table.getColumnCount(); i++) { TableColumn column = table.getColumnModel().getColumn(i); - int defaultWidth = column.getWidth(); + int defaultWidth = (optimalWidths != null) ? optimalWidths[i] : column.getWidth(); int width = preferences.getInt(tableName + TABLE_COLUMN_WIDTH_PREFIX + column.getIdentifier(), defaultWidth); column.setPreferredWidth(width); }