Import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.tree.*;
import java.io.*;
import java.net.*;
import java.util.*;
import java.applet.*;
/**
* <p>title:applet using swing</p>
* <p>description: Use Swing's japplet to implement tree functions. </p>
* <p>copyright:copyright (c) 2003</p>
* <p>Filename:PageTree.java</p>
* @version 1.0
*/
public class Pagetree extends JApplet implements Treeselectionlistener {
defaultmutabletreenode[] nodeat = new defaultmutabletreenode[256];
String target = null;
Hashtable links = new Hashtable ();
Appletcontext context;
public void init () {
//Get the contents of the applet
context = getAppletContext ();
//define root node
Defaultmutabletreenode root = null;
Getcontentpane (). setlayout (New BorderLayout ());
//Get parameters. The data configuration file that is defined.
String s = getparameter ("file");
if (s!= null) {
try {
//Use URL to open the data file for use on the network.
url url = new URL (getdocumentbase (), s);
bufferedreader br = new BufferedReader (
New InputStreamReader (Url.openstream (), "Jisautodetect"));
//Read the first row of data, if empty then do not perform
String line = Br.readline ();
if (line = = null) return;
root = new Defaultmutabletreenode (line);
nodeat[0] = root;
int level = 0;
while (line = Br.readline ())!= null) {
//Use "|" Separate data
StringTokenizer st = new StringTokenizer (line, "|");
String token = St.nexttoken ();
int n = countspaces (token);
//Add node name
defaultmutabletreenode node = new Defaultmutabletreenode (Token.trim ());
//COMPUTE Node Series
for (int i = level + 1; i < n; i++) {
Defaultmutabletreenode dummy = new Defaultmutabletreenode ("");
Nodeat[i].add (dummy);
nodeat[i+1] = dummy;
}
level = n;
nodeat[n].add (node);
nodeat[n+1] = node;
//To "|" The data after the symbol is processed
if (St.hasmoretokens ()) {
token = St.nexttoken ();
try {
url = new URL (Token.trim ());
links.put (node, URL);
} catch (Malformedurlexception ex) {
Ex.printstacktrace ();
}
}
}
} catch (Malformedurlexception e) {
E.printstacktrace ();
} catch (IOException e) {
E.printstacktrace ();
}
s = getparameter ("target");
if (s!= null) target = S.trim ();
JTree tree = new JTree (root);
///Add Tree Selection Monitor
Tree.addtreeselectionlistener (this);
JScrollPane jsp = new JScrollPane (tree);
Getcontentpane (). Add (JSP, Borderlayout.center);
}
}
/**
*<br> Method Description: Calculate the number of spaces
*<br> input Parameters:
*<br> return type:
*/
private int countspaces (String s) {
int n = 0;
while (S.charat (n) ==´´) {
n++;
}
return n;
}
/**
*<br> Method Description: Tree selection monitoring Implementation method
*<br> input parameter: treeselectionevent e Select tree Node event
*<br> return type:
*/
public void valuechanged (Treeselectionevent e) {
TreePath Path = E.getpath ();
Object o = path.getlastpathcomponent ();
url = (URL) links.get (o);
if (URL!= null) {
System.out.println (Links.get (o));
//Open the connection to notify the browser to load the Web page (using the browser to open the applet)
context.showdocument (URL);
}
}
}