// created: 29.12.00
// modified:

import javax.swing.*;
import javax.swing.table.*;
//import javax.swing.event.*;
import java.awt.event.*;
import java.util.*;
import java.awt.*;
import java.io.*;
import java.net.*;

/**********************/
/* class WebPagePanel */
/**********************/

class WebPagePanel extends JPanel implements ActionListener, MouseListener//, ListSelectionListener
 {
 
 //Vector theLocations = new Vector(0);
 Vector thePages = new Vector(0);

 JTextField fileField;
 JButton addButton;
 JButton removeButton;
 JButton loadButton;
 //JTextArea fileDisplay;
 DefaultTableModel pageTableModel;
 JTable pageTable;

 WebPagePanel()
  {
  // set up this panel
  setBorder(BorderFactory.createTitledBorder("locations"));  

  // instantiate the components
  fileField = new JTextField(43);
  
  JToolBar toolbar = new JToolBar();
  toolbar.setFloatable(false);

  addButton = new JButton("add");
  addButton.setActionCommand("add");
  addButton.addActionListener(this);  

  loadButton = new JButton("load");
  loadButton.setActionCommand("load");
  loadButton.addActionListener(this);  

  removeButton = new JButton("remove");
  removeButton.setActionCommand("remove");
  removeButton.addActionListener(this);

  //fileDisplay = new JTextArea();
  //fileDisplay.setEditable(false);
  //JScrollPane scrollingFileDisplay = new JScrollPane(fileDisplay);
  //scrollingFileDisplay.setPreferredSize(new Dimension(580,380));
  //Object [][] data = new Object[0][0];

  // set up the web page table
  String [] columnNames = {"Name", 
                           "Location",
                           "Value",
                           "# text",
                           "# images",
                           "# tables",
                           "# links",
                           "total # downloads",
                           "# other downloads",
                           "# image downloads",
                           "# sound downloads",
                           "# video downloads",
                           "frames",
                           "extension"};

  pageTableModel = new DefaultTableModel(columnNames,0);
  pageTable = new JTable(pageTableModel);
  pageTable.addMouseListener(this);
  pageTable.setAutoResizeMode (JTable.AUTO_RESIZE_OFF);
  //pageTable.setPreferredSize(new Dimension(1000,180));

  // set initial column sizes
  pageTable.getColumnModel().getColumn(0).setPreferredWidth(150);
  pageTable.getColumnModel().getColumn(1).setPreferredWidth(360);
  pageTable.getColumnModel().getColumn(2).setPreferredWidth(62);

  //pageTable.setEnabled(true);
  //pageTable.setCellSelectionEnabled(false);
  //pageTable.setRowSelectionAllowed(false);
  //pageTable.setColumnSelectionAllowed(false);
  //pageTable.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
  //pageTable.getSelectionModel().addListSelectionListener(this); 

  JScrollPane scrollingPageTable = new JScrollPane(pageTable);
  scrollingPageTable.setPreferredSize(new Dimension(590,195));

  //GridBagLayout gridbag = new GridBagLayout();
  //GridBagConstraints c = new GridBagConstraints();
  //c.gridx = 0;
  //c.gridy = 0;
  //gridbag.setConstraints(fileField,c);
  //setLayout(gridbag);

  // add the components
  toolbar.add(fileField);
  toolbar.add(addButton);
  toolbar.add(loadButton);
  toolbar.add(removeButton);

  add(toolbar,BorderLayout.NORTH);
  add(scrollingPageTable,BorderLayout.CENTER);
  }

 /***********************/
 /* method valueChanged */
 /***********************/

 /*public void valueChanged(ListSelectionEvent e) 
  {
  //Ignore extra messages.
  if (e.getValueIsAdjusting()) 
   return;
  ListSelectionModel lsm = (ListSelectionModel)e.getSource();
  if (!lsm.isSelectionEmpty()) 
   {
   //int selectedRow = lsm.getMinSelectionIndex();
   //selectedRow is selected
   }
  }*/

 /***********************/
 /* method mouseClicked */
 /***********************/

 public void mouseClicked(MouseEvent e)
  {
  // check if double clicked, and left mouse button used
  if ((e.getClickCount()>1) && SwingUtilities.isLeftMouseButton(e)) 
   {
   int row = pageTable.rowAtPoint(e.getPoint()); 
   WebPage page = (WebPage) thePages.elementAt(row);
   String title = page.getName();
   if (!page.isParsed())
     title = "unparsed";
   InfoPanel info = new InfoPanel(page);
   JOptionPane.showMessageDialog(this,info,title,JOptionPane.INFORMATION_MESSAGE);  
   }
  }

 /*****************************/
 /* other mouse event methods */
 /*****************************/

 public void mousePressed(MouseEvent e){}
 public void mouseReleased(MouseEvent e){}
 public void mouseEntered(MouseEvent e){}
 public void mouseExited(MouseEvent e){}

 /**************************/
 /* method actionPerformed */
 /**************************/

 public void actionPerformed(java.awt.event.ActionEvent e) 
  {
  if (e.getActionCommand().equals("add")) 
   {
   String text = fileField.getText();
   try
    {
    //theLocations.addElement(text);
    addPage(text);
    fileField.setText("");
    //pageTable.notifyChange();
    //fileDisplay.append(text + "\n");
    }
   catch (MalformedURLException e2){System.out.println("Cannot add page as \""+text+"\" is a bad URL");}
   }
  else if (e.getActionCommand().equals("load"))
   {
   JFileChooser fc = new JFileChooser();
   int returnVal = fc.showOpenDialog(this);
   if (returnVal == JFileChooser.APPROVE_OPTION) 
     {
     File file = fc.getSelectedFile();
     String line = null;
     //open and read the file
     BufferedReader reader = null;
     try
      {
      FileReader in = new FileReader(file);
      reader = new BufferedReader(in);
      line = "";
      }
     catch (FileNotFoundException e4)
      {
      System.out.println("Cannot find file");
      }
     while (line != null)
      {
      try 
       {
       line = reader.readLine();
       }
      catch (IOException e3) {}
      if (line != null)
        {
        //theLocations.addElement(line);
        try 
         {
         addPage(line);
         //fileDisplay.append(line + "\n");
         } 
        catch (MalformedURLException e2){System.out.println("Cannot add page as \""+line+"\" is a bad URL");} 
        }
      }
     } 
   }
  else if (e.getActionCommand().equals("remove"))  
   {
   int [] selectedRows = pageTable.getSelectedRows();
   for (int i=0; i<selectedRows.length; i++)
    {
    // remove page
    // need to decrement i from the selected row number as each call to removePage decreases
    //  the size of thePages vector by 1.
    removePage(selectedRows[i]-i);  
    }
   }

  }

 /******************/
 /* method addPage */
 /******************/
 
 private void addPage(String loc) throws MalformedURLException
  {
  // check whether the page already exists
  for (int i=0; i<thePages.size(); i++)
   {
   WebPage page = (WebPage) thePages.elementAt(i);
   if ( page.getLocation().equals(loc) )
    return;
   }
  WebPage newPage = new WebPage(loc);
  thePages.addElement(newPage);
  pageTableModel.addRow(new Object [13]);
  pageTableModel.setValueAt(loc,pageTableModel.getRowCount()-1,1);   
  }

 /*********************/
 /* method removePage */
 /*********************/

 private void removePage(int pageNum)
  { 
  thePages.removeElementAt(pageNum);
  pageTableModel.removeRow(pageNum);
  }

 /*******************/
 /* method setPages */
 /*******************/

 void setPages(WebPage [] pages)
  {
  thePages = new Vector(pages.length);  
  for (int i=0; i<pages.length; i++)
   {
   thePages.addElement(pages[i]);
   }
  displayPages();
  }

 /***********************/
 /* method displayPages */
 /***********************/
  
 void displayPages()
  {
  pageTableModel.setRowCount(0);
  for (int r=0; r<thePages.size(); r++)
   {
   pageTableModel.addRow(new Object [13]);
   WebPage page = (WebPage) thePages.elementAt(r);
   pageTableModel.setValueAt(new String(page.getLocation()),r,1);
   pageTableModel.setValueAt(new String(page.getExtension()),r,13);
   if (page.isParsed())
    {
    pageTableModel.setValueAt(new String(page.getName()),r,0);
    pageTableModel.setValueAt(new Float(page.getValue()),r,2);
    pageTableModel.setValueAt(new Integer(page.getAmountText()),r,3);
    pageTableModel.setValueAt(new Integer(page.getNumImages()),r,4);
    pageTableModel.setValueAt(new Integer(page.getNumTables()),r,5);
    pageTableModel.setValueAt(new Integer(page.getNumLinks()),r,6);
    pageTableModel.setValueAt(new Integer(page.getNumDownloads()),r,7);
    pageTableModel.setValueAt(new Integer(page.getNumOtherDownloads()),r,8);
    pageTableModel.setValueAt(new Integer(page.getNumImageDownloads()),r,9);
    pageTableModel.setValueAt(new Integer(page.getNumSoundDownloads()),r,10);
    pageTableModel.setValueAt(new Integer(page.getNumVideoDownloads()),r,11);
    if (page.hasFrames())
      pageTableModel.setValueAt("yes",r,12);
    else
      pageTableModel.setValueAt("no",r,12);
    }
   }
  }

 /*******************/
 /* method getPages */
 /******************/

 WebPage [] getPages()
  {
  return ((WebPage []) thePages.toArray(new WebPage [0]));
  }
 
 public void setEnabled(boolean bool)
  {
  super.setEnabled(bool);
  fileField.setEnabled(bool);
  addButton.setEnabled(bool);
  removeButton.setEnabled(bool);
  loadButton.setEnabled(bool);
  }

 }