/**  AWTImageViewer Class
   * @version 1.0  12 June 2000
   * @author Lawrence Rodrigues
   **/
package app;
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 com.vistech.rmi.*;

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

public class AWTImageViewer extends JFrame{
   protected ImageLoader loader = null;
   protected JList itemList;
   protected String[] fileList;
   protected JTree tree;
   protected String curDir = ".";
   protected char separator;
   protected ImagePanel imCanvas;

   public AWTImageViewer(){
      setTitle("Remote Image Viewer");
      activate();
      createUI();
   }

   public void createUI (){
      System.out.println("create UI");
      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();
       getContentPane().setLayout(new GridLayout(1,1));
       treeView.setBorder(BorderFactory.createTitledBorder("Image Selection"));
       JSplitPane vsp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, treeView,imCanvas);
       getContentPane().add(vsp);
       pack();
       setSize(600,400);
       show();
   }

   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;
            //for(int i=0; i< fileList.length;i++){
            //     System.out.println(fileList[i]);
            //}
            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;
       for(int i=0; i< fileList.length;i++){
           file = new DefaultMutableTreeNode(fileList[i]);
           top.add(file);
       }
   }
    
   public void activate(){
      System.setSecurityManager(new RMISecurityManager());
      try {
        loader = (ImageLoader)Naming.lookup("SimpleImageServer");
        fileList = loader.listFiles();
        curDir = loader.getUserDir();
        separator = loader.getFileSepChar();
      }catch(Exception e){System.out.println(e); }
   }
   public static void main(String args[]){
      AWTImageViewer fc = new AWTImageViewer();
  }
}
