您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

PackingTable.java 7.1KB

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