using System;
using System.Windows.Forms;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Configuration.Assemblies;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.ComponentModel;
using System.Data;


using CategoryTheory;
using DiagramUI;
//using ElectromagneticUI;
using FormulaEditor;
using FormulaEditorUI;
using ToolBox;

using DataPerformer;

namespace DataPerformerUI
{
	/// <summary>
	/// Summary description for FormFormulaData.
	/// </summary>
	public class FormDiffEquation : Form, IUpdatableForm, IFormula
	{
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;
		public const string Variables = "abcdfghijklmnoqrstuvwxyz";
		private Hashtable aliasCombo = new Hashtable();

		private DifferentialEquationSolver solver;
		private DataConsumer consumer;
		private IObjectLabel label;
		private System.Windows.Forms.Label labelT;
		private System.Data.DataSet dataSetInitial;
		private System.Data.DataTable dataTableInitial;
		private System.Data.DataColumn dataColumnV;
		private System.Data.DataColumn dataColumnValue;
		private System.Data.DataSet dataSetP;
		private System.Data.DataTable dataTableP;
		private System.Data.DataColumn dataColumnP;
		private System.Windows.Forms.TabPage tabPageVariables;
		private System.Windows.Forms.Button buttonVar;
		private System.Windows.Forms.CheckedListBox checkedListBoxV;
		private System.Windows.Forms.TabPage tabPageEquations;
		private System.Windows.Forms.Button buttonPar;
		private System.Windows.Forms.Button buttonArgs;
		private System.Windows.Forms.Button buttonAccept;
		private System.Windows.Forms.CheckedListBox checkedListBoxP;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Panel panelMea;
		private System.Windows.Forms.Panel panelFormula;
		private System.Windows.Forms.TabPage tabPageConstants;
		private System.Windows.Forms.Button buttonAcceptAliases;
		private System.Windows.Forms.Panel panelComboAlias;
		private System.Windows.Forms.Button buttonAcceptInitial;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.DataGrid dataGridP;
		private System.Windows.Forms.DataGrid dataGridInitial;
		private System.Windows.Forms.Label labelInitial;
		private System.Windows.Forms.TabControl tabControlMain;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.ComboBox comboBoxTime;
		private System.Windows.Forms.MainMenu mainMenuConsumer;
		private System.Windows.Forms.MenuItem menuItem1;
		private System.Windows.Forms.MenuItem menuItemFont;
		private System.Windows.Forms.Button buttonAcceptComments;
		private System.Windows.Forms.Panel panelComments;
		private System.Data.DataColumn dataColumnVal;
		//private CheckBox[] checkVar;

