Пример #1
0
        public MainForm()
        {
            InitializeComponent();

            this.Icon = Win.AssemblyRoutines.GetAppIcon();
            Text      = Program.Name + " [" + Program.Version.ToString(3) + "] / " + PdfDocumentParser.Program.FullName;

            Message.Owner = this;

            LoadTemplates();

            Active.ValueType          = typeof(bool);
            Selected.ValueType        = typeof(bool);
            OrderWeight.ValueType     = typeof(float);
            FileFilterRegex.ValueType = typeof(Regex);

            Settings.Template2s.TouchedChanged += delegate
            {
                this.BeginInvoke(() =>
                {
                    saveTemplates.Enabled = Settings.Template2s.IsTouched();
                });
            };
            saveTemplates.Enabled = false;
            saveTemplates.Click  += delegate
            {
                saveTemplatesFromTableIfTouched(false);
            };

            FormClosing += delegate(object sender, FormClosingEventArgs e)
            {
                Settings.TemplateLocalInfo.Clear_Save();
                if (!saveTemplatesFromTableIfTouched(true))
                {
                    e.Cancel = true;
                }
            };

            template2s.CellValidating += delegate(object sender, DataGridViewCellValidatingEventArgs e)
            {
                try
                {
                    DataGridViewRow r = template2s.Rows[e.RowIndex];
                    Template2       t = (Template2)r.Tag;

                    switch (template2s.Columns[e.ColumnIndex].Name)
                    {
                    case "Name_":
                    {
                        if (string.IsNullOrWhiteSpace((string)e.FormattedValue))
                        {
                            if (t != null)
                            {
                                throw new Exception("Name cannot be empty!");
                            }
                            return;
                        }
                        string name2 = ((string)e.FormattedValue).Trim();
                        foreach (DataGridViewRow rr in template2s.Rows)
                        {
                            if (rr.Index != e.RowIndex && name2 == (string)rr.Cells["Name_"].Value)
                            {
                                throw new Exception("Name '" + name2 + "' is duplicated!");
                            }
                        }
                        if ((string)r.Cells["Name_"].Value != name2)
                        {
                            r.Cells["Name_"].Value = name2;
                        }
                    }
                        return;
                    }
                }
                catch (Exception ex)
                {
                    e.Cancel = true;
                    Message.Error2(ex);
                }
            };

            template2s.DataError += delegate(object sender, DataGridViewDataErrorEventArgs e)
            {
                try
                {
                    DataGridViewRow r = template2s.Rows[e.RowIndex];
                    Template2       t = (Template2)r.Tag;

                    switch (template2s.Columns[e.ColumnIndex].Name)
                    {
                    case "OrderWeight":
                        throw new Exception("Order must be a float number:\r\n" + e.Exception.Message);

                    case "FileFilterRegex":
                        throw new Exception("FileFilterRegex must be a regex.");
                    }
                }
                catch (Exception ex)
                {
                    Message.Error2(ex);
                }
            };

            template2s.CellValueChanged += delegate(object sender, DataGridViewCellEventArgs e)
            {
                try
                {
                    DataGridViewRow r = template2s.Rows[e.RowIndex];
                    Template2       t = (Template2)r.Tag;
                    if (t == null)
                    {
                        return;
                    }
                    if (e.ColumnIndex < 0)//row's header
                    {
                        return;
                    }

                    switch (template2s.Columns[e.ColumnIndex].Name)
                    {
                    case "Name_":
                        t.Template.Name = (string)r.Cells["Name_"].Value;
                        Settings.Template2s.Touch();
                        return;

                    case "Active":
                        t.Active = (bool)r.Cells["Active"].Value;
                        Settings.Template2s.Touch();
                        return;

                    case "Comment":
                        t.Comment = (string)r.Cells["Comment"].Value;
                        Settings.Template2s.Touch();
                        return;

                    case "OrderWeight":
                        t.OrderWeight = (float)r.Cells["OrderWeight"].Value;
                        Settings.Template2s.Touch();
                        return;

                    case "FileFilterRegex":
                        t.FileFilterRegex = (Regex)r.Cells["FileFilterRegex"].Value;
                        Settings.Template2s.Touch();
                        return; return;
                    }
                }
                catch (Exception ex)
                {
                    Message.Error2(ex);
                }
            };

            template2s.UserDeletingRow += delegate(object sender, DataGridViewRowCancelEventArgs e)
            {
                try
                {
                    if (e.Row == null || e.Row.Tag == null)
                    {
                        return;
                    }
                    if (!Message.YesNo("Template '" + e.Row.Cells["Name_"].Value + "' will be deleted! Proceed?"))
                    {
                        e.Cancel = true;
                        return;
                    }
                }
                catch (Exception ex)
                {
                    Win.LogMessage.Error(ex);
                }
            };

            template2s.UserDeletedRow += delegate(object sender, DataGridViewRowEventArgs e)
            {
                Settings.Template2s.Touch();
            };

            TemplateManager.Templates = template2s;

            template2s.SelectionChanged += delegate(object sender, EventArgs e)
            {
                //if (templates.SelectedRows.Count < 1)
                //    return;
                //var r = templates.SelectedRows[templates.SelectedRows.Count - 1];
                //if (r.IsNewRow)//hacky forcing commit a newly added row and display the blank row
                //{
                //    try
                //    {
                //        int i = templates.Rows.Add();
                //        templates.Rows[i].Selected = true;
                //        templates.Rows[i].Cells["Active"].Value = true;
                //    }
                //    catch { }
                //}
            };

            template2s.CellClick += delegate(object sender, DataGridViewCellEventArgs e)
            {
                if (e.RowIndex < 0)
                {
                    return;
                }
                DataGridViewRow r = template2s.Rows[e.RowIndex];
                if (e.ColumnIndex < 0)//row's header
                {
                    return;
                }

                if (r.IsNewRow)//hacky forcing commit a newly added row and display the blank row
                {
                    try
                    {
                        int i = template2s.Rows.Add();
                        r = template2s.Rows[i];
                        Template2 t = Settings.Template2s.CreateInitialTemplate();
                        r.Tag = t;
                        r.Cells["Active"].Value          = t.Active;
                        r.Cells["OrderWeight"].Value     = t.OrderWeight;
                        r.Cells["FileFilterRegex"].Value = t.FileFilterRegex;
                        r.Selected = true;
                    }
                    catch { }
                }

                switch (template2s.Columns[e.ColumnIndex].Name)
                {
                case "Edit":
                    editTemplate(r);
                    break;

                case "Copy":
                    Template2 t = (Template2)r.Tag;
                    if (t == null)
                    {
                        return;
                    }
                    Template2 t2 = Serialization.Json.Deserialize <Template2>(Serialization.Json.Serialize(t));
                    t2.Template.Name            = "";
                    t2.Template.Editor.TestFile = null;
                    int             i  = template2s.Rows.Add(new DataGridViewRow());
                    DataGridViewRow r2 = template2s.Rows[i];
                    r2.Tag = t2;
                    r2.Cells["Name_"].Value           = t2.Template.Name.Trim();
                    r2.Cells["Active"].Value          = t2.Active;
                    r2.Cells["OrderWeight"].Value     = t2.OrderWeight;
                    r2.Cells["FileFilterRegex"].Value = t2.FileFilterRegex;
                    editTemplate(r2);
                    break;

                case "Edit2":
                    edit2Template(r);
                    break;
                }
            };

            progress.Maximum = 10000;
        }