001: /*
002: * Copyright (C) 2005 Jeff Tassin
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018:
019: package com.jeta.swingbuilder.gui.formmgr;
020:
021: import java.util.Collection;
022: import java.util.Enumeration;
023: import java.util.HashMap;
024: import java.util.Iterator;
025:
026: import javax.swing.JTree;
027: import javax.swing.tree.DefaultMutableTreeNode;
028: import javax.swing.tree.DefaultTreeModel;
029: import javax.swing.tree.TreePath;
030:
031: import com.jeta.forms.gui.form.FormComponent;
032: import com.jeta.forms.gui.formmgr.FormManager;
033: import com.jeta.open.registry.JETARegistry;
034: import com.jeta.swingbuilder.gui.editor.FormEditor;
035:
036: /**
037: * Displays the forms hierarhcy in a JTree for a given editor. This is only used
038: * during debugging
039: *
040: * @author Jeff Tassin
041: */
042: public class FormsTree extends JTree {
043:
044: public FormsTree() {
045: setShowsRootHandles(true);
046: putClientProperty("JTree.lineStyle", "Angled");
047: setCellRenderer(new FormsTreeRenderer());
048: reload();
049: }
050:
051: public void reload() {
052: setRootVisible(false);
053: DefaultMutableTreeNode root = new DefaultMutableTreeNode();
054: DefaultTreeModel model = new DefaultTreeModel(root);
055:
056: HashMap nodes = new HashMap();
057: FormManager fm = (FormManager) JETARegistry
058: .lookup(FormManager.COMPONENT_ID);
059: Collection form_ids = fm.getForms();
060: Iterator iter = form_ids.iterator();
061: while (iter.hasNext()) {
062: String form_id = (String) iter.next();
063: FormComponent fc = fm.getForm(form_id);
064:
065: DefaultMutableTreeNode node = new DefaultMutableTreeNode(fc);
066: if (fc.getParentForm() == null)
067: root.add(node);
068:
069: nodes.put(form_id, node);
070: }
071:
072: iter = nodes.keySet().iterator();
073: while (iter.hasNext()) {
074: String form_id = (String) iter.next();
075: DefaultMutableTreeNode node = (DefaultMutableTreeNode) nodes
076: .get(form_id);
077: FormComponent fc = (FormComponent) node.getUserObject();
078: FormComponent parent = fc.getParentForm();
079: if (parent != null) {
080: DefaultMutableTreeNode parent_node = (DefaultMutableTreeNode) nodes
081: .get(parent.getId());
082: assert (parent_node != null);
083: parent_node.add(node);
084: }
085: }
086:
087: EditorManager emgr = (EditorManager) JETARegistry
088: .lookup(EditorManager.COMPONENT_ID);
089: if (emgr != null) {
090: Collection editors = emgr.getEditors();
091: iter = editors.iterator();
092: while (iter.hasNext()) {
093: FormEditor formeditor = (FormEditor) iter.next();
094: FormComponent top = formeditor.getTopParent();
095: // todo more
096: }
097: }
098: setModel(model);
099: expandNode(root, true);
100: }
101:
102: /**
103: * Expands a given node.
104: *
105: * @param parentNode
106: * the node to expand
107: * @param bRecursive
108: * set to true if you want to expand all descendent nodes as well
109: */
110: public void expandNode(DefaultMutableTreeNode parentNode,
111: boolean bRecursive) {
112: if (parentNode != null) {
113: expandPath(new TreePath(parentNode.getPath()));
114: if (bRecursive) {
115: for (Enumeration e = parentNode.children(); e
116: .hasMoreElements();) {
117: DefaultMutableTreeNode childnode = (DefaultMutableTreeNode) e
118: .nextElement();
119: expandNode(childnode, bRecursive);
120: }
121: }
122: }
123: }
124:
125: }
|