package authoritymanager.client;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.VerticalPanel;

public class StatusPanel extends VerticalPanel {
	private String fileName;
	private AuthorityManagerView view ;

	public StatusPanel(AuthorityManagerView view, String fileName) {
		super();
		this.fileName = fileName;
		this.view = view;
		//refresh();
	}
/*
	public void refresh() {
		DataSource ds = new DataSource();
		ds.setDataURL(fileName);
		ds.setDataFormat(DSDataFormat.XML);
		ds.setRecordXPath("/status/repository");
		ds.setClientOnly(false);

		DataSourceTextField filenameField = new DataSourceTextField("filename",
				"File name");
		DataSourceTextField sourceField = new DataSourceTextField("source",
				"Source");
		DataSourceTextField dateField = new DataSourceTextField("date", "Date");
		DataSourceTextField timeField = new DataSourceTextField("time", "Time");
		DataSourceTextField dirField = new DataSourceTextField("directory",
				"Directory");

		ds
				.setFields(filenameField, sourceField, dateField, timeField,
						dirField);
		ListGrid grid = new ListGrid();
		grid.setDataSource(ds);
		grid.setWidth100();
		grid.setHeight(200);
		grid.setAutoFetchData(true);
		grid.addCellClickHandler(new CellClickHandler() {
			@Override
			public void onCellClick(CellClickEvent event) {
				ListGridRecord record = event.getRecord();
				String dir = record.getAttribute("directory");
				String fileName = record.getAttribute("filename");
				showXML(dir.trim() + "/" + fileName.trim());
				Label lbl = new Label();
				lbl.setShowEdges(true);
				lbl.setCanDragResize(true);		
				lbl.setContents("test");
			}
		});
		// grid.draw() ;
		add(grid);
	}

	public void showXML(String fileName) {
		final Window window = new Window() ;
		window.setTitle("XML viewer") ;
		window.setWidth(400) ;
		window.setHeight(500) ;
		window.setCanDragReposition(true) ;
		window.setCanDragResize(true) ;
		window.setPadding(10) ;
		
		final Label lblLoading = new Label("Loading...");
		lblLoading.setShowEdges(true) ;
		lblLoading.setWidth100() ;
		lblLoading.setHeight100() ;
		lblLoading.setPadding(5) ;
		lblLoading.setAlign(Alignment.CENTER) ;
		lblLoading.setValign(VerticalAlignment.TOP) ;
		window.addItem(lblLoading) ;		
		
		AsyncCallback<String> callback = new AsyncCallback<String>() {
			public void onFailure(Throwable caught) {
			};

			public void onSuccess(String result) {
				lblLoading.setContents(result) ;				
			};
		};
		window.draw() ;
		
		view.getController().getXML(fileName, callback);
	}
*/
}
/*
 * protected void onRender(Element parent, int index) { super.onRender(parent,
 * index); setLayout(new FlowLayout(10));
 * 
 * List<ColumnConfig> columns = new ArrayList<ColumnConfig>(); columns.add(new
 * ColumnConfig("filename", "File name", 120)); columns.add(new
 * ColumnConfig("source", "Source", 60)); columns.add(new ColumnConfig("date",
 * "Date", 80)); columns.add(new ColumnConfig("time", "Time", 80));
 * columns.add(new ColumnConfig("directory", "Directory", 150));
 * 
 * // create the column model ColumnModel cm = new ColumnModel(columns);
 * 
 * // defines the xml structure ModelType type = new ModelType();
 * type.setRoot("status"); type.setRecordName("repository");
 * type.addField("filename", "filename"); type.addField("source", "source");
 * type.addField("date", "date"); type.addField("time", "time");
 * type.addField("directory", "directory");
 * 
 * // use a http proxy to get the data
 * 
 * RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, GWT
 * .getHostPageBaseURL() + "data/" + fileName); HttpProxy<String> proxy = new
 * HttpProxy<String>(builder);
 * 
 * // need a loader, proxy, and reader
 * XmlLoadResultReader<ListLoadResult<ModelData>> reader = new
 * XmlLoadResultReader<ListLoadResult<ModelData>>( type);
 * 
 * final BaseListLoader<ListLoadResult<ModelData>> loader = new
 * BaseListLoader<ListLoadResult<ModelData>>( proxy, reader);
 * loader.addLoadListener(new LoadListener() {
 * 
 * @Override public void handleEvent(LoadEvent e) { super.handleEvent(e); } });
 * ListStore<ModelData> store = new ListStore<ModelData>(loader); final
 * Grid<ModelData> grid = new Grid<ModelData>(store, cm); grid.setBorders(true);
 * grid.setLoadMask(true);
 * grid.getView().setEmptyText("Please hit the load button.");
 * grid.setAutoExpandColumn("filename");
 * 
 * ContentPanel panel = new ContentPanel(); panel.setFrame(true);
 * panel.setCollapsible(true); panel.setAnimCollapse(false);
 * panel.setButtonAlign(HorizontalAlignment.CENTER); //
 * panel.setIcon(Resources.ICONS.table()); panel.setHeading("XML Table Demo");
 * panel.setLayout(new FitLayout()); panel.setBorders(true); panel.add(grid);
 * panel.setSize(575, 350);
 * 
 * // add buttons Button load = new Button("Update status", new
 * SelectionListener<ButtonEvent>() { public void componentSelected(ButtonEvent
 * ce) { loader.load(); } }); Label lbl = new Label(GWT.getHostPageBaseURL() +
 * "data/" + fileName); panel.add(lbl); panel.addButton(load);
 * 
 * add(panel);
 * 
 * }
 */

