
package uk.me.graphe.client.dialogs;

import uk.me.graphe.client.Graphemeui;
import uk.me.graphe.shared.Tools;

import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyDownEvent;
import com.google.gwt.event.dom.client.KeyDownHandler;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class EdgeDialog extends PopupPanel
{
	private static EdgeDialog sInstance;
	
	private final Graphemeui parent;
	private final VerticalPanel pnlCont;
	private final Label lblTitle;
	private final TextBox txtParam;
	
	private boolean isValid;
	
	public EdgeDialog (Graphemeui gui)
	{
		this.parent = gui;
		this.setAnimationEnabled(true);
		this.setGlassEnabled(true);
		this.setAutoHideEnabled(true);
		this.setTitle("Edge weight");		
		this.setStyleName("paramDialog");

		lblTitle = new Label("Edge weight:");
		txtParam = new TextBox();
		
		pnlCont = new VerticalPanel();
		
		pnlCont.add(lblTitle);
		pnlCont.add(txtParam);
		
		isValid = true;

		txtParam.addKeyDownHandler(new KeyDownHandler()
		{
			@Override
			public void onKeyDown(KeyDownEvent arg0)
			{
				//TODO: Text validation.....set isValid true or false
			}
		});
		
		txtParam.addKeyUpHandler(new KeyUpHandler()
		{
			public void onKeyUp(KeyUpEvent e)
			{
				if (e.getNativeKeyCode() == KeyCodes.KEY_ESCAPE)
				{
					cancel();
				}
				else if (e.getNativeKeyCode() == KeyCodes.KEY_ENTER)
				{
					ok();
				}
				else
				{
					if (txtParam.getText().trim().length() > 0)
					{
						isValid = true;
					}
					else
					{
						isValid = false;
					}	
				}
			}
		});
		
		super.add(pnlCont);
	}

	public static EdgeDialog getInstance (Graphemeui gui)
	{
		if (sInstance == null) sInstance = new EdgeDialog(gui);
        return sInstance;
	}
	
	public static EdgeDialog getInstance ()
	{
		return sInstance;
	}
	
	private void ok ()
	{
		if (isValid)
		{
			parent.addEdge(parent.selectedVertices.get(0), parent.selectedVertices.get(1), Integer.parseInt(txtParam.getText()));
			parent.clearSelectedObjects();
			parent.tools.setTool(Tools.addEdge);
			parent.isHotkeysEnabled = true;
			super.hide();
		}
	}
	
	private void cancel ()
	{
		parent.isHotkeysEnabled = true;
		super.hide();
	}
	
	public void show (String initialValue, int left, int top)
	{
		parent.isHotkeysEnabled = false;
		txtParam.setText(initialValue);
		this.setPopupPosition(left, top);		
		super.show();		
		txtParam.setFocus(true);
	}
}

@@ -806,16 +806,24 @@
  isValid
+, isEdit
 ;%0A%09%0A%09pub
@@ -2113,24 +2113,110 @@
 sValid)%0A%09%09%7B%0A
+%09%09%09if (isEdit)%0A%09%09%09%7B%0A%09%09%09%09parent.editEdgeWeight(txtParam.getText());%0A%09%09%09%7D%0A%09%09%09else%0A%09%09%09%7B%0A%09
 %09%09%09parent.ad
@@ -2324,16 +2324,25 @@
 xt()));%0A
+%09%09%09%7D%0A%09%09%09%0A
 %09%09%09paren
@@ -2609,19 +2609,114 @@
 nt top)%0A
-
 %09%7B%0A
+%09%09if (!initialValue.equals(%22%22))%0A%09%09%7B%0A%09%09%09isEdit = true;%0A%09%09%7D%0A%09%09else%0A%09%09%7B%0A%09%09%09isEdit = false;%0A%09%09%7D%0A%09%09%0A
 %09%09parent

Call the edit edge weight method.