1   package org.wcb.gui.util;
2   
3   import javax.swing.*;
4   import javax.swing.table.TableColumnModel;
5   import javax.swing.table.TableColumn;
6   import javax.swing.table.TableCellRenderer;
7   import java.awt.*;
8   
9   /**
10   * <small>
11   * <p/>
12   * Copyright (c)  2006  wbogaardt.
13   * This library is free software; you can redistribute it and/or
14   * modify it under the terms of the GNU Lesser General Public
15   * License as published by the Free Software Foundation; either
16   * version 2.1 of the License, or (at your option) any later version.
17   *
18   * This library is distributed in the hope that it will be useful,
19   * but WITHOUT ANY WARRANTY; without even the implied warranty of
20   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21   * Lesser General Public License for more details.
22   *
23   * You should have received a copy of the GNU Lesser General Public
24   * License along with this library; if not, write to the Free Software
25   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
26   * <p/>
27   * $File:  $ <br>
28   * $Change:  $ submitted by $Author: wbogaardt $ at $DateTime: Sep 1, 2006 8:21:08 AM $ <br>
29   * </small>
30   *
31   * @author wbogaardt
32   *         This is a utility class to allow calculating table columns
33   */
34  
35  public class TableUtils {
36  
37      /**
38       * Pass in table information and it will reformat the cells correctly
39       * @param table Reference to your table.
40       * @param insets TO get the table insets just call yourtable.getInsets()
41       * @param setMinimum  To set all column widths to minimum, then pass true.
42       * @param setMaximum  To set all column withs to their maximum, then pass true.
43       */
44      public static void setColumnWidths(JTable table, Insets insets,
45                                         boolean setMinimum,
46                                         boolean setMaximum) {
47          int columnCount = table.getColumnCount();
48          TableColumnModel tcm = table.getColumnModel();
49          int spare = (insets == null ? 0 : insets.left + insets.right);
50          for (int i = 0; i < columnCount; i++)
51          {
52              int width = calculateColumnWidth(table, i);
53              width += spare;
54              TableColumn column = tcm.getColumn(i);
55              column.setPreferredWidth(width);
56              if (setMinimum)
57              {
58                  column.setMinWidth(width);
59              }
60              if (setMaximum)
61              {
62                  column.setMaxWidth(width);
63              }
64          }
65  
66      }
67  
68      // Calculate the required width of a table column
69      private static int calculateColumnWidth(JTable table,
70                                              int columnIndex) {
71          int width = 0;     // The return value
72          int rowCount = table.getRowCount();
73          for (int i = 0; i < rowCount; i++)
74          {
75              TableCellRenderer renderer = table.getCellRenderer(i, columnIndex);
76              Component comp = renderer.getTableCellRendererComponent(
77                      table, table.getValueAt(i, columnIndex),
78                      false, false, i, columnIndex);
79              int thisWidth = comp.getPreferredSize().width;
80              if (thisWidth > width)
81              {
82                  width = thisWidth;
83              }
84          }
85  
86          return width;
87      }
88  
89  
90      /**
91       * Pass in table information and it will reformat the cells correctly
92       * @param table Reference to your table.
93       * @param insets TO get the table insets just call yourtable.getInsets()
94       * @param setMinimum  To set all column widths to minimum, then pass true.
95       * @param setMaximum  To set all column withs to their maximum, then pass true.
96       */
97      public static void setColumnWidths(JTable table, FontMetrics metrics, Insets insets,
98                                         boolean setMinimum,
99                                         boolean setMaximum) {
100         int columnCount = table.getColumnCount();
101         TableColumnModel tcm = table.getColumnModel();
102         int spare = (insets == null ? 0 : insets.left + insets.right);
103         for (int i = 0; i < columnCount; i++)
104         {
105             int width = calculateColumnWidthMetrics(table, i, metrics);
106             width += spare;
107             TableColumn column = tcm.getColumn(i);
108             column.setPreferredWidth(width);
109             if (setMinimum)
110             {
111                 column.setMinWidth(width);
112             }
113             if (setMaximum)
114             {
115                 column.setMaxWidth(width);
116             }
117         }
118 
119     }
120 
121 // Calculate the required width of a table column
122     private static int calculateColumnWidthMetrics(JTable table,
123                                                    int columnIndex, FontMetrics metrics) {
124         int width = columnHeaderWidth(metrics, table, columnIndex);      // The return value
125         int rowCount = table.getRowCount();
126         for (int i = 0; i < rowCount; i++)
127         {
128             TableCellRenderer renderer = table.getCellRenderer(i, columnIndex);
129             Component comp = renderer.getTableCellRendererComponent(
130                     table, table.getValueAt(i, columnIndex),
131                     false, false, i, columnIndex);
132             int thisWidth = comp.getPreferredSize().width;
133             if (thisWidth > width)
134             {
135                 width = thisWidth;
136             }
137         }
138 
139         return width;
140     }
141 
142 
143     private static int columnHeaderWidth(FontMetrics metrics, JTable table, int columnIndex) {
144         String value =  table.getColumnName(columnIndex);
145         //calculate the width required for the column
146         return metrics.stringWidth(value) + (2*table.getColumnModel().getColumnMargin());
147     }
148 }