Skip to content

Commit 05c3633

Browse files
committed
Finished new filter feature.
1 parent b2562c7 commit 05c3633

File tree

5 files changed

+215
-22
lines changed

5 files changed

+215
-22
lines changed

src/Logbert/Dialogs/Docking/FrmLogFilter.Designer.cs

Lines changed: 16 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Logbert/Dialogs/Docking/FrmLogFilter.cs

Lines changed: 143 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ public partial class FrmLogFilter : DockContent, ILogPresenter, ILogFilterProvid
7878
/// </summary>
7979
private readonly IList<LogFilter> mLogFilter = new List<LogFilter>();
8080

81+
/// <summary>
82+
/// The <see cref="Image"/> to use for <see cref=LogFilter"/>s.
83+
/// </summary>
84+
private static readonly Image mFilterImage = Properties.Resources.filter_16xLG;
85+
8186
#endregion
8287

8388
#region Public Properties
@@ -136,6 +141,46 @@ protected override void Dispose(bool disposing)
136141

137142
#region Private Methods
138143

144+
/// <summary>
145+
/// Update the <see cref="DataGridView"/> of <see cref=LogFilter"/>s.
146+
/// </summary>
147+
private void UpdateLogFilters()
148+
{
149+
if (mLogFilter != null)
150+
{
151+
LogFilter selectedFilter = dgvFilter.SelectedRows.Count > 0
152+
? dgvFilter.SelectedRows[0].Tag as LogFilter
153+
: null;
154+
155+
dgvFilter.SuspendDrawing();
156+
157+
try
158+
{
159+
dgvFilter.Rows.Clear();
160+
161+
foreach (LogFilterColumn filter in mLogFilter)
162+
{
163+
int rowIndex = dgvFilter.Rows.Add(
164+
mFilterImage
165+
, filter.IsActive
166+
, mLogProvider.Columns[filter.ColumnIndex - 1]
167+
, filter.ColumnMatchValueRegEx);
168+
169+
dgvFilter.Rows[rowIndex].Tag = filter;
170+
171+
if (Equals(selectedFilter, filter))
172+
{
173+
dgvFilter.Rows[rowIndex].Selected = true;
174+
}
175+
}
176+
}
177+
finally
178+
{
179+
dgvFilter.ResumeDrawing();
180+
}
181+
}
182+
}
183+
139184
/// <summary>
140185
/// Handles the Click event of the zoom in <see cref="ToolStripItem"/>.
141186
/// </summary>
@@ -167,7 +212,51 @@ private void TsbAddFilterClick(object sender, EventArgs e)
167212
{
168213
if (addEditFilterDlg.ShowDialog(this) == DialogResult.OK)
169214
{
170-
// TODO (CC): Add the new filter.
215+
LogFilterColumn newLogFilter = new LogFilterColumn(
216+
addEditFilterDlg.IsFilterActive
217+
, addEditFilterDlg.ColumnIndex
218+
, addEditFilterDlg.ExpressionRegex);
219+
220+
mLogFilter.Add(newLogFilter);
221+
222+
// Update the data grid.
223+
UpdateLogFilters();
224+
225+
// Inform the filter handler about the changed filters.
226+
mLogFilterHandler.FilterChanged();
227+
}
228+
}
229+
}
230+
231+
/// <summary>
232+
/// Handles the Click event of the edit filter <see cref="ToolStripItem"/>.
233+
/// </summary>
234+
/// <param name="sender"></param>
235+
/// <param name="e"></param>
236+
private void TsbEditFilterClick(object sender, EventArgs e)
237+
{
238+
if (dgvFilter.SelectedRows.Count > 0)
239+
{
240+
LogFilterColumn filterToEdit = dgvFilter.SelectedRows[0].Tag as LogFilterColumn;
241+
242+
if (filterToEdit != null)
243+
{
244+
using (FrmAddEditFilter addEditFilterDlg = new FrmAddEditFilter(mLogProvider, filterToEdit))
245+
{
246+
if (addEditFilterDlg.ShowDialog(this) == DialogResult.OK)
247+
{
248+
filterToEdit.Update(
249+
addEditFilterDlg.IsFilterActive
250+
, addEditFilterDlg.ColumnIndex
251+
, addEditFilterDlg.ExpressionRegex);
252+
253+
// Update the data grid.
254+
UpdateLogFilters();
255+
256+
// Inform the filter handler about the changed filters.
257+
mLogFilterHandler.FilterChanged();
258+
}
259+
}
171260
}
172261
}
173262
}
@@ -177,26 +266,78 @@ private void TsbAddFilterClick(object sender, EventArgs e)
177266
/// </summary>
178267
private void TsbRemoveFilterClick(object sender, EventArgs e)
179268
{
269+
if (dgvFilter.SelectedRows.Count > 0)
270+
{
271+
// Remove the filter from the collction.
272+
mLogFilter.Remove(dgvFilter.SelectedRows[0].Tag as LogFilter);
273+
274+
// Update the data grid.
275+
UpdateLogFilters();
180276

277+
// Inform the filter handler about the changed filters.
278+
mLogFilterHandler.FilterChanged();
279+
}
181280
}
182281

