Plese help...

P. 786 Chap 21 Wiley book "Java2 & JS for C/C++
Programmers" has a notable example which does not compile or run. I've enclosed files 
in question 
but all Chapter code may be obtained from URL:
http://www.cis.udel.edu/~vagrawal/367/examples/jwiley/chp21/

Consider Jtree example "TocFrame.java" which
constructs a tree from a text file. To run (once is
compiled) we would execute: "java TocFrame book.toc".  Instead, this creates following 
javac error:
-----------------------------------------
TocFrame.java:33: cannot resolve symbol
symbol  : class TocTreePanel
location: class jwiley.chp21.TocFrame
        TocTreePanel tp = null;
        ^
TocFrame.java:36: cannot resolve symbol
symbol  : class TocTreePanel
location: class jwiley.chp21.TocFrame
            tp = new TocTreePanel(sTocFile);
-------------------------------------------

However, the erroring class in question "TocTreePanel"
compiles to completion.  How would I go about
fixing & getting example to run.  My env & all
seems fine as I compile & run other examples using 
JDK1.3.  I've enclosed both files ( although
these and all chapt files may be found at
above URL).  Otherwise, woud anyone have a 
differing jdbc/jtree or jtree/text-file example which actually works similar to this?

TIA
------------------------------------------
code for 2 files plus data file is next...
TocTreePanel.java compiles fine...
------------------------------------------

/** TocTreePanel.java */
package jwiley.chp21;

import javax.swing.*;
import javax.swing.tree.*;
import javax.swing.event.*;

import java.awt.*;
import java.awt.event.*;
import java.io.*;

import java.util.Vector;

/**
 * Class that generates a Tree from a heirarchically formatted text file. *
@author Michael C. Daconta
 */
public class TocTreePanel extends JPanel implements TreeSelectionListener
{
    /** Debug variable. */
    public static boolean debug;

    /** Tree to generate. */
    DefaultMutableTreeNode dynamicTree;

    /** Graphical Tree Outline. */
    JTree jt;

    /** List Selection Model. */
    TreeSelectionModel tsm;

    /** Tree Model. */
    TreeModel jtm;

    /** Accessor. */
    public DefaultMutableTreeNode getDynamicTree() { return dynamicTree; }

    /** utility method to determine what level this node is at by
        counting white space characters.  The simplest way to format
        the text file is to use one tab per level. */
    private int countWhiteSpace(String sBuf)
    {
        for (int i=0; i < sBuf.length(); i++)
            if (sBuf.charAt(i) != '\t' &&
                sBuf.charAt(i) != ' ')
                return i;
        return -1; // error
    }
    /** Utility inner class to store both the position and a
        reference to the DefaultMutableTreeNode at that position. */
    class LevelNode
    {
        /** position of node. */
        public int iPosition;
        /** Tree node at that position. */
        public DefaultMutableTreeNode node;

        /** constructor. */
        LevelNode(int iPos, DefaultMutableTreeNode t)
        { iPosition = iPos; node = t; }
    }

    /** Utility method to find the parent of the current node.
        The algorithm is to start with the end of the Vector and
        find out where this node "fits".  A fit is when this
        nodes position is greater than the previous one. */
    private DefaultMutableTreeNode findParent(Vector levels, int nodePos)
    {
        int iLast = levels.size();
        // start from the bottom
        for (int i = (iLast - 1); i >= 0; i--)
        {
            LevelNode ln = (LevelNode) levels.elementAt(i);
            if (nodePos > ln.iPosition)
                return ln.node;
        }
        return null; // error
    }

    /** Utility method to parse a "Toc" file.  File where each new
        level is indicated by indenting that word.  One word per line. */
    private DefaultMutableTreeNode parseTableOfContents(FileInputStream fis)
throws IOException
    {
        DefaultMutableTreeNode outNode = null;
        int iCurLevel = 0;
        Vector vLevels = new Vector();
        BufferedReader br = new BufferedReader(
                            new InputStreamReader(fis));
        String sLine = null;
        while ( (sLine = br.readLine()) != null)
        {
            if (debug) System.out.println("Line: " + sLine);

            // get position of token
            int pos = countWhiteSpace(sLine);
            DefaultMutableTreeNode aNode = new
DefaultMutableTreeNode(sLine.trim());
            vLevels.addElement(new LevelNode(pos, aNode));

            if (outNode == null)
            {
                outNode = aNode;
            }
            else
            {
                // find who to add this node to
                DefaultMutableTreeNode parent = findParent(vLevels, pos);
                if (parent != null)
                {
                    parent.add(aNode);
                }
            }
        }

        return outNode;
    }

