1 19 20 package org.netbeans.modules.tasklist.core.table; 21 22 import java.io.Serializable ; 23 import java.util.ArrayList ; 24 import javax.swing.JTable ; 25 import javax.swing.table.TableColumn ; 26 import javax.swing.table.TableColumnModel ; 27 28 33 public class ColumnsConfig implements Serializable { 34 public static final long serialVersionUID = 2L; 35 36 42 public static ColumnsConfig getColumnsConfig(JTable table) { 43 ColumnsConfig cc = new ColumnsConfig(); 44 45 TableColumnModel ctm = table.getColumnModel(); 46 assert ctm != null : "ctm == null"; 48 cc.columns = new int[ctm.getColumnCount()]; 49 cc.columnWidths = new int[ctm.getColumnCount()]; 50 for (int i = 0; i < ctm.getColumnCount(); i++) { 51 TableColumn c = ctm.getColumn(i); 52 cc.columns[i] = c.getModelIndex(); 53 cc.columnWidths[i] = c.getWidth(); 54 } 55 56 if (table instanceof SortableTable) { 57 cc.sortedColumn = 58 ((SortableTable) table).getSortingModel().getSortedColumn(); 59 cc.ascending = !((SortableTable) table).getSortingModel(). 60 isSortOrderDescending(); 61 } 62 63 return cc; 64 } 65 66 72 public static void setColumnsConfig(JTable table, ColumnsConfig config) { 73 76 assert config != null : "config == null"; 78 table.createDefaultColumnsFromModel(); 79 80 ColumnsConfig cc = (ColumnsConfig) config; 81 82 ArrayList <TableColumn > newc = new ArrayList <TableColumn >(); 83 TableColumnModel tcm = table.getColumnModel(); 84 assert tcm != null : "tcm == null"; 86 for (int i = 0; i < cc.columns.length; i++) { 87 for (int j = 0; j < tcm.getColumnCount(); j++) { 88 TableColumn c = tcm.getColumn(j); 89 if (cc.columns[i] == c.getModelIndex()) { 90 newc.add(c); 91 tcm.removeColumn(c); 92 c.setPreferredWidth(cc.columnWidths[i]); 93 c.setWidth(cc.columnWidths[i]); 94 break; 95 } 96 } 97 } 98 while (tcm.getColumnCount() > 0) { 99 tcm.removeColumn(tcm.getColumn(0)); 100 } 101 for (int i = 0; i < newc.size(); i ++) { 102 tcm.addColumn(newc.get(i)); 103 } 104 } 105 106 109 public int[] columns; 110 111 114 public int[] columnWidths; 115 116 119 public int sortedColumn = -1; 120 121 124 public boolean ascending; 125 126 public String toString() { 127 StringBuffer sb = new StringBuffer (); 128 sb.append("ColumnsConfig["); sb.append("sortedColumn=").append(sortedColumn); sb.append(", ascending=").append(sortedColumn); sb.append(", columns=["); for (int i = 0; i < columns.length; i++) { 133 if (i != 0) 134 sb.append(", "); sb.append(columns[i]); 136 sb.append("->"); sb.append(columnWidths[i]); 138 } 139 sb.append("]"); sb.append("]"); return sb.toString(); 142 } 143 } 144 145 | Popular Tags |