import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import com.webrenderer.*;


public class ExecuteScriptBrowser implements ActionListener {
	
	IBrowserCanvas browser;
	JTextField textfield;
	JButton runJavascript, runWithReturn;

	public ExecuteScriptBrowser() {
		JFrame frame = new JFrame();
		
		frame.setContentPane(content());
		frame.setSize(600, 400);
		frame.setVisible(true);
	}
	public JPanel content() {
		JPanel panel = new JPanel(new BorderLayout());
		
		textfield = new JTextField();
		
		JPanel spacerPanel = new JPanel(new FlowLayout());
		runJavascript = new JButton("Execute JavaScript");		
		runWithReturn = new JButton("Run with return");
		
		textfield.addActionListener(this); 
		runJavascript.addActionListener(this);
		runWithReturn.addActionListener(this);
		
		//Must be called to stop License dialog
		BrowserFactory.setLicenseData("30dtrial", "QD93G36AK30193AO47KLQP13");		

		//Core function to create browser
		browser = BrowserFactory.spawnInternetExplorer();

		browser.loadURL("www.google.com");

		spacerPanel.add(runJavascript);
		spacerPanel.add(runWithReturn);
		
		JPanel spacer2 = new JPanel(new BorderLayout());
		spacer2.add(BorderLayout.NORTH, spacerPanel);
		spacer2.add(BorderLayout.CENTER, textfield);

		panel.add(BorderLayout.NORTH, spacer2);
		panel.add(BorderLayout.CENTER, (Canvas)browser);
	
		return panel;
	}
	public void actionPerformed(ActionEvent e){
		if(e.getSource() == textfield){
			System.out.println("Textfield");
			//Loading a URL
			browser.loadURL(textfield.getText());
	
		} else if(e.getSource() == runJavascript){
			//Executing a simple Alert dialog in JavaScript and changing background
			//color of document
			browser.executeScript("document.body.style.backgroundColor='green';");
			browser.executeScript("alert('Background color changed');");
	
		} else if(e.getSource() == runWithReturn){
			System.out.println("runWithReturn");
			//Executing some JavaScript - Calling a function in JavaScript that
			//has a return value and passing the return value back to Java.
			System.out.println(browser.executeScriptWithReturn("document.title"));
	
		}
	}
	public static void main(String[] args) {
		new ExecuteScriptBrowser();
	}
}

