//package com.vistech.internet;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.awt.event.*;
import java.awt.*;
import java.awt.image.*;
import java.util.*;

public class ImageViewerServlet extends HttpServlet{

   public void doGet(HttpServletRequest req, HttpServletResponse res)
                  throws ServletException{
      String dir = null;
      String fileList[] = null;
      try {
        res.setContentType("text/html");
        PrintWriter out = res.getWriter();
        String filename = "/examples/lena.gif";
        out.println("<HTML>");
        out.println("<HEAD><TITLE>File list</TITLE></HEAD> ");
        StringBuffer reqUrl = HttpUtils.getRequestURL(req);
        out.println("<FORM ACTION="+ reqUrl.toString() +" method=\"Get\">");
        out.println("<BODY BGCOLOR= lime>");
        out.println("<TABLE border=1 align=\"left\">");
        out.println("<TR><TH>ID</TH><TH>Image</TH></TR>");

        HttpSession session = req.getSession(false);
        if(session != null) {
           String param = req.getParameter("images");
           String fl = null;
           if(param != null){
              fl = new String(session.getValue("imagepath")+"/"+param);
              dir = new String((String)session.getValue("curDir"));
           }else {
              dir = req.getPathTranslated();
              if(session != null)session.putValue("curDir", dir);
           }
           if(fl != null) filename = fl;
           if(dir != null) fileList = listFiles(dir);
        }else {
           //Create a session the first time 
           session = req.getSession(true);
           dir = req.getPathTranslated();
           fileList = listFiles(dir);
           if(session != null) {
             session.putValue("imagepath", "/examples");
             session.putValue("curDir", dir);
           }
        }
        String extra = req.getPathInfo();
        if(fileList != null) {
           for(int i=0;i<fileList.length;i++) {
               out.println("<TR><TD>");
               out.println(i);
               out.println("</TD>");
               out.println("<TD>");
               out.println("<INPUT TYPE=\"RADIO\" name=\"images\" ");
               if(extra == null)
               out.println("value=\""+fileList[i]+"\" >"+fileList[i]);
               else out.println("value=\""+extra+ "/"+fileList[i]+"\" >"+fileList[i]);
               out.println("</TD>");
               out.println("</TR>");
           }
        }
        out.println("</TABLE>");
        out.println("<INPUT TYPE=\"SUBMIT\" VALUE=\"DISPLAY\" align=\"top\">" );
        out.println("<IMG SRC=\""+filename+"\" align=right>");
        out.println("<FORM>");
        out.println("</BODY></HTML>");
      } catch(Exception e) {System.out.println(e);}
   }

   public void doPost(HttpServletRequest request,
                      HttpServletResponse response)
        throws IOException, ServletException {
        doGet(request, response);
   }

   public synchronized String[] listFiles(String dirstr) {
      String fl[] = null;
      try {
         ExtensionFilter filter =  new ExtensionFilter("jpeg,gif,JPEG,GIF JPG,jpg, png, PNG");
         File dir = new File(dirstr);
         fl = dir.list(filter);
      } catch (Exception e){System.out.println(e);}
      return fl;
   }

   class  ExtensionFilter implements FilenameFilter{
      private String extString;
      public ExtensionFilter(String ext){
         extString = ext;
      }
      public boolean accept(File dir, String name){
	       StringTokenizer st = new StringTokenizer(extString, ",");
         while(st.hasMoreTokens()){
               String str = st.nextToken();
               String tok = str.trim();
               if(name.endsWith(tok)){ return true;}
         }
         return false;
      }
   }
}
