using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DiagramUI;
using DataPerformer;
using Regression;
using DiagramUI.Labels;
using DiagramUI.Utils;
using DataPerformerUI.UserControls;
using Diagram.UI.Interfaces;
namespace DataPerformerUI
{
/// <summary>
/// Editor of propetries of General Linear Method iterator
/// </summary>
public partial class FormIterateGLM : Form, IUpdatableForm
{
private IObjectLabel comp;
private IteratorGLM iterator;
private List<ComboBox> left = new List<ComboBox>();
private List<ComboBox> right = new List<ComboBox>();
private List<ComboBox> alias = new List<ComboBox>();
List<TextBox> disp = new List<TextBox>();
private List<Label> labs = new List<Label>();
Control cLeftPart;
Control cRightPart;
Control cPar;
Control cSigma;
/// <summary>
/// Constructor
/// </summary>
/// <param name="comp">Component label</param>
public FormIterateGLM(IObjectLabel comp)
{
InitializeComponent();
this.LoadResources();
this.comp = comp;
iterator = comp.Object as IteratorGLM;
cLeftPart = panelLeftPart;
cRightPart = panelRightPart;
cPar = panelParameter;
cSigma = panelSigma;
toolStripButtonNumberAli.FillCombo(1, 100);
toolStripComboBoxOut.FillCombo(1, 100);
toolStripButtonNumberAli.SelectedIndex = iterator.AliasesCount - 1;
toolStripComboBoxOut.SelectedIndex = iterator.DataCount - 1;
fillOut();
fillAli();
selectCombo();
Text = comp.Name;
}
private FormIterateGLM()
{
InitializeComponent();
}
#region IUpdatableForm Members
void IUpdatableForm.UpdateFormUI()
{
Text = comp.Name;
}
#endregion
private void removeData()
{
foreach (ComboBox cb in left)
{
Control p = cb.Parent;
p.Controls.Remove(cb);
}
left.Clear();
foreach (ComboBox cb in right)
{
Control p = cb.Parent;
p.Controls.Remove(cb);
}
right.Clear();
foreach (Label l in labs)
{
Control p = l.Parent;
p.Controls.Remove(l);
}
labs.Clear();
}
private void removeAli()
{
foreach (ComboBox box in alias)
{
Control p = box.Parent;
p.Controls.Remove(box);
}
alias.Clear();
foreach (TextBox tb in disp)
{
Control p = tb.Parent;
p.Controls.Remove(tb);
}
}
void fillOut()
{
int y = 10;
removeData();
int n = (int)toolStripComboBoxOut.SelectedIndex + 1;
for (int i = 0; i < n; i++)
{
ComboBox comboLeft = new ComboBox();
left.Add(comboLeft);
fillData(comboLeft);
comboLeft.Top = y;
comboLeft.Left = 5;
comboLeft.Width = cLeftPart.Width - 10;
cLeftPart.Controls.Add(comboLeft);
/* Label l = new Label();
l.Text = "=";
l.Left = comboLeft.Left + comboLeft.Width + 5;
l.Top = comboLeft.Top;
l.Width = 20;
labs.Add(l);*/
//cRightPart.Controls.Add(l);
ComboBox comboRight = new ComboBox();
right.Add(comboRight);
fillData(comboRight);
comboRight.Top = y;
comboRight.Left = 5;// l.Right + 5;
comboRight.Width = cRightPart.Width - 10;
cRightPart.Controls.Add(comboRight);
y = comboLeft.Top + comboLeft.Height + 10;
}
}
void fillAli()
{
int y = 10;
removeAli();
int n = toolStripButtonNumberAli.SelectedIndex + 1;
for (int i = 0; i < n; i++)
{
ComboBox box = new ComboBox();
box.Top = y;
alias.Add(box);
fillAlias(box);
box.Left = 10;
box.Width = cPar.Width - 10;
cPar.Controls.Add(box);
y = box.Top + box.Height + 10;
TextBox tb = new TextBox();
tb.Top = box.Top;
tb.Left = 5;
tb.Text = 0 + "";
disp.Add(tb);
cSigma.Controls.Add(tb);
}
}
void selectCombo()
{
for (int i = 0; i < left.Count; i++)
{
ComboBox l = left[i];
selectCombo(l, iterator.GetLeftName(i));
ComboBox r = right[i];
selectCombo(r, iterator.GetRightName(i));
}
for (int i = 0; i < alias.Count; i++)
{
selectCombo(alias[i], iterator.GetAliasName(i));
}
double[,] d = iterator.CorrectionMatrix;
if (d != null)
{
for (int i = 0; i < disp.Count; i++)
{
if (i >= d.GetLength(0))
{
continue;
}
if (i >= d.GetLength(1))
{
continue;
}
TextBox tb = disp[i];
tb.Text = d[i, i] + "";
}
}
}
void selectCombo(ComboBox box, string item)
{
for (int i = 0; i < box.Items.Count; i++)
{
string s = box.Items[i] + "";
if (s.Equals(item))
{
box.SelectedIndex = i;
return;
}
}
}
void fillData(ComboBox box)
{
List<string> l = iterator.AllMeasurements;
foreach (string s in l)
{
box.Items.Add(s);
}
}
void fillAlias(ComboBox box)
{
List<string> l = iterator.AllAliases;
foreach (string s in l)
{
box.Items.Add(s);
}
}
void accept()
{
try
{
List<string> l = getComboList(left);
List<string> ri = getComboList(right);
List<string> ali = getComboList(alias);
List<List<string>> r = new List<List<string>>();
int n = ali.Count;
double[,] d = new double[n, n];
double[] dx = new double[n];
for (int i = 0; i < dx.Length; i++)
{
dx[i] = 0.00000001;
}
for (int i = 0; i < left.Count; i++)
{
List<string> list = new List<string>();
r.Add(list);
for (int j = 0; j <= i; j++)
{
list.Add("");
d[i, j] = d[j, i] = 0;
}
}
for (int i = 0; i < disp.Count; i++)
{
d[i, i] = Double.Parse(disp[i].Text);
}
iterator.Set(ali, l, ri, r, dx, d);
}
catch (Exception ex)
{
ex.ShowError(10);
this.ShowError(ex);
}
}
private List<string> getComboList(List<ComboBox> l)
{
List<string> list = new List<string>();
foreach (ComboBox box in l)
{
list.Add(box.SelectedItem + "");
}
return list;
}
private void buttonAcceptAll_Click(object sender, EventArgs e)
{
accept();
}
private void buttonIterate_Click(object sender, EventArgs e)
{
double s = iterator.Iterate();
labelSigma.Text = "Sigma = " + s + "";
}
private void fill()
{
fillAli();
fillOut();
}
private void toolStripButtonRefresh_Click(object sender, EventArgs e)
{
fill();
}
private void buttonAcceptNumber_Click(object sender, EventArgs e)
{
fill();
}
}
}