/plugins/SVNPlugin/tags/1.6.0/src/ise/plugin/svn/gui/BestRowTable.java

# · Java · 139 lines · 69 code · 13 blank · 57 comment · 2 complexity · 18d0fcfbbb0ad27b78b79347efb8da7f MD5 · raw file

  1. /*
  2. Copyright (c) 2007, Dale Anson
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without modification,
  5. are permitted provided that the following conditions are met:
  6. * Redistributions of source code must retain the above copyright notice,
  7. this list of conditions and the following disclaimer.
  8. * Redistributions in binary form must reproduce the above copyright notice,
  9. this list of conditions and the following disclaimer in the documentation
  10. and/or other materials provided with the distribution.
  11. * Neither the name of the author nor the names of its contributors
  12. may be used to endorse or promote products derived from this software without
  13. specific prior written permission.
  14. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  15. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  16. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  17. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
  18. ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  19. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  20. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  21. ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  23. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. package ise.plugin.svn.gui;
  26. import java.awt.*;
  27. import java.util.*;
  28. import javax.swing.*;
  29. import javax.swing.table.*;
  30. import javax.swing.event.*;
  31. import javax.swing.text.html.*;
  32. /**
  33. * A JTable that lays out the rows based on the best height for the individual
  34. * rows.
  35. */
  36. public class BestRowTable extends JTable {
  37. private int bestHeight = 0;
  38. public BestRowTable() {
  39. super();
  40. }
  41. public BestRowTable( int numRows, int numColumns ) {
  42. super( numRows, numColumns );
  43. }
  44. public BestRowTable( Object[][] rowData, Object[] columnNames ) {
  45. super( rowData, columnNames );
  46. }
  47. public BestRowTable( TableModel dm ) {
  48. super( dm );
  49. }
  50. public BestRowTable( TableModel dm, TableColumnModel cm ) {
  51. super( dm, cm );
  52. }
  53. public BestRowTable( TableModel dm, TableColumnModel cm, ListSelectionModel sm ) {
  54. super( dm, cm, sm );
  55. }
  56. public BestRowTable( Vector rowData, Vector columnNames ) {
  57. super( rowData, columnNames );
  58. }
  59. @Override
  60. public void doLayout() {
  61. packRows();
  62. super.doLayout();
  63. }
  64. /**
  65. * @param row the row number in the table to calculate the best height for
  66. * @param margin cell margin
  67. * @return the preferred height of a row. JTable doesn't provide this.
  68. * The returned value is equal to the tallest preferred height of all the
  69. * cells in the row.
  70. */
  71. public int getPreferredRowHeight( int row, int margin ) {
  72. int height = 1;
  73. // determine tallest cell in the row
  74. for ( int column = 0; column < getColumnCount(); column++ ) {
  75. TableCellRenderer renderer = getCellRenderer( row, column );
  76. Component comp = prepareRenderer( renderer, row, column );
  77. int preferred = comp.getPreferredSize().height + ( 2 * margin );
  78. height = Math.max( height, preferred );
  79. }
  80. return height;
  81. }
  82. /**
  83. * @return the best height for this table.
  84. */
  85. public int getBestHeight() {
  86. return bestHeight;
  87. }
  88. /**
  89. * Calculate and set the best height of the table using the preferred height
  90. * of each row and margin size of 1.
  91. */
  92. public void packRows() {
  93. packRows( 1 );
  94. }
  95. /**
  96. * Calculate and set the best height of the table using the preferred height
  97. * of each row and the specified margin within each cell.
  98. * @param margin the size of the margin to be applied to each cell.
  99. */
  100. public void packRows( int margin ) {
  101. packRows( 0, getRowCount(), margin );
  102. }
  103. /**
  104. * Adjust the heights of a range of rows.
  105. * @param start the starting row of the range
  106. * @param end the ending row of the range
  107. * @param margin the size of the margin to be applied to each cell.
  108. */
  109. public void packRows( int start, int end, int margin ) {
  110. bestHeight = 0;
  111. for ( int row = 0; row < getRowCount(); row++ ) {
  112. int height = getPreferredRowHeight( row, margin );
  113. setRowHeight( row, height );
  114. bestHeight += height;
  115. }
  116. }
  117. @Override
  118. public Dimension getPreferredSize() {
  119. Dimension d = super.getPreferredSize();
  120. d.height = getBestHeight();
  121. return d;
  122. }
  123. }