/*
 * Fichero:     mustraDeAtributos.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 todos los atributos del 
 * 				producto especificado previamente.
 *
 * 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.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;
import org.eclipse.swt.SWT;
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 muestraDeAtributos extends org.eclipse.swt.widgets.Dialog {

	private Shell dialogShell;
	private List atributos;
	private Text eleccion_valor;
	private Text eleccion_atributo;
	private List valores;
	static String anterior_atributo=new String();
	private Button button1;
	private Label label4;
	private Label label3;
	private Label label2;
	private Label label1;
	static String atributo=new String();
	static String anterior_valor=new String();
	static String valor=new String();
	String []resultado=new String[2];

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

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

	public String [] open(String []array,String []tabla) {
		try {
			Shell parent = getParent();
			parent.setVisible(false);
			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(512, 489);
			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, 406);
				button1LData.top = new FormAttachment(0, 1000, 420);
				button1.setLayoutData(button1LData);
				button1.addMouseListener(new MouseAdapter() {
					public void mouseDoubleClick(MouseEvent evt) {
						button1MouseDoubleClick(evt);
					}
				});
			}
			{
				label4 = new Label(dialogShell, SWT.NONE);
				label4.setText("Eleccion:");
				FormData label4LData = new FormData();
				label4LData.width = 42;
				label4LData.height = 14;
				label4LData.left =  new FormAttachment(0, 1000, 287);
				label4LData.top =  new FormAttachment(0, 1000, 378);
				label4.setLayoutData(label4LData);
			}
			{
				label3 = new Label(dialogShell, SWT.NONE);
				label3.setText("Eleccion:");
				FormData label3LData = new FormData();
				label3LData.width = 42;
				label3LData.height = 14;
				label3LData.left =  new FormAttachment(0, 1000, 28);
				label3LData.top =  new FormAttachment(0, 1000, 378);
				label3.setLayoutData(label3LData);
			}
			{
				label2 = new Label(dialogShell, SWT.NONE);
				label2.setText("Valor:");
				FormData label2LData = new FormData();
				label2LData.width = 28;
				label2LData.height = 14;
				label2LData.left =  new FormAttachment(0, 1000, 294);
				label2LData.top =  new FormAttachment(0, 1000, 28);
				label2.setLayoutData(label2LData);
			}
			{
				label1 = new Label(dialogShell, SWT.NONE);
				label1.setText("Atributos:");
				FormData label1LData = new FormData();
				label1LData.width = 49;
				label1LData.height = 14;
				label1LData.left =  new FormAttachment(0, 1000, 28);
				label1LData.top =  new FormAttachment(0, 1000, 28);
				label1.setLayoutData(label1LData);
			}
			{
				eleccion_valor = new Text(dialogShell, SWT.BORDER);
				FormData eleccion_valorLData = new FormData();
				eleccion_valorLData.width = 128;
				eleccion_valorLData.height = 15;
				eleccion_valorLData.left =  new FormAttachment(0, 1000, 343);
				eleccion_valorLData.top =  new FormAttachment(0, 1000, 371);
				eleccion_valor.setLayoutData(eleccion_valorLData);
				/*eleccion_valor.addKeyListener(new KeyAdapter() {
					public void keyPressed(KeyEvent ev){
					 	int r=ev.keyCode;
					    char a=(char)r;	
					    if(r==8){  
					    	
					    	
					    		anterior_valor=valor.substring(0,(valor.length()-1));
					    		valor=anterior_valor;
					    		
					    	
					    }
					    else		
					    	valor=valor+a;
					  
					
					}
				});*/
			}
			{
				eleccion_atributo = new Text(dialogShell, SWT.BORDER);
				FormData eleccion_atributoLData = new FormData();
				eleccion_atributoLData.width = 135;
				eleccion_atributoLData.height = 15;
				eleccion_atributoLData.left =  new FormAttachment(0, 1000, 91);
				eleccion_atributoLData.top =  new FormAttachment(0, 1000, 371);
				eleccion_atributo.setLayoutData(eleccion_atributoLData);
				/*eleccion_atributo.addKeyListener(new KeyAdapter() {
					public void keyPressed(KeyEvent ev){
					 	int r=ev.keyCode;
					    char a=(char)r;	
					    if(r==8){  
					    						    	
					    		anterior_atributo=atributo.substring(0,(atributo.length()-1));
					    		atributo=anterior_atributo;
					    		
					    	
					    }
					    else		
					    	atributo=atributo+a;
					  
					
					}
				});*/
			}
			{
				valores = new List(dialogShell, SWT.MULTI
					| SWT.H_SCROLL
					| SWT.V_SCROLL
					| SWT.BORDER);
				int i=0;
				for(i=0;i<tabla.length;i++)
					valores.add(tabla[i]);
				FormData valoresLData = new FormData();
				valoresLData.width = 116;
				valoresLData.height = 315;
				valoresLData.left =  new FormAttachment(0, 1000, 343);
				valoresLData.top =  new FormAttachment(0, 1000, 28);
				valores.setLayoutData(valoresLData);
			}
			{
				atributos = new List(dialogShell, SWT.MULTI
					| SWT.H_SCROLL
					| SWT.V_SCROLL
					| SWT.BORDER);
				
				int i=0;
				for(i=0;i<array.length;i++)
					atributos.add(array[i]);
				FormData atributosLData = new FormData();
				atributosLData.width = 123;
				atributosLData.height = 315;
				atributosLData.left =  new FormAttachment(0, 1000, 91);
				atributosLData.top =  new FormAttachment(0, 1000, 28);
				atributos.setLayoutData(atributosLData);
				atributos.setCursor(SWTResourceManager.getCursor(SWT.CURSOR_ARROW));
			}
			dialogShell.open();
			Display display = dialogShell.getDisplay();
			while (!dialogShell.isDisposed()) {
				if (!display.readAndDispatch())
					display.sleep();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		/*resultado[0]=atributo;
		resultado[1]=valor;
		
		atributo="";
		valor="";*/
		
		return resultado;
	}
	
	private void button1MouseDoubleClick(MouseEvent evt) {

		if (eleccion_atributo.getText()!="" && eleccion_valor.getText()!="" ){
			
				//elementos_busqueda=open();
				atributo=eleccion_atributo.getText();
				valor=eleccion_valor.getText();
				resultado[0]=atributo;
				resultado[1]=valor;
				atributo="";
				valor="";
				dialogShell.close();
				System.gc();
			}
				
		}
		
		
	

}
