package com.impart.kurs.swt;

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

import org.eclipse.jface.resource.ImageRegistry;
import org.eclipse.jface.window.ApplicationWindow;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;

public class Main extends ApplicationWindow {
	
	// um jface zu benutzen, muessen die folgenden libraries eingebunden sein
	// org.eclipse.jface_3.1.1.jar
	// org.eclipse.swt.win32.x86_3.1.2.jar
	// org.eclipse.core.runtime_3.1.2.jar

	ImageRegistry imageRegistry=null;
	
//	final Display display = new Display();
//	final Shell shell = new Shell();
	Vector<Resource> vResource =new Vector();
	Image appImg=null;
	Image printImg=null;
	Image saveImg=null;
	
	public Main() {
		super(null);
	}
	
	protected Control createContents(Composite parent) {
		
imageRegistry=new ImageRegistry();
		
		
		
		return parent;
	}
	
	protected void configureShell(Shell shell) {
		
imageRegistry=new ImageRegistry();
		
		Image appImg = null;
		URL imgUrl = this.getClass().getResource("myapp.gif");
		appImg = new Image(shell.getDisplay(), imgUrl.getPath());
		imageRegistry.put("myapp",appImg);
		
		shell.setText("Uebung 100");
        shell.setImage(appImg);
        shell.setSize(400,300);

        
//        shell.getDisplay().syncExec(new Thread());
//        shell.getDisplay().asyncExec(new Thread());
	}
	
	public boolean close() {
		imageRegistry.dispose();
		return super.close();
	}
	
	
	
	
	
	
	
	
	
	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);
		
		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);
		
		final Label lbl1=new Label(shell,SWT.SINGLE);
		lbl1.setBounds(0,60,100,20);
		lbl1.setFont(lblFont);
		lbl1.setText("Name");
		
		final Label lbl2=new Label(shell,SWT.SINGLE);
		lbl2.setBounds(0,90,100,20);
		lbl2.setFont(lblFont);
		lbl2.setText("Vorname");
		
		final Text txtName=new Text(shell,SWT.SINGLE);
		txtName.setBounds(100,60,100,20);
		txtName.setTextLimit(10);
		txtName.setFont(txtFont);
		
		final Text txtVorname=new Text(shell,SWT.SINGLE | SWT.BORDER);
		txtVorname.setBounds(100,90,100,20);
		txtVorname.setTextLimit(10);
		txtVorname.setFont(txtFont);
		
		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();
//			}
//		}
		for(Resource res:vResource){
			try{
				res.dispose();
			}catch(Exception e){
				e.printStackTrace();
			}
		}
	}
	
//	public void open(){
//
//		shell.setSize(500, 300);
//		shell.setText("Uebung 1");
//		
//		creatImages();
//		
//		shell.setImage(appImg);
//		
//		createMenuBar();
//		createPopUp();
//		createToolBar();
//		createPartControl();
//
//		shell.open();
//		while (!shell.isDisposed()) {
//			
//			if (!display.readAndDispatch())
//				display.sleep();
//		}
		
		desposeResource();
		display.dispose();
	}

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