/*
The code is available only to persons
currently affiliated with any of the following,
      o Universities and schools
      o Non-commercial research organizations
      o Registered not-for-profit organizations
      o Registered charities
For these people, the license is of the form of
the GNU Public License version 2.  The restrictions
must remain attached to this and any subsequently derived code.
All other people have no implicit right to use, read, or copy
this software unless granted by Mr. Deck
THIS SOFTWARE IS PROVIDED "AS IS" AND MR. DECK
MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR
IMPLIED.  BY WAY OF EXAMPLE, BUT NOT LIMITATION, HE
MAKES NO REPRESENTATIONS OR WARRANTIES OF MERCHANT-
ABILITY OR FITNESS FOR ANY PARTICULAR PURPOSE OR THAT THE USE
OF THE LICENSED SOFTWARE AND DOCUMENTATION WILL NOT
INFRINGE ANY THIRD PARTY PATENTS, COPYRIGHTS, TRADEMARKS OR
*/

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

public class openDlg extends Frame implements Runnable {
TextField     tf = null; 
icontext      it = null;
Thread    thread = null;
Vector        v  = null;
Hashtable     h  = null;
List         cat = null; 
List        list = null;
label       lblA = null;
label       lblB = null;
String       tmp = null; 
String   lastCat = null;

    public openDlg(icontext i){
	super("Open");
	it = i;
	v = new Vector();
	h = new Hashtable();
	int width=460, height=486;
		
	setBackground(Color.white);
        setForeground(Color.black);
	setFont(env.font);

	setLayout(new FlowLayout());
	GridBagLayout gb = new GridBagLayout();
      	GridBagConstraints c = new GridBagConstraints();
      	setLayout(gb);

	c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx=0.5;
      	c.weighty=0.1;
	c.gridwidth = GridBagConstraints.RELATIVE;
	lblA = new label("Loading file list.");
	lblA.reshape(0,0,200,25);
	gb.setConstraints(lblA, c);
	add(lblA);
	c.gridwidth = GridBagConstraints.REMAINDER;
	lblB = new label("Please wait.");
	lblB.reshape(0,0,200,25);
	gb.setConstraints(lblB, c);
	add(lblB);

        c.weightx=1.0;
      	c.weighty=0.8;
	c.fill = GridBagConstraints.BOTH;
	c.gridwidth = GridBagConstraints.RELATIVE;
      	cat = new List(20,false);
	gb.setConstraints(cat, c);
	add(cat);
	c.gridwidth = GridBagConstraints.REMAINDER;
	list = new List(20,false);
      	gb.setConstraints(list, c);
	add(list);
		
	c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx=0;
	c.gridwidth = GridBagConstraints.RELATIVE;
	c.anchor    = GridBagConstraints.CENTER;
      	c.weighty   = 0.2;
        c.insets = new Insets(0,50,0,0);
	asciiBut cancelBut;
	cancelBut = new asciiBut("CANCEL",i);
        cancelBut.resize(73,30);
	gb.setConstraints(cancelBut, c);
	add(cancelBut);
	c.gridx=1;
    	c.gridwidth = GridBagConstraints.REMAINDER;
 	asciiBut openBut;
	openBut = new asciiBut("OPEN",i);
	openBut.resize(60,30);
	gb.setConstraints(openBut, c);
	add(openBut);

	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.start();
    }

    public void run(){
      String line = null;
      Socket sock = null;
      DataInputStream  is = null;
      DataOutputStream os = null;
	    try{ 
 	        sock = new Socket(env.host, 80); 
		if(sock!=null){
	        os = new DataOutputStream(sock.getOutputStream());
		is = new DataInputStream(sock.getInputStream());
		}
		if(os!=null)os.writeBytes("GET /icontext/cgi/getNameList.php3 HTTP/1.0\n\n");
		//Thread.sleep(200);
	
		while((line=is.readLine())!=null &&
                       line.length()>1) ; //skip HTTP header

		while(thread!=null && (line=is.readLine())!=null){
		    tmp = line.substring(line.indexOf(" "));
			if (h.containsKey(tmp)) {
			  v = (Vector)(h.get(tmp));
			  v.addElement((Object)(line.substring(0, line.indexOf(" "))));
			  h.put(tmp, v);			  
			}
			else {
                      v = new Vector();
  		      v.addElement((Object)(line.substring(0, line.indexOf(" "))));
 		      h.put(tmp, v);
		      cat.addItem((String)(line.substring(line.indexOf(" "))));
			}
		}
	    }
	    catch(EOFException eof){
		eof.printStackTrace();
	    }
            catch(IOException ex){
		ex.printStackTrace();
            }
	    finally{
		try{
		   if(is!=null)   is.close();
		   if(os!=null)   os.close();
		   if(sock!=null) sock.close();
		}catch(IOException ioe){
		  ioe.printStackTrace();
		}
	    }
	    lblA.setText("Categories");
	    lblB.setText("Icontext files");
    }

    public boolean handleEvent(Event e){
        if(e.id == Event.KEY_PRESS){
            if(e.key == 10){
	        it.da.repaint();
		if(list.getSelectedItem()!=null)
			open(list.getSelectedItem());
                this.hide();
	    }
        }
        else if(e.id == Event.ACTION_EVENT){
	    if(((List)(e.target)) ==  list){
	        it.da.repaint();
		if(list.getSelectedItem()!=null)
			open(list.getSelectedItem());
                this.hide();
	    }
	}
        else if(e.id == Event.MOUSE_DOWN){
          if(e.target instanceof asciiBut){
              this.hide();
	      it.da.repaint();
	      if(((asciiBut)e.target).getLabel().equals("CANCEL"))
		  		this.dispose();
			else if (list.getSelectedItem()!=null) 
				open(list.getSelectedItem());
	  }
        }
	else if (e.id == Event.LIST_SELECT){
	  if (((List)(e.target))==cat) {
	    if ( cat.getSelectedItem() != null && 
                 cat.getSelectedItem() != lastCat )
            {
	        if(lastCat !=null) list.clear();
		lastCat = cat.getSelectedItem();
	 	Enumeration enum = 
                ((Vector)(h.get(lastCat))).elements();
	        for(;enum.hasMoreElements();) 
	    	list.addItem((String)(enum.nextElement()));
		repaint();
	    }
	  }
	}
        else if(e.id == Event.WINDOW_DESTROY){
                this.hide();
	        it.da.repaint();
		this.dispose();
	}
        return super.handleEvent(e);
    }               

    public void open(String s){
	it.usrLab.setText(s+" loaded");
        new loadXPM(it,(String)(s+".html.gz"));
    } 
}

