
package uk.me.graphe.client.dialogs;

import uk.me.graphe.client.Graphemeui;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.VerticalPanel;

public class GraphOptionsDialog extends PopupPanel
{
	private static GraphOptionsDialog sInstance;
	
	private final Graphemeui parent;
	private final VerticalPanel pnlCont;
	private final HorizontalPanel pnlBtns;
	private final Label lblTitle;
	private final CheckBox cbIsWeighted, cbIsDigraph, cbIsFlowChart;
	private final Button btnOk, btnCancel;
	
	private boolean isValid;
	
	public GraphOptionsDialog (Graphemeui gui)
	{
		this.parent = gui;
		this.setAnimationEnabled(true);
		this.setGlassEnabled(true);
		this.setAutoHideEnabled(true);
		this.setTitle("Graph options");		
		this.setStyleName("optionsDialog");
		
		lblTitle = new Label("Graph options:");
		cbIsFlowChart = new CheckBox("Set to flow chart.");
		cbIsDigraph = new CheckBox("Set to directed-graph.");
		cbIsWeighted = new CheckBox("Set to weighted-graph.");
		btnOk = new Button("Save");
		btnCancel = new Button ("Cancel");
		
		btnOk.addClickHandler(new ClickHandler()
		{
			@Override
			public void onClick(ClickEvent arg0)
			{
				ok();
			}
			
		});		
		
		btnCancel.addClickHandler(new ClickHandler()
		{
			@Override
			public void onClick(ClickEvent arg0)
			{
				cancel();
			}
			
		});	
		
		pnlCont = new VerticalPanel();
		pnlBtns = new HorizontalPanel();
		
		pnlCont.add(lblTitle);
		pnlCont.add(cbIsFlowChart);
		pnlCont.add(cbIsDigraph);
		pnlCont.add(cbIsWeighted);
		
		pnlBtns.add(btnOk);
		pnlBtns.add(btnCancel);		
		
		pnlCont.add(pnlBtns);
		
		cbIsFlowChart.addValueChangeHandler(new ValueChangeHandler<Boolean>()
		{
			@Override
			public void onValueChange(ValueChangeEvent<Boolean> arg0)
			{
				if (arg0.getValue())
				{
					cbIsDigraph.setEnabled(false);
					cbIsWeighted.setEnabled(false);
				}
				else
				{
					cbIsDigraph.setEnabled(true);
					cbIsWeighted.setEnabled(true);
				}
			}
			
		});
		
		isValid = true;
		
		super.add(pnlCont);
	}

	public static GraphOptionsDialog getInstance (Graphemeui gui)
	{
		if (sInstance == null) sInstance = new GraphOptionsDialog(gui);
        return sInstance;
	}
	
	public static GraphOptionsDialog getInstance ()
	{
		return sInstance;
	}
	
	private void ok ()
	{
		if (isValid)
		{
			//TODO: implement save
			parent.isHotkeysEnabled = true;
			super.hide();
		}
	}
	
	private void cancel ()
	{
		//TODO: implement cancel
		parent.isHotkeysEnabled = true;
		super.hide();
	}
	
	public void show (String s)
	{
		parent.isHotkeysEnabled = false;
		super.center();
		super.show();		
	}
}

@@ -946,37 +946,8 @@
 el;%0A
-%09%0A%09private boolean isValid;%0A%09
 %0A%09pu
@@ -2386,29 +2386,8 @@
 %0A%09%09%0A
-%09%09isValid = true;%0A%09%09%0A
 %09%09su
@@ -2404,16 +2404,16 @@
 lCont);%0A
+
 %09%7D%0A%0A%09pub
@@ -2677,52 +2677,111 @@
 %7B%0A%09%09
-if (isValid)%0A%09%09%7B%0A%09%09%09//TODO: implement save%0A%09
+parent.editGraphProperties(cbIsDigraph.getValue(), cbIsFlowChart.getValue(), cbIsWeighted.getValue());%0A
 %09%09pa
@@ -2812,17 +2812,16 @@
 true;%0A%09%09
-%09
 super.hi
@@ -2826,20 +2826,16 @@
 hide();%0A
-%09%09%7D%0A
 %09%7D%0A%09%0A%09pr
@@ -2862,35 +2862,8 @@
 %0A%09%7B%0A
-%09%09//TODO: implement cancel%0A
 %09%09pa
@@ -2942,19 +2942,181 @@
 ring s)%0A
-
 %09%7B%0A
+%09%09cbIsDigraph.setValue(parent.drawing.isDigraph());%0A%09%09cbIsWeighted.setValue(parent.drawing.isWeighted());%0A%09%09cbIsFlowChart.setValue(parent.drawing.isFlowChart());%0A
 %09%09parent

Save and load properties of graph.