001: /*
002: Copyright (c) 2003 eInnovation Inc. All rights reserved
003:
004: This library is free software; you can redistribute it and/or modify it under the terms
005: of the GNU Lesser General Public License as published by the Free Software Foundation;
006: either version 2.1 of the License, or (at your option) any later version.
007:
008: This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
009: without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
010: See the GNU Lesser General Public License for more details.
011: */
012:
013: package com.openedit.webui.tree;
014:
015: import java.util.ArrayList;
016: import java.util.List;
017:
018: import com.openedit.util.strainer.Filter;
019:
020: /**
021: * This is a default implementation of a {@link WebTreeModel}, which assumes that all its nodes are
022: * <code>{@link DefaultWebTreeNode}</code>s.
023: *
024: * @author Eric Galluzzo
025: */
026: public class DefaultWebTreeModel implements WebTreeModel {
027: protected DefaultWebTreeNode fieldRoot;
028: protected Filter fieldFilter;
029:
030: /**
031: * Construct a tree model with no root (i.e. no tree).
032: */
033: public DefaultWebTreeModel() {
034: }
035:
036: /**
037: * Construct a tree model with the given root node.
038: *
039: * @param inRoot DOCUMENT ME!
040: */
041: public DefaultWebTreeModel(DefaultWebTreeNode inRoot) {
042: fieldRoot = inRoot;
043: }
044:
045: /* (non-Javadoc)
046: * @see TreeModel#getChild(Object, int)
047: */
048: public Object getChild(Object parent, int index) {
049: return ((DefaultWebTreeNode) parent).getChild(index);
050: }
051:
052: public List getChildrenInRows(Object inParent, int inColCount) {
053: //Now break up the page into rows by dividing the count they wanted
054: List children = getChildren(inParent);
055: double rowscount = (double) children.size()
056: / (double) inColCount;
057:
058: List rows = new ArrayList();
059: for (int i = 0; i < rowscount; i++) {
060: int start = i * inColCount;
061: int end = i * inColCount + inColCount;
062: List sublist = children.subList(start, Math.min(children
063: .size(), end));
064: rows.add(sublist);
065: }
066: return rows;
067:
068: }
069:
070: public List getChildren(Object parent) {
071: return ((DefaultWebTreeNode) parent).getChildren();
072: }
073:
074: /* (non-Javadoc)
075: * @see TreeModel#getChildCount(Object)
076: */
077: public int getChildCount(Object parent) {
078: return ((DefaultWebTreeNode) parent).getChildCount();
079: }
080:
081: /* (non-Javadoc)
082: * @see TreeModel#getIndexOfChild(Object, Object)
083: */
084: public int getIndexOfChild(Object parent, Object child) {
085: return ((DefaultWebTreeNode) parent)
086: .getIndexOfChild((DefaultWebTreeNode) child);
087: }
088:
089: /* (non-Javadoc)
090: * @see TreeModel#isLeaf(Object)
091: */
092: public boolean isLeaf(Object node) {
093: return ((DefaultWebTreeNode) node).isLeaf();
094: }
095:
096: /**
097: * Set the root node of this tree model.
098: *
099: * @param inRoot DOCUMENT ME!
100: */
101: public void setRoot(DefaultWebTreeNode inRoot) {
102: fieldRoot = inRoot;
103: }
104:
105: /* (non-Javadoc)
106: * @see TreeModel#getRoot()
107: */
108: public Object getRoot() {
109: return fieldRoot;
110: }
111:
112: public String getId(Object inNode) {
113: return String.valueOf(((DefaultWebTreeNode) inNode).getID());
114: }
115:
116: public Object getParent(Object inNode) {
117: DefaultWebTreeNode child = (DefaultWebTreeNode) inNode;
118: return child.getParent();
119: }
120:
121: public Object getChildById(String inId) {
122: return findNodeById(getRoot(), inId);
123: }
124:
125: public Object findNodeById(Object inRoot, String inId) {
126: String test = getId(inRoot);
127: if (test.equals(inId)) {
128: return inRoot;
129: }
130: if (hasLoadedChildren(inRoot)) {
131: int count = getChildCount(inRoot);
132: for (int i = 0; i < count; i++) {
133: Object child = getChild(inRoot, i);
134: child = findNodeById(child, inId);
135: if (child != null) {
136: return child;
137: }
138: }
139: }
140: return null;
141: }
142:
143: //can be overriden
144: protected boolean hasLoadedChildren(Object inRoot) {
145: //Only look in nodes with already loaded children
146: DefaultWebTreeNode parent = (DefaultWebTreeNode) inRoot;
147: return parent.hasLoadedChildren();
148: }
149:
150: public Filter getFilter() {
151: return fieldFilter;
152: }
153:
154: public void setFilter(Filter inFilter) {
155: fieldFilter = inFilter;
156: }
157:
158: }
|