		public FormDiffEquation()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}
		public FormDiffEquation(IObjectLabel label)
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();
			ResourceService.Resources.LoadControlResources(this);
			EditorReceiver.AddEditorDrag(panelComments);
			PictureReceiver.AddImageDrag(panelComments);
			this.labelT.Text = NamedComponent.GetToolTip(label);
			this.label = label;
			solver = label.Object as DifferentialEquationSolver;
			consumer = solver;
			ArrayList comments = solver.Comments;
			ControlPanel.LoadControls(panelComments, comments);
			string var = "";
			foreach (char c in solver.Keys)
			{
				var += c;
			}
			for (int i = 0; i < Variables.Length; i++)
			{
				char v = Variables[i];
				if (var.IndexOf(v) > -1)
				{
					checkedListBoxV.Items.Add("" + v, CheckState.Checked);
				}
				else
				{
					checkedListBoxV.Items.Add("" + v, CheckState.Unchecked);
				}
			}
			string str = solver.AllParameters;
			List<string> l = solver.AliasNames;
			foreach (char c in str)
			{
				string s = c + "";
				if (l.Contains(s))
				{
					checkedListBoxP.Items.Add(s, CheckState.Checked);
				}
				else
				{
					checkedListBoxP.Items.Add(s, CheckState.Unchecked);
				}
			}
			int top = 0;
			foreach (char c in solver.Keys)
			{
				PanelFormula p = new PanelFormula("" + c, this, panelFormula.Width, 200, Variables, true, null, null);
				p.Left = 0;
				p.Top = top;
				top += p.Height;
				p.Formula = solver[c];
				panelFormula.Controls.Add(p);

			}
			UpdateFormUI();
			fillTable();
			createAndFillAliasComboBox();
		}


		/// <summary>
		/// Updates form UI
		/// </summary>
		public void UpdateFormUI()
		{
			Text = label.RootName;//NamedComponent.GetText(label);
			int y = 20;
			string variables = "";
			for (int i = 0; i < solver.Count; i++)
			{
				IMeasurements arrow = solver[i];
				PanelMeasureFormula panel = new PanelMeasureFormula(arrow, variables, solver);
				panel.Width = 300;
				Panel pan = new Panel();
				pan.Width = panel.Width;
				pan.BackColor = Color.Black;
				pan.Top = y;
				pan.Height = 2;
				panelMea.Controls.Add(pan);
				y += pan.Height;
				panelMea.Controls.Add(panel);
				panel.Left = 0;
				panel.Top = y;
				y += panel.Height + 1;
			}
			foreach (string str in solver.Arguments)
			{
				comboBoxTime.Items.Add(str[0] + "");
				variables += str[0];
			}
			foreach (string str in solver.Arguments)
			{
				string s = str;
				int n = str.Length;
				if (s.Substring(n - 4, 4).Equals("Time"))
				{
					for (int i = 0; i < variables.Length; i++)
					{
						if (variables[i] == str[0])
						{
							this.comboBoxTime.SelectedIndex = i;
							return;
						}
					}
				}
			}
			
			
		}

		public string Formula
		{
			get
			{
				return null;
				//return formulaString;
			}
			set
			{
				/*formulaString = value;
				formula = MathFormula.FromString(FormulaEditorPanel.Sizes,value);
				formula.Position = pointFormula;
				formula.CalculateFullRelativeRectangle();
				formula.CalculatePositions();
				MathFormula f = formula.FullTransform;
				tree = new FormulaTree(f);
				Refresh();*/
			}
		}

		public ArrayList Arguments
		{
			get
			{
				ArrayList list = new ArrayList();
				foreach (Control c in panelMea.Controls)
				{
					if (!(c is PanelMeasureFormula))
					{
						continue;
					}
					PanelMeasureFormula p = c as PanelMeasureFormula;
					p.AddArgumentLabels(list);
				}
				object ob = comboBoxTime.SelectedItem;
				if (ob != null)
				{
					string sn = comboBoxTime.SelectedItem.ToString();
					if (sn.Length != 0)
					{
						list.Add(sn + " = Time");
					}
				}
				return list;
			}
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
			//NamedComponent.RemoveForm(label);
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(FormDiffEquation));
			this.labelT = new System.Windows.Forms.Label();
			this.dataTableInitial = new System.Data.DataTable();
			this.dataColumnV = new System.Data.DataColumn();
			this.dataColumnValue = new System.Data.DataColumn();
			this.dataSetInitial = new System.Data.DataSet();
			this.dataTableP = new System.Data.DataTable();
			this.dataColumnP = new System.Data.DataColumn();
			this.dataColumnVal = new System.Data.DataColumn();
			this.dataSetP = new System.Data.DataSet();
			this.tabPageVariables = new System.Windows.Forms.TabPage();
			this.buttonVar = new System.Windows.Forms.Button();
			this.checkedListBoxV = new System.Windows.Forms.CheckedListBox();
			this.tabPageEquations = new System.Windows.Forms.TabPage();
			this.label2 = new System.Windows.Forms.Label();
			this.comboBoxTime = new System.Windows.Forms.ComboBox();
			this.buttonPar = new System.Windows.Forms.Button();
			this.buttonArgs = new System.Windows.Forms.Button();
			this.buttonAccept = new System.Windows.Forms.Button();
			this.checkedListBoxP = new System.Windows.Forms.CheckedListBox();
			this.label4 = new System.Windows.Forms.Label();
			this.label1 = new System.Windows.Forms.Label();
			this.panelMea = new System.Windows.Forms.Panel();
			this.panelFormula = new System.Windows.Forms.Panel();
			this.tabPageConstants = new System.Windows.Forms.TabPage();
			this.buttonAcceptAliases = new System.Windows.Forms.Button();
			this.panelComboAlias = new System.Windows.Forms.Panel();
			this.buttonAcceptInitial = new System.Windows.Forms.Button();
			this.label3 = new System.Windows.Forms.Label();
			this.dataGridP = new System.Windows.Forms.DataGrid();
			this.dataGridInitial = new System.Windows.Forms.DataGrid();
			this.labelInitial = new System.Windows.Forms.Label();
			this.tabControlMain = new System.Windows.Forms.TabControl();
			this.mainMenuConsumer = new System.Windows.Forms.MainMenu();
			this.menuItem1 = new System.Windows.Forms.MenuItem();
			this.menuItemFont = new System.Windows.Forms.MenuItem();
			this.buttonAcceptComments = new System.Windows.Forms.Button();
			this.panelComments = new System.Windows.Forms.Panel();
			((System.ComponentModel.ISupportInitialize)(this.dataTableInitial)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.dataSetInitial)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.dataTableP)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.dataSetP)).BeginInit();
			this.tabPageVariables.SuspendLayout();
			this.tabPageEquations.SuspendLayout();
			this.tabPageConstants.SuspendLayout();
			((System.ComponentModel.ISupportInitialize)(this.dataGridP)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.dataGridInitial)).BeginInit();
			this.tabControlMain.SuspendLayout();
			this.SuspendLayout();
			// 
			// labelT
			// 
			this.labelT.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.labelT.Location = new System.Drawing.Point(32, 24);
			this.labelT.Name = "labelT";
			this.labelT.Size = new System.Drawing.Size(632, 23);
			this.labelT.TabIndex = 5;
			this.labelT.Text = "labelT";
			// 
			// dataTableInitial
			// 
			this.dataTableInitial.Columns.AddRange(new System.Data.DataColumn[] {
																					this.dataColumnV,
																					this.dataColumnValue});
			this.dataTableInitial.TableName = "TableInitial";
			// 
			// dataColumnV
			// 
			this.dataColumnV.ColumnName = "Variable";
			this.dataColumnV.ReadOnly = true;
			// 
			// dataColumnValue
			// 
			this.dataColumnValue.ColumnName = "Value";
			this.dataColumnValue.DataType = typeof(System.Double);
			// 
			// dataSetInitial
			// 
			this.dataSetInitial.DataSetName = "NewDataSet";
			this.dataSetInitial.Locale = new System.Globalization.CultureInfo("ru-RU");
			this.dataSetInitial.Tables.AddRange(new System.Data.DataTable[] {
																				this.dataTableInitial});
			// 
			// dataTableP
			// 
			this.dataTableP.Columns.AddRange(new System.Data.DataColumn[] {
																			  this.dataColumnP,
																			  this.dataColumnVal});
			this.dataTableP.TableName = "TableP";
			// 
			// dataColumnP
			// 
			this.dataColumnP.ColumnName = "Parameter";
			this.dataColumnP.ReadOnly = true;
			// 
			// dataColumnVal
			// 
			this.dataColumnVal.ColumnName = "Value";
			this.dataColumnVal.DataType = typeof(System.Double);
			// 
			// dataSetP
			// 
			this.dataSetP.DataSetName = "NewDataSet";
			this.dataSetP.Locale = new System.Globalization.CultureInfo("ru-RU");
			this.dataSetP.Tables.AddRange(new System.Data.DataTable[] {
																		  this.dataTableP});
			// 
			// tabPageVariables
			// 
			this.tabPageVariables.Controls.Add(this.panelComments);
			this.tabPageVariables.Controls.Add(this.buttonVar);
			this.tabPageVariables.Controls.Add(this.checkedListBoxV);
			this.tabPageVariables.Controls.Add(this.buttonAcceptComments);
			this.tabPageVariables.Location = new System.Drawing.Point(4, 22);
			this.tabPageVariables.Name = "tabPageVariables";
			this.tabPageVariables.Size = new System.Drawing.Size(1232, 734);
			this.tabPageVariables.TabIndex = 2;
			this.tabPageVariables.Text = "Variables";
			// 
			// buttonVar
			// 
			this.buttonVar.Location = new System.Drawing.Point(848, 672);
			this.buttonVar.Name = "buttonVar";
			this.buttonVar.Size = new System.Drawing.Size(192, 23);
			this.buttonVar.TabIndex = 1;
			this.buttonVar.Text = "Accept variables";
			this.buttonVar.Click += new System.EventHandler(this.buttonVar_Click);
			// 
			// checkedListBoxV
			// 
			this.checkedListBoxV.Location = new System.Drawing.Point(840, 80);
			this.checkedListBoxV.Name = "checkedListBoxV";
			this.checkedListBoxV.Size = new System.Drawing.Size(216, 559);
			this.checkedListBoxV.TabIndex = 0;
			// 
			// tabPageEquations
			// 
			this.tabPageEquations.Controls.Add(this.label2);
			this.tabPageEquations.Controls.Add(this.comboBoxTime);
			this.tabPageEquations.Controls.Add(this.buttonPar);
			this.tabPageEquations.Controls.Add(this.buttonArgs);
			this.tabPageEquations.Controls.Add(this.buttonAccept);
			this.tabPageEquations.Controls.Add(this.checkedListBoxP);
			this.tabPageEquations.Controls.Add(this.label4);
			this.tabPageEquations.Controls.Add(this.label1);
			this.tabPageEquations.Controls.Add(this.panelMea);
			this.tabPageEquations.Controls.Add(this.panelFormula);
			this.tabPageEquations.Location = new System.Drawing.Point(4, 22);
			this.tabPageEquations.Name = "tabPageEquations";
			this.tabPageEquations.Size = new System.Drawing.Size(1232, 734);
			this.tabPageEquations.TabIndex = 1;
			this.tabPageEquations.Text = "Differential equations";
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(864, 552);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(56, 23);
			this.label2.TabIndex = 29;
			this.label2.Text = "Time";
			// 
			// comboBoxTime
			// 
			this.comboBoxTime.ItemHeight = 13;
			this.comboBoxTime.Location = new System.Drawing.Point(944, 552);
			this.comboBoxTime.Name = "comboBoxTime";
			this.comboBoxTime.Size = new System.Drawing.Size(121, 21);
			this.comboBoxTime.TabIndex = 28;
			// 
			// buttonPar
			// 
			this.buttonPar.Location = new System.Drawing.Point(456, 664);
			this.buttonPar.Name = "buttonPar";
			this.buttonPar.Size = new System.Drawing.Size(248, 23);
			this.buttonPar.TabIndex = 21;
			this.buttonPar.Text = "Accept constants";
			this.buttonPar.Click += new System.EventHandler(this.buttonPar_Click);
			// 
			// buttonArgs
			// 
			this.buttonArgs.Location = new System.Drawing.Point(744, 664);
			this.buttonArgs.Name = "buttonArgs";
			this.buttonArgs.Size = new System.Drawing.Size(280, 23);
			this.buttonArgs.TabIndex = 20;
			this.buttonArgs.Text = "Accept input parameters";
			this.buttonArgs.Click += new System.EventHandler(this.buttonArgs_Click);
			// 
			// buttonAccept
			// 
			this.buttonAccept.Location = new System.Drawing.Point(200, 664);
			this.buttonAccept.Name = "buttonAccept";
			this.buttonAccept.Size = new System.Drawing.Size(232, 23);
			this.buttonAccept.TabIndex = 19;
			this.buttonAccept.Text = "Accept equations";
			this.buttonAccept.Click += new System.EventHandler(this.buttonAccept_Click);
			// 
			// checkedListBoxP
			// 
			this.checkedListBoxP.Location = new System.Drawing.Point(648, 104);
			this.checkedListBoxP.Name = "checkedListBoxP";
			this.checkedListBoxP.Size = new System.Drawing.Size(120, 409);
			this.checkedListBoxP.TabIndex = 17;
			// 
			// label4
			// 
			this.label4.Location = new System.Drawing.Point(664, 72);
			this.label4.Name = "label4";
			this.label4.TabIndex = 18;
			this.label4.Text = "Constants";
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(872, 48);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(168, 23);
			this.label1.TabIndex = 13;
			this.label1.Text = "Data sources";
			// 
			// panelMea
			// 
			this.panelMea.AutoScroll = true;
			this.panelMea.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
			this.panelMea.Location = new System.Drawing.Point(800, 104);
			this.panelMea.Name = "panelMea";
			this.panelMea.Size = new System.Drawing.Size(336, 408);
			this.panelMea.TabIndex = 12;
			// 
			// panelFormula
			// 
			this.panelFormula.AutoScroll = true;
			this.panelFormula.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
			this.panelFormula.Location = new System.Drawing.Point(8, 32);
			this.panelFormula.Name = "panelFormula";
			this.panelFormula.Size = new System.Drawing.Size(616, 480);
			this.panelFormula.TabIndex = 11;
			// 
			// tabPageConstants
			// 
			this.tabPageConstants.Controls.Add(this.buttonAcceptAliases);
			this.tabPageConstants.Controls.Add(this.panelComboAlias);
			this.tabPageConstants.Controls.Add(this.buttonAcceptInitial);
			this.tabPageConstants.Controls.Add(this.label3);
			this.tabPageConstants.Controls.Add(this.dataGridP);
			this.tabPageConstants.Controls.Add(this.dataGridInitial);
			this.tabPageConstants.Controls.Add(this.labelInitial);
			this.tabPageConstants.Location = new System.Drawing.Point(4, 22);
			this.tabPageConstants.Name = "tabPageConstants";
			this.tabPageConstants.Size = new System.Drawing.Size(1184, 734);
			this.tabPageConstants.TabIndex = 3;
			this.tabPageConstants.Text = "Constants";
			// 
			// buttonAcceptAliases
			// 
			this.buttonAcceptAliases.Location = new System.Drawing.Point(952, 672);
			this.buttonAcceptAliases.Name = "buttonAcceptAliases";
			this.buttonAcceptAliases.TabIndex = 29;
			this.buttonAcceptAliases.Text = "Accept";
			this.buttonAcceptAliases.Click += new System.EventHandler(this.buttonAcceptAliases_Click);
			// 
			// panelComboAlias
			// 
			this.panelComboAlias.AutoScroll = true;
			this.panelComboAlias.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
			this.panelComboAlias.Location = new System.Drawing.Point(872, 192);
			this.panelComboAlias.Name = "panelComboAlias";
			this.panelComboAlias.Size = new System.Drawing.Size(272, 400);
			this.panelComboAlias.TabIndex = 28;
			// 
			// buttonAcceptInitial
			// 
			this.buttonAcceptInitial.Location = new System.Drawing.Point(424, 632);
			this.buttonAcceptInitial.Name = "buttonAcceptInitial";
			this.buttonAcceptInitial.TabIndex = 26;
			this.buttonAcceptInitial.Text = "Accept";
			this.buttonAcceptInitial.Click += new System.EventHandler(this.buttonAcceptInitial_Click);
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(600, 136);
			this.label3.Name = "label3";
			this.label3.TabIndex = 24;
			this.label3.Text = "Constants";
			// 
			// dataGridP
			// 
			this.dataGridP.DataMember = "";
			this.dataGridP.DataSource = this.dataTableP;
			this.dataGridP.HeaderForeColor = System.Drawing.SystemColors.ControlText;
			this.dataGridP.Location = new System.Drawing.Point(496, 192);
			this.dataGridP.Name = "dataGridP";
			this.dataGridP.Size = new System.Drawing.Size(344, 400);
			this.dataGridP.TabIndex = 23;
			// 
			// dataGridInitial
			// 
			this.dataGridInitial.DataMember = "";
			this.dataGridInitial.DataSource = this.dataTableInitial;
			this.dataGridInitial.HeaderForeColor = System.Drawing.SystemColors.ControlText;
			this.dataGridInitial.Location = new System.Drawing.Point(48, 192);
			this.dataGridInitial.Name = "dataGridInitial";
			this.dataGridInitial.Size = new System.Drawing.Size(408, 400);
			this.dataGridInitial.TabIndex = 22;
			// 
			// labelInitial
			// 
			this.labelInitial.Location = new System.Drawing.Point(184, 136);
			this.labelInitial.Name = "labelInitial";
			this.labelInitial.Size = new System.Drawing.Size(184, 23);
			this.labelInitial.TabIndex = 21;
			this.labelInitial.Text = "Initial values";
			// 
			// tabControlMain
			// 
			this.tabControlMain.Controls.Add(this.tabPageVariables);
			this.tabControlMain.Controls.Add(this.tabPageEquations);
			this.tabControlMain.Controls.Add(this.tabPageConstants);
			this.tabControlMain.ItemSize = new System.Drawing.Size(59, 18);
			this.tabControlMain.Location = new System.Drawing.Point(24, 64);
			this.tabControlMain.Name = "tabControlMain";
			this.tabControlMain.SelectedIndex = 0;
			this.tabControlMain.Size = new System.Drawing.Size(1192, 760);
			this.tabControlMain.TabIndex = 20;
			// 
			// mainMenuConsumer
			// 
			this.mainMenuConsumer.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																							 this.menuItem1});
			// 
			// menuItem1
			// 
			this.menuItem1.Index = 0;
			this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					  this.menuItemFont});
			this.menuItem1.Text = "Format";
			// 
			// menuItemFont
			// 
			this.menuItemFont.Index = 0;
			this.menuItemFont.Text = "Font";
			this.menuItemFont.Click += new System.EventHandler(this.menuItemFont_Click);
			// 
			// buttonAcceptComments
			// 
			this.buttonAcceptComments.Location = new System.Drawing.Point(408, 40);
			this.buttonAcceptComments.Name = "buttonAcceptComments";
			this.buttonAcceptComments.Size = new System.Drawing.Size(224, 23);
			this.buttonAcceptComments.TabIndex = 20;
			this.buttonAcceptComments.Text = "Accept comments";
			this.buttonAcceptComments.Click += new System.EventHandler(this.buttonAcceptComments_Click);
			// 
			// panelComments
			// 
			this.panelComments.BackColor = System.Drawing.Color.White;
			this.panelComments.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.panelComments.Location = new System.Drawing.Point(12, 104);
			this.panelComments.Name = "panelComments";
			this.panelComments.Size = new System.Drawing.Size(780, 584);
			this.panelComments.TabIndex = 21;
			// 
			// FormDiffEquation
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(1224, 837);
			this.Controls.Add(this.tabControlMain);
			this.Controls.Add(this.labelT);
			this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
			this.Menu = this.mainMenuConsumer;
			this.Name = "FormDiffEquation";
			this.Text = "FormFormulaData";
			((System.ComponentModel.ISupportInitialize)(this.dataTableInitial)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.dataSetInitial)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.dataTableP)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.dataSetP)).EndInit();
			this.tabPageVariables.ResumeLayout(false);
			this.tabPageEquations.ResumeLayout(false);
			this.tabPageConstants.ResumeLayout(false);
			((System.ComponentModel.ISupportInitialize)(this.dataGridP)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.dataGridInitial)).EndInit();
			this.tabControlMain.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		private void buttonEdit_Click(object sender, System.EventArgs e)
		{
			FormulaEditorForm form = new FormulaEditorForm();
			form.Formula = Formula;
			form.ShowDialog(this);
			Formula = form.Formula;
		}

		private DynamicalParameter Parameter
		{
			get
			{
				DynamicalParameter par = new DynamicalParameter();
				foreach (Control c in panelMea.Controls)
				{
					if (!(c is PanelMeasureFormula))
					{
						continue;
					}
					PanelMeasureFormula p = c as PanelMeasureFormula;
					p.CreateArguments(par);
				}
				object ob = comboBoxTime.SelectedItem;
				if (ob != null)
				{
					string sn = ob.ToString();
					if (sn.Length != 0)
					{
						//ElectromagneticUIFactory f = label.Desktop.Tools.Factory as ElectromagneticUIFactory;
						IMeasure m = DataConsumer.TimeMeasure;
						par.Add(sn[0], m);
					}
				}

				return par;
			}
		}


		private void fillComboBoxes(string str)
		{
			foreach (Control c in panelMea.Controls)
			{
				if (!(c is PanelMeasureFormula))
				{
					continue;
				}
				PanelMeasureFormula p = c as PanelMeasureFormula;
				p.FillComboboxes(str);
			}
			comboBoxTime.Items.Clear();
			foreach (char c in str)
			{
				comboBoxTime.Items.Add(c + "");
			}

		}

		private void buttonAccept_Click(object sender, System.EventArgs e)
		{
			acceptEquations();
			resetCombo();	
		}

		private void resetCombo()
		{
			foreach (Control c in panelMea.Controls)
			{
				if (!(c is PanelMeasureFormula))
				{
					continue;
				}
				PanelMeasureFormula p = c as PanelMeasureFormula;
				p.ResetCombo();
			}
		}

		private void acceptEquations()
		{
			try
			{
				solver.ClearParameters();
				Hashtable t = new Hashtable();
				foreach (DataRow row in dataTableInitial.Rows)
				{
					string s = row[0] as string;
					double x = (double) row[1];
					t[s[0]] = x;
				}
				dataTableInitial.Clear();
				foreach (Control c in panelFormula.Controls)
				{
					if (!(c is PanelFormula))
					{
						continue;
					}
					PanelFormula p = c as PanelFormula;
					char var = p.Variable;
					solver.AddVariable(var);
					solver.SetVariable(var, p.Formula);
					double x = 0;
					if (t.ContainsKey(var))
					{
						x = (double) t[var];
					}
					dataTableInitial.Rows.Add(new object[]{var + "", x});
				}
				string par = solver.AllParameters;
				checkedListBoxP.Items.Clear();
				foreach (char c in par)
				{
					checkedListBoxP.Items.Add("" + c);
				}
			}
			catch (Exception e)
			{
				DefaultForm.ShowError(this, e);
			}
		}

		private void fillTable()
		{
			try
			{
				dataTableInitial.Clear();
				foreach (char c in solver.Keys)
				{
					dataTableInitial.Rows.Add(new object[]{c + "", solver.GetInitialValue(c)});
				}
				dataTableP.Clear();
				string str = solver.ConstantNames;
				foreach (char c in str)
				{
					string s = "" + c;
					double a = (double)solver[s];
					dataTableP.Rows.Add(new object[]{s, a});
				}
			}
			catch (Exception e)
			{
				DefaultForm.ShowError(this, e);
			}

		}

		private void acceptArg()
		{
			try
			{
				solver.ClearParameters();
				DynamicalParameter p = Parameter;
				foreach (char c in p.Variables)
				{
					IMeasure m = p[c];
                    MeasureParameter par = MeasureDerivation.GetDerivation(m);
                    IMeasure mea = null;
                    if (par == null)
                    {
                        mea = new Measure(m.Parameter, c + "");
                    }
                    else
                    {
                        Double a = 0;
                        mea = new MeasureDerivation(a, m.Parameter, par, c + "");
                    }
					solver.SetParameter(c, mea);
				}
				solver.Arguments = Arguments;
				solver.Prepare();
			}
			catch (Exception e)
			{
				DefaultForm.ShowError(this, e);
			}
		}

		

		private void buttonArgs_Click(object sender, System.EventArgs e)
		{
			acceptArg();
		}

		private void buttonVar_Click(object sender, System.EventArgs e)
		{
			showEquations();
		}
		private void showEquations()
		{
			solver.ClearVariables();
			panelFormula.Controls.Clear();
			int w = panelFormula.Width;
			int y = 0;
			foreach (string s in checkedListBoxV.CheckedItems)
			{
				Panel p = new PanelFormula(s, this, panelFormula.Width, 200, Variables, true, null, null);
				p.Left = 0;
				p.Top = y;
				y += p.Height;
				panelFormula.Controls.Add(p);
			}
		}

		private void buttonAcceptInitial_Click(object sender, System.EventArgs e)
		{
			setValues();
		}

		private void setValues()
		{
			try
			{
				foreach (DataRow row in dataTableInitial.Rows)
				{
//					char c = ((string)row[0])[0];
//					double v = (double)row[1];
					string s = (string)row[0];
					double a = (double)row[1];
					solver[s] = a;//.SetValue(c, v);
				}
				foreach (DataRow row in dataTableP.Rows)
				{
					string s = (string)row[0];
					double a = (double)row[1];
					solver[s] = a;
				}
			}
			catch (Exception e)
			{
				DefaultForm.ShowError(this, e);
			}
		}

		private void buttonPar_Click(object sender, System.EventArgs e)
		{
			acceptParameters();
		}
		private void acceptParameters()
		{
			solver.ResetAliases();
			try
			{
				foreach (string s in checkedListBoxP.CheckedItems)
				{
					solver.AddAlias(s);
				}
				string str = solver.InputParameters;
				fillComboBoxes(str);
				fillTable();
			}
			catch (Exception e)
			{
				DefaultForm.ShowError(this, e);
			}
		}

		private void createAndFillAliasComboBox()
		{
			IMeasurements m = this.solver;
			Panel panelComboAliasInner = new Panel();
			panelComboAliasInner.Width = panelComboAlias.Width;
			panelComboAliasInner.Height = 5;
			panelComboAlias.Controls.Add(panelComboAliasInner);
			int y = 0;
            List<string> al = new List<string>();
			DataConsumer.GetAliases(solver, al);
			Hashtable ea = solver.ExternalAliases;
			for (int i = 0; i < m.Count; i++)
			{
				IMeasure mea = m[i];
				char c = mea.Name[0];
				ComboBox cb = new ComboBox();
				panelComboAliasInner.Controls.Add(cb);
				aliasCombo[c] = cb;
				cb.Width = 121;
				cb.Height = 21;
				cb.Top = 10 + y;
				cb.Left = 10;
				Label l = new Label();
				panelComboAliasInner.Controls.Add(l);
				l.Text = c + "";
				l.Top = cb.Top;
				l.Left = cb.Left + cb.Width + 10;
				foreach (string s in al)
				{
					cb.Items.Add(s);
				}
				y += cb.Height + 10;
				if (ea == null)
				{
					continue;
				}
				if (ea.ContainsKey(c))
				{
					string str = ea[c] as string;
					for (int j = 0; j < cb.Items.Count; j++)
					{
						if (str.Equals(cb.Items[j].ToString()))
						{
							cb.SelectedIndex = j;
							break;
						}
					}
				}
			}
			panelComboAliasInner.Height = y;
			
		}

		private void buttonAcceptAliases_Click(object sender, System.EventArgs e)
		{
			try
			{
				if(solver.ExternalAliases != null)
				{
					solver.ExternalAliases.Clear();
				}
				Hashtable table = new Hashtable();
				foreach (char c in aliasCombo.Keys)
				{
					ComboBox cb = aliasCombo[c] as ComboBox;
					object o = cb.SelectedItem;
					if (o == null)
					{
						continue;
					}
					table[c] = cb.SelectedItem.ToString();
				}
				solver.ExternalAliases = table;
			}
			catch (Exception ex)
			{
				DefaultForm.ShowError(this, ex);
			}
		}

		private void buttonAcceptComments_Click(object sender, System.EventArgs e)
		{
			ArrayList comments = ControlPanel.GetControls(panelComments);
			solver.Comments = comments;
		}

		private void menuItemFont_Click(object sender, System.EventArgs e)
		{
			try
			{
				TextBox box = ControlPanel.GetActiveTextBox(panelComments);
				if (box == null)
				{
					return;
				}
				FontDialog dlg = new FontDialog();
				dlg.ShowDialog(this);
				Font font = dlg.Font;
				box.Font = font;
			}
			catch (Exception ex)
			{
				DefaultForm.ShowError(this, ex);
			}
		}

	}




}
