/**  ImageViewerServletApplet Class
   * @version 1.0  18 October 2000
   * @author Lawrence Rodrigues
   **/
package com.vistech.net;
import java.io.*;
import java.awt.event.*;
import java.awt.*;
import java.awt.image.*;
import java.applet.*;
import java.net.*;
import java.util.*;

public class ImageViewerServletApplet extends Applet {
   static int viewerWid = 400;
   static int viewerHt = 300;
   protected String[] fileList;
   protected String curDir = ".";
   protected char separator;
   protected ImageCanvas imCanvas;
   protected String rotAngle ="0";
   protected String magFactor = "100";
   protected TextField magf, rotf;
   protected String currentFile = null;
 
   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(){
      fileList = listImages();
      createUI();
   }
   public void createUI (){
       imCanvas = (ImageCanvas)createImageCanvas();
       magf = new TextField("5");
       magf.setText(magFactor);
       rotf = new TextField("5");
       rotf.setText(rotAngle);

       magf.addActionListener(
          new ActionListener() {
             public void actionPerformed(ActionEvent e) {
                magFactor = ((TextField)e.getSource()).getText();
                 if(currentFile != null) load(currentFile);
             }
          }
       );

       rotf.addActionListener(
          new ActionListener() {
             public void actionPerformed(ActionEvent e) {
                rotAngle = ((TextField)e.getSource()).getText();
                if(currentFile != null) load(currentFile);
             }
          }
       );

       Button resetButton = new Button("Reset");
       resetButton.addActionListener(
          new ActionListener() {
             public void actionPerformed(ActionEvent e) {
                reset();
             }
          }
       );

       Button edButton = new Button("Edge Detect");
       edButton.addActionListener(
          new ActionListener() {
             public void actionPerformed(ActionEvent e) {
                if(currentFile != null)edgeDetect(currentFile);
             }
          }
       );

       int len = fileList.length;
       java.awt.List li = new java.awt.List(len);
       for(int i=0;i<len;i++){
           li.add(fileList[i]);
       }
       li.setBackground(Color.lightGray);
       li.setForeground(Color.blue);

       li.addItemListener(
          new ItemListener() {
             public void itemStateChanged(ItemEvent e) {
                String[] str = (String[])e.getItemSelectable().getSelectedObjects();
                currentFile = str[0];
                load(str[0]);
             }
          }
       );

       Panel pan = new Panel();
       pan.add(new Label("Mag Factor"));
       pan.add(magf);
       pan.add(new Label("Rotation Angle"));
       pan.add(rotf);
       pan.add(edButton);
       pan.add(resetButton);

       setLayout(new BorderLayout());
       add(BorderLayout.WEST, li);
       add(BorderLayout.CENTER, imCanvas);
       add(BorderLayout.SOUTH,pan);
       setBackground(Color.lightGray);
   }

   public void reset(){
      magf.setText("100");
      rotf.setText("0");
      rotAngle = "0";
      magFactor = "100";
      if(currentFile != null) load(currentFile);
    }

   protected Panel createImageCanvas() {
       return new ImageCanvas();
   }

   protected String[] listImages() {
      try {
        URL servlet = new URL("http://localhost:8080/examples/servlet/ImageViewer");
       
        Properties prop = new Properties();
        prop.put("command", "listfiles");
        ServletInvoker si = new ServletInvoker(servlet);
        InputStream is = si.performPost(prop);

        BufferedReader d  = new BufferedReader(new InputStreamReader(is));
        String line = null;
        Vector lst = new Vector();
        while( (line = d.readLine())!= null){
           lst.addElement(line);
        }
        int size = lst.size();
        String flist[] = new String[size];
        lst.copyInto(flist);
        return flist;
      } catch (Exception e) {
        e.printStackTrace();
        return null;
      }
   }

   protected void load(String fl) {
      try {
        URL servlet = new URL("http://localhost:8080/examples/servlet/ImageViewer");
        
        Properties prop = new Properties();
        prop.put("command", "manip");
        prop.put("images", fl);
        prop.put("magFactor",magFactor);
        prop.put("rotAngle",rotAngle);

        ServletInvoker si = new ServletInvoker(servlet);
        InputStream is = si.performPost(prop);

        paintImage(is);
      } catch (Exception e) {e.printStackTrace();}
   }

   public void edgeDetect(String file) {
      try {
        URL servlet = new URL("http://localhost:8080/examples/servlet/ImageViewer");
        
        Properties prop = new Properties();
        prop.put("images", file);
        prop.put("command", "edgedetect");
        ServletInvoker si = new ServletInvoker(servlet);
        InputStream is = si.performPost(prop);

        paintImage(is);
      } catch (Exception e) {e.printStackTrace();}
   }
   protected void paintImage(InputStream is) {
      try {
      byte[] imageBuf = new byte[1024];
      ByteArrayOutputStream boutstream = new ByteArrayOutputStream();
      for(;;){
          int bytesread = is.read(imageBuf);
          if (bytesread <0) {break;}
          boutstream.write(imageBuf,0,bytesread);
      }
      byte[] buffer =boutstream.toByteArray();
      Image img = Toolkit.getDefaultToolkit().createImage(buffer);
      imCanvas.setImage(img);
      imCanvas.repaint();
      }catch (Exception e) {e.printStackTrace();}
   }

public class ImageCanvas extends Panel{
   protected Image image;
   protected int viewerWid = 400;
   protected int viewerHt = 600;

   public void setImage(Image img){ image = img;}

   public void paint(Graphics g){
      Rectangle rect = this.getBounds();
      g.setColor(Color.black);
      if((viewerWid != rect.width) || (viewerHt != rect.height)){
          viewerWid = rect.width; viewerHt = rect.height ;
          repaint(); return;
      }
      g.fillRect(0,0,rect.width, rect.height);
      if(image != null){
         g.drawImage(image,0,0, this);
      }
   }
}
}
