Image subclass not getting its model object
Hi, I'm trying to make an image subclass that shows an icon. Which icon it shows depends on its model object, which is a boolean. Problem is, I'm not getting the model object: it's always null. If I use a Label instead of InStockIconImage, it works: the label shows a boolean (true/false). Any idea what could cause this? Thanks, Erwin Here's the icon: public class InStockIconImage extends Image { private static final String PATH_IN_STOCK = in_stock.png; private static final String PATH_NOT_IN_STOCK = not_in_stock.png; private static final String PATH_NO_MODEL_OBJECT = empty.png; public InStockIconImage(String id, IModelBoolean model) { super(id, model); } @Override protected ResourceReference getImageResourceReference() { Boolean inStock = (Boolean) getDefaultModelObject(); String path; if (inStock == null) { path = PATH_NO_MODEL_OBJECT; } else if (inStock) { path = PATH_IN_STOCK; } else { path = PATH_NOT_IN_STOCK; } return new ResourceReference(Icons.class, path); } } Here's how I use it: RefreshingListViewProductListing listingRepeater = new RefreshingListViewProductListing( listing, new PropertyModelSetProductListing(getModel(), listings)) { @Override protected void populateItem(ItemProductListing item) { item.add(new Label(webShop.name)); item.add(new Label(price)); item.add(new InStockIconImage(inStock)); } }; add(listingRepeater); RefreshingListView is a utility class which decorates a normal RefreshingView: public abstract class RefreshingListViewT extends RefreshingViewT { public RefreshingListView(String id) { super(id); } public RefreshingListView(String id, IModel? extends CollectionT model) { super(id, model); } @Override protected IteratorIModelT getItemModels() { CollectionT modelObject = getModelObject(); if (modelObject == null) { modelObject = new ArrayListT(); } return new ModelIteratorAdapterT(modelObject.iterator()) { @Override protected IModelT model(T object) { return new CompoundPropertyModelT(object); } }; } public CollectionT getModelObject() { return getModel().getObject(); } @SuppressWarnings(unchecked) public IModelListT getModel() { return (IModelListT) getDefaultModel(); } } - To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org For additional commands, e-mail: users-h...@wicket.apache.org
Re: Image subclass not getting its model object
Oops, I went a bit too far in pruning non-essential code. InStockIconImage has a second constructor: public InStockIconImage(String id) { super(id); } I think it centers around this. If I pass a PropertyModel(listingModel, inStock) explicitly to the constructor, it works, but not if I depend on the automatic property resolution that works well if I use a Label. How can I get it to work if I don't want to pass a PropertyModel explicitly? Erwin On 6/4/10 9:15 AM, Erwin Bolwidt wrote: Hi, I'm trying to make an image subclass that shows an icon. Which icon it shows depends on its model object, which is a boolean. Problem is, I'm not getting the model object: it's always null. If I use a Label instead of InStockIconImage, it works: the label shows a boolean (true/false). Any idea what could cause this? Thanks, Erwin Here's the icon: public class InStockIconImage extends Image { private static final String PATH_IN_STOCK = in_stock.png; private static final String PATH_NOT_IN_STOCK = not_in_stock.png; private static final String PATH_NO_MODEL_OBJECT = empty.png; public InStockIconImage(String id, IModelBoolean model) { super(id, model); } @Override protected ResourceReference getImageResourceReference() { Boolean inStock = (Boolean) getDefaultModelObject(); String path; if (inStock == null) { path = PATH_NO_MODEL_OBJECT; } else if (inStock) { path = PATH_IN_STOCK; } else { path = PATH_NOT_IN_STOCK; } return new ResourceReference(Icons.class, path); } } Here's how I use it: RefreshingListViewProductListing listingRepeater = new RefreshingListViewProductListing( listing, new PropertyModelSetProductListing(getModel(), listings)) { @Override protected void populateItem(ItemProductListing item) { item.add(new Label(webShop.name)); item.add(new Label(price)); item.add(new InStockIconImage(inStock)); } }; add(listingRepeater); RefreshingListView is a utility class which decorates a normal RefreshingView: public abstract class RefreshingListViewT extends RefreshingViewT { public RefreshingListView(String id) { super(id); } public RefreshingListView(String id, IModel? extends CollectionT model) { super(id, model); } @Override protected IteratorIModelT getItemModels() { CollectionT modelObject = getModelObject(); if (modelObject == null) { modelObject = new ArrayListT(); } return new ModelIteratorAdapterT(modelObject.iterator()) { @Override protected IModelT model(T object) { return new CompoundPropertyModelT(object); } }; } public CollectionT getModelObject() { return getModel().getObject(); } @SuppressWarnings(unchecked) public IModelListT getModel() { return (IModelListT) getDefaultModel(); } } - To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org For additional commands, e-mail: users-h...@wicket.apache.org - To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org For additional commands, e-mail: users-h...@wicket.apache.org
Re: Image subclass not getting its model object
Hi Ernesto, Just got the 1.4.9 source code and I see what you mean. But why is it like this? I copied the whole Image source code to a new class, removed this initModel method, and then things work fine if I don't supply a model. Why would the Image component behave different from the other components? This isn't documented in the Javadoc so I didn't expect this, but even then, what is the need for making Image work differently than others like Label? I can think of some usecases where it is fine to display in image that is derived from some property on a domain object that is accessed with a CompoundPropertyModel: - when you have a Boolean (like here) or Enum-type property and you want to change the image depending on its value - when you have want to dynamically generate an image with some text (on top of a template image), the image is much like a label then I can't disable this initModel behavior in a subclass, since you can't call super.super.initModel, so to disable this behavior, I have to copy the whole source of the Image class and remove this method. That seems like waste and you're not benefiting from future improvements in the Image class then. Cheers, Erwin Bolwidt On 6/4/10 9:33 AM, Ernesto Reinaldo Barreiro wrote: Maybe this is related to this override on Image class? @Override protected IModel? initModel() { // Images don't support Compound models. They either have a simple // model, explicitly set, or they use their tag's src or value // attribute to determine the image. return null; } Ernesto On Fri, Jun 4, 2010 at 9:30 AM, Erwin Bolwidtebolw...@worldturner.nl wrote: Oops, I went a bit too far in pruning non-essential code. InStockIconImage has a second constructor: public InStockIconImage(String id) { super(id); } I think it centers around this. If I pass a PropertyModel(listingModel, inStock) explicitly to the constructor, it works, but not if I depend on the automatic property resolution that works well if I use a Label. How can I get it to work if I don't want to pass a PropertyModel explicitly? Erwin On 6/4/10 9:15 AM, Erwin Bolwidt wrote: Hi, I'm trying to make an image subclass that shows an icon. Which icon it shows depends on its model object, which is a boolean. Problem is, I'm not getting the model object: it's always null. If I use a Label instead of InStockIconImage, it works: the label shows a boolean (true/false). Any idea what could cause this? Thanks, Erwin Here's the icon: public class InStockIconImage extends Image { private static final String PATH_IN_STOCK = in_stock.png; private static final String PATH_NOT_IN_STOCK = not_in_stock.png; private static final String PATH_NO_MODEL_OBJECT = empty.png; public InStockIconImage(String id, IModelBoolean model) { super(id, model); } @Override protected ResourceReference getImageResourceReference() { Boolean inStock = (Boolean) getDefaultModelObject(); String path; if (inStock == null) { path = PATH_NO_MODEL_OBJECT; } else if (inStock) { path = PATH_IN_STOCK; } else { path = PATH_NOT_IN_STOCK; } return new ResourceReference(Icons.class, path); } } Here's how I use it: RefreshingListViewProductListing listingRepeater = new RefreshingListViewProductListing( listing, new PropertyModelSetProductListing(getModel(), listings)) { @Override protected void populateItem(ItemProductListing item) { item.add(new Label(webShop.name)); item.add(new Label(price)); item.add(new InStockIconImage(inStock)); } }; add(listingRepeater); RefreshingListView is a utility class which decorates a normal RefreshingView: public abstract class RefreshingListViewT extends RefreshingViewT { public RefreshingListView(String id) { super(id); } public RefreshingListView(String id, IModel? extends CollectionT model) { super(id, model); } @Override protected IteratorIModelT getItemModels() { CollectionT modelObject = getModelObject(); if (modelObject == null) { modelObject = new ArrayListT(); } return new ModelIteratorAdapterT(modelObject.iterator()) { @Override protected IModelT model(T object) { return new CompoundPropertyModelT(object); } }; } public CollectionT getModelObject() { return getModel().getObject(); } @SuppressWarnings(unchecked) public IModelListT getModel() { return (IModelListT) getDefaultModel(); } } - To unsubscribe, e-mail: users-unsubscr
How to make sure that you always have a CompoundPropertyModel
Hi, I try to make my components re-usable whenever this is feasible. A problem that I'm running into is this: A Component itself decides that it needs a CompoundPropertyModel, because it wants to use the automatic property model lookups feature when a model is omitted on a child component. public class MyPanel extends Panel { public MyPanel(String id, IModel model) { super(id, new CompoundPropertyModel(model)); add(new Label(person.name)); add(new TextField(person.address.street)); // etc. } } But, the method setDefaultModel (inherited from Component) on MyPanel can be used by outside code to change the model. And that should be ok. However, when an outside class does that, it needs to know that it should set a CompoundPropertyModel, or else MyPanel will break. I don't really like that, because the fact that I need a CompoundPropertyModel is an implementation detail that I want to have encapsulated in my Component, and not something that the rest of my code should know about. I could override setDefaultModel and wrap any argument inside a CompountPropertyModel, but I would still have to do the same in the constructor, since a model that was passed in the constructor is not set using the method setDefaultModel, but using setModelImpl. Is there a clean and simple solution to this? I'm beginning to think that the feature provided by CompoundPropertyModel (and perhaps even any IComponentInheritedModel) is perhaps not something that should conceptually be provided by a model, but rather by a new concept; something like an IModelAccessor or IModelModifier (I hope you catch my meaning). The way that a model is used inside a Component is, I think, an implementation detail, private to a component itself. The model itself is something that should be set by the outside world on the Component. So even though they can, technically, be integrated in one concept (as it is now), it may be better from the perspective of encapsulation and reusability to separate these two concepts. I'm interested to know what the wicket designers think about this. Kind regards, Erwin Bolwidt - To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org For additional commands, e-mail: users-h...@wicket.apache.org
Re: How make continous row index in dataTable
Hi, You can do it like this: private private DefaultDataTableT dataTable; // ... new AbstractColumnWeekSchema(new ModelString(#)) { public void populateItem(ItemICellPopulatorWeekSchema cellItem, String componentId, IModelWeekSchema rowModel) { Item? item = (Item?) cellItem.getParent().getParent(); int index = dataTable.getCurrentPage() * dataTable.getRowsPerPage() + item.getIndex() + 1; cellItem.add(new Label(componentId, String.valueOf(index))); } } Regards, Erwin Bolwidt On 5/11/10 9:56 AM, cleverpig wrote: hi,everybody! I used this way to build dataTable: DataProvider+Column defines+DataTable. I want to add a index column at the first column,and hope that keeping continuous index number. So whenever turn to another page it will hold continuously,just not renew from 1. But now,I can get a sample code to implement index number in each page,not ontinuous index number yet. IColumn[] columns={ new AbstractColumn(new ResourceModel(column.index)){ private static final long serialVersionUID = 0L; @Override public void populateItem(Item cellItem, String componentId, IModel rowModel) { Item item=(Item)cellItem.getParent().getParent(); cellItem.add(new Label(componentId,new Model(item.getIndex()+1))); } }, ... }; DataTable dataTable=new DataTable(entries,columns,dataProvider,5); ... How to make continous row index in dataTable? Hoping that the wise friend to help me. - To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org For additional commands, e-mail: users-h...@wicket.apache.org
New blog post Refactoring wicket Pages to Components
Hi, I created a new blog posted titled Refactoring wicket Pages to Components http://blog.worldturner.com/worldturner/entry/refactoring_wicket_pages_to_components It describes some issues that we ran into doing this refactoring, and is a logical sequal to my previous post on Components versus Pages. I also introduce two new useful components: GenericPanel and ComponentLink. This is the second post on the topic of Components versus Pages and the last one for now; next article will be about models. Regards, Erwin Bolwidt - To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org For additional commands, e-mail: users-h...@wicket.apache.org
New blog post Components v.s. Pages
Hi, I created a blog posted titled Wicket best practices: Components v.s. Pages http://blog.worldturner.com/worldturner/entry/wicket_best_practices_components_v We've been using wicket at my current location for quite a few projects now and I'm writing a few blog entries on the things that we've found most convenient. Regards, Erwin Bolwidt