You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by Erwin Bolwidt <eb...@worldturner.nl> on 2010/06/04 09:15:02 UTC

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, IModel<Boolean> 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:

         RefreshingListView<ProductListing> listingRepeater = new 
RefreshingListView<ProductListing>(
                 "listing", new 
PropertyModel<Set<ProductListing>>(getModel(), "listings")) {
             @Override
             protected void populateItem(Item<ProductListing> 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 RefreshingListView<T> extends RefreshingView<T> {

     public RefreshingListView(String id) {
         super(id);
     }

     public RefreshingListView(String id, IModel<? extends 
Collection<T>> model) {
         super(id, model);
     }

     @Override
     protected Iterator<IModel<T>> getItemModels() {
         Collection<T> modelObject = getModelObject();
         if (modelObject == null) {
             modelObject = new ArrayList<T>();
         }
         return new ModelIteratorAdapter<T>(modelObject.iterator()) {
             @Override
             protected IModel<T> model(T object) {
                 return new CompoundPropertyModel<T>(object);
             }
         };
     }

     public Collection<T> getModelObject() {
         return getModel().getObject();
     }

     @SuppressWarnings("unchecked")
     public IModel<List<T>> getModel() {
         return (IModel<List<T>>) getDefaultModel();
     }

}


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Image subclass not getting its model object

Posted by James Carman <ja...@carmanconsulting.com>.
I would say you need to use your own image resource, not subclass Image itself.

On Fri, Jun 4, 2010 at 7:03 AM, Erwin Bolwidt <eb...@worldturner.nl> wrote:
> 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 Bolwidt<eb...@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, IModel<Boolean>  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:
>>>>
>>>>        RefreshingListView<ProductListing>  listingRepeater = new
>>>> RefreshingListView<ProductListing>(
>>>>                "listing", new
>>>> PropertyModel<Set<ProductListing>>(getModel(), "listings")) {
>>>>            @Override
>>>>            protected void populateItem(Item<ProductListing>  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 RefreshingListView<T>  extends RefreshingView<T>
>>>>  {
>>>>
>>>>    public RefreshingListView(String id) {
>>>>        super(id);
>>>>    }
>>>>
>>>>    public RefreshingListView(String id, IModel<? extends Collection<T>>
>>>> model) {
>>>>        super(id, model);
>>>>    }
>>>>
>>>>    @Override
>>>>    protected Iterator<IModel<T>>  getItemModels() {
>>>>        Collection<T>  modelObject = getModelObject();
>>>>        if (modelObject == null) {
>>>>            modelObject = new ArrayList<T>();
>>>>        }
>>>>        return new ModelIteratorAdapter<T>(modelObject.iterator()) {
>>>>            @Override
>>>>            protected IModel<T>  model(T object) {
>>>>                return new CompoundPropertyModel<T>(object);
>>>>            }
>>>>        };
>>>>    }
>>>>
>>>>    public Collection<T>  getModelObject() {
>>>>        return getModel().getObject();
>>>>    }
>>>>
>>>>    @SuppressWarnings("unchecked")
>>>>    public IModel<List<T>>  getModel() {
>>>>        return (IModel<List<T>>) getDefaultModel();
>>>>    }
>>>>
>>>> }
>>>>
>>>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Image subclass not getting its model object

Posted by Ernesto Reinaldo Barreiro <re...@gmail.com>.
Hi Erwin,

On Fri, Jun 4, 2010 at 1:03 PM, Erwin Bolwidt <eb...@worldturner.nl> wrote:
> Hi Ernesto,
>
> Just got the 1.4.9 source code and I see what you mean.

As a rule I always install the source code of any OpenSouce software I
use: Javadoc could be wrong or outdated but source code will never
mislead you;-)

> 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.
>

Sorry Erwin but i don't know why this decision was taken... I just did
the same as you did: look into the source code and figured out this
was the reason. Maybe some of the core developers can comment on this?
Or if you think your  use case is a valid one just file a RFE on
wicket site.

Best,

Ernesto

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Image subclass not getting its model object

Posted by Erwin Bolwidt <eb...@worldturner.nl>.
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 Bolwidt<eb...@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, IModel<Boolean>  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:
>>>
>>>         RefreshingListView<ProductListing>  listingRepeater = new
>>> RefreshingListView<ProductListing>(
>>>                 "listing", new
>>> PropertyModel<Set<ProductListing>>(getModel(), "listings")) {
>>>             @Override
>>>             protected void populateItem(Item<ProductListing>  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 RefreshingListView<T>  extends RefreshingView<T>  {
>>>
>>>     public RefreshingListView(String id) {
>>>         super(id);
>>>     }
>>>
>>>     public RefreshingListView(String id, IModel<? extends Collection<T>>
>>> model) {
>>>         super(id, model);
>>>     }
>>>
>>>     @Override
>>>     protected Iterator<IModel<T>>  getItemModels() {
>>>         Collection<T>  modelObject = getModelObject();
>>>         if (modelObject == null) {
>>>             modelObject = new ArrayList<T>();
>>>         }
>>>         return new ModelIteratorAdapter<T>(modelObject.iterator()) {
>>>             @Override
>>>             protected IModel<T>  model(T object) {
>>>                 return new CompoundPropertyModel<T>(object);
>>>             }
>>>         };
>>>     }
>>>
>>>     public Collection<T>  getModelObject() {
>>>         return getModel().getObject();
>>>     }
>>>
>>>     @SuppressWarnings("unchecked")
>>>     public IModel<List<T>>  getModel() {
>>>         return (IModel<List<T>>) getDefaultModel();
>>>     }
>>>
>>> }
>>>
>>>        

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Image subclass not getting its model object

Posted by Ernesto Reinaldo Barreiro <re...@gmail.com>.
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 Bolwidt <eb...@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, IModel<Boolean> 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:
>>
>>        RefreshingListView<ProductListing> listingRepeater = new
>> RefreshingListView<ProductListing>(
>>                "listing", new
>> PropertyModel<Set<ProductListing>>(getModel(), "listings")) {
>>            @Override
>>            protected void populateItem(Item<ProductListing> 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 RefreshingListView<T> extends RefreshingView<T> {
>>
>>    public RefreshingListView(String id) {
>>        super(id);
>>    }
>>
>>    public RefreshingListView(String id, IModel<? extends Collection<T>>
>> model) {
>>        super(id, model);
>>    }
>>
>>    @Override
>>    protected Iterator<IModel<T>> getItemModels() {
>>        Collection<T> modelObject = getModelObject();
>>        if (modelObject == null) {
>>            modelObject = new ArrayList<T>();
>>        }
>>        return new ModelIteratorAdapter<T>(modelObject.iterator()) {
>>            @Override
>>            protected IModel<T> model(T object) {
>>                return new CompoundPropertyModel<T>(object);
>>            }
>>        };
>>    }
>>
>>    public Collection<T> getModelObject() {
>>        return getModel().getObject();
>>    }
>>
>>    @SuppressWarnings("unchecked")
>>    public IModel<List<T>> getModel() {
>>        return (IModel<List<T>>) getDefaultModel();
>>    }
>>
>> }
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
>> For additional commands, e-mail: users-help@wicket.apache.org
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Image subclass not getting its model object

Posted by Erwin Bolwidt <eb...@worldturner.nl>.
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, IModel<Boolean> 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:
>
>         RefreshingListView<ProductListing> listingRepeater = new 
> RefreshingListView<ProductListing>(
>                 "listing", new 
> PropertyModel<Set<ProductListing>>(getModel(), "listings")) {
>             @Override
>             protected void populateItem(Item<ProductListing> 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 RefreshingListView<T> extends RefreshingView<T> {
>
>     public RefreshingListView(String id) {
>         super(id);
>     }
>
>     public RefreshingListView(String id, IModel<? extends 
> Collection<T>> model) {
>         super(id, model);
>     }
>
>     @Override
>     protected Iterator<IModel<T>> getItemModels() {
>         Collection<T> modelObject = getModelObject();
>         if (modelObject == null) {
>             modelObject = new ArrayList<T>();
>         }
>         return new ModelIteratorAdapter<T>(modelObject.iterator()) {
>             @Override
>             protected IModel<T> model(T object) {
>                 return new CompoundPropertyModel<T>(object);
>             }
>         };
>     }
>
>     public Collection<T> getModelObject() {
>         return getModel().getObject();
>     }
>
>     @SuppressWarnings("unchecked")
>     public IModel<List<T>> getModel() {
>         return (IModel<List<T>>) getDefaultModel();
>     }
>
> }
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org