/*
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.io.*;
import java.net.*;
import java.util.*;
import java.awt.*;

public class loadXPM extends Thread{

DataInputStream d = null;
icontext it;
byte data;
int numColor = 0;
String line = null, s2, fname;
int s1;

  public loadXPM(icontext parent, String fileName) {
    it = parent;
    fname = "http://www.artcontext.com/icontext/cgi/zcat.php3?" + fileName;
	env.name = fileName.substring(0,fileName.length()-8);
	start();
  }

  public void run(){
    try {
      URL url = new URL(fname);
	    try {
		  d = new DataInputStream(url.openStream());
          		  
  		  d.readLine();d.readLine();d.readLine();
		  StringTokenizer st = new StringTokenizer(d.readLine(), " ");
		  st.nextToken();st.nextToken();
		  		  
		  numColor = Integer.parseInt(st.nextToken());
		  
		  while ((line=d.readLine()) != null && numColor-- > 0) {
		    s1 = Math.min(126,line.charAt(1));
		    s2 = line.charAt(1) + "  " + line.substring(5, 12);

		    Color c = new Color(getDecimal(line.substring(6,8)),
					getDecimal(line.substring(8,10)),
				        getDecimal(line.substring(10,12)));

		      it.ctab.put(new Character((char)line.charAt(1)), c);
		      if(s1<34&&s1>=32) it.lst.replaceItem(s2,s1-32);
		      else if(s1<60)    it.lst.replaceItem(s2,s1-33);
		      else              it.lst.replaceItem(s2,s1-34);
		  }
		  Character ch;
		  int x=0,y=0;
		  for (int i=0; i<50; i++) {
		    line = d.readLine().substring(1, 51);
		    for (int j=0; j<50; j++) {
			ch = new Character((char)line.charAt(j));
		        it.ta.text[j][i] = ch;
			it.da.img_g.setColor((Color)(it.ctab.get(ch)));
      		        //it.da.img_g.drawLine(j,i,j,i);
			//Would stretch a smaller image, 
                        //but Mac Netscape JVM doesn't allow it!

      		        it.da.img_g.fillRect(x,y,5,5);
			x+=5;
		    }
		    y+=5; 
                    x=0;
		  }
		  it.da.repaint();
		  it.ta.repaint();
        } catch(IOException ioe) {
            System.out.println("unable to open stream");
          }
	  catch (NoSuchElementException nee) {System.out.println("huh?");System.out.println(nee);}

    } catch(Exception ex) {
        ex.printStackTrace();
      }
	  	  finally {
	    try {
	      if (d != null) d.close();
		} catch (IOException ioee) {
		  System.out.println("Error closing input stream");
		}
      }
  }
 
  private int getDecimal(String s) {
    int temp = 0;
    char t1, t2;
	t1 = s.charAt(0);
	t2 = s.charAt(1);
	     if (t1>=97) temp = ((t1-87)<<4);
	else if (t1>=65) temp = ((t1-55)<<4);
	else             temp = ((t1-48)<<4);
	     if (t2>=97) temp += t2-87;
	else if (t2>=65) temp += t2-55;
	else             temp += t2-48;
    return temp;
  }
}

