import org.eclipse.swt.SWT;
import org.eclipse.swt.events.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.*;

public class ToolBarDemo {

	public static Display myDisplay;
	public static boolean internalCall = false;

	public static void main(String[] args) {
		internalCall = true;
		myDisplay = new Display();
		ToolBarDemo tbd = new ToolBarDemo();
		tbd.runDemo(myDisplay);
	}

	public void runDemo(Display display) {
		myDisplay = display;
		Shell shell = new Shell(display);
		shell.setText("Tool Bar Demo");

		//create the toolbar, then position and size it
		final ToolBar bar = new ToolBar(shell, SWT.HORIZONTAL);
		bar.setSize(470, 150);
		bar.setLocation(10, 10);

		//load an image for the buttons
		Image icon = new Image(display, "EclipseBannerPic.jpg");

		//Create 3 push buttons: 
		//-one with text
		//-one with an image
		//-one with both
		ToolItem pushItem1 = new ToolItem(bar, SWT.PUSH);
		pushItem1.setText("Push");

		ToolItem pushItem2 = new ToolItem(bar, SWT.PUSH);
		pushItem2.setImage(icon);

		ToolItem pushItem3 = new ToolItem(bar, SWT.PUSH);
		pushItem3.setText("Push");
		pushItem3.setImage(icon);

		//Place a separator (space between this and next button)
		ToolItem sep = new ToolItem(bar, SWT.SEPARATOR);

		//Add a checkable tool button
		ToolItem checkItem = new ToolItem(bar, SWT.CHECK);
		checkItem.setText("Check");

		//Place a 2nd separator 
		ToolItem sep2 = new ToolItem(bar, SWT.SEPARATOR);

		//Add 2 radio-style buttons
		//note that the radio behaviour is automatic
		ToolItem radioItem1 = new ToolItem(bar, SWT.RADIO);
		radioItem1.setText("Radio 1");
		ToolItem radioItem2 = new ToolItem(bar, SWT.RADIO);
		radioItem2.setText("Radio 2");

		//Place a 3rd separator 
		ToolItem sep3 = new ToolItem(bar, SWT.SEPARATOR);
		//demonstrate that only adjacent radio buttons
		//behave as a radio grouping
		ToolItem radioItem3 = new ToolItem(bar, SWT.RADIO);
		radioItem3.setText("Radio 3");
		ToolItem radioItem4 = new ToolItem(bar, SWT.RADIO);
		radioItem4.setText("Radio 4");

		//Add a drop-down button to the bar
		//To create the drop-down menu effect, you must trap the 
		//selection event and use its (x,y) coordinates to position
		//a pop-up menu just below the button.  
		//An example snippet is provided on the Eclipse.org
		//web site.
		final ToolItem dropdown = new ToolItem(bar, SWT.DROP_DOWN);
		dropdown.setText("Drop-down");
		final Menu menu = new Menu(shell, SWT.POP_UP);
		MenuItem choice = new MenuItem(menu, SWT.PUSH);
		choice.setText("Choices");

		dropdown.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event event) {
				if (event.detail == SWT.ARROW) {
					Rectangle rect = dropdown.getBounds();
					Point pt = new Point(rect.x, rect.y + rect.height);
					pt = bar.toDisplay(pt);
					menu.setLocation(pt.x, pt.y);
					menu.setVisible(true);
				}
			}
		});

		//the following code adds a standard selectionlistener
		//that prints to the console when the first SWT.PUSH button 
		//gets selected
		pushItem1.addSelectionListener(new SelectionListener() {
			public void widgetSelected(SelectionEvent e) {
				System.out.println("Push button one selected.");
			}
			public void widgetDefaultSelected(SelectionEvent e) {
			}
		});

		shell.setSize(500, 110);
		shell.open();
		while (!shell.isDisposed())
			if (!display.readAndDispatch())
				display.sleep();
		if (internalCall) display.dispose();
		icon.dispose();
	}

}
