import com.sun.java.swing.*;
import com.sun.java.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.*;
import java.util.*;

public class ListIconPanel extends JPanel {

	Explorer explorer;

  void buildConstraints(GridBagConstraints gbc, int gx, int gy, int gw, int gh, int wx, int wy) {
    gbc.gridx      = gx;
    gbc.gridy      = gy;
    gbc.gridwidth  = gw;
    gbc.gridheight = gh;
    gbc.weightx    = wx;
    gbc.weighty    = wy;
  }

  public ListIconPanel(Explorer e, WebTree w, String d) {
    GridBagLayout gridbaglayout = new GridBagLayout();
    GridBagConstraints constraints = new GridBagConstraints();

    ImageIcon  ImgDIR   = new ImageIcon("images/sDIR.gif"  ,"Directory");
    ImageIcon  ImgUNKW  = new ImageIcon("images/sUNKW.gif" ,"Unknown");
    ImageIcon  ImgHTML  = new ImageIcon("images/sHTML.gif" ,"Html");
    ImageIcon  ImgGIF   = new ImageIcon("images/sGIF.gif"  ,"Gif image");
    ImageIcon  ImgJPEG  = new ImageIcon("images/sJPEG.gif" ,"JPeg image");
    ImageIcon  ImgTXT   = new ImageIcon("images/sTXT.gif"  ,"Text");
    ImageIcon  ImgPDF   = new ImageIcon("images/sPDF.gif"  ,"PDF");
    ImageIcon  ImgMPEG  = new ImageIcon("images/sMPEG.gif" ,"MPeg");
    ImageIcon  ImgPS    = new ImageIcon("images/sPS.gif"   ,"Postscript");
    ImageIcon  ImgGTAR  = new ImageIcon("images/sGTAR.gif" ,"GTar file");
    ImageIcon  ImgTAR   = new ImageIcon("images/sTAR.gif"  ,"TAR file");
    ImageIcon  ImgZIP   = new ImageIcon("images/sZIP.gif"  ,"Zip file");
    ImageIcon  ImgMPEG2 = new ImageIcon("images/sMPEG2.gif","MPeg 2");
    ImageIcon  ImgWAV   = new ImageIcon("images/sWAV.gif"  ,"Wav File");
    ImageIcon  ImgBMP   = new ImageIcon("images/sBMP.gif"  ,"BMP image");
    ImageIcon  ImgTIFF  = new ImageIcon("images/sTIFF.gif" ,"Tiff image");
    ImageIcon  ImgDOC   = new ImageIcon("images/sDOC.gif"  ,"Doc file");

    int          i = 0;
    Info         info;
    JRadioButton file;
    JLabel       label;
    int          cpt = 0;
	 explorer = e;

    while ((info = w.getChild(d, i++)) != null) {

      /* icone par defaut */
      file = new JRadioButton(info.getName(),ImgUNKW);

      /* affectation de l'icone suivant le type du fichier */
      if ( info.getType() == Info.DIR )
	file = new JRadioButton(info.getName(),ImgDIR);
      if ( info.getType() == Info.HTML )
	file = new JRadioButton(info.getName(),ImgHTML);
      if ( info.getType() == Info.GIF )
	file = new JRadioButton(info.getName(),ImgGIF);
      if ( info.getType() == Info.JPEG )
	file = new JRadioButton(info.getName(),ImgJPEG);
      if ( info.getType() == Info.TXT )
	file = new JRadioButton(info.getName(),ImgTXT);
      if ( info.getType() == Info.PDF )
	file = new JRadioButton(info.getName(),ImgPDF);
      if ( info.getType() == Info.MPEG )
	file = new JRadioButton(info.getName(),ImgMPEG);
      if ( info.getType() == Info.PS )
	file = new JRadioButton(info.getName(),ImgPS);
      if ( info.getType() == Info.GTAR )
	file = new JRadioButton(info.getName(),ImgGTAR);
      if ( info.getType() == Info.TAR )
	file = new JRadioButton(info.getName(),ImgTAR);
      if ( info.getType() == Info.ZIP )
	file = new JRadioButton(info.getName(),ImgZIP);
      if ( info.getType() == Info.MPEG2 )
	file = new JRadioButton(info.getName(),ImgMPEG2);
      if ( info.getType() == Info.WAV )
	file = new JRadioButton(info.getName(),ImgWAV);
      if ( info.getType() == Info.BMP )
	file = new JRadioButton(info.getName(),ImgBMP);
      if ( info.getType() == Info.TIFF )
	file = new JRadioButton(info.getName(),ImgTIFF);
      if ( info.getType() == Info.DOC )
	file = new JRadioButton(info.getName(),ImgDOC);


      /* icone du fichier */
      constraints.fill = GridBagConstraints.BOTH;
      buildConstraints(constraints,0,cpt,1,1,100,0);
      gridbaglayout.setConstraints(file,constraints);
      add(file);
      
      /* nom du fichier */
      constraints.fill = GridBagConstraints.BOTH;
      buildConstraints(constraints,1,cpt,1,1,100,0);
      label = new JLabel(info.getName());
      gridbaglayout.setConstraints(label,constraints);
      add(label); 
      
      /* titre du fichier  */
      constraints.fill = GridBagConstraints.BOTH;
      buildConstraints(constraints,2,cpt,1,1,100,0);
      label = new JLabel(info.getTitle());
      gridbaglayout.setConstraints(label,constraints);
      add(label);

      /* longueur du fichier  */
      constraints.fill = GridBagConstraints.BOTH;
      buildConstraints(constraints,3,cpt,1,1,100,0);
		if(info.getType()!=Info.DIR)
      	label = new JLabel(new String(String.valueOf(info.getLen())));
		else label = new JLabel("");
      gridbaglayout.setConstraints(label,constraints);
      add(label);

      /* date du fichier  */
      constraints.fill = GridBagConstraints.BOTH;
      buildConstraints(constraints,4,cpt,1,1,100,0);
      label = new JLabel(info.getDate());
      gridbaglayout.setConstraints(label,constraints);
      add(label);

      cpt++;

      /* ajout d'un listener pour un repertoire */
      if (info.getType() == Info.DIR ) {
   final String path = d;
   final String directory = info.getName();
   file.addActionListener( new ActionListener() {
     public void actionPerformed(ActionEvent e) {
         explorer.downDirectory(path,directory);
     }
   });
      }
 
      /* ajout d'un listener pour une page html */
      if (info.getType() == Info.HTML ) {
   final String path = d;
   final String page = info.getName();
   file.addActionListener( new ActionListener() {
     public void actionPerformed(ActionEvent e) {
         explorer.openURL(path,page);
     }
   });
      }
 
      /* ajout d'un listener pour un fichier gif */
      if (info.getType() == Info.GIF) {
   final String path = d;
   final String page = info.getName();
   file.addActionListener( new ActionListener() {
     public void actionPerformed(ActionEvent e) {
         explorer.openFILE(path,page);
     }
   });
      }

      /* ajout d'un listener pour un fichier texte */
      if (info.getType() == Info.TXT) {
   final String path = d;
   final String page = info.getName();
   file.addActionListener( new ActionListener() {
     public void actionPerformed(ActionEvent e) {
         explorer.openTXT(path,page);
     }
   });
      }

      /* ajout d'un listener pour une Applet */
      if (info.getType() == Info.CLASS) {
   final String path = d;
   final String page = info.getName();
   file.addActionListener( new ActionListener() {
     public void actionPerformed(ActionEvent e) {
         explorer.openApplet(path,page);
     }
   });
      }

    }

    constraints.fill = GridBagConstraints.BOTH;
    buildConstraints(constraints,0,cpt,3,1,100,100);
    label = new JLabel("");
    gridbaglayout.setConstraints(label,constraints);
    add(label);     

    setLayout(gridbaglayout);
    setBackground(Color.white);
  }
}
