Author: ivaynberg Date: Thu Apr 19 15:11:22 2007 New Revision: 530567 URL: http://svn.apache.org/viewvc?view=rev&rev=530567 Log: extracted AbstractRepeater that all repeaters now implement. besides the obvious benefits this should make it easier to identify them and forbid their addition into ajax request target.
Added: incubator/wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/markup/repeater/AbstractRepeater.java incubator/wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/util/collections/ReadOnlyIterator.java Modified: incubator/wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/markup/html/list/ListView.java incubator/wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/markup/html/list/Loop.java incubator/wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/markup/repeater/RepeatingView.java Modified: incubator/wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/markup/html/list/ListView.java URL: http://svn.apache.org/viewvc/incubator/wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/markup/html/list/ListView.java?view=diff&rev=530567&r1=530566&r2=530567 ============================================================================== --- incubator/wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/markup/html/list/ListView.java (original) +++ incubator/wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/markup/html/list/ListView.java Thu Apr 19 15:11:22 2007 @@ -22,11 +22,11 @@ import java.util.List; import org.apache.wicket.Component; -import org.apache.wicket.markup.MarkupStream; -import org.apache.wicket.markup.html.WebMarkupContainer; import org.apache.wicket.markup.html.link.Link; +import org.apache.wicket.markup.repeater.AbstractRepeater; import org.apache.wicket.model.IModel; import org.apache.wicket.model.Model; +import org.apache.wicket.util.collections.ReadOnlyIterator; import org.apache.wicket.version.undo.Change; @@ -34,7 +34,8 @@ * A ListView is a repeater that makes it easy to display/work with [EMAIL PROTECTED] List}s. * However, there are situations where it is necessary to work with other * collection types, for repeaters that might work better with non-list or - * database-driven collections see the org.apache.wicket.markup.repeater package. + * database-driven collections see the org.apache.wicket.markup.repeater + * package. * * Also notice that in a list the item's uniqueness/primary key/id is identified * as its index in the list. If this is not the case you should either override @@ -105,7 +106,7 @@ * @author Johan Compagner * @author Eelco Hillenius */ -public abstract class ListView extends WebMarkupContainer +public abstract class ListView extends AbstractRepeater { /** Index of the first item to show */ private int firstIndex = 0; @@ -643,38 +644,28 @@ } /** - * - * @see org.apache.wicket.Component#onRender(org.apache.wicket.markup.MarkupStream) + * @see org.apache.wicket.markup.repeater.AbstractRepeater#renderIterator() */ - protected void onRender(final MarkupStream markupStream) + protected Iterator renderIterator() { - // Save position in markup stream - final int markupStart = markupStream.getCurrentIndex(); - // Get number of items to be displayed final int size = getViewSize(); - if (size > 0) + return new ReadOnlyIterator() { - // Loop through the markup in this container for each item - for (int i = 0; i < size; i++) - { - // Get index - final int index = firstIndex + i; + private int index = 0; - // Get list item for index - ListItem item = (ListItem)get(Integer.toString(index)); - - // Rewind to start of markup for kids - markupStream.setCurrentIndex(markupStart); + public boolean hasNext() + { + return index < size; + } - // Render - renderItem(item); + public Object next() + { + final String id = Integer.toString(firstIndex + index); + index++; + return get(id); } - } - else - { - markupStream.skipComponent(); - } + }; } /** @@ -698,6 +689,15 @@ * The item to populate */ protected abstract void populateItem(final ListItem item); + + + /** + * @see org.apache.wicket.markup.repeater.AbstractRepeater#renderChild(org.apache.wicket.Component) + */ + protected final void renderChild(Component child) + { + renderItem((ListItem)child); + } /** * Render a single item. Modified: incubator/wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/markup/html/list/Loop.java URL: http://svn.apache.org/viewvc/incubator/wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/markup/html/list/Loop.java?view=diff&rev=530567&r1=530566&r2=530567 ============================================================================== --- incubator/wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/markup/html/list/Loop.java (original) +++ incubator/wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/markup/html/list/Loop.java Thu Apr 19 15:11:22 2007 @@ -16,11 +16,14 @@ */ package org.apache.wicket.markup.html.list; -import org.apache.wicket.WicketRuntimeException; -import org.apache.wicket.markup.MarkupStream; +import java.util.Iterator; + +import org.apache.wicket.Component; import org.apache.wicket.markup.html.WebMarkupContainer; +import org.apache.wicket.markup.repeater.AbstractRepeater; import org.apache.wicket.model.IModel; import org.apache.wicket.model.Model; +import org.apache.wicket.util.collections.ReadOnlyIterator; /** * A very simple loop component whose model is an Integer defining the number of @@ -34,7 +37,7 @@ * @author Eelco Hillenius * @author Jonathan Locke */ -public abstract class Loop extends WebMarkupContainer +public abstract class Loop extends AbstractRepeater { /** * Item container for a Loop iteration. @@ -44,7 +47,7 @@ public static final class LoopItem extends WebMarkupContainer { private static final long serialVersionUID = 1L; - + /** The iteration number */ private final int iteration; @@ -111,7 +114,7 @@ protected void onAttach() { super.onAttach(); - + // Remove any previous loop contents removeAll(); @@ -145,42 +148,27 @@ } /** - * - * @see org.apache.wicket.Component#onRender(org.apache.wicket.markup.MarkupStream) + * @see org.apache.wicket.markup.repeater.AbstractRepeater#renderIterator() */ - protected final void onRender(final MarkupStream markupStream) + protected Iterator renderIterator() { - // Save position in markup stream - final int markupStart = markupStream.getCurrentIndex(); - - // Get number of iterations final int iterations = getIterations(); - if (iterations > 0) - { - // Loop through the markup in this container for each item - for (int iteration = 0; iteration < iterations; iteration++) - { - // Get item for iteration - final LoopItem item = (LoopItem)get(Integer.toString(iteration)); - // Item should have been constructed in internalOnBeginRequest - if (item == null) - { - throw new WicketRuntimeException( - "Loop item is null. Probably the number of loop iterations were changed between onBeginRequest and render time."); - } + return new ReadOnlyIterator() + { + private int index = 0; - // Rewind to start of markup for kids - markupStream.setCurrentIndex(markupStart); + public boolean hasNext() + { + return index < iterations; + } - // Render iteration - renderItem(item); + public Object next() + { + return get(Integer.toString(index++)); } - } - else - { - markupStream.skipComponent(); - } + + }; } /** @@ -190,6 +178,14 @@ * The iteration of the loop */ protected abstract void populateItem(LoopItem item); + + /** + * @param child + */ + protected final void renderChild(Component child) + { + renderItem((LoopItem)child); + } /** * Renders this loop iteration. Added: incubator/wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/markup/repeater/AbstractRepeater.java URL: http://svn.apache.org/viewvc/incubator/wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/markup/repeater/AbstractRepeater.java?view=auto&rev=530567 ============================================================================== --- incubator/wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/markup/repeater/AbstractRepeater.java (added) +++ incubator/wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/markup/repeater/AbstractRepeater.java Thu Apr 19 15:11:22 2007 @@ -0,0 +1,109 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.wicket.markup.repeater; + +import java.util.Iterator; + +import org.apache.wicket.Component; +import org.apache.wicket.markup.MarkupStream; +import org.apache.wicket.markup.html.WebMarkupContainer; +import org.apache.wicket.model.IModel; + +/** + * Base class for repeaters. This container renders each of its children using + * its own markup. + * + * The children are collected using [EMAIL PROTECTED] #renderIterator()} method. This class + * will take care of properly positioning and rewinding its markup stream so + * before each child renders it points to the beginning of this component. Each + * child is rendered by a call to [EMAIL PROTECTED] #renderChild(Component)}. A typical + * implementation simply does <code>child.render(getMarkupStream());</code>. + * + * @author Igor Vaynberg (ivaynberg) + */ +public abstract class AbstractRepeater extends WebMarkupContainer +{ + private static final long serialVersionUID = 1L; + + /** + * Constructor + * + * @param id + */ + public AbstractRepeater(String id) + { + super(id); + } + + /** + * Constructor + * + * @param id + * @param model + */ + public AbstractRepeater(String id, IModel model) + { + super(id, model); + } + + /** + * Returns an iterator for the collection of child components to be + * rendered. Users can override this to change order of rendered children. + * + * @return iterator over child components to be rendered + */ + protected abstract Iterator renderIterator(); + + /** + * Renders all child items in no specified order + * + * @param markupStream + * The markup stream + */ + protected final void onRender(final MarkupStream markupStream) + { + final int markupStart = markupStream.getCurrentIndex(); + + Iterator it = renderIterator(); + if (it.hasNext()) + { + do + { + markupStream.setCurrentIndex(markupStart); + renderChild((Component)it.next()); + } + while (it.hasNext()); + } + else + { + markupStream.skipComponent(); + } + } + + /** + * Render a single child. This method can be overridden to modify how a + * single child component is rendered. + * + * @param child + * Child component to be rendered + */ + protected void renderChild(final Component child) + { + child.render(getMarkupStream()); + } + +} Modified: incubator/wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/markup/repeater/RepeatingView.java URL: http://svn.apache.org/viewvc/incubator/wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/markup/repeater/RepeatingView.java?view=diff&rev=530567&r1=530566&r2=530567 ============================================================================== --- incubator/wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/markup/repeater/RepeatingView.java (original) +++ incubator/wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/markup/repeater/RepeatingView.java Thu Apr 19 15:11:22 2007 @@ -19,8 +19,6 @@ import java.util.Iterator; import org.apache.wicket.Component; -import org.apache.wicket.markup.MarkupStream; -import org.apache.wicket.markup.html.WebMarkupContainer; import org.apache.wicket.model.IModel; @@ -73,7 +71,7 @@ * @author Igor Vaynberg ( ivaynberg ) * */ -public class RepeatingView extends WebMarkupContainer +public class RepeatingView extends AbstractRepeater { /** * @@ -116,59 +114,11 @@ } /** - * Renders all child items in no specified order - * - * @param markupStream - * The markup stream - */ - protected void onRender(final MarkupStream markupStream) - { - final int markupStart = markupStream.getCurrentIndex(); - - Iterator it = renderIterator(); - if (it.hasNext()) - { - do - { - markupStream.setCurrentIndex(markupStart); - renderChild((Component)it.next()); - } - while (it.hasNext()); - } - else - { - markupStream.skipComponent(); - } - } - - /** - * Returns an iterator for the collection of child components to be - * rendered. - * - * Child component are rendered in the order they are in the iterator. Since - * we use the iterator returned by wicket's - * <code>MarkupContainer#iterator()</code> method and that method does not - * guarantee any kind of ordering neither do we. This method can be - * overridden by subclasses to create an ordering scheme, see - * <code>OrderedRepeatingView#renderIterator()</code>. - * - * @return iterator over child components to be rendered + * @see org.apache.wicket.markup.repeater.AbstractRepeater#renderIterator() */ protected Iterator renderIterator() { return iterator(); - } - - /** - * Render a single child. This method can be overridden to modify how a - * single child component is rendered. - * - * @param child - * Child component to be rendered - */ - protected void renderChild(final Component child) - { - child.render(getMarkupStream()); } } Added: incubator/wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/util/collections/ReadOnlyIterator.java URL: http://svn.apache.org/viewvc/incubator/wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/util/collections/ReadOnlyIterator.java?view=auto&rev=530567 ============================================================================== --- incubator/wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/util/collections/ReadOnlyIterator.java (added) +++ incubator/wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/util/collections/ReadOnlyIterator.java Thu Apr 19 15:11:22 2007 @@ -0,0 +1,34 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.wicket.util.collections; + +import java.util.Iterator; + +/** + * Base class for read-only iterators. Calls to [EMAIL PROTECTED] #remove()} will result in + * an [EMAIL PROTECTED] UnsupportedOperationException} + * + * @author Igor Vaynberg (ivaynberg) + */ +public abstract class ReadOnlyIterator implements Iterator +{ + public final void remove() + { + throw new UnsupportedOperationException("Iterator " + getClass().getName() + + " is a read-only iterator. Calls to remove() are not allowed"); + } +}