import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.*;

public class PopupMenuDemo {

	public static Display myDisplay;
	public static boolean internalCall = false;

	public static void main(String[] args) {
		internalCall = true;
		myDisplay = new Display();
		PopupMenuDemo md = new PopupMenuDemo();
		md.runDemo(myDisplay);	
	}
	
	public void runDemo(Display display) {
		myDisplay = display;
		Shell shell = new Shell(display);
		shell.setText("PopUp Menu Demo");
		
		//create a popup menu
		Menu popupmenu = new Menu(shell, SWT.POP_UP);

		//add a MenuItem to the Popup menu
		MenuItem actionItem = new MenuItem(popupmenu, SWT.PUSH);
		actionItem.setText("Other Action");		

		//add a listener for the action
		actionItem.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event e) {
				System.out.println("Other Action performed!");			
			}
		});

		//create a menu for the File option
		Menu popupmenu2 = new Menu(shell, SWT.POP_UP);

		//add a MenuItem to the File menu
		MenuItem buttonItem = new MenuItem(popupmenu2, SWT.PUSH);
		buttonItem.setText("Button Action");		

		//add a listener for the action
		buttonItem.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event e) {
				System.out.println("Button Action performed!");			
			}
		});

		//create a composite to add a pop-up menu to
		Composite c1 = new Composite (shell, SWT.BORDER);
		c1.setSize (100, 100);
		c1.setLocation(25,25);

		//create a button, to add a different menu to
		Button b = new Button(c1, SWT.PUSH);
		b.setText("Button");
		b.setSize(50,50);
		b.setLocation(25,25);
		
		//set the pop-up menus for the button, composite, and shell
		b.setMenu(popupmenu2);
		c1.setMenu (popupmenu);
		shell.setMenu (popupmenu);

		shell.setSize(175,200);
		shell.open();
		while(!shell.isDisposed())
			if(!display.readAndDispatch())
				display.sleep();
		if (internalCall) display.dispose();		
	}
}
