/** * Page.java 1.00 96/10/07 Merlin Hughes * * Copyright (c) 1996 Prominence Dot Com, Inc. All Rights Reserved. * * Permission to use, copy, modify, and distribute this software * for non-commercial purposes and without fee is hereby granted * provided that this copyright notice appears in all copies. * * http://prominence.com/ merlin@prominence.com */ import java.awt.*; import java.util.*; import java.net.*; public class Page extends Canvas { Sextant parent; Scrollbar bar; Image doc, dir, open; Element root, selected; Stack tree = new Stack (); int treeDepth = 0; public Page (Scrollbar bar) { this.bar = bar; } public void init (Sextant parent) { this.parent = parent; doc = parent.getImage (parent.getDocumentBase (), parent.getParameter ("doc", "images/doc.gif")); prepareImage (doc, this); dir = parent.getImage (parent.getDocumentBase (), parent.getParameter ("dir", "images/dir.gif")); prepareImage (dir, this); open = parent.getImage (parent.getDocumentBase (), parent.getParameter ("open", "images/openDir.gif")); prepareImage (open, this); } int docW, docH, dirW, dirH, openW, openH, textH, textA; void getDimensions () { docW = doc.getWidth (this); docH = doc.getHeight (this); dirW = dir.getWidth (this); dirH = dir.getHeight (this); openW = open.getWidth (this); openH = open.getHeight (this); textH = getFontMetrics (getFont ()).getHeight (); textA = getFontMetrics (getFont ()).getAscent (); } public void layout () { if ((size ().height > 0) && (root != null)) { getDimensions (); int h = root.getHeight () + 3; bar.setValues (bar.getValue (), size ().height, 0, h - size ().height); bar.setLineIncrement (textH); bar.setPageIncrement (size ().height); } } public void addLine (int depth, URL url, String name, String comment) { Element element = new Element (url, name, comment, this); if (root == null) { if (depth != 0) throw new RuntimeException ("Initial depth non-zero."); else tree.push (root = element); } else { if (depth == treeDepth) { tree.push (((Element) tree.pop ()).sibling = element); } else if (depth > treeDepth) { if (depth > treeDepth + 1) throw new RuntimeException ("Depth change error: " + name); else tree.push (((Element) tree.peek ()).offspring = element); } else if (depth < treeDepth) { for (int i = depth; i < treeDepth; ++ i) tree.pop (); tree.push (((Element) tree.pop ()).sibling = element); } treeDepth = depth; } } public URL currentURL () { if (selected == null) return null; else return selected.url; } public void paint (Graphics g) { if (root != null) { getDimensions (); root.paint (g, -1, -bar.getValue (), new Point (10, 2 - bar.getValue ())); } } public boolean mouseDown (Event ev, int x, int y) { if (root != null) { boolean doRepaint = false; getDimensions (); Point xy = new Point (10, 2 - bar.getValue ()); Element l = root.locate (x, y, xy); if ((l != null) && (l.offspring != null) && (x < xy.x + openW)) { l.open = !l.open; layout (); doRepaint = true; } if (selected != l) { if (selected != null) selected.selected = false; selected = l; if (selected != null) selected.selected = true; doRepaint = true; postEvent (new Event (this, Event.LIST_SELECT, (selected != null) ? selected.comment : "")); } else if ((selected != null) && (ev.clickCount == 2) && ((selected.offspring == null) || (x >= xy.x + openW))) { postEvent (new Event (this, Event.ACTION_EVENT, l.url)); } if (doRepaint) repaint (); } return super.mouseDown (ev, x, y); } }