001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.wicket.markup.repeater;
018:
019: import java.util.Iterator;
020:
021: import org.apache.wicket.Component;
022: import org.apache.wicket.model.IModel;
023:
024: /**
025: * <p>
026: * A repeater view that renders all of its children, using its body markup, in
027: * the order they were added.
028: *
029: * </p>
030: * Example:
031: * <p>
032: * <u>Java:</u>
033: *
034: * <pre>
035: * RepeatingView view = new RepeatingView("repeater");
036: * view.add(new Label("1", "hello"));
037: * view.add(new Label("2", "goodbye"));
038: * view.add(new Label("3", "good morning"));
039: * </pre>
040: *
041: * </p>
042: * <p>
043: * <u>Markup:</u>
044: *
045: * <pre>
046: * <ul><li wicket:id="repeater"></li></ul>
047: * </pre>
048: *
049: * </p>
050: * <p>
051: * <u>Yields:</u>
052: *
053: * <pre>
054: * <ul><li>hello</li><li>goodbye</li><li>good morning</li></ul>
055: * </pre>
056: *
057: * To expand a bit: the repeater itself produces no markup, instead every direct
058: * child inherits the entire markup of the repeater. In the example above
059: * reeaters's markup is:
060: *
061: * <pre>
062: * <li wicket:id="repeater"></li>
063: * </pre>
064: *
065: * and so this is the markup that is available to the direct children - the
066: * Label components. So as each label renders it produces a line of the output
067: * that has the <code>li</code>tag.
068: *
069: *
070: * @author Igor Vaynberg ( ivaynberg )
071: *
072: */
073: public class RepeatingView extends AbstractRepeater {
074: /**
075: *
076: */
077: private static final long serialVersionUID = 1L;
078:
079: /** Counter used for generating unique child component ids. */
080: private long childIdCounter = 0;
081:
082: /** @see Component#Component(String) */
083: public RepeatingView(String id) {
084: super (id);
085: }
086:
087: /** @see Component#Component(String, IModel) */
088: public RepeatingView(String id, IModel model) {
089: super (id, model);
090: }
091:
092: /**
093: * Generates a unique id string. This makes it easy to add items to be
094: * rendered w/out having to worry about generating unique id strings in your
095: * code.
096: *
097: * @return unique child id
098: */
099: public String newChildId() {
100: childIdCounter++;
101:
102: if (childIdCounter == Long.MAX_VALUE) {
103: // mmm yeah...like this will ever happen
104: throw new RuntimeException(
105: "generateChildId() out of space.");
106: }
107:
108: // We prepend the id's with the text 'id' so they will generate valid
109: // markup id's if needed.
110: return String.valueOf(childIdCounter);
111: }
112:
113: /**
114: * @see org.apache.wicket.markup.repeater.AbstractRepeater#renderIterator()
115: */
116: protected Iterator renderIterator() {
117: return iterator();
118: }
119:
120: }
|