
public class BondPanel {

	static String spn = "";
	static String ccy = "";
	final static SelectItem curveItem = new SelectItem();
	final static SelectItem cb = new SelectItem();
	final static DataSource  curveListDS = ComboBoxOptionDS.getInstance("Curves");
	final static DataSource  gridDS = GridDS.getInstance("Bonds");
	
	public static Canvas getCanvas() {
		Canvas canvas = new Canvas();
		final DynamicForm form = new DynamicForm();
		final ListGrid grid;

		VLayout vLayout = new VLayout(15);
		vLayout.setAutoWidth();
		vLayout.setAutoHeight();

		HLayout layout = new HLayout(15);
		layout.setWidth("100%");
		layout.setHeight("60%");

		IButton saveButton = new IButton("Save");
		saveButton.setTop(250);
		saveButton.setLeft(110);

		canvas.addChild(saveButton);

		grid = getGrid();

		//Save all Edits
		saveButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				grid.saveAllEdits();

			}
		});

		
		//Refreshes all Edits
		IButton refreshButton = new IButton("Refresh");
		refreshButton.setTop(250);
		refreshButton.setLeft(220);
		refreshButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				grid.refreshFields();
			}
		});

		//For testing combobox indepe
		cb.setAutoFetchData(false);
		cb.setDisplayField("curveName");
		cb.setValueField("curveId");
		cb.setName("curveName");
		form.setItems(cb);
		cb.addDataArrivedHandler(new DataArrivedHandler() {
			
			public void onDataArrived(DataArrivedEvent event) {
				//System.out.println("onDataArrived"+event.getData().getResultSize());
				System.out.println(" onDataArrived :"+event.getData());
				System.out.println(" onDataArrived :"+event.getStartRow());
				System.out.println(" onDataArrived    :"+cb.getDisplayValue());
				System.out.println(" onDataArrived  :"+cb.getValue());
				
			}
		});
		
		
		cb.addChangedHandler(new ChangedHandler() {
			
			public void onChanged(ChangedEvent event) {
				System.out.println("selected val here "+event.getValue());
				System.out.println("on change "+event.getItem().getDisplayValue());
				System.out.println("on change "+event.getItem().getDisplayField());
				System.out.println(""+event.getItem().getDisplayFieldName());
				System.out.println(""+event.getItem().getValueField());
				System.out.println(""+event.getItem().getValueFieldName());
				
			}
		});
		
		layout.addMember(form);
		layout.addMember(refreshButton);
		layout.addMember(saveButton);
		vLayout.addMember(grid);
		vLayout.addMember(layout);
	
		canvas.addChild(vLayout);
		return canvas;
	}

	
	
	
	private static ListGrid getGrid() {

		//final DataSource dataSource = new DataSource();
		final ListGrid grid = new ListGrid();
		grid.setDataSource(gridDS);
		
		 ListGridField INST_NAME = new ListGridField("instrumentName", "Instrument Name");
		 ListGridField FAMILY_ID = new ListGridField("familyId",
					"Family ID");
		 ListGridField SPN = new ListGridField("spn", "SPN");
			SPN.setName("spn");
		 ListGridField curveName = new ListGridField(
					"curveName", "curveName");
		 
		 curveName.setDisplayField("curveName");
		 curveName.setValueField("curveId");
		 curveName.setCanEdit(true);
		 getCurveCombo();
		 curveName.setEditorType(curveItem);
		
		 ListGridField CCY = new ListGridField("ccy", "CCY");
		 CCY.setName("ccy"); 
		 
		 ListGridField curveId = new ListGridField(
					"curveId", "curveId");
		
		 curveId.setHidden(true);
		 
		 grid.setFields(INST_NAME,FAMILY_ID,SPN,curveName,CCY,curveId);
		 
		 grid.addCellClickHandler(new CellClickHandler() {
			Integer colnum = null;

			public void onCellClick(CellClickEvent event) {
				ListGridRecord record = event.getRecord();
				colnum = event.getColNum();
				System.out.println("colnum" + colnum);
				if (colnum.toString().matches("3")) {
					ccy = record.getAttribute("ccy");
					spn = record.getAttribute("spn");
					System.out.println("calling fetchData");
					curveItem.setAutoFetchData(true);
					curveListDS.fetchData();
			
				}

			}
		});

		grid.setWidth(700);
		grid.setHeight(500);
		grid.setStyleName("listGrid");
		grid.setUseAllDataSourceFields(true);
		grid.setAutoFetchData(false);
		grid.setShowAllRecords(true);
		grid.setShowFilterEditor(true);
		return grid;

	}

	private static void getCurveCombo(){
		 
	     OperationBinding fetchCurves = new OperationBinding();
		 fetchCurves.setOperationType(DSOperationType.FETCH);
		 fetchCurves.setDataProtocol(DSProtocol.GETPARAMS);
		 String curveurl = GWT.getModuleBaseURL()+ "getCurves?fetch=curves";
		 fetchCurves.setDataURL(curveurl);
		 
		 curveListDS.setOperationBindings(fetchCurves);
		 
		 curveItem.setAllowEmptyValue(true);
	     curveItem.setAutoFetchData(false);
		 curveItem.setDisplayField("curveName");
		 curveItem.setValueField("curveId");
		 curveItem.setName("curveName");
		 curveItem.setOptionDataSource(curveListDS);
			
		 cb.setOptionDataSource(curveListDS);
	}
	
	
	public static class ComboBoxOptionDS extends DataSource {
		private static ComboBoxOptionDS instance = null;
		public static ComboBoxOptionDS getInstance(String sId) {
		    if (instance == null) {
		    	instance = new ComboBoxOptionDS(sId);
		    }
		    return instance;
		  }
		public ComboBoxOptionDS(String sid){
			setID(id);
			setDataFormat(DSDataFormat.JSON);
			setShowPrompt(false);
			DataSourceTextField curveName = new DataSourceTextField("curveName", "curveName");
			DataSourceTextField curveId = new DataSourceTextField("curveId", "curveId");
			curveId.setHidden(true);
		   	setFields(curveName,curveId);
					
		}
		
		protected Object  transformRequest(DSRequest dsRequest) {
		        System.out.println("inside transformRequest");
			    dsRequest.setPrompt("Hi there " + dsRequest.getDataSource());
		        String operationType = dsRequest.getOperationType().toString();
		        System.out.println("operationType >>>>"+operationType);
			    if (operationType.equals("FETCH")) {   
			    	System.out.println("before" + xmlSerialize(dsRequest.getData()) + "before");
			    	dsRequest.setActionURL(GWT.getModuleBaseURL()+ "getCurves?fetch=curves&spn="+spn+"&ccy="+ccy);
			    }
			    super.transformRequest(dsRequest);
			    return dsRequest.getData();

		    }
		
		protected void transformResponse(DSResponse response, DSRequest request, Object data) {
			
			if (request.getOperationType().equals(DSOperationType.FETCH))
			{
				System.out.println("response here"+response.getData());
				System.out.println(" "+response.getTotalRows());
				
			}
			
			super.transformResponse(response, request, data);

	    }
		
				
		
	}
	
	
	public static class GridDS extends DataSource {
		private static GridDS instance = null;
		
		public static GridDS getInstance(String sId) {
		    if (instance == null) {
		    	instance = new GridDS(sId);
		    }
		    return instance;
		  }
		public GridDS(String sid){
			setID(id);
			setDataFormat(DSDataFormat.JSON);
			setShowPrompt(false);
			String url = GWT.getModuleBaseURL() + "getBonds?fetch=Bonds";
			System.out.println("url here is" + url);
			setDataURL(url);
			setDataFormat(DSDataFormat.JSON);
			
			OperationBinding fetch = new OperationBinding();
			fetch.setOperationType(DSOperationType.FETCH);
			fetch.setDataProtocol(DSProtocol.GETPARAMS);
			fetch.setDataURL(url);
			
		    OperationBinding update = new OperationBinding();
	    	update.setOperationType(DSOperationType.UPDATE);
	    	update.setDataProtocol(DSProtocol.GETPARAMS);
	    	update.setAllowMultiUpdate(true);
	    	update.setDataURL(GWT.getModuleBaseURL()+ "updateCurve?fetch=update");
	    	
		    DataSourceTextField INST_NAME = new DataSourceTextField(
					"instrumentName", "Instrument Name");
			DataSourceTextField FAMILY_ID = new DataSourceTextField("familyId",
					"Family ID");
			DataSourceTextField SPN = new DataSourceTextField("spn", "SPN");
			SPN.setName("spn");
			final DataSourceTextField curveName = new DataSourceTextField(
					"curveName", "curveName");
			curveName.setCanEdit(true);
			curveName.setEditorType(curveItem);
			
			DataSourceTextField CCY = new DataSourceTextField("ccy", "CCY");
			CCY.setName("ccy");
		
			DataSourceTextField curveId = new DataSourceTextField("curveId", "curveId");
			curveId.setName("curveId");
			
			curveId.setHidden(true);
			
			//Fetches and updates the wkcurves
			//setOperationBindings(fetch,update);
			setFields(INST_NAME, FAMILY_ID, SPN, curveName, CCY,curveId);
						
		}
	}

}
