Changeset 4911

Show
Ignore:
Timestamp:
06/18/08 07:46:45 (5 months ago)
Author:
marc.englund@…
Message:

Initial commit for #1739, implements Table.addGeneratedColumn()

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/src/com/itmill/toolkit/ui/Table.java

    r4752 r4911  
    181181 
    182182    /** 
     183     * Holds column generators 
     184     */ 
     185    private final HashMap columnGenerators = new HashMap(); 
     186 
     187    /** 
    183188     * Holds value of property pageLength. 0 disables paging. 
    184189     */ 
     
    320325 
    321326    /** 
    322      * Gets the array of visible column property id:s. 
     327     * Gets the array of visible column id:s, including generated columns. 
    323328     *  
    324329     * <p> 
     
    326331     * </p> 
    327332     *  
    328      * @return the Value of property availableColumns. 
     333     * @return an array of currently visible propertyIds and generated column 
     334     *         ids. 
    329335     */ 
    330336    public Object[] getVisibleColumns() { 
     
    358364        for (int i = 0; i < visibleColumns.length; i++) { 
    359365            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])) { 
    362369                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: " 
    364371                                + visibleColumns[i]); 
    365372            } 
    366373        } 
    367374 
    368         // If this is called befor the constructor is finished, it might be 
     375        // If this is called before the constructor is finished, it might be 
    369376        // uninitialized 
    370377        final LinkedList newVC = new LinkedList(); 
     
    375382        // Removes alignments, icons and headers from hidden columns 
    376383        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); 
    384397            } 
    385398        } 
     
    12001213            final boolean[] iscomponent = new boolean[cols]; 
    12011214            for (int i = 0; i < cols; i++) { 
    1202                 iscomponent[i] = Component.class 
    1203                         .isAssignableFrom(getType(colids[i])); 
     1215                iscomponent[i] = columnGenerators.containsKey(colids[i]) 
     1216                        || Component.class.isAssignableFrom(getType(colids[i])); 
    12041217            } 
    12051218            int firstIndexNotInCache; 
     
    12301243                if (cols > 0) { 
    12311244                    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 
    12341254                        // check in current pageBuffer already has row 
    12351255                        int index = firstIndex + i; 
    1236                         if (p != null) { 
     1256                        if (p != null || isGenerated) { 
    12371257                            if (p instanceof Property.ValueChangeNotifier) { 
    12381258                                if (oldListenedProperties == null 
     
    12521272                                value = pageBuffer[CELL_FIRSTCOL + j][indexInOldBuffer]; 
    12531273                            } 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]) { 
    12561281                                    value = p.getValue(); 
    12571282                                } else if (p != null) { 
     
    12621287                                } 
    12631288                            } 
    1264                         } else { 
    1265                             value = ""; 
    12661289                        } 
    12671290 
     
    14711494            collapsedColumns.clear(); 
    14721495        } 
    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()); 
    14741511 
    14751512        // null value as we may not be sure that currently selected identifier 
     
    14781515 
    14791516        // Assure visual refresh 
     1517        resetPageBuffer(); 
    14801518        refreshRenderedCells(); 
    14811519    } 
     
    17401778                && iscomponentIndex < iscomponent.length;) { 
    17411779            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            } 
    17451787        } 
    17461788        target.startTag("rows"); 
     
    22362278 
    22372279    /** 
     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    /** 
    22382340     * Returns the list of items on the current page 
    22392341     *  
     
    26532755    } 
    26542756 
     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    } 
    26552782}