using System;

using MindFusion.Diagramming;


public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
		if (!IsPostBack)
		{
            _node = -1;
            _column = -1;
            _row = -1;

            //Load the initial diagram
            Diagram diagram = diagramView.Diagram;
            string path = Server.MapPath("~") + "\\App_Data\\Diagram.xml";
            diagram.LoadFromXml(path);
		}
    }

    protected override void OnLoadComplete(EventArgs e)
    { 
     	if (IsPostBack)

            //Update the text of the currently selected object
            if (_node != -1)
            {
                DiagramNode node  = (DiagramNode)diagramView.Diagram.Items[_node];

                if (node is TableNode)
                {
                    if (_row == -1 || _column == -1)
                        text.Text = ((TableNode)node).Caption;
                    else
                        text.Text = ((TableNode)node)[_column, _row].Text;  
                }

                if (node is ShapeNode)
                    text.Text = ((ShapeNode)node).Text;
            }
    }

    protected void diagramView_NodeClicked(object sender, NodeEventArgs e)
    {
        e.Node.Selected = true;

        _node = e.Node.ZIndex;
        _row = -1;
        _column = -1;

        if (e.Node is TableNode)
            LogEvent("Table '" + ((TableNode)e.Node).Caption + "' has been clicked.");
        if (e.Node is ShapeNode)
            LogEvent("Shape '" + ((ShapeNode)e.Node).Text + "' has been clicked.");
    }

    protected void diagramView_TreeCollapsed(object sender, NodeEventArgs e)
    {
          if (e.Node is TableNode)
            LogEvent("Table '" + ((TableNode)e.Node).Caption + "' has been collapsed.");

        if (e.Node is ShapeNode)
            LogEvent("Shape '" + ((ShapeNode)e.Node).Text + "' has been collapsed.");
    }

    protected void diagramView_TreeExpanded(object sender, NodeEventArgs e)
    {
        if (e.Node is TableNode)
            LogEvent("Table '" + ((TableNode)e.Node).Caption + "' has been expanded.");

     if (e.Node is ShapeNode)
            LogEvent("Shape '" + ((ShapeNode)e.Node).Text + "' has been expanded.");
    }

    protected void diagramView_TableCellClicked(object sender, CellEventArgs e)
    {
        e.Table.Selected = true;

        _node = e.Table.ZIndex;
        _row = e.Row;
        _column = e.Column;

        LogEvent("Cell (" + _column + ", " + _row + ") of table '" + e.Table.Caption + "' has been clicked.");
    }

    protected void button_Click(object sender, EventArgs e)
    { 
        DiagramNode node;
        //Change the text of the selected item
        if (_node != -1)
        {
            node = (DiagramNode)diagramView.Diagram.Items[_node];

            if (node is TableNode)
            {
                if (_row == -1 || _column == -1)
                    ((TableNode)node).Caption = text.Text;
                else
                    ((TableNode)node)[_column, _row].Text = text.Text;
            }

            if (node is ShapeNode)
                ((ShapeNode)node).Text = text.Text;

        } 
    }

	protected void button1_Click(object sender, EventArgs e)
    {
        //Change the text of the selected item
        switch (imageFormat.SelectedIndex)
        {
            case 0:
                diagramView.ImageFormat = "image/jpeg";
                break;
            case 1:
                diagramView.ImageFormat = "image/bmp";
                break;
            case 2:
                diagramView.ImageFormat = "image/gif";
                break;
            case 3:
                diagramView.ImageFormat = "image/png";
                break;
        }
    }

    // Appends the specified event to the event log.
    private void LogEvent(string eventMessage)
    {
        System.Text.StringBuilder builder = new System.Text.StringBuilder();
        builder.Append(eventlog.Text);
        builder.Append(String.Format("<b><font color='#999999'>[{0}]</font></b> ", DateTime.Now.ToString("HH:mm:ss")));
        builder.Append(eventMessage);
        builder.Append("<br>");

        eventlog.Text = builder.ToString();
    }

    
    protected override object SaveViewState()
    {
        System.Collections.Hashtable map = new System.Collections.Hashtable();

        map.Add("Node", _node);
        map.Add("Column", _column);
        map.Add("Row", _row);
        map.Add("ImageFormat", diagramView.ImageFormat);

        return map;
    }

    protected override void LoadViewState(object savedState)
    {

        System.Collections.Hashtable map = (System.Collections.Hashtable)savedState;

        if (map == null) return;

        _node = (int)map["Node"];
        _column = (int)map["Column"];
        _row = (int)map["Row"];

        string format = map["ImageFormat"].ToString();
        diagramView.ImageFormat = format;

        // Update the format choice
        switch (format)
        {

            case "image/jpeg":
                imageFormat.SelectedIndex = 0;
                break;
            case "image/bmp":
                imageFormat.SelectedIndex = 1;
                break;
            case "image/gif":
                imageFormat.SelectedIndex = 2;
                break;
            case "image/png":
                imageFormat.SelectedIndex = 3;
                break;
        }
    }

    // The x-index of the selected node.
    int _node ;
    // The column of the selected cell.
    int _column;
    // The row of the selected cell.
    int _row;
}