// A class for loading pages into the WWW browser.
// Barry Cornelius, 13th February 2000
import java.awt.event. ActionEvent;
import java.awt.event. ActionListener;
import java.util.      ArrayList;
import java.io.        IOException;
import java.util.      Iterator;
import javax.swing.    JEditorPane;
import javax.swing.    JTextField;
import java.util.      List;
public class PagesHandler implements ActionListener
{
   private List iHistory;
   private JEditorPane iJEditorPane;
   private JTextField iURLJTextField;
   private void iOutputHistory()
   {
      iURLJTextField.setText("");
      final StringBuffer tStringBuffer = new StringBuffer();
      tStringBuffer.append("<html><body><table border=\"1\">");
      int tURLNumber = 0;
      final Iterator tIterator = iHistory.iterator();
      while (tIterator.hasNext())
      {
         tURLNumber++;
         tStringBuffer.append("<tr><td>" + tURLNumber + "</td><td>" +
                              tIterator.next() + "</td></tr>");
      }
      tStringBuffer.append("</table></body></html>\n");
      iJEditorPane.setContentType("text/html");
      iJEditorPane.setText("" + tStringBuffer);
   }
   private String iGetURLFromHistory(int pURLNumber)
   {
      final boolean tAdd = pURLNumber>=0;
      pURLNumber = Math.abs(pURLNumber);
      if (pURLNumber==0 || pURLNumber>iHistory.size())
      {
         return "";
      }
      // pURLNumber>=1 && pURLNumber<=iHistory.size()
      if (tAdd)
      {
         return (String)iHistory.get(pURLNumber - 1);
      }
      iHistory.remove(pURLNumber - 1);
      return "";
   }
   public PagesHandler(final JEditorPane pJEditorPane,
                       final JTextField pURLJTextField)
   {
      iHistory = new ArrayList();
      iJEditorPane = pJEditorPane;
      iURLJTextField = pURLJTextField;
   }
   public void actionPerformed(final ActionEvent pActionEvent)
   {
      iOutputHistory();
   }
   public void setPage(String pURLString)   
   {
      if (pURLString==null || pURLString.equals(""))
      {
         iOutputHistory();
         return;
      }
      final char tFirstChar = pURLString.charAt(0);
      int tURLNumber = 0;
      if ( tFirstChar=='-' || Character.isDigit(tFirstChar))
      {
         try
         {
            tURLNumber = Integer.parseInt(pURLString);
         }
         catch(final NumberFormatException pNumberFormatException)
         {
            tURLNumber = 0;
         }
         pURLString = iGetURLFromHistory(tURLNumber);
      }
      if (pURLString.equals(""))
      {
         iOutputHistory();
         return;
      }
      try
      {
         iURLJTextField.setText(pURLString);
         // The setPage call (below) will change the ContentType to text/html.
         // This seemingly nonsensical call of setContentType ensures that
         // setPage reloads the WWW page even if it is already loaded.
         iJEditorPane.setContentType("text/plain");
         iJEditorPane.setPage(pURLString);
         if ( ! iHistory.contains(pURLString) )
         {
            iHistory.add(pURLString);
         }
      }
      catch(IOException pIOException)
      {
         iJEditorPane.setContentType("text/html");
         iJEditorPane.setText("<html>" +
                              "<body>" +
                              "<p>error in URL</p>" +
                              "</body>" +
                              "</html>\n");
      }
   }
}
