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

Reply via email to