    /** Constructor. */
    public TocTreePanel(String sTocFile) throws IOException
    {
        setLayout(new BorderLayout());

        // check if file exists
        File f = new File(sTocFile);
        if (f.exists())
        {
            FileInputStream fis = new FileInputStream(sTocFile);
            dynamicTree = parseTableOfContents(fis);
            fis.close();
        }
        else
            throw new IOException(sTocFile + " does not exist.");

        if (dynamicTree != null)
        {
            // create a JTree
            jt = new JTree(dynamicTree);

            // get the Tree Model
            jtm = jt.getModel();

            // listen to the selections
            tsm = jt.getSelectionModel();
            tsm.setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
            tsm.addTreeSelectionListener(this);

            // create a scrollPane
                JScrollPane scrollpane = new JScrollPane();
                scrollpane.getViewport().add(jt);

                add("Center", scrollpane);
        }
        else
            throw new IOException(sTocFile + " is empty.");
    }

    public void valueChanged(TreeSelectionEvent tse)
    {
        // get the path selected
        TreePath tp = tse.getPath();
        // get the leaf node of the path
        Object o = tp.getLastPathComponent();
        // for now, just print out the leaf node (a String)
        System.out.println(o);
    }

    /** Main method for unit testing. */
    public static void main(String args[])
    {
        if (args.length < 1)
        {
            System.out.println("USAGE: java TocTreePanel tocFile");
            System.exit(1);
        }

        TocTreePanel.debug = true;
        try
        {
            TocTreePanel tp = new TocTreePanel(args[0]);
        } catch (IOException ioe)
          {
            ioe.printStackTrace();
          }
        System.exit(0);
    }
}
---------------------------------------------------
source for TocFrame.java
---------------------------------------------------

D:\Java1.2Exp\jwiley\chp21>type TocFrame.java
/** TocFrame.java */
package jwiley.chp21;

import javax.swing.*;
import javax.swing.tree.*;

import java.awt.*;
import java.awt.event.*;
import java.io.IOException;

/**
 * Simple Frame to demonstrate a dynamic use for a
 * JTree.
 * @author Michael C. daconta
 * @see TocTreePanel
 */
public class TocFrame extends Frame
{
    /** Simple adapter class to shutdown the application
        when the close button is pushed. */
    class ShutdownAdapter extends WindowAdapter
    {
        public void windowClosing(WindowEvent we)
        { System.exit(0); }
    }

    /** Constructor.
     * @param sTocFile Name of ".toc" file.
     */
    public TocFrame(String sTocFile)
    {
        super(sTocFile);
        TocTreePanel tp = null;
        try
        {
            tp = new TocTreePanel(sTocFile);
            add("Center", tp);
        } catch(IOException ioe)
          { }

        addWindowListener(new ShutdownAdapter());

        setSize(200,200);
        setLocation(50,50);
        setVisible(true);
    }

    /** Main() method to invoke from the JVM. */
    public static void main(String args[])
    {
        if (args.length < 1)
        {
            System.out.println("USAGE: java TocFrame tocFile");
            System.exit(1);
        }

        new TocFrame(args[0]);
    }
}
-------------------------------------------------
The book.toc datafile
-------------------------------------------------
D:\Java1.2Exp\jwiley\chp21>type book.toc
Java and JavaScript for C/C++ Programmers
        Part I Java Language
                Chapter 1 Introduction to Java
                Chapter 2 Comparing Java to ANSI C
                Chapter 3 Comparing Java to C++
                Chapter 4 Language Features Not Shared
                Chapter 5 Connect to Legacy Code
        Part II Java Packages
                Chapter 6 lang
                Chapter 7 util
                Chapter 8 io
                Chapter 9 net
                Chapter 10 awt
                Chapter 11 beans
                Chapter 12 applet
                Chapter 13 idl
                Chapter 14 java2D
                Chapter 15 math
                Chapter 16 rmi
                Chapter 17 security
                Chapter 18 sql
                Chapter 19 text
                Chapter 20 Servlet
                Chapter 21 JavaMedia
                Chapter 22 MS-COM & DCOM
                Chapter 23 Java Foundation Classes
        Part III JavaScript
                Chapter 24 Comparing JavaScript to Java
                Chapter 25 Calling Java from JavaScript
        Part IV Appendices
                Appendix A Coding Conventions
                Appendix B References




_______________________________________________
Swing mailing list
[EMAIL PROTECTED]
http://eos.dk/mailman/listinfo/swing

Reply via email to