import java.io.*;
import java.net.Socket;
import java.awt.*;
import java.util.StringTokenizer;

public class openDlg extends Frame implements  Runnable {
Thread thread;
java.util.Vector v  = new java.util.Vector();
int scrPos=0;
List list;
Label lbl;
Label lbl2;
drawPanel dp;

	public openDlg(drawPanel dp) {
		super("Open");
		int width=325, height=465;
		this.dp = dp;
		GridBagLayout gb = new GridBagLayout();
      		GridBagConstraints c = new GridBagConstraints();
      		setFont(new Font("Helvetica", Font.PLAIN, 14));
      		setLayout(gb);
      		c.weightx=1.0;
      		c.weighty=1.0;
      		c.fill = GridBagConstraints.BOTH;
      		c.gridwidth = GridBagConstraints.REMAINDER;
		list = new List(20,false);
      		gb.setConstraints(list, c);
		add(list);
      		c.gridwidth = GridBagConstraints.RELATIVE;
		lbl = new Label();
      		gb.setConstraints(lbl, c);
      		add(lbl);
      		c.gridwidth = GridBagConstraints.REMAINDER;
      		c.fill = GridBagConstraints.HORIZONTAL;
		lbl2 = new Label("Please wait");
      		gb.setConstraints(lbl, c);
      		add(lbl2);
      		c.gridwidth = GridBagConstraints.RELATIVE;
      		c.weighty=0.01;
      		c.weightx=0.10;
		Button cancel = new Button("Cancel");
      		gb.setConstraints(cancel, c);
		add(cancel);
      		c.gridwidth = GridBagConstraints.REMAINDER;
		Button open = new Button("Open");
      		gb.setConstraints(open, c);
		add(open);
		pack();
		reshape(getToolkit().getScreenSize().width/2-width/2,
                       getToolkit().getScreenSize().height/2-height/2,width,height);   		
		start();
	}


	public void stop(){
		if(thread!=null) thread.stop();
		thread = null;
	}

	public void start(){
		thread = new Thread(this);
		thread.setPriority(Thread.MAX_PRIORITY-1);
		thread.start();
	}

	public void run(){
	    lbl.setText("Contacting archive.");
	    lbl2.setText("Please wait.");
	    show();
  	    String line = null;
	    Socket sock = null;
  	    DataInputStream in = null;
	    try{ 
 	        sock = new Socket(Env.host, 80); 
	        DataOutputStream os = new DataOutputStream(sock.getOutputStream());
		in = new DataInputStream(new BufferedInputStream(sock.getInputStream()));
		os.writeBytes("GET /draw/cgi/getFiles.php HTTP/1.0\n\n");
		while((line= in.readLine())!= null) if(line.length()<2) break; 
	    lbl.setText("Reading file list.");
		while( (line = in.readLine()) != null && thread !=null){
		   Thread.sleep(20);
			System.out.println(line);
		   v.addElement((Object)line);
		}
	    lbl.setText("Done reading.");
	    }
	    catch(Exception io){
		stop();
	    }
	    try{sock.close();}catch(IOException ioe){
			ioe.printStackTrace();
	    }
	    repaint();
		
	    StringTokenizer st;
	    for(int i = 0; i< v.size(); i++){
		   st = new StringTokenizer((String)v.elementAt(i));
		   st.nextToken();
                   list.addItem(st.nextToken());	
	    }
	    this.layout();
	    lbl.setText("");
	    lbl2.setText("");
    }

    public boolean handleEvent(java.awt.Event e){
	 if(e.id==701){
            String s,s2;
            s2 = (String)((List)e.target).getSelectedItem();
            lbl.setText(s2);
            char c = s2.charAt(s2.length()-5);
			int n = v.size();
            for(int i=0;i<n;i++){
                s  = (String)v.elementAt(i);
                if(s.charAt(s.length()-5)==c)
                {
                    if(s.indexOf(s2)>=0){
                      StringTokenizer st;
                      st = new StringTokenizer(s);
                      lbl2.setText(st.nextToken()+"  bytes");
                      break;
                    }
                }
            }
        }      
	else if(e.id == Event.ACTION_EVENT){
            if(e.target instanceof Button){
               if((e.arg).equals("Open")) dp.play((String)list.getSelectedItem());
	    }
	    else if(e.target instanceof List){
		dp.play((String)list.getSelectedItem());
	    }
	    this.dispose();
	}
        return super.handleEvent(e);
    }   
}
