/****************************************
*HTMLGen.java
*From http://home.att.net/~gobruen
*
*	Called by MainInterface.java
*	Opens an interface for entering text and settings
*	that will go into an HTML file.
*	The file content is generated by Hlib.java
*
*
*	HTMLGen()
*	reset()
*	actionPerformed()
*	genHTMLFile()
*
*
*****************************************/



import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.awt.event.*;


public class HTMLGen extends JFrame implements ActionListener
{


	private JLabel fnLabel = new JLabel("File Name:", JLabel.LEFT);
	private JLabel tLabel = new JLabel("Title:", JLabel.LEFT);
	private JLabel mdLabel = new JLabel("Meta Data:", JLabel.LEFT);
	private JLabel bgLabel = new JLabel("Background Color:", JLabel.LEFT);
	private JLabel tcLabel = new JLabel("Text Color:", JLabel.LEFT);
	private JLabel tfLabel = new JLabel("Text Font:", JLabel.LEFT);
	private JLabel glLabel = new JLabel("General Link:", JLabel.LEFT);
	private JLabel glnLabel = new JLabel("General Link Name:", JLabel.LEFT);
	private JLabel imgLabel = new JLabel("Image:", JLabel.LEFT);

	private JTextField fnameField = new JTextField(20);//file name
	private JTextField titleP = new JTextField(20);//page title
	private JTextField mData = new JTextField(20);//meta data
	private JTextField bgColor = new JTextField(20);//background color
	private JTextField tColor = new JTextField(20);//text color
	private JTextField tFont = new JTextField(20);//text font
	private JTextField genLink = new JTextField(20);//text color
	private JTextField genLinkN = new JTextField(20);//text font
	private JTextField img = new JTextField(20);//image

	private JTextArea paragContent = new JTextArea();
	private JButton genHTMLBut = new JButton("Publish"); //See detailed
	private JButton launchBut = new JButton("Launch");   //descriptions
	private JButton helpBut = new JButton("Help");	     //for these
	private JButton addPara = new JButton("Add Paragraph"); //functions

	private JPanel commands = new JPanel();
	private JPanel create = new JPanel();

	private int max = 10;
	private int pCnt = 0;
	private String paragArr[] = new String[max];

	public HTMLGen()
	{
		super("HTML Page Maker");

		commands.setLayout(new GridLayout(9, 1));

		fnLabel.setFont(new Font("Courier", Font.BOLD, 15));
		fnameField.setToolTipText("Your file name, leave out .html");
		fnameField.setFont(new Font("Courier", Font.BOLD, 15));

		commands.add(fnLabel);
		commands.add(fnameField);

		paragContent.setFont(new Font("Courier", Font.BOLD, 20));
		paragContent.setToolTipText("Enter you text content here, press Add Paragraph for a new paragraph");

		titleP.setFont(new Font("Courier", Font.BOLD, 15));
		titleP.setToolTipText("Page title");
		titleP.setFont(new Font("Courier", Font.BOLD, 15));

		commands.add(tLabel);
		commands.add(titleP);

		tLabel.setFont(new Font("Courier", Font.BOLD, 15));
		mData.setToolTipText("enter meta data or keywords. These are hidden, not displayed on page");
		mData.setFont(new Font("Courier", Font.BOLD, 15));

		commands.add(mdLabel);
		commands.add(mData);

		mdLabel.setFont(new Font("Courier", Font.BOLD, 15));
		bgColor.setToolTipText("Page background color");
		bgColor.setFont(new Font("Courier", Font.BOLD, 15));

		commands.add(bgLabel);
		commands.add(bgColor);

		bgLabel.setFont(new Font("Courier", Font.BOLD, 15));
		bgColor.setToolTipText("");
		bgColor.setFont(new Font("Courier", Font.BOLD, 15));

		commands.add(tcLabel);
		commands.add(tColor);

		tcLabel.setFont(new Font("Courier", Font.BOLD, 15));
		tColor.setToolTipText("Page text color");
		tColor.setFont(new Font("Courier", Font.BOLD, 15));

		commands.add(tfLabel);
		commands.add(tFont);

		tfLabel.setFont(new Font("Courier", Font.BOLD, 15));
		tFont.setToolTipText("Page text font");
		tFont.setFont(new Font("Courier", Font.BOLD, 15));

		commands.add(glLabel);
		commands.add(genLink);

		glLabel.setFont(new Font("Courier", Font.BOLD, 15));
		genLink.setToolTipText("Enter the full URL, ex: http://www.yahoo.com");
		genLink.setFont(new Font("Courier", Font.BOLD, 15));

		commands.add(glnLabel);
		commands.add(genLinkN);

		glnLabel.setFont(new Font("Courier", Font.BOLD, 15));
		genLinkN.setToolTipText("Enter the name of the above link, ex: Yahoo!");
		genLinkN.setFont(new Font("Courier", Font.BOLD, 15));

		commands.add(imgLabel);
		commands.add(img);

		imgLabel.setFont(new Font("Courier", Font.BOLD, 15));
		img.setToolTipText("Enter the name of an image file, include the file extension, ex: .jpg, .gif");
		img.setFont(new Font("Courier", Font.BOLD, 15));

		paragContent.setLineWrap(true);

		genHTMLBut.addActionListener(this);
		genHTMLBut.setForeground(Color.black);
		genHTMLBut.setFont(new Font("Courier", Font.BOLD,10));
		create.add(genHTMLBut);

		addPara.addActionListener(this);
		addPara.setForeground(Color.black);
		addPara.setFont(new Font("Courier", Font.BOLD,10));
		create.add(addPara);

		launchBut.addActionListener(this);
		launchBut.setForeground(Color.black);
		launchBut.setFont(new Font("Courier", Font.BOLD,10));
		create.add(launchBut);

		helpBut.addActionListener(this);
		helpBut.setForeground(Color.black);
		helpBut.setFont(new Font("Courier", Font.BOLD,10));
		create.add(helpBut);

		Container c = getContentPane();
		c.setLayout(new BorderLayout());
		c.add("North", commands);
		c.add("South", create);
		c.add(new JScrollPane(paragContent));
		c.add("Center", paragContent);


	}//end con HTMLGen()
		
/****************************
*
*	reset()
*		Sets all the Jtext fields and other parameters
*		back to blank or 0 to create a new page
*
*****************************/


