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.ArrayList;
020: import java.util.Iterator;
021: import java.util.List;
022:
023: import javax.swing.tree.DefaultMutableTreeNode;
024: import javax.swing.tree.DefaultTreeModel;
025: import javax.swing.tree.TreeModel;
026: import javax.swing.tree.TreeNode;
027:
028: import org.apache.wicket.PageParameters;
029: import org.apache.wicket.examples.WicketExamplePage;
030: import org.apache.wicket.examples.ajax.builtin.tree.SimpleTreePage;
031: import org.apache.wicket.extensions.markup.html.tree.Tree;
032: import org.apache.wicket.extensions.markup.html.tree.DefaultAbstractTree.LinkType;
033: import org.apache.wicket.markup.html.link.BookmarkablePageLink;
034: import org.apache.wicket.markup.html.link.Link;
035:
036: /**
037: * Examples that shows how you can display a tree like structure (in this case
038: * nested lists with string elements) using nested panels and using a tree
039: * component.
040: *
041: * @author Eelco Hillenius
042: */
043: public class Home extends WicketExamplePage {
044: /**
045: * Constructor.
046: *
047: * @param parameters
048: * Page parameters
049: */
050: public Home(final PageParameters parameters) {
051: // create a list with sublists
052: List l1 = new ArrayList();
053: l1.add("test 1.1");
054: l1.add("test 1.2");
055: List l2 = new ArrayList();
056: l2.add("test 2.1");
057: l2.add("test 2.2");
058: l2.add("test 2.3");
059: List l3 = new ArrayList();
060: l3.add("test 3.1");
061: l2.add(l3);
062: l2.add("test 2.4");
063: l1.add(l2);
064: l1.add("test 1.3");
065:
066: // construct the panel
067: add(new RecursivePanel("panels", l1));
068:
069: // create a tree
070: TreeModel treeModel = convertToTreeModel(l1);
071: final Tree tree = new Tree("tree", treeModel) {
072: protected String renderNode(TreeNode node) {
073: DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode) node;
074: Object userObject = treeNode.getUserObject();
075: return (userObject instanceof List) ? "<subtree>"
076: : String.valueOf(treeNode.getUserObject());
077: }
078: };
079: // disable ajax links in this example
080: tree.setLinkType(LinkType.REGULAR);
081:
082: add(tree);
083: add(new Link("expandAll") {
084: public void onClick() {
085: tree.getTreeState().expandAll();
086: }
087: });
088:
089: add(new Link("collapseAll") {
090: public void onClick() {
091: tree.getTreeState().collapseAll();
092: }
093: });
094:
095: add(new BookmarkablePageLink("ajaxTreeLink",
096: SimpleTreePage.class));
097: }
098:
099: /**
100: * Add a sublist to the parent.
101: *
102: * @param parent
103: * the parent
104: * @param sub
105: * the sub list
106: */
107: private void add(DefaultMutableTreeNode parent, List sub) {
108: for (Iterator i = sub.iterator(); i.hasNext();) {
109: Object o = i.next();
110: if (o instanceof List) {
111: DefaultMutableTreeNode child = new DefaultMutableTreeNode(
112: o);
113: parent.add(child);
114: add(child, (List) o);
115: } else {
116: DefaultMutableTreeNode child = new DefaultMutableTreeNode(
117: o);
118: parent.add(child);
119: }
120: }
121: }
122:
123: /**
124: * Convert the nested lists to a tree model
125: *
126: * @param list
127: * the list
128: * @return tree model
129: */
130: private TreeModel convertToTreeModel(List list) {
131: TreeModel model = null;
132: DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode(
133: "<root>");
134: add(rootNode, list);
135: model = new DefaultTreeModel(rootNode);
136: return model;
137: }
138: }
|