private void ApplyColors(TableRow row, DiffComparison comparison) { switch (comparison.State) { case DiffState.Added: row.CssClass = "bg-novo"; if (AddedBackColor.HasValue) row.BackColor = AddedBackColor.Value; if (AddedForeColor.HasValue) row.ForeColor = AddedForeColor.Value; break; case DiffState.Deleted: row.CssClass = "bg-excluido"; if (DeletedBackColor.HasValue) row.BackColor = DeletedBackColor.Value; if (DeletedForeColor.HasValue) row.ForeColor = DeletedForeColor.Value; break; case DiffState.Modified: row.CssClass = "bg-alterado"; if (ModifiedBackColor.HasValue) row.BackColor = ModifiedBackColor.Value; if (ModifiedForeColor.HasValue) row.ForeColor = ModifiedForeColor.Value; break; case DiffState.NotChanged: row.CssClass = "bg-igual"; if (NotChangedBackColor.HasValue) row.BackColor = NotChangedBackColor.Value; if (NotChangedForeColor.HasValue) row.ForeColor = NotChangedForeColor.Value; break; case DiffState.Unknow: row.CssClass = "bg-inconclusivo"; if (UnknowBackColor.HasValue) row.BackColor = UnknowBackColor.Value; if (UnknowForeColor.HasValue) row.ForeColor = UnknowForeColor.Value; break; } }
protected override void PerformDataBinding(IEnumerable retrievedData) { if (this.DesignMode) { tbl.Rows.Clear(); } base.PerformDataBinding(retrievedData); // Verify data exists. if (retrievedData != null) { ConfigureFont(); ConfigureBorder(); ConfigureLayout(); TableRow row; TableCell cell1 = null, cell2 = null, cell3 = null, cell4 = null, cell5 = null, cell6 = null, cell7 = null, cell8 = null; string dataStr = String.Empty; bool titleAlreadyShowed = false; foreach (object dataItem in retrievedData) { DiffComparison comparison; if (this.DesignMode) { comparison = new DiffComparison(); comparison.NewValue = "abc"; comparison.OldValue = "abc"; comparison.PropertyName = "PropertyName"; } else { comparison = ((DiffComparison)dataItem); if (!StateFilter.Has(comparison.State)) continue; if (PutSpacesInPropertyName) comparison.PropertyName = PutSpaces(comparison.PropertyName); comparison.NewValue = TranslateData(comparison.NewValue); comparison.OldValue = TranslateData(comparison.OldValue); } if (LabelDisposition == LabelDisposition.Row) { if (!titleAlreadyShowed) { row = new TableRow(); tbl.Rows.Add(row); row.CssClass = "header-row"; if (TitleBackColor.HasValue) row.BackColor = TitleBackColor.Value; if (TitleForeColor.HasValue) row.ForeColor = TitleForeColor.Value; CreateCell(cell1, row, true, PropertyNameTextField, PropertyNameStyle); CreateCell(cell3, row, true, OldValueTextField, OldValueStyle); CreateCell(cell5, row, true, NewValueTextField, NewValueStyle); CreateCell(cell7, row, ShowState, StatusTextField, StatusStyle); titleAlreadyShowed = true; } row = new TableRow(); ApplyColors(row, comparison); tbl.Rows.Add(row); CreateCell(cell2, row, true, comparison.PropertyName.PadLeft(comparison.Depth * 3, " ")); CreateCell(cell4, row, true, comparison.OldValue.ToString()); CreateCell(cell6, row, true, comparison.NewValue.ToString()); CreateCell(cell8, row, ShowState, GetDiffState(comparison.State).ToString()); if (comparison.LastProperty) tbl.Rows[tbl.Rows.Count - 1].CssClass += " separator-row"; } else if (LabelDisposition == LabelDisposition.Column) { row = new TableRow(); ApplyColors(row, comparison); tbl.Rows.Add(row); CreateCell(cell1, row, ShowPropertyName, PropertyNameTextField, PropertyNameStyle); CreateCell(cell2, row, true, comparison.PropertyName.PadLeft(comparison.Depth * 3, " ")); CreateCell(cell3, row, ShowOldValueName, OldValueTextField, OldValueStyle); CreateCell(cell4, row, true, comparison.OldValue.ToString()); CreateCell(cell5, row, ShowNewValueName, NewValueTextField, NewValueStyle); CreateCell(cell6, row, true, comparison.NewValue.ToString()); CreateCell(cell7, row, ShowState, GetDiffState(comparison.State).ToString()); } else { row = new TableRow(); ApplyColors(row, comparison); tbl.Rows.Add(row); CreateCell(cell2, row, true, comparison.PropertyName.PadLeft(comparison.Depth * 3, " ")); CreateCell(cell4, row, true, comparison.OldValue.ToString()); CreateCell(cell6, row, true, comparison.NewValue.ToString()); CreateCell(cell7, row, ShowState, GetDiffState(comparison.State).ToString()); } } this.Controls.Add(tbl); } }
private static LongestCommonSubstringResult FindLongestCommonSubstring <T> ( IList <T> listA, IList <T> listB, int startA, int endA, int startB, int endB, DiffComparison update, DiffCache cache) where T : IDiffComparable { // default result, if we can't find anything var result = new LongestCommonSubstringResult(); for (int index1 = startA; index1 < endA; index1++) { for (int index2 = startB; index2 < endB; index2++) { int length = CountEqual(listA, listB, index1, endA, index2, endB, update, cache); if (length > 0) { // Is longer than what we already have? --> record new LCS if (length > result.Length) { result = new LongestCommonSubstringResult(index1, index2, length); } break; } } } return(result); }
private static int CountEqual <T> ( IList <T> listA, IList <T> listB, int startA, int endA, int startB, int endB, DiffComparison update, DiffCache cache) where T : IDiffComparable { int length = 0; while (startA < endA && startB < endB) { var comp = cache.Compare(listA, startA, listB, startB); if (comp != DiffComparison.Same && comp != update) { break; } startA++; startB++; length++; } return(length); }
private static IEnumerable <DiffSection <T> > Calculate <T> ( IList <T> listA, IList <T> listB, int startA, int endA, int startB, int endB, DiffComparison update, DiffCache cache) where T : IDiffComparable { var lcs = FindLongestCommonSubstring(listA, listB, startA, endA, startB, endB, update, cache); if (lcs.Success) { // deal with the section before var sectionsBefore = Calculate(listA, listB, startA, lcs.PositionA, startB, lcs.PositionB, update, cache); foreach (var section in sectionsBefore) { yield return(section); } // output the copy operation for (int i = 0; i < lcs.Length; i++) { int indexA = lcs.PositionA + i, indexB = lcs.PositionB + i; yield return(new DiffSection <T> (DiffType.Copy, indexA, listA [indexA], indexB, listB [indexB])); } // deal with the section after var sectionsAfter = Calculate(listA, listB, lcs.PositionA + lcs.Length, endA, lcs.PositionB + lcs.Length, endB, update, cache); foreach (var section in sectionsAfter) { yield return(section); } yield break; } else if (update == DiffComparison.SoftUpdate) { var sectionsUpdate = Calculate(listA, listB, startA, endA, startB, endB, DiffComparison.Update, cache); foreach (var section in sectionsUpdate) { yield return(section); } yield break; } // if we get here, no LCS if (endA > startA) { // we got content from first collection --> deleted for (int i = 0; i < endA - startA; i++) { yield return(new DiffSection <T> (DiffType.Remove, startA + i, listA[startA + i], startB, default(T))); } } if (endB > startB) { // we got content from second collection --> inserted for (int i = 0; i < endB - startB; i++) { yield return(new DiffSection <T> (DiffType.Add, startA, default(T), startB + i, listB[startB + i])); } } }