package edu.csusb.danby.util;

import javax.swing.*;
import javax.swing.text.*;
import javax.swing.text.html.*;
import java.io.*;
import java.util.*;
import java.awt.Graphics.*;
import java.awt.*;
import java.net.URL;

/**
* Provides panel that displays a text file
* @author M. Scot McKaskle
* @version 1.0
*/
public class HTMLDisplay extends JScrollPane {
  private JEditorPane pane;
  private boolean html;

/**
* Constructor
*/
  public HTMLDisplay() {
    html = false;
    pane = new JEditorPane();
    pane.setEditable(false);
    setViewportView(pane);
    setMinimumSize(new Dimension(75, 0));
  }

/**
* Constructor
* @param fileName url to file for display.  must be in same location as applet
* @param _html display as HTML?
*/
  public HTMLDisplay(String fileName, boolean _html) {
    html = _html;
    pane = new JEditorPane();
    pane.setEditable(false);
    setViewportView(pane);
    setMinimumSize(new Dimension(75, 0));
    openFile(fileName);
  }

/**
* Set to diaply HTML or plain text
* @param _html Displays files as HTML if true
*/
  public void setHTML(boolean _html) {
    html = _html;
  }

/**
* Opens a file
* @param fileName url to file for display.  must be in same location as applet
* @return True/False if page was opened successfully;
*/
  public boolean openFile(String fileName) {
    NetFile netFile = new NetFile();
    try {
      URL url = new URL(fileName);
      if (html)
        pane.setContentType("text/html");
      else
        pane.setContentType("text/plain");
      pane.read(netFile.getReader(fileName), null);
      return true;
    }
    catch (IOException io) {
      System.out.println ("Can't access the file");
      InfoDialog warning = new InfoDialog("Warning", "Can't access the file");
      warning.show();
      return false;
    }
  }
}
