01: /*
02: * Copyright 2006-2007 Pentaho Corporation. All rights reserved.
03: * This software was developed by Pentaho Corporation and is provided under the terms
04: * of the Mozilla Public License, Version 1.1, or any later version. You may not use
05: * this file except in compliance with the license. If you need a copy of the license,
06: * please go to http://www.mozilla.org/MPL/MPL-1.1.txt.
07: *
08: * Software distributed under the Mozilla Public License is distributed on an "AS IS"
09: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
10: * the license for the specific language governing your rights and limitations.
11: *
12: * Additional Contributor(s): Martin Schmid gridvision engineering GmbH
13: */
14: package org.pentaho.reportdesigner.lib.client.util;
15:
16: import org.jetbrains.annotations.NonNls;
17: import org.jetbrains.annotations.NotNull;
18:
19: import javax.swing.*;
20: import javax.swing.table.TableCellRenderer;
21: import java.awt.*;
22: import java.util.logging.Level;
23: import java.util.logging.Logger;
24:
25: /**
26: * User: Martin
27: * Date: 10.02.2006
28: * Time: 13:34:18
29: */
30: public class TableUtils {
31: @NotNull
32: @NonNls
33: private static final Logger LOG = Logger.getLogger(TableUtils.class
34: .getName());
35:
36: private TableUtils() {
37: }
38:
39: public static void resizeAllColumns(@NotNull
40: JTable table, int maxColumnWidth) {
41: int columnCount = table.getColumnModel().getColumnCount();
42: for (int i = 0; i < columnCount; i++) {
43: int columnWidth = getColumnWidth(table, i, 100);
44: columnWidth = Math.min(maxColumnWidth, columnWidth);
45: table.getColumnModel().getColumn(i).setWidth(columnWidth);
46: table.getColumnModel().getColumn(i).setPreferredWidth(
47: columnWidth);
48: }
49: }
50:
51: public static int getColumnWidth(@NotNull
52: JTable table, int columnIndex, int maxRowsToCheck) {
53: int width = -1;
54: int rowCount = Math.min(table.getRowCount(), maxRowsToCheck);
55:
56: for (int i = 0; i < rowCount; i++) {
57: TableCellRenderer tableCellRenderer = table
58: .getCellRenderer(i, columnIndex);
59: Component component = tableCellRenderer
60: .getTableCellRendererComponent(table, table
61: .getValueAt(i, columnIndex), false, false,
62: i, columnIndex);
63: int this Width = component.getPreferredSize().width;
64: if (this Width > width) {
65: width = this Width;
66: }
67: }
68:
69: try {
70: TableCellRenderer tableCellRenderer = table
71: .getTableHeader().getColumnModel().getColumn(
72: columnIndex).getHeaderRenderer();
73: if (tableCellRenderer == null) {
74: tableCellRenderer = table.getTableHeader()
75: .getDefaultRenderer();
76: }
77: Object title = table.getTableHeader().getColumnModel()
78: .getColumn(columnIndex).getHeaderValue();
79: Component component = tableCellRenderer
80: .getTableCellRendererComponent(table, title, false,
81: false, 0, columnIndex);
82: int this Width = component.getPreferredSize().width;
83: if (this Width > width) {
84: width = this Width;
85: }
86: } catch (Throwable ex) {
87: /* eat the exception */
88: if (LOG.isLoggable(Level.FINE))
89: LOG.log(Level.FINE, "TableUtils.getColumnWidth ", ex);
90: }
91:
92: width += 10;
93:
94: return width;
95: }
96: }
|