1 package net.sf.jftp.gui.base.dir; 2 3 import java.awt.BorderLayout ; 4 import java.awt.Component ; 5 import java.lang.reflect.Constructor ; 6 import java.lang.reflect.Method ; 7 import java.util.Vector ; 8 9 import javax.swing.DefaultListModel ; 10 import javax.swing.ImageIcon ; 11 import javax.swing.JComponent ; 12 import javax.swing.JLabel ; 13 import javax.swing.JList ; 14 import javax.swing.JTable ; 15 import javax.swing.table.JTableHeader ; 16 import javax.swing.table.TableCellRenderer ; 17 import javax.swing.table.TableColumn ; 18 import javax.swing.table.TableColumnModel ; 19 import javax.swing.table.TableModel ; 20 21 import net.sf.jftp.config.Settings; 22 23 24 public class TableUtils { 25 26 27 34 public static void calcColumnWidths(JTable table) 35 { 36 JTableHeader header = table.getTableHeader(); 37 38 TableCellRenderer defaultHeaderRenderer = null; 39 40 if (header != null) 41 defaultHeaderRenderer = header.getDefaultRenderer(); 42 43 TableColumnModel columns = table.getColumnModel(); 44 TableModel data = table.getModel(); 45 46 int margin = columns.getColumnMargin(); 48 int rowCount = data.getRowCount(); 49 50 int totalWidth = 0; 51 52 for (int i = columns.getColumnCount() - 1; i >= 0; --i) 53 { 54 TableColumn column = columns.getColumn(i); 55 56 int columnIndex = column.getModelIndex(); 57 58 int width = -1; 59 60 TableCellRenderer h = column.getHeaderRenderer(); 61 62 if (h == null) 63 h = defaultHeaderRenderer; 64 65 if (h != null) { 67 Component c = h.getTableCellRendererComponent 68 (table, column.getHeaderValue(), 69 false, false, -1, i); 70 71 width = c.getPreferredSize().width; 72 } 73 74 for (int row = rowCount - 1; row >= 0; --row) 75 { 76 TableCellRenderer r = table.getCellRenderer(row, i); 77 78 Component c = r.getTableCellRendererComponent 79 (table, 80 data.getValueAt(row, columnIndex), 81 false, false, row, i); 82 83 width = Math.max(width, c.getPreferredSize().width); 84 } 85 86 if (width >= 0) 87 column.setPreferredWidth(width + margin); else 89 ; 91 totalWidth += column.getPreferredWidth(); 92 } 93 94 96 97 104 105 107 } 110 111 public static void setFixedWidths(JTable table) { 112 JTableHeader header = table.getTableHeader(); 113 114 TableCellRenderer defaultHeaderRenderer = null; 115 116 if (header != null) 117 defaultHeaderRenderer = header.getDefaultRenderer(); 118 119 TableColumnModel columns = table.getColumnModel(); 120 TableModel data = table.getModel(); 121 122 int rowCount = data.getRowCount(); 123 124 for (int i = 0; i<columns.getColumnCount(); i++) 125 { 126 TableColumn column = columns.getColumn(i); 127 int columnIndex = column.getModelIndex(); 128 int width = -1; 129 130 if(i == 0) { 131 column.setPreferredWidth(20); 132 column.setMaxWidth(20); 133 } 134 else if(i == 1) { 135 column.setMinWidth(100); 136 column.setPreferredWidth(400); 137 } 138 else if(i == 2) { 139 column.setMinWidth(60); 140 column.setPreferredWidth(80); 141 } 143 else if(i == 3) { 144 column.setMinWidth(25); 145 column.setPreferredWidth(25); 146 } 148 } 149 } 150 151 159 public static void copyTableSelectionsToJList(JList list, JTable listTbl) { 160 161 list.setSelectedIndices(new int[0]); 162 163 int rows = listTbl.getRowCount(); 164 Vector sel = new Vector (); 165 166 for(int i=0; i<rows; i++) { 167 if(listTbl.getSelectionModel().isSelectedIndex(i)) sel.add(new Integer (i)); 168 } 169 170 int tmp[] = new int[sel.size()]; 171 for(int i=0; i<sel.size(); i++) tmp[i] = ((Integer )sel.get(i)).intValue(); 172 173 list.setSelectedIndices(tmp); 174 } 175 176 184 private static synchronized TableModel generateTableModel(JList l) { 185 186 TableModel dataModel = new MaterializedTableModel(l) { 187 188 public Class getColumnClass(int columnIndex) { 189 if(columnIndex == 0) return ImageIcon .class; 190 else if(columnIndex == 3) return JLabel .class; 191 else return String .class; 192 } 193 194 public int getColumnCount() { 195 return 4; 197 } 198 199 public int getRowCount() { 200 return list.getModel().getSize(); 201 } 202 203 public Object getValueAt(int row, int col) { 204 205 if(list.getModel().getSize() == 0) return ""+null; 206 207 DirEntry ret = (DirEntry) list.getModel().getElementAt(row); 208 209 if(col == 0) return ret.getImageIcon(); 210 else if(col == 1) return ret.toString(); 211 else if(col == 2) { 212 String tmp = ""+ret.getFileSize(); 213 String r = tmp.replaceAll(" >", ""); 214 return r; 215 } 216 else if(col == 3) { 217 return ret; 218 } 219 220 221 223 return ret; 225 } 226 }; 227 228 return dataModel; 229 } 230 231 232 240 public static void layoutTable(JList list, JTable listTbl) { 241 layoutTable(list, listTbl, null); 242 } 243 244 249 public static void layoutTable(JList list, JTable listTbl, Vector names) { 250 listTbl.setModel(generateTableModel(list)); 251 252 if(Settings.useFixedTableWidths) { 253 setFixedWidths(listTbl); 254 } 255 else { 256 calcColumnWidths(listTbl); 257 } 258 259 if(names != null) modifyTableHeader(listTbl.getTableHeader(), names); 260 261 tryToEnableRowSorting(listTbl); 263 264 } 266 267 275 public static void tryToEnableRowSorting(JTable listTbl) { 276 301 } 302 303 308 public static void modifyTableHeader(JTableHeader head, Vector columnNames) { 309 310 TableColumnModel m = head.getColumnModel(); 311 312 if(m.getColumnCount() != columnNames.size()) { 313 System.out.println("Column mismatch: "+m.getColumnCount()+"/"+columnNames.size()); 314 return; 315 } 316 317 for(int i=0; i<columnNames.size(); i++) { 318 TableColumn c = m.getColumn(i); 319 c.sizeWidthToFit(); 320 c.setHeaderValue(columnNames.get(i)); 321 } 322 } 323 324 328 public static JComponent makeTable(JTable table, JComponent cont) { 329 JTableHeader header = table.getTableHeader(); 330 331 cont.setLayout(new BorderLayout ()); 332 cont.add(header, BorderLayout.NORTH); 333 cont.add(table, BorderLayout.CENTER); 334 335 return cont; 336 } 337 338 } 339 | Popular Tags |