/*
 * Fichero:     seleccion_busqueda.java
 * Fecha:       20/10/06
 * Autor:       Alvaro Sarmiento Losada
 * Asignatura:  Proyecto Fin de Carrera
 * Descripción: Clase que representa una interfaz de diálogo con el usuario
 * 				a través de la cual se muestran los diferentes tipos de 
 * 				elementos encontrados en la base de datos que se ajustaban
 * 				a su petición de búsqueda.
 *
 * Este fichero pertenece al proyecto fin de carrera del autor,
 * consistente en el estudio de JXTA y su aplicación al comercio 
 * electrónico b2b.
 */


package com.pfc.ventanas;
import com.cloudgarden.resource.SWTResourceManager;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

/**
 * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI Builder, which is free for non-commercial use. If Jigloo is being used commercially (ie, by a corporation, company or business for any purpose whatever) then you should purchase a license for each developer using Jigloo. Please visit www.cloudgarden.com for details. Use of Jigloo implies acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
public class seleccion_busqueda extends org.eclipse.swt.widgets.Dialog {

	private Shell dialogShell;
	private Text Eleccion;
	private List list1;
	static String anterior_eleccion=new String();
	static String eleccion=new String();
	String resultado=new String();
	private Button button1;
	private Label label2;
	private Label label1;

	/**
	* Auto-generated main method to display this 
	* org.eclipse.swt.widgets.Dialog inside a new Shell.
	*/
	/*public static void main(String[] args) {
		try {
			Display display = Display.getDefault();
			Shell shell = new Shell(display);
			seleccion_busqueda inst = new seleccion_busqueda(shell, SWT.NULL);
			inst.open(array);
			System.out.println(eleccion);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}*/

	public seleccion_busqueda(Shell parent, int style) {
		super(parent, style);
	}

	public String open(String []array) {
		try {
			Shell parent = getParent();
			dialogShell = new Shell(parent, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);

				{
					//Register as a resource user - SWTResourceManager will
					//handle the obtaining and disposing of resources
					SWTResourceManager.registerResourceUser(dialogShell);
				}


			FormLayout dialogShellLayout = new FormLayout();
			dialogShell.setLayout(dialogShellLayout);
			dialogShell.layout();
			dialogShell.pack();
			dialogShell.setSize(463, 237);
			dialogShell.setText("Catalogo v1.0");
			{
				button1 = new Button(dialogShell, SWT.PUSH | SWT.CENTER);
				button1.setText("Aceptar");
				FormData button1LData = new FormData();
				button1LData.width = 49;
				button1LData.height = 21;
				button1LData.left = new FormAttachment(0, 1000, 364);
				button1LData.top = new FormAttachment(0, 1000, 168);
				button1.setLayoutData(button1LData);
				button1.addMouseListener(new MouseAdapter() {
					public void mouseDoubleClick(MouseEvent evt) {
						button1MouseDoubleClick(evt);
					}
				});
			}
			{
				label2 = new Label(dialogShell, SWT.NONE);
				label2.setText("Eleccion:");
				FormData label2LData = new FormData();
				label2LData.width = 42;
				label2LData.height = 14;
				label2LData.left =  new FormAttachment(0, 1000, 70);
				label2LData.top =  new FormAttachment(0, 1000, 161);
				label2.setLayoutData(label2LData);
			}
			{
				label1 = new Label(dialogShell, SWT.NONE);
				label1.setText("Elementos:");
				FormData label1LData = new FormData();
				label1LData.width = 56;
				label1LData.height = 14;
				label1LData.left =  new FormAttachment(0, 1000, 63);
				label1LData.top =  new FormAttachment(0, 1000, 21);
				label1.setLayoutData(label1LData);
			}
			{
				/*anterior_eleccion=null;
				eleccion=null;*/
			
				Eleccion = new Text(dialogShell, SWT.MULTI
					| SWT.WRAP
					| SWT.H_SCROLL
					| SWT.BORDER);
				FormData EleccionLData = new FormData();
				EleccionLData.width = 170;
				EleccionLData.height = 15;
				EleccionLData.top =  new FormAttachment(0, 1000, 154);
				EleccionLData.left =  new FormAttachment(0, 1000, 140);
				Eleccion.setLayoutData(EleccionLData);
				Eleccion.setCursor(SWTResourceManager.getCursor(SWT.CURSOR_WAIT));
				Eleccion.addKeyListener(new KeyAdapter() {
					public void keyPressed(KeyEvent ev){
					 	int r=ev.keyCode;
					    char a=(char)r;	
					    if(r==8){  
					    	
					    	
					    		anterior_eleccion=eleccion.substring(0,(eleccion.length()-1));
					    		eleccion=anterior_eleccion;
					    		
					    	
					    }
					    else		
					    	eleccion=eleccion+a;
					  
					
					}});
			}
			{
				int i=0;
				
				list1 = new List(dialogShell, SWT.MULTI
					| SWT.H_SCROLL
					| SWT.V_SCROLL
					| SWT.BORDER);
				for(i=0;i<array.length;i++)
					list1.add(array[i]);
				
				FormData list1LData = new FormData();
				list1LData.width = 158;
				list1LData.height = 91;
				list1LData.left =  new FormAttachment(0, 1000, 140);
				list1LData.top =  new FormAttachment(0, 1000, 21);
				list1.setLayoutData(list1LData);
			}
			dialogShell.open();
			Display display = dialogShell.getDisplay();
			while (!dialogShell.isDisposed()) {
				if (!display.readAndDispatch())
					display.sleep();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		/*resultado=eleccion;
		eleccion="";*/
		
		return resultado;
	}
	
	/**
	 * @return  the list1
	 * @uml.property  name="list1"
	 */
	public List getList1() {
		return list1;
	}
	
	private void button1MouseDoubleClick(MouseEvent evt) {
		if (Eleccion.getText()!="" ){
			
			//elementos_busqueda=open();
			eleccion=Eleccion.getText();
			resultado=eleccion;			
			eleccion="";
			dialogShell.close();
			System.gc();
		}
	}

}
