1 | package de.dass_it.vanhelsing.gui;
|
---|
2 |
|
---|
3 | //import org.apache.log4j.Logger;
|
---|
4 | import javax.swing.tree.DefaultTreeModel;
|
---|
5 | import javax.swing.tree.DefaultMutableTreeNode;
|
---|
6 | import de.dass_it.vanhelsing.gui.items.*;
|
---|
7 |
|
---|
8 |
|
---|
9 | /*
|
---|
10 | * Helper class to encapsulate all procedure
|
---|
11 | * s to work with the tree structure.
|
---|
12 | */
|
---|
13 | public class TreeNavigation extends BeanUtil {
|
---|
14 | private DefaultTreeModel tree;
|
---|
15 | //private Logger logger = Logger.getLogger(this.getClass());
|
---|
16 | public DefaultTreeModel createTree(DefaultMutableTreeNode root, String[] nodes){
|
---|
17 |
|
---|
18 | tree = new DefaultTreeModel(root);
|
---|
19 | for(int i = 0; i< nodes.length; i++){
|
---|
20 | addNode(root, nodes[i]);
|
---|
21 | }
|
---|
22 |
|
---|
23 | return tree;
|
---|
24 | }
|
---|
25 |
|
---|
26 | public DefaultMutableTreeNode addNode(DefaultMutableTreeNode parent, String type){
|
---|
27 | DefaultMutableTreeNode node = new DefaultMutableTreeNode();
|
---|
28 | node.setParent(parent);
|
---|
29 | UserObjectItemType userObject = new UserObjectItemType(node);
|
---|
30 | type = "de.dass_it.vanhelsing.gui.items." + type + "Item";
|
---|
31 | try {
|
---|
32 | Class myClass = Class.forName(type);
|
---|
33 | UserObjectItem item = (UserObjectItem) myClass.newInstance();
|
---|
34 | userObject.setUserObjectItem(item);
|
---|
35 |
|
---|
36 | } catch (ClassNotFoundException e){
|
---|
37 | System.err.println("Itemclass "+ type +" must be in the classpath");
|
---|
38 | } catch (InstantiationException e){
|
---|
39 | System.err.println("Class"+type+"must be a concrete class, not an interface or an abstract class");
|
---|
40 | } catch (IllegalAccessException e){
|
---|
41 | System.err.println("Class" + type + " lacks a no-argument constructor");
|
---|
42 | }
|
---|
43 |
|
---|
44 | return node;
|
---|
45 | }
|
---|
46 | }
|
---|