ServerManagement: automatically adust table and window size to display table contents...
authorTJ <hacker@iam.tj>
Sat, 6 Jun 2015 15:41:35 +0000 (16:41 +0100)
committerTJ <hacker@iam.tj>
Sat, 6 Jun 2015 15:41:35 +0000 (16:41 +0100)
src/uk/ac/ntu/n0521366/wsyd/management/ServerManagement.java

index 5dbfa01..20b74b3 100644 (file)
@@ -121,6 +121,20 @@ public class ServerManagement extends javax.swing.JFrame implements NetworkMessa
      */
     private boolean _autoScroll;
 
+    /**
+     * Track the most recently calculated minimum width of all log table columns that doesn't truncate any content.
+     * 
+     * @see ServerManagement#isLoggable(java.util.logging.LogRecord)
+     */
+    private int currentLogTableRowWidth = 0;
+
+    /**
+     * Track the most recently calculated minimum width of each log table column.
+     * 
+     * @see ServerManagement#isLoggable(java.util.logging.LogRecord)
+     */
+    private int currentLogTableRowWidths[] = new int[4];
+    
     /**
      * Regular presence announcements
      */
@@ -302,10 +316,33 @@ public class ServerManagement extends javax.swing.JFrame implements NetworkMessa
                             record.getMessage()
                         }
                 );
-                if (_autoScroll) { // keep last record added in the view
-                    int row = gLogTable.getModel().getRowCount();
-                    gLogTable.scrollRectToVisible(gLogTable.getCellRect(row, 0, true));
+                // calculate row preferredWidth
+                int windowMargin = this.getWidth() - gLogTable.getWidth();
+                int tableMargin = gLogTable.getWidth() - gLogTable.getColumnModel().getTotalColumnWidth();
+                int lastRow = gLogTable.getRowCount() - 1;
+                int spacing = gLogTable.getIntercellSpacing().width;
+                int rowWidth = 0;
+                for (int column = 0; column < gLogTable.getColumnCount(); column++) {
+                    int preferredWidth = gLogTable.prepareRenderer(gLogTable.getCellRenderer(lastRow, column), lastRow, column).getPreferredSize().width;
+                    int actualWidth = gLogTable.prepareRenderer(gLogTable.getCellRenderer(lastRow, column), lastRow, column).getSize().width;
+                    int minimumWidth = java.lang.Math.max(actualWidth, preferredWidth);
+                    if (minimumWidth > currentLogTableRowWidths[column])
+                        currentLogTableRowWidths[column] = minimumWidth + 8; // add hard-coded margin to ensure column is wider than rendered contents
+
+                    rowWidth += spacing + currentLogTableRowWidths[column];
+                    gLogTable.getColumnModel().getColumn(column).setPreferredWidth(currentLogTableRowWidths[column]);
+                }
+                if (rowWidth > currentLogTableRowWidth) {
+                    java.awt.Dimension windowSize = new java.awt.Dimension(windowMargin + tableMargin + rowWidth, this.getHeight());
+                    // FIXME: isLoggable(): remove auto table column and window resize console debug messages once resizing is confirmed to work correctly
+                    System.err.println(MessageFormat.format("Before: window={0} margin={2} table={1} margin={3} currentRowWidth={4} newRowWidth={5}", this.getWidth(), gLogTable.getWidth(), windowMargin, tableMargin, currentLogTableRowWidth, rowWidth));
+                    gLogTable.validate();
+                    this.setSize(windowSize);
+                    currentLogTableRowWidth = rowWidth;
+                    System.err.println(MessageFormat.format(" After: window={0} margin={2} table={1} margin={3} currentRowWidth={4} newRowWidth={5}", this.getWidth(), gLogTable.getWidth(), windowMargin, tableMargin, currentLogTableRowWidth, rowWidth));
                 }
+                if (_autoScroll) // keep last record added in the view
+                    gLogTable.scrollRectToVisible(gLogTable.getCellRect(lastRow, 0, true));
             }
         }
         return _doLogging;