001: /*
002: * Beryl - A web platform based on XML, XSLT and Java
003: * This file is part of the Beryl XML GUI
004: *
005: * Copyright (C) 2004 Wenzel Jakob <wazlaf@tigris.org>
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011:
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-3107 USA
020: */
021:
022: package org.beryl.gui.widgets;
023:
024: import java.util.Enumeration;
025: import java.util.Iterator;
026:
027: import javax.swing.tree.MutableTreeNode;
028: import javax.swing.tree.TreeNode;
029:
030: import org.beryl.gui.GUIException;
031: import org.beryl.gui.Widget;
032: import org.beryl.gui.WidgetInfo;
033:
034: public class TreeItem extends Item implements MutableTreeNode {
035: protected static WidgetInfo treeItemInfo = null;
036: private Tree tree = null;
037: private String name = null;
038: protected boolean checkLeaf = true;
039:
040: static {
041: treeItemInfo = new WidgetInfo(Widget.class, itemInfo);
042: treeItemInfo.setSupportsAnchor(false);
043: };
044:
045: public TreeItem(Widget parent, String name) throws GUIException {
046: super (parent, name);
047: if (parent != null) {
048: if (parent instanceof Tree)
049: tree = (Tree) parent;
050: else if (parent instanceof TreeItem)
051: tree = ((TreeItem) parent).getTree();
052: }
053: this .name = name;
054: }
055:
056: /**
057: * Check whether this TreeItem is a leaf tree node?
058: * If set to false, the tree item will always be rendered
059: * with a non-leaf icon
060: */
061: public void setCheckLeaf(boolean checkLeaf) {
062: this .checkLeaf = checkLeaf;
063: }
064:
065: public void addChild(Widget widget, Object constraint)
066: throws GUIException {
067: if (widget instanceof TreeItem) {
068: TreeItem treeItem = (TreeItem) widget;
069: treeItem.tree = tree;
070: addChild(widget);
071: } else {
072: throw new GUIException(
073: "Only TreeItem children are supported");
074: }
075: }
076:
077: public Tree getTree() {
078: return tree;
079: }
080:
081: public void insert(MutableTreeNode child, int index) {
082: throw new UnsupportedOperationException(
083: "Use addChild(Widget) instead");
084: }
085:
086: public void remove(int index) {
087: try {
088: removeChildWidget((Widget) getChildAt(index));
089: } catch (GUIException e) {
090: /* Will not happen */
091: throw new RuntimeException(e);
092: }
093: }
094:
095: public void removeAllChildren() {
096: removeAllChildWidgets();
097: }
098:
099: public void remove(MutableTreeNode node) {
100: try {
101: removeChildWidget((Widget) node);
102: } catch (GUIException e) {
103: /* Will not happen */
104: throw new RuntimeException(e);
105: }
106: }
107:
108: public void removeFromParent() {
109: try {
110: getParentWidget().removeChildWidget(this );
111: } catch (GUIException e) {
112: /* Will not happen */
113: throw new RuntimeException(e);
114: }
115: }
116:
117: public void setParent(MutableTreeNode newParent) {
118: throw new UnsupportedOperationException(
119: "Parent cannot be changed, recreate the node");
120: }
121:
122: public Enumeration children() {
123: final Iterator iterator = getChildren().iterator();
124: return new Enumeration() {
125: public boolean hasMoreElements() {
126: return iterator.hasNext();
127: }
128:
129: public Object nextElement() {
130: return iterator.next();
131: }
132: };
133: }
134:
135: public boolean getAllowsChildren() {
136: return true;
137: }
138:
139: public TreeNode getChildAt(int childIndex) {
140: return (TreeNode) super .getChild(childIndex);
141: }
142:
143: public int getIndex(TreeNode node) {
144: return super .getChildIndex((Widget) node);
145: }
146:
147: public TreeNode getParent() {
148: Widget parent = getParentWidget();
149: try {
150: return (TreeNode) parent;
151: } catch (ClassCastException e) {
152: return null;
153: }
154: }
155:
156: public int getChildCount() {
157: return super .getChildCount();
158: }
159:
160: public boolean isLeaf() {
161: return checkLeaf && (getChildCount() == 0);
162: }
163:
164: public void revalidate() throws GUIException {
165: if (tree != null) {
166: tree.structureChanged(this );
167: }
168: }
169:
170: public WidgetInfo getWidgetInfo() {
171: return treeItemInfo;
172: }
173: }
|