private void btnCompare_Click(object sender, EventArgs e) { Button btn = sender as Button; if (btn != null) { if (btn.Equals(this.btnCompare)) { this.lbxChangedLine.Items.Clear(); this.tsLblSource.Text = this.tsLblDest.Text = ""; string pText1 = this.ucCompareSelectSource.GetString(); string pText2 = this.ucCompareSelectDestination.GetString(); if (string.IsNullOrEmpty(pText1)) { this.gbxSource.Visible = this.gbxDest.Visible = true; this.pnlCompareInfo.Visible = !this.gbxSource.Visible; } else { if (string.IsNullOrEmpty(pText2)) { this.gbxSource.Visible = this.gbxDest.Visible = true; this.pnlCompareInfo.Visible = !this.gbxSource.Visible; } else { this.tsLblSource.Text = this.ucCompareSelectSource.GetTitle(); this.tsLblDest.Text = this.ucCompareSelectDestination.GetTitle(); this._sorurceText = new DiffListText(pText1); this._destText = new DiffListText(pText2); this.lvSource.Items.Clear(); this.lvDestination.Items.Clear(); DiffEngine diffEngine = new DiffEngine(); DiffEngineLevel level = !this.rbtnFast.Checked ? (!this.rbtnMedium.Checked ? DiffEngineLevel.SlowPerfect : DiffEngineLevel.Medium) : DiffEngineLevel.FastImperfect; diffEngine.ProcessDiff((IDiffList)this._sorurceText, (IDiffList)this._destText, level); this.ShowDiff(this._sorurceText, this._destText, diffEngine.DiffReport()); this.gbxSource.Visible = this.gbxDest.Visible = false; this.pnlCompareInfo.Visible = !this.gbxSource.Visible; } } } else if (btn.Equals(this.btnVisible)) { this.gbxSource.Visible = this.gbxDest.Visible = !this.gbxSource.Visible; this.pnlCompareInfo.Visible = !this.gbxSource.Visible; } else if (btn.Equals(this.btnClose)) { this.Close(); } } }
public void TextDiff(string Source, bool SourceIsFile, string Destination, bool DestinationIsFile) { using (new DisposableState(this, null)) { DiffListText sLF = null; DiffListText dLF = null; try { sLF = new DiffListText(Source, SourceIsFile); dLF = new DiffListText(Destination, DestinationIsFile); } catch (Exception ex) { MessageBox.Show(ex.Message, "File Error"); return; } try { double time = 0; DiffEngine de = new DiffEngine(); time = de.ProcessDiff(sLF, dLF, DiffEngineLevel.SlowPerfect); List <DiffResultSpan> rep = de.DiffReport(); ShowDiff(sLF, dLF, rep, time); } catch (Exception ex) { string tmp = string.Format("{0}{1}{1}***STACK***{1}{2}", ex.Message, Environment.NewLine, ex.StackTrace); MessageBox.Show(tmp, "Compare Error"); } } }
private void ShowDiff(DiffListText source, DiffListText destination, List <DiffResultSpan> DiffLines, double seconds) { currentIndex = 0; cmdDifferenceNext.Enabled = true; cmdDifferencePrevious.Enabled = true; differences = new List <int>(); rtbSourceScript.BeginUpdate(); rtbDestinationScript.BeginUpdate(); List <KeyValuePair <string, Color> > sourceLines = new List <KeyValuePair <string, Color> >(); List <KeyValuePair <string, Color> > destinationLines = new List <KeyValuePair <string, Color> >(); int i; foreach (DiffResultSpan drs in DiffLines) { switch (drs.Status) { case DiffResultSpanStatus.DeleteSource: differences.Add(sourceLines.Count); for (i = 0; i < drs.Length; i++) { sourceLines.Add(new KeyValuePair <string, Color>(((TextLine)source.GetByIndex(drs.SourceIndex + i)).Line, Color.Red)); destinationLines.Add(new KeyValuePair <string, Color>(new string(' ', ((TextLine)source.GetByIndex(drs.SourceIndex + i)).Line.Length), Color.Blue)); } break; case DiffResultSpanStatus.NoChange: for (i = 0; i < drs.Length; i++) { sourceLines.Add(new KeyValuePair <string, Color>(((TextLine)source.GetByIndex(drs.SourceIndex + i)).Line, Color.Empty)); destinationLines.Add(new KeyValuePair <string, Color>(((TextLine)destination.GetByIndex(drs.DestIndex + i)).Line, Color.Empty)); } break; case DiffResultSpanStatus.AddDestination: differences.Add(sourceLines.Count); for (i = 0; i < drs.Length; i++) { sourceLines.Add(new KeyValuePair <string, Color>(new string(' ', ((TextLine)destination.GetByIndex(drs.DestIndex + i)).Line.Length), Color.Blue)); destinationLines.Add(new KeyValuePair <string, Color>(((TextLine)destination.GetByIndex(drs.DestIndex + i)).Line, Color.Blue)); } break; case DiffResultSpanStatus.Replace: differences.Add(sourceLines.Count); for (i = 0; i < drs.Length; i++) { sourceLines.Add(new KeyValuePair <string, Color>(((TextLine)source.GetByIndex(drs.SourceIndex + i)).Line, Color.Red)); destinationLines.Add(new KeyValuePair <string, Color>(((TextLine)destination.GetByIndex(drs.DestIndex + i)).Line, Color.Green)); } break; } } FillScript(sourceLines, rtbSourceScript); FillScript(destinationLines, rtbDestinationScript); rtbSourceScript.EndUpdate(); rtbDestinationScript.EndUpdate(); this.Text = differences.Count + " differences."; }
private void ShowDiff(DiffListText source, DiffListText destination, ArrayList DiffLines) { this.lvSource.SuspendLayout(); this.lvDestination.SuspendLayout(); this.pbxNavigator.Image = (Image)null; Color color1 = Color.FromArgb((int)byte.MaxValue, 175, 238, 238); Color color2 = Color.FromArgb((int)byte.MaxValue, 152, 251, 152); Color color3 = Color.FromArgb((int)byte.MaxValue, (int)byte.MaxValue, 192, 203); int num = 1; foreach (DiffResultSpan diffLine in DiffLines) { switch (diffLine.Status) { case DiffResultSpanStatus.NoChange: for (int index = 0; index < diffLine.Length; ++index) { this.lvSource.Items.Add(new ListViewItem(num.ToString()) { SubItems = { ((TextLine)source.GetByIndex(diffLine.SourceIndex + index)).Line } }); this.lvDestination.Items.Add(new ListViewItem(num.ToString()) { SubItems = { ((TextLine)destination.GetByIndex(diffLine.DestIndex + index)).Line } }); ++num; } break; case DiffResultSpanStatus.Replace: this.lbxChangedLine.Items.Add(num.ToString()); for (int index = 0; index < diffLine.Length; ++index) { ListViewItem listViewItem1 = new ListViewItem(num.ToString()); listViewItem1.SubItems.Add(((TextLine)source.GetByIndex(diffLine.SourceIndex + index)).Line); this.lvSource.Items.Add(listViewItem1); ListViewItem listViewItem2 = new ListViewItem(num.ToString()); listViewItem2.SubItems.Add(((TextLine)destination.GetByIndex(diffLine.DestIndex + index)).Line); this.lvDestination.Items.Add(listViewItem2); listViewItem1.BackColor = listViewItem2.BackColor = color2; ++num; } break; case DiffResultSpanStatus.DeleteSource: this.lbxChangedLine.Items.Add(num.ToString()); for (int index = 0; index < diffLine.Length; ++index) { ListViewItem listViewItem1 = new ListViewItem(num.ToString()); listViewItem1.SubItems.Add(((TextLine)source.GetByIndex(diffLine.SourceIndex + index)).Line); this.lvSource.Items.Add(listViewItem1); ListViewItem listViewItem2 = new ListViewItem(num.ToString()); listViewItem2.SubItems.Add(""); this.lvDestination.Items.Add(listViewItem2); listViewItem1.BackColor = listViewItem2.BackColor = color3; ++num; } break; case DiffResultSpanStatus.AddDestination: this.lbxChangedLine.Items.Add(num.ToString()); for (int index = 0; index < diffLine.Length; ++index) { ListViewItem listViewItem1 = new ListViewItem(num.ToString()); listViewItem1.SubItems.Add(""); this.lvSource.Items.Add(listViewItem1); ListViewItem listViewItem2 = new ListViewItem(num.ToString()); listViewItem2.SubItems.Add(((TextLine)destination.GetByIndex(diffLine.DestIndex + index)).Line); this.lvDestination.Items.Add(listViewItem2); listViewItem1.BackColor = listViewItem2.BackColor = color1; ++num; } break; } } this.lvSource.ResumeLayout(); this.lvDestination.ResumeLayout(); this.lvSource.Scrollable = true; this.lvDestination.Scrollable = true; ++this.lvSource.Columns[0].Width; --this.lvSource.Columns[0].Width; ++this.lvDestination.Columns[0].Width; --this.lvDestination.Columns[0].Width; this.CreateNavigatorBitmap(); }