001: /*
002: * $Id: RecursivePanel.java 461614 2006-07-27 09:32:41Z knopp $ $Revision: 461614 $
003: * $Date: 2006-07-27 11:32:41 +0200 (Thu, 27 Jul 2006) $
004: *
005: * ==================================================================== Licensed
006: * under the Apache License, Version 2.0 (the "License"); you may not use this
007: * file except in compliance with the License. You may obtain a copy of the
008: * License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
014: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
015: * License for the specific language governing permissions and limitations under
016: * the License.
017: */
018: package wicket.examples.nested;
019:
020: import java.util.List;
021:
022: import wicket.markup.html.WebMarkupContainer;
023: import wicket.markup.html.basic.Label;
024: import wicket.markup.html.list.ListItem;
025: import wicket.markup.html.list.ListView;
026: import wicket.markup.html.panel.Panel;
027:
028: /**
029: * This example list knows how to display sublists. It expects a list where each
030: * element is either a string or another list.
031: *
032: * @author Eelco Hillenius
033: */
034: public final class RecursivePanel extends Panel {
035: /**
036: * Constructor.
037: *
038: * @param id
039: * The id of this component
040: * @param list
041: * a list where each element is either a string or another list
042: */
043: public RecursivePanel(final String id, List list) {
044: super (id);
045: add(new Rows("rows", list));
046: setVersioned(false);
047: }
048:
049: /**
050: * The list class.
051: */
052: private static class Rows extends ListView {
053: /**
054: * Construct.
055: *
056: * @param name
057: * name of the component
058: * @param list
059: * a list where each element is either a string or another
060: * list
061: */
062: public Rows(String name, List list) {
063: super (name, list);
064: }
065:
066: /**
067: * @see wicket.markup.html.list.ListView#populateItem(wicket.markup.html.list.ListItem)
068: */
069: protected void populateItem(ListItem listItem) {
070: Object modelObject = listItem.getModelObject();
071:
072: if (modelObject instanceof List) {
073: // create a panel that renders the sub lis
074: RecursivePanel nested = new RecursivePanel("nested",
075: (List) modelObject);
076: listItem.add(nested);
077: // if the current element is a list, we create a dummy row/
078: // label element
079: // as we have to confirm to our HTML definition, and set it's
080: // visibility
081: // property to false as we do not want LI tags to be rendered.
082: WebMarkupContainer row = new WebMarkupContainer("row");
083: row.setVisible(false);
084: row.add(new WebMarkupContainer("label"));
085: listItem.add(row);
086: } else {
087: // if the current element is not a list, we create a dummy panel
088: // element
089: // to confirm to our HTML definition, and set this panel's
090: // visibility
091: // property to false to avoid having the UL tag rendered
092: RecursivePanel nested = new RecursivePanel("nested",
093: null);
094: nested.setVisible(false);
095: listItem.add(nested);
096: // add the row (with the LI element attached, and the label with
097: // the
098: // row's actual value to display
099: WebMarkupContainer row = new WebMarkupContainer("row");
100: row.add(new Label("label", modelObject.toString()));
101: listItem.add(row);
102: }
103: }
104: }
105: }
|