[4] | 1 | /* |
---|
| 2 | * This program is free software; you can redistribute it and/or modify |
---|
| 3 | * it under the terms of the GNU General Public License as published by |
---|
| 4 | * the Free Software Foundation; either version 2 of the License, or |
---|
| 5 | * (at your option) any later version. |
---|
| 6 | * |
---|
| 7 | * This program is distributed in the hope that it will be useful, |
---|
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 10 | * GNU General Public License for more details. |
---|
| 11 | * |
---|
| 12 | * You should have received a copy of the GNU General Public License |
---|
| 13 | * along with this program; if not, write to the Free Software |
---|
| 14 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
| 15 | */ |
---|
| 16 | |
---|
| 17 | /* |
---|
| 18 | * JTableHelper.java |
---|
| 19 | * Copyright (C) 2005 University of Waikato, Hamilton, New Zealand |
---|
| 20 | * |
---|
| 21 | */ |
---|
| 22 | |
---|
| 23 | package weka.gui; |
---|
| 24 | |
---|
| 25 | import java.awt.Component; |
---|
| 26 | import java.awt.Point; |
---|
| 27 | import java.awt.Rectangle; |
---|
| 28 | import javax.swing.JTable; |
---|
| 29 | import javax.swing.JViewport; |
---|
| 30 | import javax.swing.table.JTableHeader; |
---|
| 31 | import javax.swing.table.TableCellRenderer; |
---|
| 32 | import javax.swing.table.TableColumn; |
---|
| 33 | import javax.swing.table.TableColumnModel; |
---|
| 34 | import javax.swing.table.TableModel; |
---|
| 35 | |
---|
| 36 | /** |
---|
| 37 | * A helper class for JTable, e.g. calculating the optimal colwidth. |
---|
| 38 | * |
---|
| 39 | * |
---|
| 40 | * @author FracPete (fracpete at waikato dot ac dot nz) |
---|
| 41 | * @version $Revision: 1.2 $ |
---|
| 42 | */ |
---|
| 43 | |
---|
| 44 | public class JTableHelper { |
---|
| 45 | // the table to work with |
---|
| 46 | private JTable jtable; |
---|
| 47 | |
---|
| 48 | /** |
---|
| 49 | * initializes the object |
---|
| 50 | */ |
---|
| 51 | public JTableHelper(JTable jtable) { |
---|
| 52 | this.jtable = jtable; |
---|
| 53 | } |
---|
| 54 | |
---|
| 55 | /** |
---|
| 56 | * returns the JTable |
---|
| 57 | */ |
---|
| 58 | public JTable getJTable() { |
---|
| 59 | return jtable; |
---|
| 60 | } |
---|
| 61 | |
---|
| 62 | /** |
---|
| 63 | * calcs the optimal column width of the given column |
---|
| 64 | */ |
---|
| 65 | public int calcColumnWidth(int col) { |
---|
| 66 | return calcColumnWidth(getJTable(), col); |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | /** |
---|
| 70 | * Calculates the optimal width for the column of the given table. The |
---|
| 71 | * calculation is based on the preferred width of the header and cell |
---|
| 72 | * renderer. |
---|
| 73 | * <br> |
---|
| 74 | * Taken from the newsgoup de.comp.lang.java with some modifications.<br> |
---|
| 75 | * Taken from FOPPS/EnhancedTable - http://fopps.sourceforge.net/<br> |
---|
| 76 | * |
---|
| 77 | * @param table the table to calculate the column width |
---|
| 78 | * @param col the column to calculate the widths |
---|
| 79 | * @return the width, -1 if error |
---|
| 80 | */ |
---|
| 81 | public static int calcColumnWidth(JTable table, int col) { |
---|
| 82 | int width = calcHeaderWidth(table, col); |
---|
| 83 | if (width == -1) |
---|
| 84 | return width; |
---|
| 85 | |
---|
| 86 | TableColumnModel columns = table.getColumnModel(); |
---|
| 87 | TableModel data = table.getModel(); |
---|
| 88 | int rowCount = data.getRowCount(); |
---|
| 89 | TableColumn column = columns.getColumn(col); |
---|
| 90 | try { |
---|
| 91 | for (int row = rowCount - 1; row >= 0; --row) { |
---|
| 92 | Component c = table.prepareRenderer( |
---|
| 93 | table.getCellRenderer(row, col), |
---|
| 94 | row, col); |
---|
| 95 | width = Math.max(width, c.getPreferredSize().width + 10); |
---|
| 96 | } |
---|
| 97 | } |
---|
| 98 | catch (Exception e) { |
---|
| 99 | e.printStackTrace(); |
---|
| 100 | } |
---|
| 101 | |
---|
| 102 | return width; |
---|
| 103 | } |
---|
| 104 | |
---|
| 105 | /** |
---|
| 106 | * calcs the optimal header width of the given column |
---|
| 107 | */ |
---|
| 108 | public int calcHeaderWidth(int col) { |
---|
| 109 | return calcHeaderWidth(getJTable(), col); |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | /** |
---|
| 113 | * Calculates the optimal width for the header of the given table. The |
---|
| 114 | * calculation is based on the preferred width of the header renderer. |
---|
| 115 | * |
---|
| 116 | * @param table the table to calculate the column width |
---|
| 117 | * @param col the column to calculate the widths |
---|
| 118 | * @return the width, -1 if error |
---|
| 119 | */ |
---|
| 120 | public static int calcHeaderWidth(JTable table, int col) { |
---|
| 121 | if (table == null) |
---|
| 122 | return -1; |
---|
| 123 | |
---|
| 124 | if (col < 0 || col > table.getColumnCount()) { |
---|
| 125 | System.out.println("invalid col " + col); |
---|
| 126 | return -1; |
---|
| 127 | } |
---|
| 128 | |
---|
| 129 | JTableHeader header = table.getTableHeader(); |
---|
| 130 | TableCellRenderer defaultHeaderRenderer = null; |
---|
| 131 | if (header != null) defaultHeaderRenderer = header.getDefaultRenderer(); |
---|
| 132 | TableColumnModel columns = table.getColumnModel(); |
---|
| 133 | TableModel data = table.getModel(); |
---|
| 134 | TableColumn column = columns.getColumn(col); |
---|
| 135 | int width = -1; |
---|
| 136 | TableCellRenderer h = column.getHeaderRenderer(); |
---|
| 137 | if (h == null) h = defaultHeaderRenderer; |
---|
| 138 | if (h != null) { |
---|
| 139 | // Not explicitly impossible |
---|
| 140 | Component c = h.getTableCellRendererComponent( |
---|
| 141 | table, |
---|
| 142 | column.getHeaderValue(), |
---|
| 143 | false, false, -1, col); |
---|
| 144 | width = c.getPreferredSize().width + 5; |
---|
| 145 | } |
---|
| 146 | |
---|
| 147 | return width; |
---|
| 148 | } |
---|
| 149 | |
---|
| 150 | /** |
---|
| 151 | * sets the optimal column width for the given column |
---|
| 152 | */ |
---|
| 153 | public void setOptimalColumnWidth(int col) { |
---|
| 154 | setOptimalColumnWidth(getJTable(), col); |
---|
| 155 | } |
---|
| 156 | |
---|
| 157 | /** |
---|
| 158 | * sets the optimal column width for the given column |
---|
| 159 | |
---|
| 160 | */ |
---|
| 161 | public static void setOptimalColumnWidth(JTable jtable, int col) { |
---|
| 162 | int width; |
---|
| 163 | TableColumn column; |
---|
| 164 | JTableHeader header; |
---|
| 165 | |
---|
| 166 | if ( (col >= 0) && (col < jtable.getColumnModel().getColumnCount()) ) { |
---|
| 167 | width = calcColumnWidth(jtable, col); |
---|
| 168 | |
---|
| 169 | if (width >= 0) { |
---|
| 170 | header = jtable.getTableHeader(); |
---|
| 171 | column = jtable.getColumnModel().getColumn(col); |
---|
| 172 | column.setPreferredWidth(width); |
---|
| 173 | jtable.sizeColumnsToFit(-1); |
---|
| 174 | header.repaint(); |
---|
| 175 | } |
---|
| 176 | } |
---|
| 177 | } |
---|
| 178 | |
---|
| 179 | /** |
---|
| 180 | * sets the optimal column width for all columns |
---|
| 181 | */ |
---|
| 182 | public void setOptimalColumnWidth() { |
---|
| 183 | setOptimalColumnWidth(getJTable()); |
---|
| 184 | } |
---|
| 185 | |
---|
| 186 | /** |
---|
| 187 | * sets the optimal column width for alls column if the given table |
---|
| 188 | */ |
---|
| 189 | public static void setOptimalColumnWidth(JTable jtable) { |
---|
| 190 | int i; |
---|
| 191 | |
---|
| 192 | for (i = 0; i < jtable.getColumnModel().getColumnCount(); i++) |
---|
| 193 | setOptimalColumnWidth(jtable, i); |
---|
| 194 | } |
---|
| 195 | |
---|
| 196 | /** |
---|
| 197 | * sets the optimal header width for the given column |
---|
| 198 | */ |
---|
| 199 | public void setOptimalHeaderWidth(int col) { |
---|
| 200 | setOptimalHeaderWidth(getJTable(), col); |
---|
| 201 | } |
---|
| 202 | |
---|
| 203 | /** |
---|
| 204 | * sets the optimal header width for the given column |
---|
| 205 | |
---|
| 206 | */ |
---|
| 207 | public static void setOptimalHeaderWidth(JTable jtable, int col) { |
---|
| 208 | int width; |
---|
| 209 | TableColumn column; |
---|
| 210 | JTableHeader header; |
---|
| 211 | |
---|
| 212 | if ( (col >= 0) && (col < jtable.getColumnModel().getColumnCount()) ) { |
---|
| 213 | width = calcHeaderWidth(jtable, col); |
---|
| 214 | |
---|
| 215 | if (width >= 0) { |
---|
| 216 | header = jtable.getTableHeader(); |
---|
| 217 | column = jtable.getColumnModel().getColumn(col); |
---|
| 218 | column.setPreferredWidth(width); |
---|
| 219 | jtable.sizeColumnsToFit(-1); |
---|
| 220 | header.repaint(); |
---|
| 221 | } |
---|
| 222 | } |
---|
| 223 | } |
---|
| 224 | |
---|
| 225 | /** |
---|
| 226 | * sets the optimal header width for all columns |
---|
| 227 | */ |
---|
| 228 | public void setOptimalHeaderWidth() { |
---|
| 229 | setOptimalHeaderWidth(getJTable()); |
---|
| 230 | } |
---|
| 231 | |
---|
| 232 | /** |
---|
| 233 | * sets the optimal header width for alls column if the given table |
---|
| 234 | */ |
---|
| 235 | public static void setOptimalHeaderWidth(JTable jtable) { |
---|
| 236 | int i; |
---|
| 237 | |
---|
| 238 | for (i = 0; i < jtable.getColumnModel().getColumnCount(); i++) |
---|
| 239 | setOptimalHeaderWidth(jtable, i); |
---|
| 240 | } |
---|
| 241 | |
---|
| 242 | /** |
---|
| 243 | * Assumes table is contained in a JScrollPane. |
---|
| 244 | * Scrolls the cell (rowIndex, vColIndex) so that it is visible |
---|
| 245 | * within the viewport. |
---|
| 246 | */ |
---|
| 247 | public void scrollToVisible(int row, int col) { |
---|
| 248 | scrollToVisible(getJTable(), row, col); |
---|
| 249 | } |
---|
| 250 | |
---|
| 251 | /** |
---|
| 252 | * Assumes table is contained in a JScrollPane. |
---|
| 253 | * Scrolls the cell (rowIndex, vColIndex) so that it is visible |
---|
| 254 | * within the viewport. |
---|
| 255 | */ |
---|
| 256 | public static void scrollToVisible(JTable table, int row, int col) { |
---|
| 257 | if (!(table.getParent() instanceof JViewport)) |
---|
| 258 | return; |
---|
| 259 | |
---|
| 260 | JViewport viewport = (JViewport) table.getParent(); |
---|
| 261 | |
---|
| 262 | // This rectangle is relative to the table where the |
---|
| 263 | // northwest corner of cell (0,0) is always (0,0). |
---|
| 264 | Rectangle rect = table.getCellRect(row, col, true); |
---|
| 265 | |
---|
| 266 | // The location of the viewport relative to the table |
---|
| 267 | Point pt = viewport.getViewPosition(); |
---|
| 268 | |
---|
| 269 | // Translate the cell location so that it is relative |
---|
| 270 | // to the view, assuming the northwest corner of the |
---|
| 271 | // view is (0,0) |
---|
| 272 | rect.setLocation(rect.x - pt.x, rect.y - pt.y); |
---|
| 273 | |
---|
| 274 | // Scroll the area into view |
---|
| 275 | viewport.scrollRectToVisible(rect); |
---|
| 276 | } |
---|
| 277 | } |
---|