1 |
|
---|
2 | package de.dass_it.vanhelsing.gui;
|
---|
3 |
|
---|
4 | import javax.swing.tree.DefaultTreeModel;
|
---|
5 | import javax.swing.tree.DefaultMutableTreeNode;
|
---|
6 | import de.dass_it.vanhelsing.gui.items.*;
|
---|
7 |
|
---|
8 | /*
|
---|
9 | * Helper class to encapsulate all procedure
|
---|
10 | * s to work with the tree structure.
|
---|
11 | */
|
---|
12 | public class TreeNavigation extends BeanUtil {
|
---|
13 | private DefaultTreeModel tree;
|
---|
14 |
|
---|
15 | public DefaultTreeModel createTree(DefaultMutableTreeNode root, String[] nodes){
|
---|
16 |
|
---|
17 | tree = new DefaultTreeModel(root);
|
---|
18 | for(int i = 0; i< nodes.length; i++){
|
---|
19 | addNode(root, nodes[i]);
|
---|
20 | }
|
---|
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 | return node;
|
---|
44 |
|
---|
45 | }
|
---|
46 | }
|
---|