	public void reset(){
		paragContent.setText("");
		fnameField.setText("");
		titleP.setText("");
		mData.setText("");
		bgColor.setText("");
		tColor.setText("");
		tFont.setText("");
		genLink.setText("");
		genLinkN.setText("");
		img.setText("");
		for(int i=0; i<max; i++){
			paragArr[i] = null;
		}
		pCnt = 0;
	}//end reset()

/***********************************************
*
* Action Performed
*
*	Publish
*	Add Paragraph
*	Help
*	Launch
*
*
************************************************/

	public void actionPerformed(ActionEvent evt)
	{
		if(evt.getActionCommand().equals("Publish")){
			String fileName = fnameField.getText();
			fileName = fileName + ".html";
			genHTMLFile(titleP, mData, bgColor, 
				    tColor, tFont, paragArr, 
				    genLink, genLinkN, img, fileName);
			reset();
		}//End Publish ActionEvent
		if(evt.getActionCommand().equals("Add Paragraph")){
			if(pCnt<max){
				paragArr[pCnt] = paragContent.getText();
				pCnt++;
				paragContent.setText("");
				JOptionPane.showMessageDialog(null, "Paragraph Added!");
			}else{
				JOptionPane.showMessageDialog(null, "Paragraph limit reached, create new page.");
			}
		}//End Add Paragraph ActionEvent
		if(evt.getActionCommand().equals("Help")){
     			try{
				Runtime.getRuntime().exec
			    	("rundll32 url.dll,FileProtocolHandler " + "helpfiles.html");

       			}
     			catch (Exception e) {
       				e.printStackTrace();
       			}
		}//End Help ActionEvent
		if(evt.getActionCommand().equals("Launch")){
			String fileName = fnameField.getText();
			fileName = fileName + ".html";
			genHTMLFile(titleP, mData, bgColor, 
				    tColor, tFont, paragArr, 
				    genLink, genLinkN, img, fileName);
		
			
     			try{
				fileName = "c:\\windows\\desktop\\myWeb\\"+fileName;
				Runtime.getRuntime().exec
			    	("rundll32 url.dll,FileProtocolHandler " + fileName);

       			}
     			catch (Exception e) {
       				e.printStackTrace();
       			}
		}//End Launch ActionEvent

	}//end actionPerformed()

/***********************************************
* genHTMLFile()
* Make file
*	makePage() in Hlib..java
*
*
*
*
************************************************/

	private void genHTMLFile( JTextField titleP, JTextField mData, 
				  JTextField bgColor, JTextField tColor, 
				  JTextField tFont, String paragArr[], 
				  JTextField genLink, JTextField genLinkN, 
				  JTextField img, String fileName){
		String fileContent;

		try
		{
			if(fileName.equals(".html")){
				JOptionPane.showMessageDialog(null, "No File Name Entered!");
			}else{
				fileContent = Hlib.makePage(titleP.getText(),
							mData.getText(),
							bgColor.getText(),
							tColor.getText(),
							tFont.getText(),
							genLink.getText(),
							genLinkN.getText(),
							img.getText(),
							paragArr);
				fileName = "c:\\windows\\desktop\\myWeb\\"+fileName;
				FileWriter outStream = new FileWriter(fileName);
				outStream.write (fileContent);
				outStream.close();
				JOptionPane.showMessageDialog(null, "Gernerated "+fileName);
			}
		}catch(IOException e)
		{
			paragContent.setText("IOERROR: "+e.getMessage()+"\n");
			e.printStackTrace();
		}
		
	}//genHTMLFile


}//end class HTMLGen()