/*
 * public class StatusPanel extends VerticalPanel { final private FlexTable
 * table = new FlexTable() ; private GreetingServiceAsync serverService ;
 * 
 * public StatusPanel(GreetingServiceAsync serverService) { super() ;
 * this.serverService = serverService ; table.setText(0, 0, "Filename") ;
 * table.setText(0, 1, "Source") ; table.setText(0, 2, "Date of ingest") ;
 * table.setText(0, 3, "Time of ingest") ; table.setText(0, 4, "View XML") ;
 * 
 * table.setBorderWidth(2) ;
 * 
 * Label lblTitle = new Label("Last data ingests") ; lblTitle.setSize("120px",
 * "20px") ;
 * 
 * this.add(lblTitle) ; this.add(table) ;
 * 
 * this.update() ; }
 * 
 * public void update() { AsyncCallback<String> callback = new
 * AsyncCallback<String>() {
 * 
 * @Override public void onFailure(Throwable caught) { }
 * 
 * @Override public void onSuccess(String result) { Document doc =
 * XMLParser.parse(result) ; Element root = doc.getDocumentElement() ; NodeList
 * repositories = root.getChildNodes() ; for (int iRep = 0 ; iRep <
 * repositories.getLength() ; iRep ++) { Node curNode = repositories.item(iRep)
 * ; if (curNode.getNodeType() == Node.ELEMENT_NODE) { Element rep = (Element)
 * curNode ; final String fileName =
 * rep.getElementsByTagName("filename").item(0)
 * .getChildNodes().item(0).getNodeValue().trim() ; String source =
 * rep.getElementsByTagName
 * ("source").item(0).getChildNodes().item(0).getNodeValue().trim() ; String
 * date =
 * rep.getElementsByTagName("date").item(0).getChildNodes().item(0).getNodeValue
 * ().trim() ; String time =
 * rep.getElementsByTagName("time").item(0).getChildNodes
 * ().item(0).getNodeValue().trim() ; final String dir =
 * rep.getElementsByTagName
 * ("directory").item(0).getChildNodes().item(0).getNodeValue().trim() ;
 * table.setText(iRep + 1, 0, fileName) ; table.setText(iRep + 1, 1, source) ;
 * table.setText(iRep + 1, 2, date) ; table.setText(iRep + 1, 3, time) ; Label
 * lblViewXML = new Label("[View XML]") ; //lblViewXML. table.setWidget(iRep +
 * 1, 4, lblViewXML) ; lblViewXML.addClickHandler(new ClickHandler() {
 * 
 * @Override public void onClick(ClickEvent event) { final DialogBox popXML =
 * new DialogBox() ; popXML.setText("XML Viewer") ;
 * popXML.setTitle("XML Viewer") ; popXML.setHeight("400px") ;
 * popXML.setWidth("400px") ;
 * 
 * final VerticalPanel pnlXML = new VerticalPanel() ; pnlXML.setWidth("390px") ;
 * pnlXML.setHeight("380px") ;
 * pnlXML.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER) ; Label
 * lblLoading = new Label("Loading...") ; pnlXML.add(lblLoading) ; final
 * RichTextArea txtXML = new RichTextArea() ; txtXML.setWidth("385px") ;
 * txtXML.setHeight("350px") ; BasicFormatter bscFormatter =
 * txtXML.getBasicFormatter() ; try { if (bscFormatter != null) {
 * bscFormatter.setFontSize(RichTextArea.FontSize.SMALL) ; } ExtendedFormatter
 * extFormatter = txtXML.getExtendedFormatter() ; if (extFormatter != null) {
 * extFormatter.setFontSize(RichTextArea.FontSize.SMALL) ; } } catch (Exception
 * e) {} //txtXML. popXML.add(pnlXML) ; AsyncCallback<String> callback = new
 * AsyncCallback<String>() { public void onFailure(Throwable caught) {}; public
 * void onSuccess(String result) { pnlXML.clear() ; txtXML.setText(result) ;
 * pnlXML.add(txtXML) ; Button btnClose = new Button("Close", new ClickHandler()
 * { public void onClick(ClickEvent event) { pnlXML.clear() ; popXML.hide(); };
 * }) ;
 * 
 * pnlXML.add(btnClose) ; }; } ;
 * 
 * popXML.center() ; popXML.show() ; serverService.getXML(dir + "/" + fileName,
 * callback) ; } }) ; } } } }; serverService.getStatus(callback) ;
 * 
 * } }
 */