Пример #1
0
        private void btnProcess_Click(object sender, EventArgs e)
        {
            //process everything
            System.Reflection.PropertyInfo pi = (cmbStepTwoField.SelectedItem as PropInfo).PropertyInfo;
            object newValue;

            switch ((cmbStepTwoField.SelectedItem as PropInfo).PropertyInfo.Name)
            {
            case "Type":
            case "SubType":
            case "Special":
            case "Size":
                newValue = cmbStepTwoValue.SelectedIndex;
                break;

            case "Name":
                newValue = txtStepTwoValue.Text;
                break;

            default:
                int dummy;
                newValue = txtStepTwoValue.Text.ToString();
                if (!int.TryParse(newValue.ToString(), out dummy))
                {
                    MessageBox.Show("Only integer values are supported for the value field in step two.");
                    return;
                }
                newValue = dummy;
                break;
            }

            if (!chkFilterOn.Checked)
            {             //process the change for EVERY item record. save changes immediately.
                if (MessageBox.Show(
                        "This change will be processed for every item immediately. The change is irreversible. Are you sure you want to continue? You may specify a filter by checking the filter option.", "No filter selected",
                        MessageBoxButtons.YesNo,
                        MessageBoxIcon.Exclamation) == DialogResult.No
                    )
                {
                    return;
                }

                rtfOutput.Text += "Processing change: set " + pi.Name + "(" + pi.PropertyType.ToString() + ")=" + newValue.ToString() + " for all items...";
                eif.Data.ForEach((EOLib.Data.IDataRecord record) =>
                {
                    ItemRecord rec = (ItemRecord)record;
                    System.Reflection.PropertyInfo prop = rec.GetType().GetProperty(pi.Name);
                    prop.SetValue(rec, Convert.ChangeType(newValue, pi.PropertyType));
                });

                rtfOutput.Text += "done.\n\n";
            }
            else
            {
                object          compareValue;
                CompareOperator op;
                try
                {
                    op = (CompareOperator)cmbCompareType.SelectedIndex;
                    if (op == CompareOperator.REGEX && cmbCompareValue.Enabled)
                    {
                        MessageBox.Show("You can't use a regex to parse enumerated types");
                        return;
                    }
                }
                catch
                {
                    MessageBox.Show("Invalid comparison operator specified.");
                    return;
                }

                Regex comparer = null;
                if (op == CompareOperator.REGEX)
                {
                    try
                    {
                        compareValue = txtCompareValue.Text;
                        comparer     = new Regex(compareValue.ToString());
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("Unable to parse regular expression:\n " + ex.Message, "Error!");
                        return;
                    }
                }
                else
                {
                    switch ((cmbStepThreeField.SelectedItem as PropInfo).PropertyInfo.Name)
                    {
                    case "Type":
                        compareValue = (ItemType)cmbCompareValue.SelectedIndex;
                        break;

                    case "SubType":
                        compareValue = (ItemSubType)cmbCompareValue.SelectedIndex;
                        break;

                    case "Special":
                        compareValue = (ItemSpecial)cmbCompareValue.SelectedIndex;
                        break;

                    case "Size":
                        compareValue = (ItemSize)cmbCompareValue.SelectedIndex;
                        break;

                    case "Name":
                        compareValue = txtCompareValue.Text;
                        break;

                    default:
                        compareValue = txtCompareValue.Text;
                        int dummy;
                        if (!int.TryParse(compareValue.ToString(), out dummy))
                        {
                            MessageBox.Show("Only integer values are allowed for this comparison type.", "Error parsing");
                            return;
                        }
                        compareValue = dummy;
                        break;
                    }
                }

                List <ItemRecord> filtered = eif.Data.Where(record =>
                {
                    ItemRecord rec = (ItemRecord)record;
                    if (rec == null || rec.ID == 0)
                    {
                        return(false);
                    }

                    System.Reflection.PropertyInfo comparePropertyInfo = (cmbStepThreeField.SelectedItem as PropInfo).PropertyInfo;
                    System.Reflection.PropertyInfo currentInfo         = rec.GetType().GetProperty(comparePropertyInfo.Name);
                    switch (op)
                    {
                    case CompareOperator.EQ:
                        return(compareValue.ToString() == currentInfo.GetValue(rec).ToString());

                    case CompareOperator.GT:
                        return(compareValue.ToString().CompareTo(currentInfo.GetValue(rec).ToString()) > 0);

                    case CompareOperator.GTE:
                        return(compareValue.ToString().CompareTo(currentInfo.GetValue(rec).ToString()) >= 0);

                    case CompareOperator.LT:
                        return(compareValue.ToString().CompareTo(currentInfo.GetValue(rec).ToString()) < 0);

                    case CompareOperator.LTE:
                        return(compareValue.ToString().CompareTo(currentInfo.GetValue(rec).ToString()) <= 0);

                    case CompareOperator.NE:
                        return(compareValue.ToString() != currentInfo.GetValue(rec).ToString());

                    case CompareOperator.REGEX:
                        object curValAsString = currentInfo.GetValue(rec);
                        if (curValAsString == null)
                        {
                            return(false);
                        }

                        return(comparer.IsMatch(curValAsString.ToString()));

                    default:
                        return(false);
                    }
                }).ToList().ConvertAll <ItemRecord>((recc) => { return((ItemRecord)recc); });

                filtered.ForEach((ItemRecord rec) =>
                {
                    if (!changes)
                    {
                        changes = true;
                    }

                    int index = eif.Data.FindIndex(x => (x as ItemRecord).ID == rec.ID);

                    rtfOutput.Text          += "Found matching item " + rec.Name + " (" + rec.ID + ")\n";
                    rtfOutput.Text          += "  replacing " + pi.Name + " (currently " + pi.GetValue(rec).ToString() + ") with new value " + newValue.ToString() + "\n";
                    rtfOutput.SelectionStart = rtfOutput.TextLength;
                    rtfOutput.ScrollToCaret();

                    object setter;
                    //enums are special: convert them to object
                    if (pi.PropertyType == typeof(ItemType) ||
                        pi.PropertyType == typeof(ItemSubType) ||
                        pi.PropertyType == typeof(ItemSpecial) ||
                        pi.PropertyType == typeof(ItemSize))
                    {
                        setter = Convert.ChangeType(Enum.ToObject(pi.PropertyType, newValue), pi.PropertyType);
                    }
                    else
                    {
                        setter = Convert.ChangeType(newValue, pi.PropertyType);
                    }

                    pi.SetValue(rec, setter);

                    eif.Data[index] = rec;
                });
            }
        }