package org.fedorahosted.flies.webtrans.editor;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.dom.client.HasMouseOutHandlers;
import com.google.gwt.event.dom.client.HasMouseOverHandlers;
import com.google.gwt.event.dom.client.MouseOutEvent;
import com.google.gwt.event.dom.client.MouseOutHandler;
import com.google.gwt.event.dom.client.MouseOverEvent;
import com.google.gwt.event.dom.client.MouseOverHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DecoratedPopupPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.widgetideas.client.ProgressBar;
import com.google.gwt.widgetideas.client.ProgressBar.TextFormatter;

public class StatusBar extends Composite implements HasTransUnitCount, ClickHandler, MouseOverHandler, MouseOutHandler, HasClickHandlers, HasMouseOverHandlers, HasMouseOutHandlers {
	
	public int fuzzy;
	public int untranslated;
	public int translated;
	private final static int popupoffset = 35;
	private final ProgressBar bar = new ProgressBar();
	private double curprogress;
	private PopupWindow popupWindow;
	
	private class PopupWindow extends DecoratedPopupPanel {
		    public PopupWindow() {
		      super(true);
		      this.setWidget(new Label("Status of Translation Unit"));
		    }
	}

	
	public StatusBar() {
		setFuzzy(20);
		setUntranslated(50);
		setTranslated(30);
		calcCurrentProgress();
		
		HorizontalPanel panel = new HorizontalPanel();
		initWidget(panel);
	    		
		bar.setTextVisible(true); 
		bar.setMaxProgress(100.0);
		bar.setWidth("200px");
		bar.setProgress(curprogress);
		
		panel.add(bar);
		addClickHandler(this);
		addMouseOverHandler(this);
		addMouseOutHandler(this);
	}
	
	private void calcCurrentProgress() {
		// TODO Auto-generated method stub
		int fuzzy = getFuzzy();
		int untrans = getUntranslated();
		int trans = getTranslated();
		if( translated < 0 || untranslated < 0 || fuzzy < 0 || (translated+untranslated+fuzzy) == 0) {
			curprogress = 0.0;
		} else {
			curprogress = ((double) trans)/(fuzzy+untrans+trans)*100;
		}
	}

	@Override
	public void onMouseOver(MouseOverEvent event) {
		// TODO Auto-generated method stub
		popupWindow = new PopupWindow();
		int top = bar.getAbsoluteTop();
	    int left = bar.getAbsoluteLeft();
		popupWindow.setPopupPosition(left, top-popupoffset);
		popupWindow.show();
	}

	@Override
	public HandlerRegistration addMouseOverHandler(MouseOverHandler handler) {
		// TODO Auto-generated method stub
		return addDomHandler(handler, MouseOverEvent.getType());
	}

	@Override
	public void onMouseOut(MouseOutEvent event) {
		// TODO Auto-generated method stub
		popupWindow.hide();
	}

	@Override
	public HandlerRegistration addMouseOutHandler(MouseOutHandler handler) {
		// TODO Auto-generated method stub
		return addDomHandler(handler, MouseOutEvent.getType());
	}

	@Override
	public void onClick(ClickEvent event) {
		// TODO Auto-generated method stub
		if(bar.getTextFormatter()==null) {
			TextFormatter formatter = new TextFormatter() {
				protected String getText(ProgressBar
				bar, double curProgress) {
				return "["+untranslated+"/"+translated+"/"+fuzzy+"] ";
				}
				};
			bar.setTextFormatter(formatter);
			bar.setProgress(curprogress);
		} else {
			bar.setTextFormatter(null);
			bar.setProgress(curprogress);
		}
	}

	@Override
	public HandlerRegistration addClickHandler(ClickHandler handler) {
		// TODO Auto-generated method stub
		return addDomHandler(handler, ClickEvent.getType());
	}

	@Override
	public int getFuzzy() {
		// TODO Auto-generated method stub
		return fuzzy;
	}

	@Override
	public int getTranslated() {
		// TODO Auto-generated method stub
		return translated;
	}

	@Override
	public int getUntranslated() {
		// TODO Auto-generated method stub
		return untranslated;
	}

	@Override
	public void setFuzzy(int fuzzy) {
		// TODO Auto-generated method stub
		this.fuzzy = fuzzy;
	}

	@Override
	public void setStatus(int fuzzy, int translated, int untranslated) {
		// TODO Auto-generated method stub
		this.fuzzy = fuzzy;
		this.translated = translated;
		this.untranslated = untranslated;
	}

	@Override
	public void setTranslated(int translated) {
		// TODO Auto-generated method stub
		this.translated = translated;
	}

	@Override
	public void setUntranslated(int untranslated) {
		// TODO Auto-generated method stub
		this.untranslated = untranslated;
	}

}

@@ -1282,37 +1282,8 @@
 ();%0A
-%09private double curprogress;%0A
 %09pri
@@ -1586,33 +1586,8 @@
 0);%0A
-%09%09calcCurrentProgress();%0A
 %09%09%0A%09
@@ -1761,34 +1761,44 @@
 etProgress(c
-urp
+alcCurrentP
 rogress
+()
 );%0A%09%09%0A%09%09pane
@@ -1905,20 +1905,22 @@
 private 
-void
+double
  calcCur
@@ -2072,29 +2072,24 @@
 %0A%09%09if( trans
-lated
  %3C 0 %7C%7C untr
@@ -2091,21 +2091,16 @@
  untrans
-lated
  %3C 0 %7C%7C 
@@ -2122,21 +2122,16 @@
 rans
-lated
 +untrans
 late
@@ -2126,21 +2126,16 @@
 +untrans
-lated
 +fuzzy) 
@@ -2145,29 +2145,22 @@
 0) %7B%0A%09%09%09
-curprogress =
+return
  0.0;%0A%09%09
@@ -2175,21 +2175,14 @@
 %0A%09%09%09
-curprogress =
+return
  ((d
@@ -3272,17 +3272,20 @@
 urn %22%5B%22+
-u
+getU
 ntransla
@@ -3291,14 +3291,19 @@
 ated
+()
 +%22/%22+
-t
+getT
 rans
@@ -3311,18 +3311,25 @@
 ated
+()
 +%22/%22+
-f
+getF
 uzzy
+()
 +%22%5D 
@@ -3396,34 +3396,44 @@
 etProgress(c
-urp
+alcCurrentP
 rogress
+()
 );%0A%09%09%7D else 
@@ -3489,18 +3489,28 @@
 ss(c
-urp
+alcCurrentP
 rogress
+()
 );%0A%09

Remove the curprogress field and fix some mistake.