import java.util.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class GeneralApplet extends JApplet { // ----------------------------------Instance variables------------------------------ // tree data to compute with and display GeneralTree gt = new GeneralTree(); GNode cursor = null; // applet components Container p; JTextArea picture, old; JPanel panB, pan, pan2, top; JButton add, minus, goRoot, goUp, goRight, goDown; JButton preOrd, postOrd, levOrd, ht, depth, history; JTextField readIn, printOut; String hiss = ""; boolean showHistory; // listeners: to buttons or to the mouse Ear e = new Ear(); Cat r = new Cat(); // ------------------------------ init() method ----------------------------- // set up the initial applet display and attach listeners public void init() { p = getContentPane(); p.setLayout(new BorderLayout()); // set up the central picture area picture = new JTextArea(); setHelp(); picture.setFont(new Font("Monospaced", Font.PLAIN, 14)); p.add(picture, BorderLayout.CENTER); picture.addMouseListener(r); // set the bottom button panel panB = new JPanel(); panB.setLayout(new GridLayout(2,1)); p.add(panB, BorderLayout.SOUTH); pan = new JPanel(); panB.add(pan); pan2 = new JPanel(); panB.add(pan2); pan.add(new JLabel("Adjust tree: ")); add = makeButton("+", pan); minus = makeButton("-", pan); pan.add(new JLabel("Enter data: ")); readIn = new JTextField(" "); pan.add(readIn); history = makeButton("history", pan2); pan2.add(new JLabel("To move cursor, click, or: ")); goRoot = makeButton(">root", pan2); goUp = makeButton(">parent", pan2); goRight = makeButton(">sibling", pan2); goDown = makeButton(">child", pan2); // set the top button panel top = new JPanel(); p.add(top, BorderLayout.NORTH); preOrd = makeButton("pre", top); postOrd = makeButton("post", top); levOrd = makeButton("level", top); ht = makeButton("ht", top); depth = makeButton("depth", top); printOut = new JTextField(" "); top.add(printOut); // set the history panel old = new JTextArea(); old.setFont(new Font("Monospaced", Font.PLAIN, 10)); p.add(old, BorderLayout.EAST); old.setVisible(false); showHistory = false; } // ----------------------------------- inner (listener) classes ------------------- // watch for mouse clicks to select nodes on the picture class Cat extends MouseAdapter { public void mousePressed(MouseEvent e) { int x = e.getX(); int y = e.getY(); x /= 8; y /= 20; setCursor(x, y); rePicture(); } } // listen to buttons class Ear implements ActionListener { public void actionPerformed(ActionEvent e) { Object s = e.getSource(); if (s == history) { // history button showHistory = !showHistory; old.setText(hiss); old.setVisible(showHistory); } if (s == add) { // add button String entry = readIn.getText().trim(); readIn.setText(" "); if (entry.length() == 0) return; hiss = gt.treePrint(); if (cursor == null && gt.root() == null) gt.add(entry); if (cursor != null) gt.add(cursor, entry); } if (s == goRoot|| s == goUp|| s == goRight || s == goDown) { // move cursor button try { if (s == goRoot) cursor = (GNode) gt.root(); if (s == goUp) cursor = (GNode) cursor.getParent(); if (s == goDown) { cursor = (GNode) cursor.children().next(); } if (s == goRight) { GNode p = (GNode) cursor.getParent(); Iterator sibs = p.children(); while (sibs.hasNext() && sibs.next() != cursor); cursor = (GNode) sibs.next(); } } catch (Exception curse) { cursor = null; } } if (s == minus && cursor != null) { // remove button hiss = gt.treePrint(); gt.remove(cursor); cursor = null; } if (s == ht) printOut.setText(""+gt.height()); // ht, depth button if (s == depth && cursor != null) printOut.setText(""+gt.depth(cursor)); if (s == preOrd || s == postOrd || s == levOrd) { // ordering data button Iterator v = null; if (s == preOrd) v = gt.preOrder().iterator(); if (s == postOrd) v = gt.postOrder().iterator(); if (s == levOrd) v = gt.levelOrder().iterator(); String ans = ""; while (v.hasNext()) { Object o = v.next(); if (o instanceof Node) ans += ((Node) o).getData(); } printOut.setText(ans); } rePicture(); // draw new picture } } // ------------------------------------- utility methods --------------------------- // create smart buttons public JButton makeButton(String s, JPanel panel) { JButton b = new JButton(s); panel.add(b); b.addActionListener(e); return b; } // redraw the picture, and get read for new text input public void rePicture() { picture.setText(gt.treePrint(cursor)); readIn.requestFocus(); } // figure out where the mouse was clicked void setCursor(int x, int y) { Iterator lev = gt.levelOrder().iterator(); lev.next(); Iterator flat = gt.flatOrder().iterator(); while (lev.hasNext() && y >= 0) { Object o = lev.next(); if (o.equals("\n")) { y--; } else if (y == 0) while (true) { boolean atCursor = false; Node n = (Node) flat.next(); if (n == cursor) atCursor = true; String s = (String) n.getData(); if (atCursor) s = "* " + s + " *"; x -= s.length(); if (n == o) if (x < 0) { cursor = (GNode) n; return; } else break; } } cursor = null; } // initial help screen public void setHelp() { picture.setText("The button + adds data to the tree." + " Before data is added, a string \nshould be typed in the" + " enter data field to serve as data for the new tree node." + "\n" + "One node of the tree serves as a cursor. It will be marked" + " on the display by * * " + "\nAny added data appears below the cursor (or at the root). " + "\n" + "The cursor can be moved, either with >root, >parent, >down," + " >right. It can also be \nset by clicking on a node in the view" + " area." + "\n" + "The - button removes the cursor node (and children) from the tree." + "\nThe pre, post, and level buttons print various tree traversals." + "\nThe ht button prints tree height and the depth button prints" + " cursor depth." + "\n" + "The history button gives access to a view of the last state" + " of the tree, before the \nmost recent change. The history view" + " is turned on or off by pressing the button. \nWhen it is turned on" + " the last state is recalculated and displayed."); } }