Re: DataTable's Handling of NULL Values
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 DefaultModelT extends AbstractWrapModel { private T default; private IModelT wrappedModel; public DefaultModel(IModelT 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? PropertyColumnAsset propertyColumn = new PropertyColumnAsset( new ModelString(AuditTrain), audit_train) { private static final long serialVersionUID = 1L; @Override public void populateItem(ItemICellPopulatorAsset item, String componentId, IModelAsset 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 ModelString(noaudit))); } } }; Best, Ernesto On Sat, Jan 2, 2010 at 4:18 AM, Norman Elton normel...@gmail.com 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 PropertyColumnAsset(new ModelString(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: users-unsubscr...@wicket.apache.org For additional commands, e-mail: users-h...@wicket.apache.org -- Jason Lea
Re: DataTable's Handling of NULL Values
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 ja...@kumachan.net.nz 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 DefaultModelT extends AbstractWrapModel { private T default; private IModelT wrappedModel; public DefaultModel(IModelT 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? PropertyColumnAsset propertyColumn = new PropertyColumnAsset( new ModelString(AuditTrain), audit_train) { private static final long serialVersionUID = 1L; @Override public void populateItem(ItemICellPopulatorAsset item, String componentId, IModelAsset 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 ModelString(noaudit))); } } }; Best, Ernesto On Sat, Jan 2, 2010 at 4:18 AM, Norman Elton normel...@gmail.com 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 PropertyColumnAsset(new ModelString(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: users-unsubscr...@wicket.apache.org For additional commands, e-mail: users-h...@wicket.apache.org -- Jason Lea
Re: DataTable's Handling of NULL Values
Thanks for both responses. I'm going to try them both. Two clarification questions: - In Ernesto's idea (override populateItem()), would the model still be dynamic? That is, if the table is used in a form and the data updated, would the table reflect the new change? My terminology may be a little off here. - In Jason's idea (create a wrapped model), the notion of default is actually an audit train object, not just a string? This wouldn't be the end of the world, since AuditTrain is just an interface and I could provide an instance of an anonymous inner class. It would have to return undefined for it's name property, etc. Thanks again, I will tinker with them both today. Norman - To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org For additional commands, e-mail: users-h...@wicket.apache.org
Re: DataTable's Handling of NULL Values
Ah I see. In the end you are going to display a string n the cell, but if the string is null then it should display the default value. I guess the problem is because you are using the PropertyColumn which is a convienience implementation, and the property you are using doesn't display the right values. I think you need to change to the AbstractColumn and then you can use the DefaultModel when you override populateItem(). void populateItem(final Item cellItem, final String componentId, final IModel rowModel) { IModelString auditModel = new DefaultModelString(new PropertyModelString(rowModel, audit_train), undefined); cellItem.add(new Label(componentId, auditModel); } The PropertyModel does the same thing that the PropertyColumn did in getting the audit_train out of the rowModel, and if it is null the DefaultModel will display the word 'undefined'. I just noticed getCssClass() doesn't get a rowModel so it sets the css class for every cell in the column but has no way to know which row the css class is for. So you will need to use the AttributeModifier to add the css class as Ernesto suggested. Norman Elton wrote: Thanks for both responses. I'm going to try them both. Two clarification questions: - In Ernesto's idea (override populateItem()), would the model still be dynamic? That is, if the table is used in a form and the data updated, would the table reflect the new change? My terminology may be a little off here. - In Jason's idea (create a wrapped model), the notion of default is actually an audit train object, not just a string? This wouldn't be the end of the world, since AuditTrain is just an interface and I could provide an instance of an anonymous inner class. It would have to return undefined for it's name property, etc. Thanks again, I will tinker with them both today. Norman - To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org For additional commands, e-mail: users-h...@wicket.apache.org -- Jason Lea - To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org For additional commands, e-mail: users-h...@wicket.apache.org