/*
 * Fichero:     elementosBusqueda.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 indica el tipo de productos que 
 * 				deseamos encontrar en nuestra búsqueda en la base de datos.
 *
 * 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 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.Shell;
import org.eclipse.swt.widgets.Text;
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;

/**
* 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 elementosBusqueda extends org.eclipse.swt.widgets.Dialog {

	private Shell dialogShell;
	private Text categoria;
	private Text producto;
	private Text atributos;
	static String category=new String();
	static String anterior_categoria=new String();
	static String product=new String();
	private Label label4;
	private Label label3;
	private Label label2;
	private Button button1;
	private Label label1;
	public String[] elementos_busqueda;
	static String anterior_producto=new String();
	static String atributs=new String();
	static String anterior_atributos=new String();
	String []resultado=new String[4];

	/**
	* 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);
			elementosBusqueda inst = new elementosBusqueda(shell, SWT.NULL);
			//inst.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

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

	public String[] open() {

		try {
			Shell parent = getParent();
			dialogShell = new Shell(parent, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);

			FormLayout dialogShellLayout = new FormLayout();
			dialogShell.setLayout(dialogShellLayout);
			dialogShell.layout();
			dialogShell.pack();
			dialogShell.setSize(442, 230);
			dialogShell.setText("Catalogo v1.0");
			{
				label4 = new Label(dialogShell, SWT.NONE);
				label4.setText("Número de atributos:");
				FormData label4LData = new FormData();
				label4LData.width = 105;
				label4LData.height = 14;
				label4LData.left =  new FormAttachment(0, 1000, 42);
				label4LData.top =  new FormAttachment(0, 1000, 140);
				label4.setLayoutData(label4LData);
			}
			{
				label3 = new Label(dialogShell, SWT.NONE);
				label3.setText("Producto:");
				FormData label3LData = new FormData();
				label3LData.width = 49;
				label3LData.height = 14;
				label3LData.left =  new FormAttachment(0, 1000, 42);
				label3LData.top =  new FormAttachment(0, 1000, 98);
				label3.setLayoutData(label3LData);
			}
			{
				label2 = new Label(dialogShell, SWT.NONE);
				label2.setText("Categoria:");
				FormData label2LData = new FormData();
				label2LData.width = 49;
				label2LData.height = 14;
				label2LData.left =  new FormAttachment(0, 1000, 42);
				label2LData.top =  new FormAttachment(0, 1000, 56);
				label2.setLayoutData(label2LData);
			}
			{
				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, 343);
				button1LData.top = new FormAttachment(0, 1000, 154);
				button1.setLayoutData(button1LData);
				button1.addMouseListener(new MouseAdapter() {
					public void mouseDoubleClick(MouseEvent evt) {
						button1MouseDoubleClick(evt);
					}
				});
			}
			{
				label1 = new Label(dialogShell, SWT.NONE);
				label1.setText("Introduzca los valores por los que efectuará la búsqueda:");
				FormData label1LData = new FormData();
				label1LData.width = 301;
				label1LData.height = 14;
				label1LData.left =  new FormAttachment(0, 1000, 84);
				label1LData.top =  new FormAttachment(0, 1000, 21);
				label1.setLayoutData(label1LData);
			}
			{
				atributos = new Text(dialogShell, SWT.MULTI | SWT.BORDER);
				FormData atributosLData = new FormData();
				atributosLData.width = 37;
				atributosLData.height = 15;
				atributosLData.left =  new FormAttachment(0, 1000, 154);
				atributosLData.top =  new FormAttachment(0, 1000, 133);
				atributos.setLayoutData(atributosLData);
			/*	atributos.addKeyListener(new KeyAdapter() {
					public void keyPressed(KeyEvent ev){
					 	int r=ev.keyCode;
					 	char a=(char)r;	
					    if(r==8){  
					    	anterior_atributos=atributs.substring(0,(atributs.length()-1));
					    	atributs=anterior_atributos;
					    }
					    else		
					    	atributs=atributs+a;
				
					    
					 }
				});*/
			}
			{
				producto = new Text(dialogShell, SWT.MULTI | SWT.BORDER);
				FormData productoLData = new FormData();
				productoLData.width = 219;
				productoLData.height = 15;
				productoLData.left =  new FormAttachment(0, 1000, 154);
				productoLData.top =  new FormAttachment(0, 1000, 91);
				producto.setLayoutData(productoLData);
			/*	producto.addKeyListener(new KeyAdapter() {
					public void keyPressed(KeyEvent ev){
					 	int r=ev.keyCode;
					 	char a=(char)r;	
					    if(r==8){  
					    	anterior_producto=product.substring(0,(product.length()-1));
					    	product=anterior_producto;
					    }
					    else		
					    	product=product+a;
				
					    
					 }
				});*/
			}
			{
				categoria = new Text(dialogShell, SWT.MULTI | SWT.BORDER);
				FormData categoriaLData = new FormData();
				categoriaLData.width = 219;
				categoriaLData.height = 15;
				categoriaLData.left =  new FormAttachment(0, 1000, 154);
				categoriaLData.top =  new FormAttachment(0, 1000, 49);
				categoria.setLayoutData(categoriaLData);
				/*categoria.addKeyListener(new KeyAdapter() {
					public void keyPressed(KeyEvent ev){
					 	int r=ev.keyCode;
					 	char a=(char)r;	
					    if(r==8){  
					    	anterior_categoria=category.substring(0,(category.length()-1));
					    	category=anterior_categoria;
					    }
					    else		
					    	category=category+a;
				
					    
					 }
				});*/
			}
			dialogShell.open();
			Display display = dialogShell.getDisplay();
			while (!dialogShell.isDisposed()) {
				if (!display.readAndDispatch())
					display.sleep();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		/*category=categoria.getText();
		product=producto.getText();
		atributs=atributos.getText();
		resultado[0]=category;
		resultado[1]=product;
		resultado[2]="atributos";
		resultado[3]=atributs;
		
		category="";
		product="";
		atributs="";*/
		return resultado;
	}
	
	private void button1MouseDoubleClick(MouseEvent evt) {
		int atributo=-1;
		if (categoria.getText()!="" && producto.getText()!="" && atributos.getText()!="" ){
			try{
				atributo = Integer.parseInt(atributos.getText());
			}catch(Exception e){
				atributo =-1;
			}
			if (atributo !=-1){
				//elementos_busqueda=open();
				category=categoria.getText();
				product=producto.getText();
				atributs=atributos.getText();
				resultado[0]=category;
				resultado[1]=product;
				resultado[2]="atributos";
				resultado[3]=atributs;
				dialogShell.close();
				System.gc();
			}
			
			//...
		
			System.gc();
		}
		
		System.out.println("button1.mouseDoubleClick, event=" + evt);
		//TODO add your code for button1.mouseDoubleClick
	}

}
