/** * Copyright 2013 55 Minutes (http://www.55minutes.com) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package lt.inventi.wicket.component.repeater; import java.util.List; import org.apache.wicket.markup.html.list.ListView; import org.apache.wicket.model.IModel; /** * A {@link ListView} that returns <code>false</code> for {@link #isVisible} if * its list is empty. This allows you to use the * <code><wicket:enclosure></code> tag to hide surrounding markup. * Therefore you can use this component to ensure that, for example, the * <code><ul></code> is ommitted when you have zero * <code><li></code> elements. * <em>This is necessary for valid XHTML markup.</em> * <p> * Example usage: * * <pre class="example"> * <wicket:enclosure child="item"> * <ul> * <li wicket:id="item">...</li> * </ul> * </wicket:enclosure> * * add(new ConditionalListView<Person>("people", personList) { * @Override protected void populateItem(ListItem<Person> item) * { * item.add(...); * } * }); * </pre> * <p> * In this example, if the {@code personList} model is {@code null} or empty, * the entire enclosure will be omitted from the HTML output. */ public abstract class ConditionalListView<T> extends ListView<T> { public ConditionalListView(String id) { super(id); } public ConditionalListView(String id, IModel<? extends List<? extends T>> model) { super(id, model); } public ConditionalListView(String id, List<? extends T> list) { super(id, list); } /** * Returns <code>false</code> if the list is empty. */ @Override public boolean isVisible() { return getViewSize() > 0; } }