/**  RemoteImageViewerApplet Class
   * @version 1.0  18 October 2000
   * @author Lawrence Rodrigues
   **/
package com.vistech.rmi;
import java.io.*;
import java.rmi.*;
import java.rmi.registry.*;
import java.awt.event.*;
import java.awt.*;
import java.awt.image.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.tree.*;
import java.applet.*;
import java.net.*;
import com.vistech.rmi.*;

/** An RMI client applets that lists and displays images loaded
  * by an RMI server.
  * @version 1.0 12 june 2000
  * @author Lawrence Rodrigues
  **/

public class RemoteImageViewerApplet extends Applet {
   static int viewerWid = 400;
   static int viewerHt = 300;
   protected ImageLoader loader = null;
   protected JList itemList;
   protected String[] fileList;
   protected JTree tree;
   protected String curDir = ".";
   protected char separator;
   protected ImagePanel imCanvas;
 
   public void init() {
       String param = getParameter("WIDTH");
       if(param != null) viewerWid = Integer.valueOf(param).intValue();
       param = getParameter("HEIGHT");
       if(param != null) viewerHt = Integer.valueOf(param).intValue();
       initApp();
   }

   public void initApp() {
      this.setSize(viewerWid, viewerHt);
      enableEvents(AWTEvent.MOUSE_MOTION_EVENT_MASK |
                          AWTEvent.MOUSE_EVENT_MASK);
   }

   public void start(){
       activate();
       createUI();
   }
   public void createUI (){
      DefaultMutableTreeNode top = new DefaultMutableTreeNode(curDir);
      createNodes(top, curDir);
      tree = new JTree(top);
      tree.getSelectionModel().setSelectionMode
                           (TreeSelectionModel.SINGLE_TREE_SELECTION);
      tree.addTreeSelectionListener(
             new TreeSelectionListener() {
             public void valueChanged(TreeSelectionEvent e) {
                 TreePath selpath = e.getPath();
                 expandNode(selpath);
              }
         });

       JScrollPane treeView = new JScrollPane(tree);
       treeView.setPreferredSize(new Dimension(200,400));
       treeView.setBackground(Color.gray);
       imCanvas = (ImagePanel)createImageCanvas();
       setLayout(new GridLayout(1,1));
       treeView.setBorder(BorderFactory.createTitledBorder("Image Selection"));
       JSplitPane vsp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, treeView,imCanvas);
       add(vsp);
   }

   protected JPanel createImageCanvas() {
       return new ImagePanel();
   }

   private void expandNode(TreePath selpath) {
      DefaultMutableTreeNode node = (DefaultMutableTreeNode)
                                              (selpath.getLastPathComponent());
      Object[] pathobj = selpath.getPath();
      if(pathobj == null) return;
      StringBuffer path=null;
      for(int i=0;i<pathobj.length;i++){
          if(path== null) path= new StringBuffer(pathobj[0].toString());
          else path.append(pathobj[i].toString());
          if(i != pathobj.length-1) path.append(separator);
      }
      try {
         if(node.isLeaf()){
            String filename = path.toString();
            if(loader.isDirectory(path.toString())){
            curDir = path.toString();
            fileList = loader.listFiles(curDir);
            if(fileList == null) return;
            createNodes(node,pathobj[0].toString());
            tree.expandPath(selpath);
         } else
            load(filename);
         }
      } catch (Exception e){System.out.println(e);}
   }

   protected void load(String filename) throws RemoteException{
      if(filename.endsWith(".jpg")  || filename.endsWith(".jpeg") ||
         filename.endsWith(".JPG")  || filename.endsWith(".JPEG") ||
         filename.endsWith(".gif")  || filename.endsWith(".GIF")){
         byte[] buffer = loader.loadImageAsBytes(filename);
         Image img = Toolkit.getDefaultToolkit().createImage(buffer);
         imCanvas.setImage(img);
         imCanvas.repaint();
      }
   }

   private void doubleClick(int selrow, TreePath selpath) {
       if(!tree.isCollapsed(selpath)) tree.collapsePath(selpath);
       else expandNode(selpath);
   }

   private void singleClick(int selrow, TreePath selpath) {
       if(tree.isCollapsed(selpath)) expandNode(selpath);
   }

   private void createNodes(DefaultMutableTreeNode top, String dirname) {
       DefaultMutableTreeNode file = null;
       if(fileList == null)  return;
       for(int i=0; i< fileList.length;i++){
           file = new DefaultMutableTreeNode(fileList[i]);
           top.add(file);
       }
   }

   public void activate(){
      try {
        loader = (ImageLoader)Naming.lookup("rmi://"+getCodeBase().getHost()+"/SimpleImageServer");
        fileList = loader.listFiles();
        curDir = loader.getUserDir();
        separator = loader.getFileSepChar();
      }catch(Exception e){System.out.println(e); }
   }
}
