File Source: PackingTable.java

         /* 
    P/P   *  Method: com.dmdirc.addons.ui_swing.components.PackingTable__static_init
          */
     1  /*
     2   * Copyright (c) 2006-2009 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  
    23  package com.dmdirc.addons.ui_swing.components;
    24  
    25  import java.awt.Graphics;
    26  
    27  import javax.swing.JScrollPane;
    28  import javax.swing.JTable;
    29  import javax.swing.table.DefaultTableModel;
    30  import javax.swing.table.TableColumn;
    31  import javax.swing.table.TableColumnModel;
    32  import javax.swing.table.TableModel;
    33  import net.miginfocom.layout.PlatformDefaults;
    34  
    35  /**
    36   * Creates a new table that automatically sizes its columns to the size of its
    37   * data.
    38   */
    39  public class PackingTable extends JTable {
    40  
    41      /**
    42       * A version number for this class. It should be changed whenever the class
    43       * structure is changed (or anything else that would prevent serialized
    44       * objects being unserialized with the new class).
    45       */
    46      private static final long serialVersionUID = 1;
    47      /** Whether the table should be editable. */
    48      private final boolean editable;
    49      /** Scrollpane. */
    50      private final JScrollPane scrollPane;
    51      /** Should the last column fit text (true), or fit viewport (false). */
    52      private boolean lastColumnFit;
    53      /** Border padding. */
    54      private final int padding = (int) PlatformDefaults.getUnitValueX("related").
    55              getValue();
    56      
    57      /**
    58       * Creates a new packing table.
    59       *
    60       * @param rows Row data
    61       * @param cols Column data
    62       * @param editable Whether the table should be editable or not
    63       * @param scrollPane Scrollpane parent
    64       */
    65      public PackingTable(final Object[][] rows, final Object[] cols,
    66              final boolean editable, final JScrollPane scrollPane) {
                 /* 
    P/P           *  Method: void com.dmdirc.addons.ui_swing.components.PackingTable(Object[][], Object[], bool, JScrollPane)
                  * 
                  *  Presumptions:
                  *    (int) (net.miginfocom.layout.UnitValue:getValue(...)@54) in {-231..232-1}
                  * 
                  *  Postconditions:
                  *    this.editable == editable
                  *    init'ed(this.editable)
                  *    this.lastColumnFit == 1
                  *    init'ed(this.padding)
                  *    this.scrollPane == scrollPane
                  *    init'ed(this.scrollPane)
                  */
    67          this(new DefaultTableModel(rows, cols), editable, scrollPane, true);
    68      }
    69  
    70      /**
    71       * Creates a new packing table.
    72       *
    73       * @param rows Row data
    74       * @param cols Column data
    75       * @param editable Whether the table should be editable or not
    76       * @param scrollPane Scrollpane parent
    77       * @param lastColumnFit Should the last column fit text (true), or fit viewport (false).
    78       */
    79      public PackingTable(final Object[][] rows, final Object[] cols,
    80              final boolean editable, final JScrollPane scrollPane,
    81              final boolean lastColumnFit) {
                 /* 
    P/P           *  Method: void com.dmdirc.addons.ui_swing.components.PackingTable(Object[][], Object[], bool, JScrollPane, bool)
                  * 
                  *  Presumptions:
                  *    (int) (net.miginfocom.layout.UnitValue:getValue(...)@54) in {-231..232-1}
                  * 
                  *  Postconditions:
                  *    this.editable == editable
                  *    init'ed(this.editable)
                  *    this.lastColumnFit == lastColumnFit
                  *    init'ed(this.lastColumnFit)
                  *    init'ed(this.padding)
                  *    this.scrollPane == scrollPane
                  *    init'ed(this.scrollPane)
                  */
    82          this(new DefaultTableModel(rows, cols), editable, scrollPane,
    83                  lastColumnFit);
    84      }
    85      
    86      /**
    87       * Creates a new packing table.
    88       *
    89       * @param tableModel Table data model
    90       * @param editable Whether the table should be editable or not
    91       * @param scrollPane Scrollpane parent
    92       */
    93      public PackingTable(final TableModel tableModel, final boolean editable,
    94              final JScrollPane scrollPane) {
                 /* 
    P/P           *  Method: void com.dmdirc.addons.ui_swing.components.PackingTable(TableModel, bool, JScrollPane)
                  * 
                  *  Presumptions:
                  *    (int) (net.miginfocom.layout.UnitValue:getValue(...)@54) in {-231..232-1}
                  * 
                  *  Postconditions:
                  *    this.editable == editable
                  *    init'ed(this.editable)
                  *    this.lastColumnFit == 1
                  *    init'ed(this.padding)
                  *    this.scrollPane == scrollPane
                  *    init'ed(this.scrollPane)
                  */
    95          this(tableModel, editable, scrollPane, true);
    96      }
    97  
    98      /**
    99       * Creates a new packing table.
   100       *
   101       * @param tableModel Table data model
   102       * @param editable Whether the table should be editable or not
   103       * @param scrollPane Scrollpane parent
   104       * @param lastColumnFit Should the last column fit text (true), or fit viewport (false).
   105       */
   106      public PackingTable(final TableModel tableModel, final boolean editable,
   107              final JScrollPane scrollPane, final boolean lastColumnFit) {
                 /* 
    P/P           *  Method: void com.dmdirc.addons.ui_swing.components.PackingTable(TableModel, bool, JScrollPane, bool)
                  * 
                  *  Presumptions:
                  *    (int) (net.miginfocom.layout.UnitValue:getValue(...)@54) in {-231..232-1}
                  *    javax.swing.JTable:getTableHeader(...)@115 != null
                  *    javax.swing.JTable:getTableHeader(...)@116 != null
                  *    net.miginfocom.layout.PlatformDefaults:getUnitValueX(...)@54 != null
                  * 
                  *  Postconditions:
                  *    this.editable == editable
                  *    init'ed(this.editable)
                  *    this.lastColumnFit == lastColumnFit
                  *    init'ed(this.lastColumnFit)
                  *    init'ed(this.padding)
                  *    this.scrollPane == scrollPane
                  *    init'ed(this.scrollPane)
                  */
   108          super(tableModel);
   109  
   110          this.editable = editable;
   111          this.scrollPane = scrollPane;
   112          this.lastColumnFit = lastColumnFit;
   113  
   114          super.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
   115          super.getTableHeader().setResizingAllowed(false);
   116          super.getTableHeader().setReorderingAllowed(false);
   117          super.setDragEnabled(false);
   118      }
   119  
   120      /** {@inheritDoc} */
   121      @Override
   122      public void setAutoResizeMode(final int mode) {
   123      //Ignore
             /* 
    P/P       *  Method: void setAutoResizeMode(int)
              */
   124      }
   125  
   126      /** {@inheritDoc} */
   127      @Override
   128      public final boolean getScrollableTracksViewportHeight() {
                 /* 
    P/P           *  Method: bool getScrollableTracksViewportHeight()
                  * 
                  *  Presumptions:
                  *    com.dmdirc.addons.ui_swing.components.PackingTable:getParent(...)@129 != null
                  *    com.dmdirc.addons.ui_swing.components.PackingTable:getPreferredSize(...)@129 != null
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  */
   129          return getPreferredSize().height < getParent().getHeight();
   130      }
   131  
   132      /** {@inheritDoc} */
   133      @Override
   134      public final void paint(final Graphics g) {
                 /* 
    P/P           *  Method: void paint(Graphics)
                  * 
                  *  Preconditions:
                  *    (soft) init'ed(this.lastColumnFit)
                  *    (soft) this.scrollPane != null
                  */
   135          packColumns();
   136          super.paint(g);
   137      }
   138  
   139      /** Packs the columns to their width. */
   140      public final void packColumns() {
                 /* 
    P/P           *  Method: void packColumns()
                  * 
                  *  Preconditions:
                  *    (soft) init'ed(this.lastColumnFit)
                  *    (soft) this.scrollPane != null
                  * 
                  *  Presumptions:
                  *    com.dmdirc.addons.ui_swing.components.PackingTable:getTableHeader(...)@149 != null
                  *    javax.swing.JScrollPane:getViewportBorderBounds(...)@151 != null
                  *    javax.swing.table.JTableHeader:getColumnModel(...)@149 != null
                  *    javax.swing.table.TableColumnModel:getColumn(...)@170 != null
                  *    javax.swing.table.TableColumnModel:getColumnCount(...)@150 >= 1
                  * 
                  *  Test Vectors:
                  *    this.lastColumnFit: {1}, {0}
                  *    com.dmdirc.addons.ui_swing.components.PackingTable:getColumnCount(...)@145: {-231..-1, 1..232-1}, {0}
                  *    com.dmdirc.addons.ui_swing.components.PackingTable:isShowing(...)@141: {1}, {0}
                  */
   141          if (!isShowing()) {
   142              return;
   143          }
   144  
   145          if (getColumnCount() == 0) {
   146              return;
   147          }
   148  
   149          final TableColumnModel myColumnModel = getTableHeader().getColumnModel();
   150          final int numCols = myColumnModel.getColumnCount();
   151          final int totalSize = scrollPane.getViewportBorderBounds().width;
   152          final int[] widths = new int[numCols];
   153          int widthsTotal = 0;
   154  
   155          int checkNumCols = numCols;
   156          if (!lastColumnFit) {
   157              checkNumCols--;
   158          }
   159          for (int i = 0; i < checkNumCols; i++) { //NOPMD im not copying a damn array fgs
   160              widths[i] = getWidth(i);
   161              widthsTotal += widths[i];
   162          }
   163  
   164          final int extra = totalSize - widthsTotal;
   165          if (extra > 0) {
   166              widths[numCols - 1] += extra;
   167          }
   168  
   169          for (int i = 0; i < numCols; i++) {
   170              final TableColumn col = myColumnModel.getColumn(i);
   171              col.setPreferredWidth(widths[i]);
   172          }
   173  
   174      }
   175  
   176      /**
   177       * Returns the width of a column.
   178       *
   179       * @param col Column to retrieve width for
   180       *
   181       * @return Width of the specified column
   182       */
   183      private int getWidth(final int col) {
                 /* 
    P/P           *  Method: int getWidth(int)
                  * 
                  *  Presumptions:
                  *    (int) (java.awt.Dimension:getWidth(...)@192) in {-231..232-1}
                  *    (int) (java.awt.Dimension:getWidth(...)@202) in {-231..232-1}
                  *    com.dmdirc.addons.ui_swing.components.PackingTable:getCellRenderer(...)@202 != null
                  *    com.dmdirc.addons.ui_swing.components.PackingTable:getColumnModel(...)@191 != null
                  *    com.dmdirc.addons.ui_swing.components.PackingTable:getTableHeader(...)@192 != null
                  *    ...
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  * 
                  *  Test Vectors:
                  *    com.dmdirc.addons.ui_swing.components.PackingTable:getCellRenderer(...)@201: Addr_Set{null}, Inverse{null}
                  *    com.dmdirc.addons.ui_swing.components.PackingTable:getColumnCount(...)@184: {-231..-1, 1..232-1}, {0}
                  *    com.dmdirc.addons.ui_swing.components.PackingTable:getRowCount(...)@196: {-231..-1, 1..232-1}, {0}
                  */
   184          if (getColumnCount() == 0) {
   185              return 0;
   186          }
   187          if (getColumnCount() <= col) {
   188              return 0;
   189          }
   190  
   191          final TableColumn column = getColumnModel().getColumn(col);
   192          int width = (int) getTableHeader().getDefaultRenderer().
   193                  getTableCellRendererComponent(this, column.getIdentifier(),
   194                  false, false, -1, col).getPreferredSize().getWidth();
   195  
   196          if (getRowCount() == 0) {
   197              return width + padding;
   198          }
   199  
   200          for (int row = 0; row < getRowCount(); row++) {
   201              if (getCellRenderer(row, col) != null) {
   202                  width = Math.max(width, (int) getCellRenderer(row, col).
   203                          getTableCellRendererComponent(this, getValueAt(row,
   204                          col), false, false, row, col).getPreferredSize().
   205                          getWidth());
   206              }
   207          }
   208  
   209          return width + padding;
   210      }
   211  }








SofCheck Inspector Build Version : 2.17854
PackingTable.java 2009-Jun-25 01:54:24
PackingTable.class 2009-Sep-02 17:04:14