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