Debian Codegraph @

rcdkjar/src/org/guha/rcdk/util/TablePacker.java

raw
1 package org.guha.rcdk.util;
2
3 import javax.swing.*;
4 import javax.swing.table.TableColumn;
5 import javax.swing.table.TableColumnModel;
6 import java.awt.*;
7
8 /**
9 * MySwing: Advanced Swing Utilites
10 * Copyright (C) 2005 Santhosh Kumar T
11 * <p/>
12 * This library is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public
14 * License as published by the Free Software Foundation; either
15 * version 2.1 of the License, or (at your option) any later version.
16 * <p/>
17 * This library is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
21 *
22 * @author Santhosh Kumar T
23 */
24 public class TablePacker {
25 public static final int VISIBLE_ROWS = 0;
26 public static final int ALL_ROWS = 1;
27 public static final int NO_ROWS = 2;
28
29 private int rowsIncluded = VISIBLE_ROWS;
30 private boolean distributeExtraArea = true;
31
32 public TablePacker(int rowsIncluded, boolean distributeExtraArea) {
33 this.rowsIncluded = rowsIncluded;
34 this.distributeExtraArea = distributeExtraArea;
35 }
36
37 private int preferredWidth(JTable table, int col) {
38 TableColumn tableColumn = table.getColumnModel().getColumn(col);
39 int width = (int) table.getTableHeader().getDefaultRenderer()
40 .getTableCellRendererComponent(table, tableColumn.getIdentifier()
41 , false, false, -1, col).getPreferredSize().getWidth();
42
43 if (table.getRowCount() != 0) {
44 int from = 0, to = 0;
45 if (rowsIncluded == VISIBLE_ROWS) {
46 Rectangle rect = table.getVisibleRect();
47 from = table.rowAtPoint(rect.getLocation());
48 to = table.rowAtPoint(new Point((int) rect.getMaxX(), (int) rect.getMaxY())) + 1;
49 } else if (rowsIncluded == ALL_ROWS) {
50 from = 0;
51 to = table.getRowCount();
52 }
53 for (int row = from; row < to; row++) {
54 int preferedWidth = (int) table.getCellRenderer(row, col).getTableCellRendererComponent(table,
55 table.getValueAt(row, col), false, false, row, col).getPreferredSize().getWidth();
56 width = Math.max(width, preferedWidth);
57 }
58 }
59 return width + table.getIntercellSpacing().width;
60 }
61
62 public void pack(JTable table) {
63 if (!table.isShowing())
64 throw new IllegalStateException("table must be showing to pack");
65
66 if (table.getColumnCount() == 0)
67 return;
68
69 int width[] = new int[table.getColumnCount()];
70 int total = 0;
71 for (int col = 0; col < width.length; col++) {
72 width[col] = preferredWidth(table, col);
73 total += width[col];
74 }
75
76 int extra = table.getVisibleRect().width - total;
77 if (extra > 0) {
78 if (distributeExtraArea) {
79 int bonus = extra / table.getColumnCount();
80 for (int i = 0; i < width.length; i++)
81 width[i] += bonus;
82 extra -= bonus * table.getColumnCount();
83 }
84 width[width.length - 1] += extra;
85 }
86
87 TableColumnModel columnModel = table.getColumnModel();
88 for (int col = 0; col < width.length; col++) {
89 TableColumn tableColumn = columnModel.getColumn(col);
90 table.getTableHeader().setResizingColumn(tableColumn);
91 tableColumn.setWidth(width[col]);
92 }
93 }
94 }