001: /**
002: * Copyright 2006 Webmedia Group Ltd.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: **/package org.araneaframework.example.main.release.demos;
016:
017: import java.util.ArrayList;
018: import java.util.Iterator;
019: import java.util.List;
020: import java.util.Set;
021: import javax.servlet.ServletContext;
022: import org.araneaframework.example.main.TemplateBaseWidget;
023: import org.araneaframework.uilib.core.BaseUIWidget;
024: import org.araneaframework.uilib.tree.TreeDataProvider;
025: import org.araneaframework.uilib.tree.TreeNodeContext;
026: import org.araneaframework.uilib.tree.TreeNodeWidget;
027: import org.araneaframework.uilib.tree.TreeWidget;
028:
029: /**
030: * Widget that shows two simple trees. One tree uses events for submit links and
031: * the other uses actions (in that case only the current tree node and its
032: * children are rendered).
033: *
034: * @author Alar Kvell (alar@araneaframework.org)
035: * @author Taimo Peelo (taimo@araneaframework.org)
036: */
037: public class SimpleTreeWidget extends TemplateBaseWidget {
038: protected void init() throws Exception {
039: setViewSelector("release/demos/tree/simpleTree");
040:
041: TreeWidget tree1 = new TreeWidget(new SimpleTreeDataProvider());
042: tree1.setRemoveChildrenOnCollapse(false);
043: addWidget("tree1", tree1);
044:
045: TreeWidget tree2 = new TreeWidget(new SimpleTreeDataProvider());
046: tree2.setUseActions(true);
047: addWidget("tree2", tree2);
048: }
049:
050: private class SimpleTreeDataProvider implements TreeDataProvider {
051: public List getChildren(TreeNodeContext parent) {
052: List children = new ArrayList();
053:
054: Iterator i = getResourceIterator(parent);
055:
056: if (i == null || !i.hasNext()) {
057: return null;
058: }
059:
060: while (i.hasNext()) {
061: children
062: .add(new TreeNodeWidget(
063: new SimpleTreeDisplayWidget(i.next()
064: .toString())));
065: }
066:
067: return children;
068: }
069:
070: public boolean hasChildren(TreeNodeContext parent) {
071: return (getChildren(parent) != null && !getChildren(parent)
072: .isEmpty());
073: }
074:
075: private Iterator getResourceIterator(TreeNodeContext widget) {
076: ServletContext servletContext = (ServletContext) SimpleTreeWidget.this
077: .getEnvironment().getEntry(ServletContext.class);
078: String path = null;
079: if (widget instanceof TreeWidget)
080: path = "/";
081: else
082: path = ((SimpleTreeDisplayWidget) widget
083: .getDisplayWidget()).getPath();
084: Set set = servletContext.getResourcePaths(path);
085: return set != null ? set.iterator() : null;
086: }
087:
088: }
089:
090: public static class SimpleTreeDisplayWidget extends BaseUIWidget {
091: private String path;
092:
093: public SimpleTreeDisplayWidget(String name) {
094: this .path = name;
095: }
096:
097: protected void init() throws Exception {
098: setViewSelector("release/demos/tree/simpleTreeDisplay");
099: }
100:
101: public String getPath() {
102: return path;
103: }
104:
105: public String getName() {
106: if (!path.endsWith("/"))
107: return path.substring(path.lastIndexOf("/") + 1);
108:
109: String s = path.substring(0, path.length() - 1);
110: return s.substring(s.lastIndexOf("/"));
111: }
112: }
113: }
|