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 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. * Copyright (c) 2006-2011 Chris Smith, Shane Mc Cormack, Gregory Holmes
  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
  33. * data.
  34. */
  35. public class PackingTable extends JTable {
  36. /**
  37. * A version number for this class. It should be changed whenever the class
  38. * structure is changed (or anything else that would prevent serialized
  39. * objects being unserialized with the new class).
  40. */
  41. private static final long serialVersionUID = 1;
  42. /** Scrollpane. */
  43. private final JScrollPane scrollPane;
  44. /** Should the last column fit text (true), or fit viewport (false). */
  45. private boolean lastColumnFit;
  46. /** Border padding. */
  47. private final int padding = (int) PlatformDefaults.getUnitValueX("related").
  48. getValue();
  49. /**
  50. * Creates a new packing table.
  51. *
  52. * @param rows Row data
  53. * @param cols Column data
  54. * @param editable Whether the table should be editable or not
  55. * @param scrollPane Scrollpane parent
  56. */
  57. public PackingTable(final Object[][] rows, final Object[] cols,
  58. final boolean editable, final JScrollPane scrollPane) {
  59. this(new DefaultTableModel(rows, cols), editable, scrollPane, true);
  60. }
  61. /**
  62. * Creates a new packing table.
  63. *
  64. * @param rows Row data
  65. * @param cols Column data
  66. * @param editable Whether the table should be editable or not
  67. * @param scrollPane Scrollpane parent
  68. * @param lastColumnFit Should the last column fit text (true),
  69. * or fit viewport (false).
  70. */
  71. public PackingTable(final Object[][] rows, final Object[] cols,
  72. final boolean editable, final JScrollPane scrollPane,
  73. final boolean lastColumnFit) {
  74. this(new DefaultTableModel(rows, cols), editable, scrollPane,
  75. lastColumnFit);
  76. }
  77. /**
  78. * Creates a new packing table.
  79. *
  80. * @param tableModel Table data model
  81. * @param editable Whether the table should be editable or not
  82. * @param scrollPane Scrollpane parent
  83. */
  84. public PackingTable(final TableModel tableModel, final boolean editable,
  85. final JScrollPane scrollPane) {
  86. this(tableModel, editable, scrollPane, true);
  87. }
  88. /**
  89. * Creates a new packing table.
  90. *
  91. * @param tableModel Table data model
  92. * @param editable Whether the table should be editable or not
  93. * @param scrollPane Scrollpane parent
  94. * @param lastColumnFit Should the last column fit text (true),
  95. * or fit viewport (false).
  96. */
  97. public PackingTable(final TableModel tableModel, final boolean editable,
  98. final JScrollPane scrollPane, final boolean lastColumnFit) {
  99. super(tableModel);
  100. this.scrollPane = scrollPane;
  101. this.lastColumnFit = lastColumnFit;
  102. super.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
  103. super.getTableHeader().setResizingAllowed(false);
  104. super.getTableHeader().setReorderingAllowed(false);
  105. super.setDragEnabled(false);
  106. }
  107. /** {@inheritDoc} */
  108. @Override
  109. public void setAutoResizeMode(final int mode) {
  110. //Ignore
  111. }
  112. /** {@inheritDoc} */
  113. @Override
  114. public final boolean getScrollableTracksViewportHeight() {
  115. return getPreferredSize().height < getParent().getHeight();
  116. }
  117. /** {@inheritDoc} */
  118. @Override
  119. public final void paint(final Graphics g) {
  120. packColumns();
  121. super.paint(g);
  122. }
  123. /** Packs the columns to their width. */
  124. public final void packColumns() {
  125. if (!isShowing()) {
  126. return;
  127. }
  128. if (getColumnCount() == 0) {
  129. return;
  130. }
  131. final TableColumnModel myColumnModel = getTableHeader()
  132. .getColumnModel();
  133. final int numCols = myColumnModel.getColumnCount();
  134. final int totalSize = (int) (scrollPane.getViewportBorderBounds()
  135. .width * 0.99);
  136. final int[] widths = new int[numCols];
  137. int widthsTotal = 0;
  138. int checkNumCols = numCols;
  139. if (!lastColumnFit) {
  140. checkNumCols--;
  141. }
  142. for (int i = 0; i < checkNumCols; i++) { //NOPMD I'm not copying
  143. widths[i] = getWidth(i);
  144. widthsTotal += widths[i];
  145. }
  146. final int extra = totalSize - widthsTotal;
  147. if (extra > 0) {
  148. widths[numCols - 1] += extra;
  149. }
  150. for (int i = 0; i < numCols; i++) {
  151. final TableColumn col = myColumnModel.getColumn(i);
  152. col.setPreferredWidth(widths[i]);
  153. }
  154. }
  155. /**
  156. * Returns the width of a column.
  157. *
  158. * @param col Column to retrieve width for
  159. *
  160. * @return Width of the specified column
  161. */
  162. private int getWidth(final int col) {
  163. if (getColumnCount() == 0) {
  164. return 0;
  165. }
  166. if (getColumnCount() <= col) {
  167. return 0;
  168. }
  169. final TableColumn column = getColumnModel().getColumn(col);
  170. int width = (int) getTableHeader().getDefaultRenderer().
  171. getTableCellRendererComponent(this, column.getIdentifier(),
  172. false, false, -1, col).getPreferredSize().getWidth();
  173. if (getRowCount() == 0) {
  174. return width + padding;
  175. }
  176. for (int row = 0; row < getRowCount(); row++) {
  177. if (getCellRenderer(row, col) != null) {
  178. width = Math.max(width, (int) getCellRenderer(row, col).
  179. getTableCellRendererComponent(this, getValueAt(row,
  180. col), false, false, row, col).getPreferredSize().
  181. getWidth());
  182. }
  183. }
  184. return width + padding;
  185. }
  186. }