/* JPreview.java: a Swing browser/previewer for HTML files
 *
 * Copyright (c) 1998 The Regents of the University of California.
 *       All Rights Reserved.  See the COPYRIGHT file for details.
 */

package tutorial.tcltk98;

import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import com.sun.java.swing.*;
import com.sun.java.swing.event.*;
import com.sun.java.swing.tree.*;

public class JPreview {
  TreeNode root;
  JFrame window;
  JTree tree;
  JScrollPane treepane;
  JEditorPane view;
  JScrollPane viewpane;
  JSplitPane splitpane;

  public JPreview () {
    // Create a root node and a tree
    String home = "/users/johnr";   // FIXME
    root = new DefaultMutableTreeNode(home,true);
    tree = new JTree(root,true);
    treepane = new JScrollPane(tree);
    treepane.setMinimumSize(new Dimension(50,100));

    // Create an editor pane
    view = new JEditorPane();
    viewpane = new JScrollPane(view);
    viewpane.setMinimumSize(new Dimension(200, 100));

    // Create a split pane and place the two components side by side
    splitpane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,treepane,viewpane);
    splitpane.setDividerLocation(200);
    splitpane.setContinuousLayout(true);

    // Put the splitpane into a top-level frame
    window = new JFrame("Preview!");
    window.setSize(600,400);
    window.getContentPane().add(splitpane);

    // Display the window
    window.show();

    // Handle tree selection and expansion events
    tree.addTreeSelectionListener(new PreviewSelectionListener());
    tree.addTreeExpansionListener(new PreviewExpansionListener());

    // Load the root directory into the tree
    loadDirectory(new TreePath(root));
  }

  // # Load a directory into the tree
  void loadDirectory (TreePath path) {
    // Get the node and file path
    DefaultMutableTreeNode node = (DefaultMutableTreeNode)
      path.getLastPathComponent();
    String filepath = getFilePath(path);
    java.io.File file = new java.io.File(filepath);

    // Generate the child nodes if there aren't any
    if ( node.getChildCount() == 0 ) {
      FilenameFilter dirfilter = new FilenameFilter () {
	public boolean accept (File dir, String name) {
	  return (new File(dir,name)).isDirectory();
	}
      };
      String dirs[] = file.list(dirfilter);

      FilenameFilter htmlfilter = new FilenameFilter () {
	public boolean accept (File dir, String name) {
	  return name.endsWith(".html") || name.endsWith(".htm");
	}
      };
      String files[] = file.list(htmlfilter);

      // Add the nodes to this node in the model
      int i = 0;
      DefaultMutableTreeNode f;
      DefaultTreeModel model = (DefaultTreeModel)tree.getModel();
      for (int j = 0; j < dirs.length; j++, i++) {
	f = new DefaultMutableTreeNode(dirs[j], true);
	model.insertNodeInto(f, node, i);
      }
      for (int j = 0; j < files.length; j++, i++) {
	f = new DefaultMutableTreeNode(files[j], false);
	model.insertNodeInto(f, node, i);
      }
    }
  }
  // Given a path, get the file path
  String getFilePath (TreePath path) {
    File file = new File((String)((DefaultMutableTreeNode)path.getPathComponent(0)).getUserObject());
    int i = 1;
    while (i < path.getPathCount()) {
      file = new File(file, (String)((DefaultMutableTreeNode)path.getPathComponent(i)).getUserObject());
      i++;
    }
    return file.toString();
  }

  public static void main (String argv[]) {
     JPreview myself = new JPreview();
  }

  // Handle selection
  class PreviewSelectionListener implements TreeSelectionListener {
    public void valueChanged (TreeSelectionEvent e) {
      String filepath = getFilePath(e.getPath());
      System.out.println(filepath);
      if (filepath.endsWith(".html") || filepath.endsWith(".htm")) {
	try {
	  view.setPage("file:" + filepath);
	}
	catch (Exception x) {}
      }
    }
  }
 
  // Handle expansion and compression
  class PreviewExpansionListener implements TreeExpansionListener {
    public void treeExpanded (TreeExpansionEvent e) {
      loadDirectory(e.getPath());
    }
    public void treeCollapsed (TreeExpansionEvent e) {
      // System.out.println("Collapsed " + e.getPath());
    }
  }
}


