001: /*
002: * Jacareto Copyright (c) 2002-2005
003: * Applied Computer Science Research Group, Darmstadt University of
004: * Technology, Institute of Mathematics & Computer Science,
005: * Ludwigsburg University of Education, and Computer Based
006: * Learning Research Group, Aachen University. All rights reserved.
007: *
008: * Jacareto is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU General Public
010: * License as published by the Free Software Foundation; either
011: * version 2 of the License, or (at your option) any later version.
012: *
013: * Jacareto is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
016: * General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public
019: * License along with Jacareto; if not, write to the Free
020: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
021: *
022: */
023:
024: package jacareto.cleverphl.gui;
025:
026: import jacareto.comp.ComponentHandler;
027: import jacareto.comp.Components;
028: import jacareto.system.Environment;
029: import jacareto.system.Language;
030:
031: import org.apache.log4j.Logger;
032:
033: import java.awt.Component;
034:
035: import java.util.Enumeration;
036: import java.util.Hashtable;
037:
038: import javax.swing.tree.DefaultMutableTreeNode;
039: import javax.swing.tree.DefaultTreeModel;
040: import javax.swing.tree.MutableTreeNode;
041: import javax.swing.tree.TreeModel;
042:
043: /**
044: * This class creates a TreeModel for JTrees from a given components instance.
045: *
046: * @author Thomas Leonhard
047: * @version 1.0
048: */
049: public class ComponentTree {
050: /** The tree model of all components. */
051: private DefaultTreeModel treeModel;
052:
053: /** The root node of the tree model. */
054: private DefaultMutableTreeNode treeModelRootNode;
055:
056: /** Maps components to nodes. */
057: private Hashtable componentToNode;
058:
059: /** The logger. */
060: private Logger logger;
061:
062: /** The language instance. */
063: private Language language;
064:
065: /** The environment */
066: protected Environment environment;
067:
068: /** The components manager */
069: protected Components componentsManager;
070:
071: /**
072: * Creates a new Components instance. The component handlers are loaded by the given class
073: * loader.
074: *
075: * @param componentsManager the environment.
076: */
077: public ComponentTree(Components componentsManager) {
078: this .environment = componentsManager.getEnvironment();
079: this .componentsManager = componentsManager;
080: this .logger = environment.getLogger();
081: this .language = environment.getLanguage();
082: }
083:
084: /**
085: * Creates the {@link TreeModel} for this ComponentsTree.
086: *
087: * @return {@link TreeModel}
088: */
089: public TreeModel createTreeModel() {
090: componentToNode = new Hashtable();
091: treeModelRootNode = new DefaultMutableTreeNode(environment
092: .getLanguage().getString("Components.TreeNodeName"));
093: treeModel = new DefaultTreeModel(treeModelRootNode);
094:
095: Enumeration componentEnumertation = componentsManager
096: .includedRoots();
097:
098: while (componentEnumertation.hasMoreElements() == true) {
099: Component component = (Component) componentEnumertation
100: .nextElement();
101:
102: MutableTreeNode treeNode = createNode(component);
103: treeModelRootNode.add(treeNode);
104: }
105:
106: return treeModel;
107: }
108:
109: /**
110: * Creates a {@link MutableTreeNode} including the given {@link Component}.
111: *
112: * @param component The {@link Component} which should be included in the TreeNode
113: *
114: * @return {@link MutableTreeNode}
115: */
116: public MutableTreeNode createNode(Component component) {
117: String name = componentsManager.getName(component);
118: logger.debug(language
119: .getString("CleverPHL.ComponentTree.Msg.CreatingNode")
120: + " ["
121: + name
122: + "] ["
123: + component.getClass().getName()
124: + "]");
125:
126: String indicatorString = componentsManager
127: .getIndicatorString(component);
128: if (indicatorString != null) {
129: indicatorString = " [ \"" + indicatorString + "\" ]";
130: } else {
131: indicatorString = "";
132: }
133:
134: ComponentHandler compHandler = componentsManager
135: .getFirstComponentHandler(component);
136:
137: DefaultMutableTreeNode result = null;
138:
139: if ((component != null) && (name != null)) {
140: int lastDotPos = name.lastIndexOf(".");
141:
142: if (lastDotPos == -1) {
143: result = new DefaultMutableTreeNode(name
144: + indicatorString);
145: } else {
146: result = new DefaultMutableTreeNode(name
147: .substring(lastDotPos + 1)
148: + indicatorString);
149: }
150:
151: Component[] compChildren = compHandler
152: .getChildren(component);
153: int counter = 0;
154:
155: if ((compChildren != null) && (compChildren.length > 0)) {
156: MutableTreeNode childTreeNode;
157:
158: for (int i = 0; i < compChildren.length; i++) {
159: Component child = compChildren[i];
160: childTreeNode = createNode(child);
161:
162: if (childTreeNode != null) {
163: result.add(childTreeNode);
164: }
165:
166: counter++;
167: }
168: }
169: } else {
170: logger.warn(language
171: .getString("Components.Error.NoHandler")
172: + ": " + component.getClass().getName());
173: }
174:
175: if (result != null) {
176: componentToNode.put(component, result);
177: }
178:
179: return result;
180: }
181:
182: /**
183: * Returns the tree node for a given component.
184: *
185: * @param component the component
186: *
187: * @return the tree node of the component
188: */
189: public DefaultMutableTreeNode getNode(Component component) {
190: if (componentToNode.containsKey(component)) {
191: return (DefaultMutableTreeNode) componentToNode
192: .get(component);
193: }
194:
195: return null;
196: }
197: }
|