/// <summary> /// Checks if a give <see cref="SortText"/> style exists in the check list box. /// </summary> /// <param name="sortText">An instance to a <see cref="SortText"/> class to check for.</param> /// <returns><c>true</c> if the given <see cref="SortText"/> style exists in the check list box, <c>false</c> otherwise.</returns> private bool SortStyleExists(SortText sortText) { foreach (var item in listCheckSortStyles.Items) { var itemData = (SortText)item; if (itemData == sortText) { return(true); } } return(false); }
/// <summary> /// Moves an available sorting item to the check list box of sorting styles to use. /// </summary> private void MoveToSortCheckListBox() { if (listSortStylesAvailable.SelectedItem == null) { return; } var sortText = (SortText)listSortStylesAvailable.SelectedItem; if (sortText.SortTextStyle == SortTextStyle.SubString) { sortText = new SortText(sortText.SortTextStyle, sortText.Descending) { ExtraData1 = (int)nudSubStringRange1.Value, ExtraData2 = (int)nudSubStringRange2.Value, }; } if (sortText.SortTextStyle == SortTextStyle.Regex) { sortText = new SortText(sortText.SortTextStyle, sortText.Descending) { ExtraData1 = tbRegex.Text, }; if (!sortText.IsValidRegex) { return; } SaveRegex(tbRegex.Text); } if (SortStyleExists(sortText)) { return; } if (listCheckSortStyles.SelectedIndex != -1) { listCheckSortStyles.Items.Insert(listCheckSortStyles.SelectedIndex, sortText); } else { listCheckSortStyles.Items.Add(sortText); } btSort.Enabled = listCheckSortStyles.Items.Count > 0; }
private void listSortStylesAvailable_MouseMove(object sender, MouseEventArgs e) { if (!StartDragAvailableListBox || MouseDownPointAvailableListBox.X == e.X && MouseDownPointAvailableListBox.Y == e.Y) { return; } var listBox = (ListBox)sender; int index = listBox.IndexFromPoint(new Point(e.X, e.Y)); if (index != -1) { DragDropOrigin = listBox; var item = (SortText)listBox.Items[index]; if (item.SortTextStyle == SortTextStyle.SubString) { item = new SortText(item.SortTextStyle, item.Descending) { ExtraData1 = (int)nudSubStringRange1.Value, ExtraData2 = (int)nudSubStringRange2.Value }; } else if (item.SortTextStyle == SortTextStyle.Regex) { item = new SortText(item.SortTextStyle, item.Descending) { ExtraData1 = tbRegex.Text }; if (!item.IsValidRegex) { return; } SaveRegex(tbRegex.Text); } listBox.DoDragDrop(item, DragDropEffects.Copy); } }
/// <summary> /// Determines whether the specified <see cref="SortText" /> is equal to this instance. /// </summary> /// <param name="other">The other.</param> /// <returns><c>true</c> if the specified <see cref="SortText" /> is equal to this instance, <c>false</c> otherwise.</returns> protected bool Equals(SortText other) { return(SortTextStyle == other.SortTextStyle && Descending == other.Descending && other.ExtraData1 == ExtraData1 && other.ExtraData2 == ExtraData2); }
/// <summary> /// A method to sort the contents of a <see cref="Scintilla"/> control. /// </summary> /// <param name="scintilla">The scintilla control which contents to sort.</param> /// <param name="sortText">The sort text.</param> /// <param name="stringComparison">The type of string comparison to use with the sort.</param> /// <param name="funcParameters">Optional parameter array in case the sort method function requires parameters.</param> public static void Sort(Scintilla scintilla, SortText sortText, StringComparison stringComparison, params object[] funcParameters) { if ((int)sortText.SortTextStyle >= (int)StringComparison.CurrentCulture && (int)sortText.SortTextStyle < (int)StringComparison.OrdinalIgnoreCase) { Sort(scintilla, (StringComparison)sortText.SortTextStyle, sortText.Descending); } else // lambda functions here for the other sorting styles.. { if (sortText.SortTextStyle == SortTextStyle.Length) { // sort by length.. Sort(scintilla, (StringComparison)sortText.SortTextStyle, sortText.Descending, delegate(IEnumerable <string> lines, SortTextStyle style, bool descending) { var result = descending ? lines.OrderByDescending(f => f.Length) : lines.OrderBy(f => f.Length); return(result); }); return; } if (sortText.SortTextStyle == SortTextStyle.LowerFirst || sortText.SortTextStyle == SortTextStyle.UpperFirst) { // sort by explicit lower or upper case characters firs.. Sort(scintilla, (StringComparison)sortText.SortTextStyle, sortText.Descending, delegate(IEnumerable <string> lines, SortTextStyle style, bool descending) { var enumerable = lines as string[] ?? lines.ToArray(); var orphan = enumerable.Where(f => f.ToLower() == f && f.ToUpper() == f).ToList(); var lower = enumerable.Where(f => f.ToLower() == f && f.ToUpper() != f).ToList(); var upper = enumerable.Where(f => f.ToUpper() == f && f.ToLower() != f).ToList(); lower = descending ? lower.OrderByDescending(f => f.ToComparisonVariant(stringComparison)).ToList() : lower.OrderBy(f => f.ToComparisonVariant(stringComparison)).ToList(); upper = descending ? upper.OrderByDescending(f => f.ToComparisonVariant(stringComparison)).ToList() : upper.OrderBy(f => f.ToComparisonVariant(stringComparison)).ToList(); orphan = descending ? orphan.OrderByDescending(f => f.ToComparisonVariant(stringComparison)).ToList() : orphan.OrderBy(f => f.ToComparisonVariant(stringComparison)).ToList(); var result = new List <string>(); if (sortText.SortTextStyle == SortTextStyle.LowerFirst) { result.AddRange(lower); result.AddRange(upper); } else { result.AddRange(upper); result.AddRange(lower); } result.AddRange(orphan); return(result); }); } if (sortText.SortTextStyle == SortTextStyle.SubString) { Sort(scintilla, (StringComparison)sortText.SortTextStyle, sortText.Descending, delegate(IEnumerable <string> lines, SortTextStyle style, bool descending) { return(sortText.Descending ? lines.OrderByDescending(f => f.ToComparisonVariant(stringComparison).SafeSubString( (int)sortText.ExtraData1 - 1, (int)sortText.ExtraData2)) : lines.OrderBy(f => f.ToComparisonVariant(stringComparison).SafeSubString( (int)sortText.ExtraData1 - 1, (int)sortText.ExtraData2))); }); } if (sortText.SortTextStyle == SortTextStyle.Regex) { Sort(scintilla, (StringComparison)sortText.SortTextStyle, sortText.Descending, delegate(IEnumerable <string> lines, SortTextStyle style, bool descending) { var regex = new Regex(sortText.ExtraData1.ToString(), RegexOptions.Compiled); return(sortText.Descending ? lines.OrderByDescending(f => regex.Match(f).Value.ToComparisonVariant(stringComparison)) : lines.OrderByDescending(f => regex.Match(f).Value.ToComparisonVariant(stringComparison))); }); } } }