Changeset 4911
- Timestamp:
- 06/18/08 07:46:45 (5 months ago)
- Files:
-
- 1 modified
-
trunk/src/com/itmill/toolkit/ui/Table.java (modified) (14 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/com/itmill/toolkit/ui/Table.java
r4752 r4911 181 181 182 182 /** 183 * Holds column generators 184 */ 185 private final HashMap columnGenerators = new HashMap(); 186 187 /** 183 188 * Holds value of property pageLength. 0 disables paging. 184 189 */ … … 320 325 321 326 /** 322 * Gets the array of visible column property id:s.327 * Gets the array of visible column id:s, including generated columns. 323 328 * 324 329 * <p> … … 326 331 * </p> 327 332 * 328 * @return the Value of property availableColumns. 333 * @return an array of currently visible propertyIds and generated column 334 * ids. 329 335 */ 330 336 public Object[] getVisibleColumns() { … … 358 364 for (int i = 0; i < visibleColumns.length; i++) { 359 365 if (visibleColumns[i] == null) { 360 throw new NullPointerException("Properties must be non-nulls"); 361 } else if (!properties.contains(visibleColumns[i])) { 366 throw new NullPointerException("Ids must be non-nulls"); 367 } else if (!properties.contains(visibleColumns[i]) 368 && !columnGenerators.containsKey(visibleColumns[i])) { 362 369 throw new IllegalArgumentException( 363 " Properties must exist in the Container, missing property: "370 "Ids must exist in the Container or as a generated column , missing id: " 364 371 + visibleColumns[i]); 365 372 } 366 373 } 367 374 368 // If this is called befor the constructor is finished, it might be375 // If this is called before the constructor is finished, it might be 369 376 // uninitialized 370 377 final LinkedList newVC = new LinkedList(); … … 375 382 // Removes alignments, icons and headers from hidden columns 376 383 if (this.visibleColumns != null) { 377 for (final Iterator i = this.visibleColumns.iterator(); i.hasNext();) { 378 final Object col = i.next(); 379 if (!newVC.contains(col)) { 380 setColumnHeader(col, null); 381 setColumnAlignment(col, null); 382 setColumnIcon(col, null); 383 } 384 disableContentRefreshing(); 385 try { 386 for (final Iterator i = this.visibleColumns.iterator(); i 387 .hasNext();) { 388 final Object col = i.next(); 389 if (!newVC.contains(col)) { 390 setColumnHeader(col, null); 391 setColumnAlignment(col, null); 392 setColumnIcon(col, null); 393 } 394 } 395 } finally { 396 enableContentRefreshing(false); 384 397 } 385 398 } … … 1200 1213 final boolean[] iscomponent = new boolean[cols]; 1201 1214 for (int i = 0; i < cols; i++) { 1202 iscomponent[i] = Component.class1203 .isAssignableFrom(getType(colids[i]));1215 iscomponent[i] = columnGenerators.containsKey(colids[i]) 1216 || Component.class.isAssignableFrom(getType(colids[i])); 1204 1217 } 1205 1218 int firstIndexNotInCache; … … 1230 1243 if (cols > 0) { 1231 1244 for (int j = 0; j < cols; j++) { 1232 final Property p = getContainerProperty(id, colids[j]); 1233 Object value = null; 1245 Property p = null; 1246 Object value = ""; 1247 boolean isGenerated = columnGenerators 1248 .containsKey(colids[j]); 1249 1250 if (!isGenerated) { 1251 p = getContainerProperty(id, colids[j]); 1252 } 1253 1234 1254 // check in current pageBuffer already has row 1235 1255 int index = firstIndex + i; 1236 if (p != null ) {1256 if (p != null || isGenerated) { 1237 1257 if (p instanceof Property.ValueChangeNotifier) { 1238 1258 if (oldListenedProperties == null … … 1252 1272 value = pageBuffer[CELL_FIRSTCOL + j][indexInOldBuffer]; 1253 1273 } else { 1254 1255 if (iscomponent[j]) { 1274 if (isGenerated) { 1275 ColumnGenerator cg = (ColumnGenerator) columnGenerators 1276 .get(colids[j]); 1277 value = cg 1278 .generateCell(this, id, colids[j]); 1279 1280 } else if (iscomponent[j]) { 1256 1281 value = p.getValue(); 1257 1282 } else if (p != null) { … … 1262 1287 } 1263 1288 } 1264 } else {1265 value = "";1266 1289 } 1267 1290 … … 1471 1494 collapsedColumns.clear(); 1472 1495 } 1473 setVisibleColumns(getContainerPropertyIds().toArray()); 1496 1497 // columnGenerators 'override' properties, don't add the same id twice 1498 Collection col = new LinkedList(); 1499 for (Iterator it = getContainerPropertyIds().iterator(); it.hasNext();) { 1500 Object id = it.next(); 1501 if (columnGenerators == null || !columnGenerators.containsKey(id)) { 1502 col.add(id); 1503 } 1504 } 1505 // generators added last 1506 if (columnGenerators != null && columnGenerators.size() > 0) { 1507 col.addAll(columnGenerators.keySet()); 1508 } 1509 1510 setVisibleColumns(col.toArray()); 1474 1511 1475 1512 // null value as we may not be sure that currently selected identifier … … 1478 1515 1479 1516 // Assure visual refresh 1517 resetPageBuffer(); 1480 1518 refreshRenderedCells(); 1481 1519 } … … 1740 1778 && iscomponentIndex < iscomponent.length;) { 1741 1779 final Object columnId = it.next(); 1742 final Class colType = getType(columnId); 1743 iscomponent[iscomponentIndex++] = colType != null 1744 && Component.class.isAssignableFrom(colType); 1780 if (columnGenerators.containsKey(columnId)) { 1781 iscomponent[iscomponentIndex++] = true; 1782 } else { 1783 final Class colType = getType(columnId); 1784 iscomponent[iscomponentIndex++] = colType != null 1785 && Component.class.isAssignableFrom(colType); 1786 } 1745 1787 } 1746 1788 target.startTag("rows"); … … 2236 2278 2237 2279 /** 2280 * Adds a generated column to the Table. 2281 * <p> 2282 * A generated column is a column that exists only in the Table, not as a 2283 * property in the underlying Container. It shows up just as a regular 2284 * column. 2285 * </p> 2286 * <p> 2287 * A generated column will override a property with the same id, so that the 2288 * generated column is shown instead of the column representing the 2289 * property. Note that getContainerProperty() will still get the real 2290 * property. 2291 * </p> 2292 * <p> 2293 * Also note that getVisibleColumns() will return the generated columns, 2294 * while getContainerPropertyIds() will not. 2295 * </p> 2296 * 2297 * @param id 2298 * the id of the column to be added 2299 * @param generatedColumn 2300 * the {@link ColumnGenerator} to use for this column 2301 */ 2302 public void addGeneratedColumn(Object id, ColumnGenerator generatedColumn) { 2303 if (generatedColumn == null) { 2304 throw new IllegalArgumentException( 2305 "Can not add null as a GeneratedColumn"); 2306 } 2307 if (columnGenerators.containsKey(id)) { 2308 throw new IllegalArgumentException( 2309 "Can not add the same GeneratedColumn twice, id:" + id); 2310 } else { 2311 columnGenerators.put(id, generatedColumn); 2312 visibleColumns.add(id); 2313 resetPageBuffer(); 2314 refreshRenderedCells(); 2315 } 2316 } 2317 2318 /** 2319 * Removes a generated column previously added with addGeneratedColumn. 2320 * 2321 * @param id 2322 * id of the generated column to remove 2323 * @return true if the column could be removed (existed in the Table) 2324 */ 2325 public boolean removeGeneratedColumn(Object id) { 2326 if (columnGenerators.containsKey(id)) { 2327 columnGenerators.remove(id); 2328 if (!items.containsId(id)) { 2329 visibleColumns.remove(id); 2330 } 2331 resetPageBuffer(); 2332 refreshRenderedCells(); 2333 return true; 2334 } else { 2335 return false; 2336 } 2337 } 2338 2339 /** 2238 2340 * Returns the list of items on the current page 2239 2341 * … … 2653 2755 } 2654 2756 2757 /** 2758 * Used to create "generated columns"; columns that exist only in the Table, 2759 * not in the underlying Container. Implement this interface and pass it to 2760 * Table.addGeneratedColumn along with an id for the column to be generated. 2761 * 2762 */ 2763 public interface ColumnGenerator { 2764 2765 /** 2766 * Called by Table when a cell in a generated column needs to be 2767 * generated. 2768 * 2769 * @param source 2770 * the source Table 2771 * @param itemId 2772 * the itemId (aka rowId) for the of the cell to be 2773 * generated 2774 * @param columnId 2775 * the id for the generated column (as specified in 2776 * addGeneratedColumn) 2777 * @return 2778 */ 2779 public abstract Component generateCell(Table source, Object itemId, 2780 Object columnId); 2781 } 2655 2782 }
