You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PackingTable.java 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * Copyright (c) 2006-2014 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.addons.ui_swing.components;
  23. import java.awt.Graphics;
  24. import javax.swing.JScrollPane;
  25. import javax.swing.JTable;
  26. import javax.swing.table.DefaultTableModel;
  27. import javax.swing.table.TableColumn;
  28. import javax.swing.table.TableColumnModel;
  29. import javax.swing.table.TableModel;
  30. import net.miginfocom.layout.PlatformDefaults;
  31. /**
  32. * Creates a new table that automatically sizes its columns to the size of its data.
  33. */
  34. public class PackingTable extends JTable {
  35. /** A version number for this class. */
  36. private static final long serialVersionUID = 1;
  37. /** Scrollpane. */
  38. private final JScrollPane scrollPane;
  39. /** Should the last column fit text (true), or fit viewport (false). */
  40. private final boolean lastColumnFit;
  41. /** Border padding. */
  42. private final int padding = (int) PlatformDefaults.getUnitValueX("related").
  43. getValue();
  44. /**
  45. * Creates a new packing table.
  46. *
  47. * @param rows Row data
  48. * @param cols Column data
  49. * @param scrollPane Scrollpane parent
  50. */
  51. public PackingTable(final Object[][] rows, final Object[] cols,
  52. final JScrollPane scrollPane) {
  53. this(new DefaultTableModel(rows, cols), scrollPane, true);
  54. }
  55. /**
  56. * Creates a new packing table.
  57. *
  58. * @param rows Row data
  59. * @param cols Column data
  60. * @param scrollPane Scrollpane parent
  61. * @param lastColumnFit Should the last column fit text (true), or fit viewport (false).
  62. */
  63. public PackingTable(final Object[][] rows, final Object[] cols,
  64. final JScrollPane scrollPane, final boolean lastColumnFit) {
  65. this(new DefaultTableModel(rows, cols), scrollPane,
  66. lastColumnFit);
  67. }
  68. /**
  69. * Creates a new packing table.
  70. *
  71. * @param tableModel Table data model
  72. * @param scrollPane Scrollpane parent
  73. */
  74. public PackingTable(final TableModel tableModel,
  75. final JScrollPane scrollPane) {
  76. this(tableModel, scrollPane, true);
  77. }
  78. /**
  79. * Creates a new packing table.
  80. *
  81. * @param tableModel Table data model
  82. * @param scrollPane Scrollpane parent
  83. * @param lastColumnFit Should the last column fit text (true), or fit viewport (false).
  84. */
  85. public PackingTable(final TableModel tableModel,
  86. final JScrollPane scrollPane, final boolean lastColumnFit) {
  87. super(tableModel);
  88. this.scrollPane = scrollPane;
  89. this.lastColumnFit = lastColumnFit;
  90. super.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
  91. getTableHeader().setResizingAllowed(false);
  92. getTableHeader().setReorderingAllowed(false);
  93. setDragEnabled(false);
  94. }
  95. @Override
  96. public void setAutoResizeMode(final int mode) {
  97. //Ignore
  98. }
  99. @Override
  100. public final boolean getScrollableTracksViewportHeight() {
  101. return getPreferredSize().height < getParent().getHeight();
  102. }
  103. @Override
  104. public final void paint(final Graphics g) {
  105. packColumns();
  106. super.paint(g);
  107. }
  108. /** Packs the columns to their width. */
  109. public final void packColumns() {
  110. if (!isShowing()) {
  111. return;
  112. }
  113. if (getColumnCount() == 0) {
  114. return;
  115. }
  116. final TableColumnModel myColumnModel = getTableHeader()
  117. .getColumnModel();
  118. final int numCols = myColumnModel.getColumnCount();
  119. final int totalSize = (int) (scrollPane.getViewportBorderBounds().width * 0.99);
  120. final int[] widths = new int[numCols];
  121. int widthsTotal = 0;
  122. int checkNumCols = numCols;
  123. if (!lastColumnFit) {
  124. checkNumCols--;
  125. }
  126. for (int i = 0; i < checkNumCols; i++) { //NOPMD I'm not copying
  127. widths[i] = getWidth(i);
  128. widthsTotal += widths[i];
  129. }
  130. final int extra = totalSize - widthsTotal;
  131. if (extra > 0) {
  132. widths[numCols - 1] += extra;
  133. }
  134. for (int i = 0; i < numCols; i++) {
  135. final TableColumn col = myColumnModel.getColumn(i);
  136. col.setPreferredWidth(widths[i]);
  137. }
  138. }
  139. /**
  140. * Returns the width of a column.
  141. *
  142. * @param col Column to retrieve width for
  143. *
  144. * @return Width of the specified column
  145. */
  146. private int getWidth(final int col) {
  147. if (getColumnCount() == 0) {
  148. return 0;
  149. }
  150. if (getColumnCount() <= col) {
  151. return 0;
  152. }
  153. final TableColumn column = getColumnModel().getColumn(col);
  154. int width = (int) getTableHeader().getDefaultRenderer().
  155. getTableCellRendererComponent(this, column.getIdentifier(),
  156. false, false, -1, col).getPreferredSize().getWidth();
  157. if (getRowCount() == 0) {
  158. return width + padding;
  159. }
  160. for (int row = 0; row < getRowCount(); row++) {
  161. if (getCellRenderer(row, col) != null) {
  162. width = Math.max(width, (int) getCellRenderer(row, col).
  163. getTableCellRendererComponent(this, getValueAt(row,
  164. col), false, false, row, col).getPreferredSize().
  165. getWidth());
  166. }
  167. }
  168. return width + padding;
  169. }
  170. }