예제 #1
0
        /// <summary>
        /// Runs the diff process for the specified files.
        /// </summary>
        public void CompareDifference(Object sender, DoWorkEventArgs e)
        {
            this.sourceListView.Items.Clear();
            this.destinationListView.Items.Clear();
            this.sourceListView.BeginUpdate();
            this.destinationListView.BeginUpdate();
            try
            {
                String     source = ((String[])e.Argument)[0];
                String     destination = ((String[])e.Argument)[1];
                Int32      count = 1; ListViewItem lviS, lviD;
                TextFile   file1 = new TextFile(source);
                TextFile   file2 = new TextFile(destination);
                DiffEngine de    = new DiffEngine();
                de.ProcessDiff(file1, file2, DiffEngineLevel.Medium);
                ArrayList lines = de.DiffReport();
                Int32     counter = 0, total = lines.Count;
                foreach (DiffResultSpan drs in lines)
                {
                    switch (drs.Status)
                    {
                    case DiffResultSpanStatus.DeleteSource:
                        for (Int32 i = 0; i < drs.Length; i++)
                        {
                            lviS           = new ListViewItem(count.ToString("00000"));
                            lviD           = new ListViewItem(count.ToString("00000"));
                            lviS.Font      = lviD.Font = new Font("Courier New", 8.25F);
                            lviS.BackColor = Color.Red;
                            lviS.SubItems.Add(((TextLine)file1.GetByIndex(drs.SourceIndex + i)).Line);
                            lviD.BackColor = Color.LightGray;
                            lviD.SubItems.Add("");
                            this.sourceListView.Items.Add(lviS);
                            this.destinationListView.Items.Add(lviD);
                            count++;
                        }
                        break;

                    case DiffResultSpanStatus.NoChange:
                        for (Int32 i = 0; i < drs.Length; i++)
                        {
                            lviS           = new ListViewItem(count.ToString("00000"));
                            lviD           = new ListViewItem(count.ToString("00000"));
                            lviS.Font      = lviD.Font = new Font("Courier New", 8.25F);
                            lviS.BackColor = Color.White;
                            lviS.SubItems.Add(((TextLine)file1.GetByIndex(drs.SourceIndex + i)).Line);
                            lviD.BackColor = Color.White;
                            lviD.SubItems.Add(((TextLine)file2.GetByIndex(drs.DestIndex + i)).Line);
                            this.sourceListView.Items.Add(lviS);
                            this.destinationListView.Items.Add(lviD);
                            count++;
                        }
                        break;

                    case DiffResultSpanStatus.AddDestination:
                        for (Int32 i = 0; i < drs.Length; i++)
                        {
                            lviS           = new ListViewItem(count.ToString("00000"));
                            lviD           = new ListViewItem(count.ToString("00000"));
                            lviS.Font      = lviD.Font = new Font("Courier New", 8.25F);
                            lviS.BackColor = Color.LightGray;
                            lviS.SubItems.Add("");
                            lviD.BackColor = Color.LightGreen;
                            lviD.SubItems.Add(((TextLine)file2.GetByIndex(drs.DestIndex + i)).Line);
                            this.sourceListView.Items.Add(lviS);
                            this.destinationListView.Items.Add(lviD);
                            count++;
                        }
                        break;

                    case DiffResultSpanStatus.Replace:
                        for (Int32 i = 0; i < drs.Length; i++)
                        {
                            lviS           = new ListViewItem(count.ToString("00000"));
                            lviD           = new ListViewItem(count.ToString("00000"));
                            lviS.Font      = lviD.Font = new Font("Courier New", 8.25F);
                            lviS.BackColor = Color.Red;
                            lviS.SubItems.Add(((TextLine)file1.GetByIndex(drs.SourceIndex + i)).Line);
                            lviD.BackColor = Color.LightGreen;
                            lviD.SubItems.Add(((TextLine)file2.GetByIndex(drs.DestIndex + i)).Line);
                            this.sourceListView.Items.Add(lviS);
                            this.destinationListView.Items.Add(lviD);
                            count++;
                        }
                        break;
                    }
                    counter++;
                    Int32 percent = (100 * counter) / total;
                    this.backgroundWorker.ReportProgress(percent);
                }
                e.Result = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                e.Result = false;
            }
            this.destinationListView.EndUpdate();
            this.sourceListView.EndUpdate();
        }