source: vanHelsing/trunk/gui/src/de/dass_it/vanhelsing/gui/TreeNavigation.java@ 831

Last change on this file since 831 was 831, checked in by tobias, on Mar 4, 2010 at 5:43:09 PM

added support for generic node types

File size: 1.4 KB
Line 
1
2package de.dass_it.vanhelsing.gui;
3
4import javax.swing.tree.DefaultTreeModel;
5import javax.swing.tree.DefaultMutableTreeNode;
6import de.dass_it.vanhelsing.gui.items.*;
7
8/*
9 * Helper class to encapsulate all procedure
10 * s to work with the tree structure.
11 */
12public 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}
Note: See TracBrowser for help on using the repository browser.