I just hastily composed the code I posted and I missed the getCssClass() ...
besides I do not normally use DataTable on my applications. The point was to
make clear that you can override "methods"... Something that many users
somehow miss when using wicket.

I also use nested models when appropriate. As you wrote they are a good way
abstract common logic that otherwise you will end up repeating over and
over...

Ernesto

On Sat, Jan 2, 2010 at 9:10 AM, Jason Lea <[email protected]> wrote:

> PropetyColumn implements IStyledColumn so you can override getCssClass()
> and return a class if it is null instead of adding the attributemodifier.
>
> And you could create your own model to return a default string when the
> underlying model is null...
>
> something like this...
>
> public class DefaultModel<T> extends AbstractWrapModel {
>     private T default;
>     private IModel<T> wrappedModel;
>
>     public DefaultModel(IModel<T> wrappedModel, T default) {
>         this.default = default;
>         this.wrappedModel = wrappedModel;
>     }
>
>     T getObject() {
>           T object = wrappedModel.getObject();
>           if (object == null) {
>               object = default;
>           }
>         return object;
>     }
>
>   T getWrappedModel() {
>      return wrappedModel;
>   }
> }
>
> The above probably doesn't compile... but the idea is you can nest models
> to hide all the tricky logic away in one place.
> This is the first time I have seen AbstractWrapModel, so I hope the above
> is the correct usage.
>
>
>
>
> Ernesto Reinaldo Barreiro wrote:
>
>> Maybe something like this?
>>
>> PropertyColumn<Asset> propertyColumn = new PropertyColumn<Asset>(
>> new Model<String>("AuditTrain"), "audit_train") {
>>  private static final long serialVersionUID = 1L;
>>
>> @Override
>> public void populateItem(Item<ICellPopulator<Asset>> item,
>> String componentId, IModel<Asset> rowModel) {
>> if(rowModel.getObject().getAudit_train() != null)
>> super.populateItem(item, componentId, rowModel);
>> else {
>> item.add(new Label(componentId, "No audit train"));
>>        item.add(new AttributeModifier("class",true, new
>> Model<String>("noaudit")));
>>                         }
>>  }
>> };
>>
>> Best,
>>
>> Ernesto
>>
>> On Sat, Jan 2, 2010 at 4:18 AM, Norman Elton <[email protected]> wrote:
>>
>>
>>
>>> I've created a DataTable, which works great. I'd like to define the
>>> behavior if a property returns NULL. For instance, this column
>>> retrieves the AuditTrain object for a given Asset:
>>>
>>> columns.add(new PropertyColumn<Asset>(new Model<String>("Audit
>>> Train"), "audit_train");
>>>
>>> If the AuditTrain is NULL, I get a null-pointer exception. Ideally,
>>> I'd like to return a column-specific string and css class. Seems that
>>> I need to define a new IColumn.
>>>
>>> Any pointers? Has this been done before?
>>>
>>> Thanks,
>>>
>>> Norman
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: [email protected]
>>> For additional commands, e-mail: [email protected]
>>>
>>>
>>>
>>>
>>
>>
>>
>
> --
> Jason Lea
>
>
>

Reply via email to