183282
/// <summary>
184283
/// Handles the SelectionChanged event of the filter <see cref="DataGridView"/>.
185284
/// </summary>
186285
private void DgvFilterSelectionChanged(object sender, EventArgs e)
187286
{
287+
bool atLeastOneItemSelected = dgvFilter.SelectedRows.Count > 0;
188288

289+
tsbRemoveFilter.Enabled = atLeastOneItemSelected;
290+
tsbEditFilter.Enabled = atLeastOneItemSelected;
189291
}
190292

191293
/// <summary>
192294
/// Handles the CellDoubleClick event of the filter <see cref="DataGridView"/>.
193295
/// </summary>
194296
private void DgvFilterCellCoubleClick(object sender, DataGridViewCellEventArgs e)
195297
{
196-
if (e.RowIndex >= dgvFilter.RowCount || e.RowIndex < 0)
298+
if (e.RowIndex >= dgvFilter.RowCount || e.RowIndex < 0 || e.ColumnIndex == 1)
197299
{
198300
return;
199301
}
302+
303+
// Double click means editing!
304+
tsbEditFilter.PerformClick();
305+
}
306+
307+
/// <summary>
308+
/// Handles the CellValueChanged event of the filter <see cref=DataGridView"/>.
309+
/// </summary>
310+
private void DgvFilterCellValueChanged(object sender, DataGridViewCellEventArgs e)
311+
{
312+
if (e.RowIndex >= 0 && e.ColumnIndex == 1)
313+
{
314+
LogFilterColumn filterToEdit = dgvFilter.Rows[e.RowIndex].Tag as LogFilterColumn;
315+
316+
if (filterToEdit != null)
317+
{
318+
filterToEdit.Update(
319+
(bool)dgvFilter.Rows[e.RowIndex].Cells[e.ColumnIndex].Value
320+
, filterToEdit.ColumnIndex
321+
, filterToEdit.ColumnMatchValueRegEx);
322+
323+
// Update the data grid.
324+
UpdateLogFilters();
325+
326+
// Inform the filter handler about the changed filters.
327+
mLogFilterHandler.FilterChanged();
328+
}
329+
}
330+
}
331+
332+
/// <summary>
333+
/// Handles the CurrentCellDirtyStateChanged event of the filter <see cref=DataGridView"/>.
334+
/// </summary>
335+
private void DgvFilterCurrentCellDirtyStateChanged(object sender, EventArgs e)
336+
{
337+
if (dgvFilter.IsCurrentCellDirty)
338+
{
339+
dgvFilter.CommitEdit(DataGridViewDataErrorContexts.Commit);
340+
}
200341
}
201342

202343
#endregion

src/Logbert/Dialogs/FrmAddEditFilter.cs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,39 @@ public override string Text
6262
}
6363
}
6464

