package com.impart.kurs.swt;

import java.net.URL;
import java.util.Vector;

import org.eclipse.jface.window.ApplicationWindow;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Resource;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;

public class Main extends ApplicationWindow {
	final Display display;

	final Shell shell;

	Vector vResource = new Vector();

	Image appImg = null;

	Image printImg = null;

	Image saveImg = null;

	public Main() {
		super(null);
		display = new Display();
		shell = new Shell();

		GridLayout gridLayout = new GridLayout ();
		gridLayout.numColumns = 2;
		shell.setLayout (gridLayout);

	}

	protected void createMenuBar() {
		final Menu menu = new Menu(shell, SWT.BAR);
		shell.setMenuBar(menu);

		createMenuFile(menu);
		createMenuOption(menu);
	}

	private void createMenuFile(final Menu menu) {
		final MenuItem file = new MenuItem(menu, SWT.CASCADE);
		file.setText("File");

		final Menu f = new Menu(shell, SWT.DROP_DOWN);
		file.setMenu(f);

		final MenuItem open = new MenuItem(f, SWT.PUSH);
		open.setText("open");
		final MenuItem exit = new MenuItem(f, SWT.PUSH);
		exit.setText("exit");
		exit.setAccelerator(SWT.F10);

		exit.addSelectionListener(new SelectionListener() {

			public void widgetSelected(SelectionEvent e) {
				shell.close();
			}

			public void widgetDefaultSelected(SelectionEvent e) {
			}

		});
	}

	private void createMenuOption(final Menu menu) {
		final MenuItem option = new MenuItem(menu, SWT.CASCADE);
		option.setText("Option");

		final Menu o = new Menu(shell, SWT.DROP_DOWN);
		option.setMenu(o);

		final MenuItem check = new MenuItem(o, SWT.CHECK);
		check.setText("check option");
		final MenuItem sep = new MenuItem(o, SWT.SEPARATOR);
		final MenuItem radio1 = new MenuItem(o, SWT.RADIO);
		radio1.setText("radio1");
		final MenuItem radio2 = new MenuItem(o, SWT.RADIO);
		radio2.setText("radio2");

		check.addSelectionListener(new SelectionListener() {

			public void widgetSelected(SelectionEvent e) {
				if (check.getSelection()) {
					System.out.println("is checked");
				} else {
					System.out.println("is NOT checked");
				}
			}

			public void widgetDefaultSelected(SelectionEvent e) {
			}

		});
	}

	protected void createPopUp() {
		Menu menu = new Menu(shell, SWT.POP_UP);
		MenuItem item = new MenuItem(menu, SWT.CASCADE);
		item.setText("item 1");
		MenuItem item2 = new MenuItem(menu, SWT.CASCADE);
		item2.setText("item 2");
		shell.setMenu(menu);

	}

	protected void createToolBar() {
		ToolBar toolBar = new ToolBar(shell, SWT.HORIZONTAL);
		toolBar.setSize(300, 50);
		toolBar.setLocation(0, 0);

		GridData data = new GridData ();
		data.horizontalSpan=2;
		toolBar.setLayoutData(data);

		ToolItem save = new ToolItem(toolBar, SWT.PUSH);
		save.setText("save");
		save.setImage(saveImg);

		ToolItem print = new ToolItem(toolBar, SWT.PUSH);
		print.setText("print");
		print.setImage(printImg);

		ToolItem sep = new ToolItem(toolBar, SWT.SEPARATOR);

		ToolItem check = new ToolItem(toolBar, SWT.CHECK);
		check.setText("show detail");

		ToolItem sep1 = new ToolItem(toolBar, SWT.SEPARATOR);

		ToolItem radio1 = new ToolItem(toolBar, SWT.RADIO);
		radio1.setText("radio1");
		ToolItem radio2 = new ToolItem(toolBar, SWT.RADIO);
		radio2.setText("radio2");

	}

	protected void creatImages() {
		URL imgUrl = this.getClass().getResource("myapp.gif");
		appImg = new Image(display, imgUrl.getPath());

		imgUrl = this.getClass().getResource("save.gif");
		saveImg = new Image(display, imgUrl.getPath());

		imgUrl = this.getClass().getResource("print.gif");
		printImg = new Image(display, imgUrl.getPath());

		vResource.add(appImg);
		vResource.add(saveImg);
		vResource.add(printImg);

	}

	protected void createPartControl() {
		Font txtFont = new Font(display, "Arial", 10, SWT.BOLD | SWT.ITALIC);
		vResource.add(txtFont);
		Font lblFont = new Font(display, "Arial", 10, SWT.BOLD);
		vResource.add(lblFont);

		GridData data = new GridData ();
		data.widthHint = 100;
		data.heightHint = 20;

		final Label lbl1 = new Label(shell, SWT.SINGLE);
		lbl1.setFont(lblFont);
		lbl1.setText("Name");
		lbl1.setLayoutData(data);
		
		final Text txtName = new Text(shell, SWT.SINGLE);
		txtName.setTextLimit(10);
		txtName.setFont(txtFont);
		txtName.setLayoutData(data);

		final Label lbl2 = new Label(shell, SWT.SINGLE);
		lbl2.setFont(lblFont);
		lbl2.setText("Vorname");
		lbl2.setLayoutData(data);


		final Text txtVorname = new Text(shell, SWT.SINGLE | SWT.BORDER);
		txtVorname.setTextLimit(10);
		txtVorname.setFont(txtFont);
		txtVorname.setLayoutData(data);

		Control[] c = { txtVorname, txtName };
		shell.setTabList(c);

	}

	protected void desposeResource() {
		for (int i = 0; i < vResource.size(); i++) {
			try {
				((Resource) vResource.get(i)).dispose();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	protected Control createContents(Composite parent) {

		creatImages();

		createMenuBar();
		createPopUp();
		createToolBar();
		createPartControl();

		return parent;
	}
	
	protected void configureShell(Shell shell) {
		
		shell.setSize(500, 300);
		shell.setText("Uebung 1");

		shell.setImage(appImg);

		shell.pack();
		shell.open();
	}
	
	public boolean close() {
		desposeResource();
		display.dispose();
		return super.close();
	}

	public static void main(String[] args) {
		Main window = new Main();
		window.setBlockOnOpen(true);
		window.open();
	}

}
