Nada

Lab assignment 4 - Tree of life

In the course litterature you will find a program that shows a tree of a file structure with the help of the most advanced swing component, JTree. The code is to be found here: DirTree.java. Compile and run it with:

java DirTree

and it will show you the tree of the directory you are in. You could also, for example, try:

java DirTree ~henrik/labbar

and it will show you the tree of the directory stated in the argument. Check that you can click and expand directories and that you could get details about a file if Show details is checked.

A minimal Tree of life

Your task is to modify the program so that it shows a completely different tree, namely the Tree of life. The root of the tree should be Life and the root should have three children, Plants, Animals and Fungus, which in turn contains classes, orders, families and species. Start with some minor modifications:

A recursive Tree of life

Instead of creating the nodes by hand you should read the Tree of life from the file Liv.xml (also available as Liv.txt). A small test version the the file looks like this:
<Biosfär namn="Liv"> är allt som fortplantar sej 
<Rike namn="Växter"> kan inte förflytta sej 
</Rike> 
<Rike namn="Djur"> kan förflytta sej 
</Rike>
<Rike namn="Svampar"> är varken djur eller växter 
</Rike>
</Biosfär>
Apart from the name of the node there is also a level (i.e. Rike) and details but, unfortunately, there is nowhere to store this information in the class DefaultMutableTreeNode. You should therefore create a subclass Node which contains the String variables level and text. Now, all that is needed is a recursive method that should be called with:
root = readNode(); //Reads the XML-file and created the whole tree
Before the method is called the program should have "eaten" the first angle bracket from the file so that readNode, after creating an empty node, can start reading the start tag and setting the level and attribute with node.setUserObject(...), "eat" the ending angle bracket, call readBetween(node) that reads the details and adds any possible child nodes. Lifetree.gif When ending tag is reached it should be read and compared with the starting tag and eventual RuntimeException should be thrown, but normally it should return the newly created node. The method is described in lecture 7.

Just as the original program DirTree, your program should be able to take a file name as an argument:

java LifeTree life-file

and if no file name is given, the program should use the file Liv.xml.

Details about life

In the original program you could get information about a file by checking "Show details". Your program should instead show the deatils, for example Art: Mås gillar Vaxholmsbåtar. Alter the code so that showDetails(TreePath p) prints this text in its JOptionPane.

Extra assignment: Alter the code so that it prints out the whole chain on the form but all that is Mås is Fåglar is Djur is Liv. Usefull method in TreePath is getLastPathComponent() and in DefaultMutableTreeNode, and thus in Node, the method getParent().



A tree full of life by (student) ........................................................................

appreciated (teacher) ............................................. on (date) ........................


Published by: <karlm@nada.kth.se>
Updated 2008-04-15
Technical support: <webmaster@nada.kth.se>