package com.impart.kurs.swt;

import java.net.URL;

import org.eclipse.jface.resource.ImageRegistry;
import org.eclipse.jface.window.ApplicationWindow;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.RowData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Button;

import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;

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

	Shell shell = null;
	ImageRegistry imageRegistry=null;
	
	public Main() {
		super(null);
	}
	
	protected Control createContents(Composite parent) {
		
		final Label lbl1=new Label(shell,SWT.SINGLE);
		lbl1.setBounds(0,60,100,20);
		lbl1.setText("Name");
		
		final Text txtName=new Text(shell,SWT.SINGLE);
		txtName.setBounds(100,60,100,20);
		txtName.setTextLimit(10);
		
		final Button button = new Button(shell, SWT.PUSH);
		button.setBounds(0,100,100,20);
		button.setText("OK");
		
		button.addSelectionListener(new SelectionAdapter(){
			
			public void widgetSelected(SelectionEvent event) {
				System.out.println("Click");
			}
		});
		
		button.addMouseListener(new MouseAdapter() {
			
			public void mouseDoubleClick(MouseEvent event) {
				System.out.println("Double Click");
			}
		});
		
//		RowData rd=new RowData(300,100);
		PersonTable personTable=new PersonTable(shell,SWT.NO_BACKGROUND);
//		personTable.setLayoutData(rd);
		
		return parent;
	}
	
	protected void configureShell(Shell shell) {
		
		this.shell = shell;
		imageRegistry=new ImageRegistry();
//		fontRegistry = new FontRegistry();
		
		Image appImg = null;
		URL imgUrl = this.getClass().getResource("myapp.gif");
		appImg = new Image(shell.getDisplay(), imgUrl.getPath());
		imageRegistry.put("myapp",appImg);
		
		shell.setText("Eine Uebung");
        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();
	}

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