/** Title: SiteMap Applet Version: 1.1 Copyright: Copyright (c) 1998 Author: Hubert A. Klein Ikkink Company: DrBob42.com Description: Show hierarchical view of web site */ package com.drbob42.jbuilder.hubert.sitemap; import java.awt.*; import java.awt.event.*; import java.applet.*; import com.sun.java.swing.*; import java.beans.*; import java.util.*; import java.net.*; import java.io.*; import borland.jbcl.control.*; import borland.jbcl.model.*; import com.sun.java.swing.UIManager; public class SiteMapApplet extends JApplet { private Vector links; boolean isStandalone = false; String pmTreeStyle; BorderLayout borderLayout1; TreeControl treeControl1; //Get a parameter value public String getParameter(String key, String def) { return isStandalone ? System.getProperty(key, def) : (getParameter(key) != null ? getParameter(key) : def); } //Construct the applet public SiteMapApplet() { } //Initialize the applet public void init() { try { pmTreeStyle = this.getParameter("treeStyle", "arrows"); } catch (Exception e) { e.printStackTrace(); } try { jbInit(); } catch (Exception e) { e.printStackTrace(); } loadTree(); buildTree(); treeControl1.setExpandByDefault(true); treeControl1.setSubfocus(treeControl1.getRoot()); treeControl1.setEditInPlace(false); if (pmTreeStyle.equalsIgnoreCase("arrows")) treeControl1.setStyle(TreeControl.STYLE_ARROWS); else treeControl1.setStyle(TreeControl.STYLE_PLUSES); } static { try { UIManager.setLookAndFeel(new com.sun.java.swing.plaf.metal.MetalLookAndFeel()); //UIManager.setLookAndFeel(new com.sun.java.swing.plaf.motif.MotifLookAndFeel()); //UIManager.setLookAndFeel(new com.sun.java.swing.plaf.windows.WindowsLookAndFeel()); } catch (Exception e) {} } //Component initialization private void jbInit() throws Exception { borderLayout1 = (BorderLayout) Beans.instantiate(getClass().getClassLoader(), BorderLayout.class.getName()); treeControl1 = (TreeControl) Beans.instantiate(getClass().getClassLoader(), TreeControl.class.getName()); this.getContentPane().setLayout(borderLayout1); this.setSize(400,300); treeControl1.setOpaque(false); treeControl1.addActionListener(new SiteMapApplet_treeControl1_actionAdapter(this)); this.getContentPane().add(treeControl1, BorderLayout.CENTER); } //Load tree data private void loadTree() { // Load file from netwerk // String toc = null; try { URL tocURL = new URL(this.getDocumentBase(), "toc.txt"); toc = readTOCFile(tocURL); } catch (MalformedURLException mfuex) { System.out.println("MalformedURLException in TOC file: " + mfuex); } // Parse resulting string // if (toc != null) { links = new Vector(5); StringTokenizer stok = new StringTokenizer(toc, "\r\n"); while ( stok.hasMoreElements() ) { String tmp = (String) stok.nextElement(); StringTokenizer node = new StringTokenizer(tmp, "|"); String depth = (String) node.nextElement(); String title = (String) node.nextElement(); String url = (String) node.nextElement(); LinkInfo link = new LinkInfo(Integer.parseInt(depth), title, url); links.addElement(link); } } } private String readTOCFile(URL url) { StringBuffer result = new StringBuffer(); try { BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream())); String line; while ( (line = in.readLine()) != null ) { result.append(line); result.append("\r\n"); } in.close(); } catch (IOException ioex) { System.out.println("IOException : " + ioex); } return result.toString(); } // Build tree private void buildTree() { LinkInfo link = null; int depth = 0; int lastDepth = 0; Hashtable roots = new Hashtable(5); for (Enumeration e=links.elements(); e.hasMoreElements(); ) { link = (LinkInfo) e.nextElement(); depth = link.getDepth(); if (depth == 1) { // This is the root node GraphLocation root = treeControl1.setRoot(link.getTitle()); roots.put(new Integer(depth), root); lastDepth = depth; } else if (depth > lastDepth) { // Next child GraphLocation node = treeControl1.addChild( (GraphLocation) roots.get(new Integer(lastDepth)), link.getTitle()); roots.put(new Integer(depth), node); lastDepth = depth; } else if (depth == lastDepth) { // Sibling GraphLocation node = treeControl1.addChild( (GraphLocation) roots.get(new Integer(lastDepth-1)), link.getTitle()); roots.remove(new Integer(depth)); roots.put(new Integer(depth), node); } else if (depth < lastDepth) { // Child to previous root GraphLocation node = treeControl1.addChild( (GraphLocation) roots.get(new Integer(depth-1)), link.getTitle()); roots.remove(new Integer(depth)); roots.put(new Integer(depth), node); lastDepth = depth; } } } //Get Applet information public String getAppletInfo() { return "SiteMapApplet - Version 1.1 - Author: Hubert A. Klein Ikkink\r\n" + "Build with Borland JBuilder 2.0\r\n" + "Shows a hierarchy of pages on a website."; } //Get parameter info public String[][] getParameterInfo() { String pinfo[][] = { {"treeStyle", "String", "Style of treeview"}, }; return pinfo; } void treeControl1_actionPerformed(ActionEvent e) { GraphLocation[] selections = treeControl1.getSelection().getAll(); String title = (String) treeControl1.get(selections[0]); String url = null; LinkInfo tmp; for (Enumeration enum=links.elements(); enum.hasMoreElements(); ) { tmp = (LinkInfo)enum.nextElement(); if (tmp.getTitle().equalsIgnoreCase(title)) { url = tmp.getURL(); break; } } try { getAppletContext().showDocument(new URL(url)); } catch (MalformedURLException mfuex) {} } class LinkInfo { // Properties // private String url; private String title; private int depth; // Constructors // public LinkInfo() { this(0, null, null); } public LinkInfo(int depth, String title, String url) { this.depth = depth; this.title = title; this.url = url; } // Set methods for properties // public void setURL(String u) { url = u; } public void setTitle(String s) { title = s; } public void setDepth(int i) { depth = i; } // Get methods for properties // public String getURL() { return url; } public String getTitle() { return title; } public int getDepth() { return depth; } } //LinkInfo } class SiteMapApplet_treeControl1_actionAdapter implements java.awt.event.ActionListener { SiteMapApplet adaptee; SiteMapApplet_treeControl1_actionAdapter(SiteMapApplet adaptee) { this.adaptee = adaptee; } public void actionPerformed(ActionEvent e) { adaptee.treeControl1_actionPerformed(e); } }