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 LargeIconPanel extends JPanel {

  Explorer explorer;
  Info info;


 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 LargeIconPanel(Explorer e, WebTree w, String d) {

    GridBagLayout gridBag          = new GridBagLayout();      /* layout */
    GridBagConstraints constraints = new GridBagConstraints(); /* contraintes du layout */

    ImageIcon  ImgDIR   = new ImageIcon("images/lDIR.gif"  ,"Directory");
    ImageIcon  ImgUNKW  = new ImageIcon("images/lUNKW.gif" ,"Unknown");
    ImageIcon  ImgHTML  = new ImageIcon("images/lHTML.gif" ,"Html");
    ImageIcon  ImgGIF   = new ImageIcon("images/lGIF.gif"  ,"Gif image");
    ImageIcon  ImgJPEG  = new ImageIcon("images/lJPEG.gif" ,"JPeg image");
    ImageIcon  ImgTXT   = new ImageIcon("images/lTXT.gif"  ,"Text");
    ImageIcon  ImgPDF   = new ImageIcon("images/lPDF.gif"  ,"PDF");
    ImageIcon  ImgMPEG  = new ImageIcon("images/lMPEG.gif" ,"MPeg");
    ImageIcon  ImgPS    = new ImageIcon("images/lPS.gif"   ,"Postscript");
    ImageIcon  ImgGTAR  = new ImageIcon("images/lGTAR.gif" ,"GTar file");
    ImageIcon  ImgTAR   = new ImageIcon("images/lTAR.gif"  ,"TAR file");
    ImageIcon  ImgZIP   = new ImageIcon("images/lZIP.gif"  ,"Zip file");
    ImageIcon  ImgMPEG2 = new ImageIcon("images/lMPEG2.gif","MPeg 2");
    ImageIcon  ImgWAV   = new ImageIcon("images/lWAV.gif"  ,"Wav File");
    ImageIcon  ImgBMP   = new ImageIcon("images/lBMP.gif"  ,"BMP image");
    ImageIcon  ImgTIFF  = new ImageIcon("images/lTIFF.gif" ,"Tiff image");
    ImageIcon  ImgDOC   = new ImageIcon("images/lDOC.gif"  ,"Doc file");

    int NB_COLONNES = 4;
    int i           = 0;
    int Cpt         = 0;
    explorer        = e;
    JRadioButton file;


    setLayout(gridBag);
    
    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);


      constraints.fill = GridBagConstraints.BOTH;
      buildConstraints(constraints,(Cpt%NB_COLONNES),(Cpt/NB_COLONNES),1,1,100,0);

      Cpt++;
      gridBag.setConstraints(file,constraints);
      add(file);

      /* sert a placer le texte en dessous, centre de l'image */
      file.setVerticalTextPosition(AbstractButton.BOTTOM);
      file.setHorizontalTextPosition(AbstractButton.CENTER);
      file.setHorizontalAlignment(AbstractButton.CENTER);
      file.setVerticalAlignment(AbstractButton.BOTTOM);

      /* 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);
     }
   });
      }

    }

    /* une nouvelle ligne qui prend toute la place, pour remonter le contenu utile */
    constraints.fill = GridBagConstraints.BOTH;
    buildConstraints(constraints,0,(Cpt/NB_COLONNES)+1,1,1,100,100);
    JLabel label = new JLabel("");
    gridBag.setConstraints(label,constraints);
    add(label);     

    setBackground(Color.white);
  }
}
