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.examples.nested;
018:
019: import java.util.List;
020:
021: import org.apache.wicket.markup.html.WebMarkupContainer;
022: import org.apache.wicket.markup.html.basic.Label;
023: import org.apache.wicket.markup.html.list.ListItem;
024: import org.apache.wicket.markup.html.list.ListView;
025: import org.apache.wicket.markup.html.panel.Panel;
026:
027: /**
028: * This example list knows how to display sublists. It expects a list where each
029: * element is either a string or another list.
030: *
031: * @author Eelco Hillenius
032: */
033: public final class RecursivePanel extends Panel {
034: /**
035: * Constructor.
036: *
037: * @param id
038: * The id of this component
039: * @param list
040: * a list where each element is either a string or another list
041: */
042: public RecursivePanel(final String id, List list) {
043: super (id);
044: add(new Rows("rows", list));
045: setVersioned(false);
046: }
047:
048: /**
049: * The list class.
050: */
051: private static class Rows extends ListView {
052: /**
053: * Construct.
054: *
055: * @param name
056: * name of the component
057: * @param list
058: * a list where each element is either a string or another
059: * list
060: */
061: public Rows(String name, List list) {
062: super (name, list);
063: }
064:
065: /**
066: * @see org.apache.wicket.markup.html.list.ListView#populateItem(org.apache.wicket.markup.html.list.ListItem)
067: */
068: protected void populateItem(ListItem listItem) {
069: Object modelObject = listItem.getModelObject();
070:
071: if (modelObject instanceof List) {
072: // create a panel that renders the sub lis
073: RecursivePanel nested = new RecursivePanel("nested",
074: (List) modelObject);
075: listItem.add(nested);
076: // if the current element is a list, we create a dummy row/
077: // label element
078: // as we have to confirm to our HTML definition, and set it's
079: // visibility
080: // property to false as we do not want LI tags to be rendered.
081: WebMarkupContainer row = new WebMarkupContainer("row");
082: row.setVisible(false);
083: row.add(new WebMarkupContainer("label"));
084: listItem.add(row);
085: } else {
086: // if the current element is not a list, we create a dummy panel
087: // element
088: // to confirm to our HTML definition, and set this panel's
089: // visibility
090: // property to false to avoid having the UL tag rendered
091: RecursivePanel nested = new RecursivePanel("nested",
092: null);
093: nested.setVisible(false);
094: listItem.add(nested);
095: // add the row (with the LI element attached, and the label with
096: // the
097: // row's actual value to display
098: WebMarkupContainer row = new WebMarkupContainer("row");
099: row.add(new Label("label", modelObject.toString()));
100: listItem.add(row);
101: }
102: }
103: }
104: }
|