65+
/// <summary>
66+
/// Gets the selected active state of the <see cref="LogFilter"/>.
67+
/// </summary>
68+
public bool IsFilterActive
69+
{
70+
get
71+
{
72+
return chkFilterIsActive.Checked;
73+
}
74+
}
75+
76+
/// <summary>
77+
/// Gets the selected column index of the <see cref=LogFilter"/>.
78+
/// </summary>
79+
public int ColumnIndex
80+
{
81+
get
82+
{
83+
return cmbColumnToFilter.SelectedIndex + 1;
84+
}
85+
}
86+
87+
/// <summary>
88+
/// Gets the entered regular expression of the <see cref=LogFilter"/>.
89+
/// </summary>
90+
public string ExpressionRegex
91+
{
92+
get
93+
{
94+
return txtExpressionToFilterFor.Text;
95+
}
96+
}
97+
6598
#endregion
6699

67100
#region Constructor
@@ -94,7 +127,7 @@ public FrmAddEditFilter(ILogProvider logProvider, LogFilterColumn filterToEdit)
94127
if (filterToEdit != null)
95128
{
96129
chkFilterIsActive.Checked = filterToEdit.IsActive;
97-
cmbColumnToFilter.SelectedIndex = filterToEdit.ColumnIndex;
130+
cmbColumnToFilter.SelectedIndex = filterToEdit.ColumnIndex - 1;
98131
txtExpressionToFilterFor.Text = filterToEdit.ColumnMatchValueRegEx;
99132
}
100133
}

src/Logbert/Logging/Filter/LogFilter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public abstract class LogFilter
3838
#region Public Properties
3939

4040
/// <summary>
41-
/// Determines whether the <see cref=LogFilter"/> is active, or not.
41+
/// Gets or sets whether the <see cref=LogFilter"/> is active, or not.
4242
/// </summary>
4343
public abstract bool IsActive
4444
{

src/Logbert/Logging/Filter/LogFilterColumn.cs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,20 @@ public sealed class LogFilterColumn : LogFilter
3939
{
4040
#region Private Fields
4141

42+
/// <summary>
43+
/// Hold the active state of the <see cref="LogFilterColumn"/>.
44+
/// </summary>
4245
private bool mIsActive;
4346

4447
/// <summary>
4548
/// Holds the index of the column to match.
4649
/// </summary>
47-
private readonly int mColumnIndex;
50+
private int mColumnIndex;
4851

4952
/// <summary>
5053
/// Holds the value the column should have to match.
5154
/// </summary>
52-
private readonly Regex mColumnMatchValueRegEx;
55+
private Regex mColumnMatchValueRegEx;
5356

5457
#endregion
5558

@@ -110,18 +113,32 @@ public override bool Match(LogMessage value)
110113
mColumnMatchValueRegEx.IsMatch(columnValue.ToString());
111114
}
112115

116+
/// <summary>
117+
/// Update the <see cref=LogFilterColumn"/> with the specified parameters.
118+
/// </summary>
119+
/// <param name="isActive">The new active state of the <see cref=LogFilterColumn"/>.</param>
120+
/// <param name="columnIndex">The new column index of the <see cref=LogFilterColumn"/>.</param>
121+
/// <param name="expression">The new regular expression of the <see cref=LogFilterColumn"/>.</param>
122+
public void Update(bool isActive, int columnIndex, string expression)
123+
{
124+
mIsActive = isActive;
125+
mColumnIndex = columnIndex;
126+
mColumnMatchValueRegEx = new Regex(expression);
127+
}
128+
113129
#endregion
114130

115131
#region Constructor
116132

117133
/// <summary>
118134
/// Creates a new instance of a <see cref="LogFilterLevel"/>.
119135
/// </summary>
136+
/// <param name="isActive">The active state of the <see cref=LogFilterColumn"/>.</param>
120137
/// <param name="columnIndex">The index of the column to match.</param>
121138
/// <param name="matchRegex">The string for the column match <see cref="Regex"/>.</param>
122-
public LogFilterColumn(int columnIndex, string matchRegex)
139+
public LogFilterColumn(bool isActive, int columnIndex, string matchRegex)
123140
{
124-
mIsActive = true;
141+
mIsActive = isActive;
125142
mColumnIndex = columnIndex;
126143
mColumnMatchValueRegEx = new Regex(matchRegex ?? ".*");
127144
}

0 commit comments

Comments
 (0)