001/* 002 * Copyright (c) 2005 Jens Schou, Staffan Gustafsson, Bjorn Lanneskog, 003 * Einar Pehrson and Sebastian Kekkonen 004 * 005 * This file is part of 006 * CleanSheets Extension for Test Cases 007 * 008 * CleanSheets Extension for Test Cases is free software; you can 009 * redistribute it and/or modify it under the terms of the GNU General Public 010 * License as published by the Free Software Foundation; either version 2 of 011 * the License, or (at your option) any later version. 012 * 013 * CleanSheets Extension for Test Cases is distributed in the hope that 014 * it will be useful, but WITHOUT ANY WARRANTY; without even the implied 015 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 016 * See the GNU General Public License for more details. 017 * 018 * You should have received a copy of the GNU General Public License 019 * along with CleanSheets Extension for Test Cases; if not, write to the 020 * Free Software Foundation, Inc., 59 Temple Place, Suite 330, 021 * Boston, MA 02111-1307 USA 022 */ 023package csheets.ext.test.ui; 024 025import java.awt.Component; 026 027import javax.swing.JTable; 028import javax.swing.ListSelectionModel; 029import javax.swing.table.DefaultTableColumnModel; 030import javax.swing.table.TableCellRenderer; 031import javax.swing.table.TableColumn; 032import javax.swing.table.TableModel; 033 034/** 035 * A base class for the test case and test case parameter tables. 036 * @author Einar Pehrson 037 */ 038@SuppressWarnings("serial") 039public class TestTable extends JTable { 040 041 /** The margin around the packed columns for this model. */ 042 public static final int COLUMN_MARGIN = 10; 043 044 /** The minimum width of a column. */ 045 public static final int MINIMUM_COLUMN_WIDTH = 30; 046 047 /** The table's column model */ 048 private DefaultTableColumnModel columnModel; 049 050 /** 051 * Creates a new test table. 052 * @param tableModel the table model 053 */ 054 public TestTable(TableModel tableModel) { 055 super(tableModel); 056 057 // Stores members 058 this.columnModel = (DefaultTableColumnModel)getColumnModel(); 059 060 // Prevents user from moving and resizing columns 061 getTableHeader().setReorderingAllowed(false); 062 getTableHeader().setResizingAllowed(false); 063 setAutoResizeMode(JTable.AUTO_RESIZE_OFF); 064 065 // Configures selection mode 066 setColumnSelectionAllowed(true); 067 setRowSelectionAllowed(true); 068 setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 069 070 // Packs columns into right size 071 packColumns(); 072 } 073 074 /** 075 * Adjusts the width of all the columns in the table. 076 */ 077 public void packColumns() { 078 for (int i = 0; i < getColumnCount(); i++) 079 packColumn(i); 080 } 081 082 /** 083 * Adjusts the width of the column at the given index. 084 * @param columnIndex the index of the column to pack 085 */ 086 public void packColumn(int columnIndex) { 087 TableColumn column = columnModel.getColumn(columnIndex); 088 089 // Get width of column header 090 TableCellRenderer renderer = column.getHeaderRenderer(); 091 if (renderer == null) 092 renderer = this.getTableHeader().getDefaultRenderer(); 093 Component comp = renderer.getTableCellRendererComponent( 094 this, column.getHeaderValue(), false, false, 0, 0); 095 int width = comp.getPreferredSize().width; 096 097 // Get maximum width of column data 098 for (int rowIndex = 0; rowIndex < this.getRowCount(); rowIndex++) { 099 renderer = this.getCellRenderer(rowIndex, columnIndex); 100 comp = renderer.getTableCellRendererComponent(this, getValueAt( 101 rowIndex, columnIndex), false, false, rowIndex, columnIndex); 102 width = Math.max(width, comp.getPreferredSize().width); 103 } 104 105 // Add margin 106 width += 2 * COLUMN_MARGIN; 107 if (width > MINIMUM_COLUMN_WIDTH) { 108 column.setPreferredWidth(width); 109 } else 110 column.setPreferredWidth(MINIMUM_COLUMN_WIDTH); 111 } 112}