
package com.curiam.client.componentes;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestBuilder;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.RequestException;
import com.google.gwt.http.client.Response;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.FlexTable.FlexCellFormatter;

public class Utiles {
	
	/*** llamada POST ***/
	
	public static void doPost(String url, String postData){
		RequestBuilder builder = new RequestBuilder(RequestBuilder.POST,url);
		builder.setHeader("Content-type","application/x-www-form-urlencoded"); 

			try {
				final Request response = builder.sendRequest(postData, new RequestCallback(){
				final DialogBox d1 = new DialogBox();
				
				public void onError(Request request, Throwable exception) {
					d1.setText("onError");
					d1.center();
					d1.show();
				}

				public void onResponseReceived(Request request, Response response) {
					
					
					if (200 == response.getStatusCode()) { 
				        // Process the response in response.getText() 
						//d1.setText("onResponseReceived request:200 " + response.getText());
						d1.add(new HTML(response.getText()));
					  } else { 
				        // Handle the error.  Can get the status text from response.getStatusText() 
				    	d1.setText("onResponseReceived request:" + response.getStatusCode());
				      } 

					
					d1.center();
					d1.show();
				}

				});
				
			} catch (RequestException e) {
				final DialogBox d_exc = new DialogBox();
				e.printStackTrace();
				d_exc.setText("Failed to send the request:" + e.getMessage());
				d_exc.center();
				d_exc.show();
			}
	}
	
	
	
	/*** Mensaje de error en dialogBox ***/
	
	public static void msj(String titulo, String texto){
		
		final DialogBox db = new DialogBox();
		final FlexTable tab = new FlexTable();
		final HTML txt = new HTML();
		
		FlexCellFormatter cellFormatter = tab.getFlexCellFormatter();
		cellFormatter.setHorizontalAlignment(1, 0, HasHorizontalAlignment.ALIGN_CENTER);
		
		txt.setHTML(texto);
		
		final Button cerrar = new Button("<font size=2>Cerrar</font>",new ClickHandler() {
			
			public void onClick(ClickEvent event) {
				db.hide();
			}
		});
		
		tab.setCellSpacing(6);
		tab.setWidget(0, 0, txt);
		tab.setCellPadding(100);
		tab.setWidget(1, 0, cerrar);
		db.setWidget(tab);
		db.setText(titulo);
		db.setGlassEnabled(true);
	    db.setAnimationEnabled(true);
		db.center();
		db.show();
	}
	
	/*** Controlar tamaņo de letra ***/
	
	public String f(String font){
		return font = "<font size=2>"+font+"<font>";
	}
	
	
}

