package com.bioserve.webstats.client;

import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.Image;


public class PlotWrapper implements Updateable, TimespanSettable {
	private DockPanel mainPanel = new DockPanel();
	private Image plotImage;
	private HttpGetWrapper imageGetWrapper = new HttpGetWrapper();
	private HTML htmlTitle = new HTML();
	
	public PlotWrapper() {
		//Set the defaults for the query.
		imageGetWrapper.setBaseUrl("http://localhost/~andrew/cgi-bin/bdmplot.py");
		setTimespan("last 30m");
		setFilter("syshealth.otis.*:Seconds-of-uptime");
	}
	
	public PlotWrapper(String packHere) {
		this();
		pack(packHere);
		setTitle("Plot");	/* Untitled */
	}
	
	public PlotWrapper(String packHere, String filter)
	{
		this();
		setFilter(filter);
		pack(packHere);
		setTitle(filter);	/* Untitled */
	}
	
	private void pack(String packHere) {
		// Draw the image once.
		update();
			
		//Pack the panel into packHere.
		RootPanel.get(packHere).add(mainPanel);
		
		//Pack the title into the panel, set attributes.
		mainPanel.add(htmlTitle, DockPanel.NORTH);
		htmlTitle.setStylePrimaryName("plotTitle");
	}
	
	public void update() {
		if(plotImage == null) {
			//Make the image and add it.
			plotImage = new Image(imageGetWrapper.toString());
			mainPanel.add(plotImage, DockPanel.CENTER);
		} else {
			//Update the image URL, which triggers a reload of the image.
			plotImage.setUrl(imageGetWrapper.toString() + 
					"&bogus=" + System.currentTimeMillis()); /* FIXME: Hack to force reload */
		}
	}
	
	public void setFilter(String filter)
	{
		imageGetWrapper.setParam("filter", filter);
	}
	
	public void setTimespan(String timespan)
	{
		imageGetWrapper.setParam("timespan", timespan);
		update();
	}
	
	public void setTitle(String title)
	{
		htmlTitle.setText(title);
	}
}

@@ -854,16 +854,40 @@
 this();%0A
+%09%09if(filter != null) %7B%0A%09
 %09%09setFil
@@ -899,16 +899,17 @@
 ilter);%0A
+%09
 %09%09pack(p
@@ -912,32 +912,33 @@
 ck(packHere);%0A%09%09
+%09
 setTitle(filter)
@@ -950,24 +950,28 @@
 Untitled */%0A
+%09%09%7D%0A
 %09%7D%0A%09%0A%09privat

Allow null filters: plot the default in that case.