001: /*
002: * Copyright (C) 2006 Methodhead Software LLC. All rights reserved.
003: *
004: * This file is part of TransferCM.
005: *
006: * TransferCM is free software; you can redistribute it and/or modify it under the
007: * terms of the GNU General Public License as published by the Free Software
008: * Foundation; either version 2 of the License, or (at your option) any later
009: * version.
010: *
011: * TransferCM is distributed in the hope that it will be useful, but WITHOUT ANY
012: * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
013: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
014: * details.
015: *
016: * You should have received a copy of the GNU General Public License along with
017: * TransferCM; if not, write to the Free Software Foundation, Inc., 51 Franklin St,
018: * Fifth Floor, Boston, MA 02110-1301 USA
019: */
020:
021: package com.methodhead.shim;
022:
023: import com.methodhead.tree.Tree;
024: import com.methodhead.tree.FoldingTreeNode;
025: import java.util.Enumeration;
026:
027: /**
028: * A tree of <tt>FoldingTreeNode</tt>s that backs the folding tree of pages
029: * used to browse to pages while adminstering a site.
030: */
031: public class SiteMapTree extends Tree {
032:
033: // constructors /////////////////////////////////////////////////////////////
034:
035: // constants ////////////////////////////////////////////////////////////////
036:
037: // classes //////////////////////////////////////////////////////////////////
038:
039: // methods //////////////////////////////////////////////////////////////////
040:
041: /**
042: * Return the node with <tt>pageId</tt> or <tt>null</tt> if there is no such
043: * node.
044: */
045: public FoldingTreeNode find(
046: int pageId ) {
047:
048: Integer i = new Integer( pageId );
049:
050: if ( getRoot() == null )
051: return null;
052:
053: Enumeration enum = getRoot().depthFirstEnumeration();
054: while ( enum.hasMoreElements() ) {
055: FoldingTreeNode node = ( FoldingTreeNode )enum.nextElement();
056: if ( i.equals( node.getUserObject() ) )
057: return node;
058: }
059:
060: return null;
061: }
062:
063: /**
064: * Builds a tree that corresponds to <tt>siteMap</tt>. For each node, the
065: * link's page id is stored as the node's user object as an <tt>Integer</tt>,
066: * the node's label is set to the link's title, the node's url is set to
067: * <tt>editPage.do?id=<pageId></tt>, and the node icon hint is set to
068: * <tt>PAGE</tt>.
069: */
070: public void build(SiteMap siteMap) {
071:
072: if (siteMap.getRoot() == null) {
073: setRoot(null);
074: return;
075: }
076:
077: setRoot(buildNode((Link) siteMap.getRoot()));
078: }
079:
080: /**
081: * Recursively builds a node for <tt>link</tt>. NOT UNIT TESTED.
082: */
083: public static FoldingTreeNode buildNode(Link link) {
084:
085: FoldingTreeNode node = new FoldingTreeNode();
086: node.setUserObject(new Integer(link.getPageId()));
087: node.setLabel(link.getTitle());
088: node.setUrl("editPage.do?id=" + link.getPageId());
089: node.setIconHint("PAGE");
090:
091: for (int i = 0; i < link.getChildCount(); i++) {
092: Link child = (Link) link.getChildAt(i);
093: node.add(buildNode(child));
094: }
095:
096: return node;
097: }
098:
099: // properties ///////////////////////////////////////////////////////////////
100:
101: // attributes ///////////////////////////////////////////////////////////////
102: }
|