PageRenderTime 169ms CodeModel.GetById 0ms RepoModel.GetById 1ms app.codeStats 0ms

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

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