The Artima Developer Community
Sponsored Link

Java Answers Forum
Using DefaultMutableTreeNode class

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Melvin

Posts: 6
Nickname: melvin
Registered: Jan, 2003

Using DefaultMutableTreeNode class Posted: Jan 18, 2003 12:08 AM
Reply to this message Reply
Advertisement
Hi,
I am doing a project in which I have to "scan" through a file and retrieved the class names and variable names and put those names into a tree structure.

For example, given a file (data.txt):

(class Man
(attribute Name )
(attribute Address)
) //file ends here



The resultant tree produced will be as followed:

structure
|_ class man
|_ attribute name
|_ attribute address

I am able to form the tree for the above simple example and the codes are as follows:

import java.util.regex.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.tree.*;
import java.io.*;

/* <applet code="RegEx14" width=400 height=300>
</applet>
*/

public class RegEx14 extends JApplet{

JTree tree;
JTextField jtf;
private BufferedReader in;
Matcher l;
String line;
String str2;
String str1;

DefaultMutableTreeNode temp;

public void init() {

Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());

Pattern strMatch = Pattern.compile("class[ \\t]+([_$\\w]+)");
Pattern VariableMatch =Pattern.compile("attribute\\s([_$\\w]+)");

try {
in = new BufferedReader(new FileReader("hello.h"));
} catch(FileNotFoundException e){
e.printStackTrace();
}

DefaultMutableTreeNode top = new DefaultMutableTreeNode("structure");
DefaultMutableTreeNode A = new DefaultMutableTreeNode();
DefaultMutableTreeNode B = new DefaultMutableTreeNode();

try{
while((line = in.readLine())!=null) {
l = strMatch.matcher( line );

if(l.find()) {
str1 = l.group();
A.setUserObject(str1);
top.add(A);
}

l = strMatch.matcher( line );
l=VariableMatch.matcher(line);

if(l.find()) {
str2 = l.group();
temp = new DefaultMutableTreeNode (str2);
A.add(temp);
}
}
} catch(IOException e){
e.printStackTrace();
}

tree = new JTree(top);
int v = ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
int h = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;
JScrollPane jsp = new JScrollPane(tree,v,h);
contentPane.add(jsp, BorderLayout.CENTER);
jtf = new JTextField("",20);
contentPane.add(jtf, BorderLayout.SOUTH);

tree.addMouseListener(new MouseAdapter() {

public void mouseClicked(MouseEvent me) {
doMouseClicked (me);
}
});
}


void doMouseClicked(MouseEvent me) {
TreePath tp = tree.getPathForLocation(me.getX(), me.getY());

if (tp != null)
jtf.setText(tp.toString());

else
jtf.setText("");
}
}

So my question is how can I alter my codes so that when there are more than one class defnitions in a file, a tree structure similar to the one given below can be built?

File:
(class Man
(attribute Name )
(attribute Address)
)

(class Machine
(attribute ID )
(attribute Parts)
) //File ends here

Resultant tree produced should be :

structure
|_ class Man
| |_ attribute Name
| |_ attribute Address
|
|_ class Machine
|_ attribute ID
|_ attribute Parts

Can somebody please help me. Thank you.

Topic: Stopping Threads Previous Topic   Next Topic Topic: Why won't my array work?

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use