import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JTextField;

import com.webrenderer.BrowserFactory;
import com.webrenderer.IBrowserCanvas;
import com.webrenderer.event.MouseAdapter;
import com.webrenderer.event.MouseEvent;

/**
 * Example of using Webrenderer with a JPopupMenu
 * for a right click context menu.
 */
public class JMenuBrowser extends java.awt.event.MouseAdapter {

	IBrowserCanvas browser;
	JTextField textfield;
	JPopupMenu menu;
	JMenuItem menuItem1, menuItem2, menuItem3;

	public JMenuBrowser() {

		JFrame frame = new JFrame();
		frame.setContentPane(content());
		frame.setSize(600, 400);
		frame.setVisible(true);

		/*
		 * Shut down the application when the window is closed.
		 */
		frame.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});
	}

	public JPanel content() {
		JPanel panel = new JPanel(new BorderLayout());

		//Must be called to stop License dialog
		BrowserFactory.setLicenseData("betatest", "CH4GCF6U0GSMB2PGN930F69J");

		//Core function to create browser
		browser = BrowserFactory.spawnInternetExplorer();

		textfield = new JTextField();
		textfield.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				browser.loadURL(textfield.getText());
			}
		});

		/*
		 * Disabling the default context menu right-click menu)
		 */
		browser.enableDefaultContextMenu(false);

		/*
		 * Creating the right-click context menu
		 */
		menu = new JPopupMenu("Context Menu");
		menuItem1 = new JMenuItem("Menu Item 1");
		menuItem2 = new JMenuItem("Menu Item 2");
		menuItem3 = new JMenuItem("Menu Item 3");

		menu.add(menuItem1);
		menu.add(menuItem2);
		menu.add(menuItem3);

		/*
		 * Making the menu heavyweight - this prevents the menu going behind
		 * the canvas. This is a bug with Swing menus,other Swing components do
		 * not have this issue.
		 */
		menu.setLightWeightPopupEnabled(false);

		/*
		 * Adding a mouse listener to determine when to show the right-click
		 * context menu
		 */
		browser.addMouseListener(new MouseAdapter() {
			public void onMouseDown(MouseEvent e) {
				//Check if it is a right click
				if (e.getModifiers() == 4) {
					//If it is a right click then show the menu
					int x = e.getX();
					int y = e.getY();
					menu.show(browser.getCanvas(), x, y);
					menu.repaint();
				} else {
					menu.setVisible(false);
				}
			}
		});

		/*
		 * Adding a listener to a menu item
		 */
		menuItem1.addMouseListener(this);
		menuItem2.addMouseListener(this);
		menuItem3.addMouseListener(this);

		/*
		 * Laying out the components with a BorderLayout on a JPanel
		 */

		panel.add(BorderLayout.NORTH, textfield);
		panel.add(BorderLayout.CENTER, browser.getCanvas());

		/*
		 * Loading the URL in the browser canvas
		 */
		browser.loadURL("www.google.com");

		return panel;
	}

	public static void main(String[] args) {
		new JMenuBrowser();
	}

	/*
	 * MouseListener for Menu Items
	 */
	public void mousePressed(java.awt.event.MouseEvent e) {
		if (e.getSource().equals(menuItem1)) {
			System.out.println("Clicked on Menu Item 1");
		}
		if (e.getSource().equals(menuItem2)) {
			System.out.println("Clicked on Menu Item 2");
		}
		if (e.getSource().equals(menuItem3)) {
			System.out.println("Clicked on Menu Item 3");
		}
	}

}

