1 19 20 package org.netbeans.modules.tasklist.core.columns; 21 22 import java.util.ArrayList ; 23 import javax.swing.event.ChangeEvent ; 24 import javax.swing.event.ChangeListener ; 25 import javax.swing.table.TableColumnModel ; 26 import javax.swing.table.TableColumn ; 27 28 import org.netbeans.modules.tasklist.core.ColumnProperty; 29 import org.netbeans.modules.tasklist.core.TaskListView; 30 31 34 public class ColumnsConfiguration { 35 private ArrayList listeners = new ArrayList (); 36 37 38 private int widths[]; 39 40 private String [] properties; 41 42 43 private String sortingColumn; 44 45 private boolean ascending; 46 47 55 public ColumnsConfiguration(String [] properties, int[] widths, String sort, 56 boolean ascending) { 57 this.properties = properties; 58 this.widths = widths; 59 this.sortingColumn = sort; 60 this.ascending = ascending; 61 } 62 63 71 public void setValues(String [] properties, int[] widths, String sort, 72 boolean ascending) { 73 this.properties = properties; 74 this.widths = widths; 75 this.sortingColumn = sort; 76 this.ascending = ascending; 77 fireChange(); 78 } 79 80 85 public int[] getWidths() { 86 return widths; 87 } 88 89 94 public String [] getProperties() { 95 return properties; 96 } 97 98 103 public String getSortingColumn() { 104 return sortingColumn; 105 } 106 107 112 public boolean getSortingOrder() { 113 return ascending; 114 } 115 116 121 public void setSortingOrder(boolean ascending) { 122 this.ascending = ascending; 123 } 124 125 130 public void addChangeListener(ChangeListener l) { 131 listeners.add(l); 132 } 133 134 139 public void removeChangeListener(ChangeListener l) { 140 listeners.remove(l); 141 } 142 143 146 protected final void fireChange() { 147 ChangeEvent e = null; 148 for (int i = 0; i < listeners.size(); i++) { 149 ChangeListener l = (ChangeListener ) listeners.get(i); 150 if (e == null) 151 e = new ChangeEvent (this); 152 l.stateChanged(e); 153 } 154 } 155 156 162 public static void loadColumnsFrom(TaskListView v, ColumnsConfiguration cc) { 163 166 ColumnProperty columns[] = v.getColumns(); 168 ColumnProperty treeColumn = null; 169 for (int i = 0; i < columns.length; i++) { 170 Object b = columns[i].getValue("TreeColumnTTV"); if (b instanceof Boolean && ((Boolean ) b).booleanValue()) { 172 treeColumn = columns[i]; 173 break; 174 } 175 } 176 assert treeColumn != null; 177 178 ColumnProperty sortedColumn = null; 180 boolean ascending = false; 181 for (int i = 0; i < columns.length; i++) { 182 Boolean sorting = (Boolean ) columns[i].getValue( 183 "SortingColumnTTV"); if ((sorting != null) && (sorting.booleanValue())) { 185 sortedColumn = columns[i]; 186 Boolean desc = (Boolean ) columns[i].getValue( 187 "DescendingOrderTTV"); ascending = (desc != Boolean.TRUE); 189 } 190 } 191 192 TableColumnModel m = v.getTable().getColumnModel(); 194 int[] widths = new int[m.getColumnCount()]; 195 String [] properties = new String [m.getColumnCount()]; 196 for (int i = 0; i < m.getColumnCount(); i++) { 197 TableColumn tc = m.getColumn(i); 198 ColumnProperty cp = null; 199 for (int j = 0; j < columns.length; j++) { 200 if (columns[j].getDisplayName().equals(tc.getHeaderValue())) { 201 cp = columns[j]; 202 } 203 } 204 if (cp != null) { 205 properties[i] = cp.getName(); 206 } else { 207 properties[i] = treeColumn.getName(); 209 } 210 widths[i] = tc.getWidth(); 211 } 212 213 cc.setValues(properties, widths, 214 sortedColumn == null ? null : sortedColumn.getName(), ascending); 215 } 216 217 223 public static void configureColumns(TaskListView v, ColumnsConfiguration cc) { 224 String [] properties = cc.getProperties(); 225 int[] widths = cc.getWidths(); 226 String sortingColumn = cc.getSortingColumn(); 227 boolean ascending = cc.getSortingOrder(); 228 229 ColumnProperty columns[] = v.getColumns(); 230 231 for (int i = 0; i < columns.length; i++) { 232 columns[i].setValue("InvisibleInTreeTableView", Boolean.TRUE); 235 } 236 237 for (int i = 0; i < properties.length; i++) { 238 ColumnProperty c = findColumn(columns, properties[i]); 239 if (c != null) { 240 c.setValue("InvisibleInTreeTableView", Boolean.FALSE); 246 c.width = widths[i]; 247 } 249 } 250 251 if (sortingColumn != null) { 253 ColumnProperty c = findColumn(columns, sortingColumn); 254 if (c != null) { 255 c.setValue("SortingColumnTTV", Boolean.TRUE); c.setValue("DescendingOrderTTV", (!ascending) ? Boolean.TRUE : Boolean.FALSE); 259 } 260 } 261 } 262 263 270 private static ColumnProperty findColumn(ColumnProperty columns[], String name) { 271 for (int i = 0; i < columns.length; i++) { 272 if (columns[i].getName().equals(name)) 273 return columns[i]; 274 } 275 276 return null; 277 } 278 } | Popular Tags |