Skip to content

Commit edb43b7

Browse files
authored
Merge pull request LogExperts#384 from LogExperts/373-v1120---importing-highlighting-config-is-broken
Fixing the import problem, adapted the import process, now only the highlightgroups and entries are imported from this file. if other data is present, it will not be able to import rename the property, this is a breaking change, as older settings will not be importable, unless the names in the json will be changed from "Hilight" to "Highlight" Export functionality added for the Highlight dialog, this does only export the currently configured highlights to a JSON File.
2 parents ba702fa + 3084f8e commit edb43b7

21 files changed

Lines changed: 209 additions & 202 deletions

src/LogExpert/Classes/PaintHelper.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public static void CellPainting(ILogPaintContext logPaintCtx, BufferedDataGridVi
4444
ILogLine line = logPaintCtx.GetLogLine(rowIndex);
4545
if (line != null)
4646
{
47-
HilightEntry entry = logPaintCtx.FindHighlightEntry(line, true);
47+
HighlightEntry entry = logPaintCtx.FindHighlightEntry(line, true);
4848
e.Graphics.SetClip(e.CellBounds);
4949
if ((e.State & DataGridViewElementStates.Selected) == DataGridViewElementStates.Selected)
5050
{
@@ -290,12 +290,12 @@ public static Rectangle BorderWidths(DataGridViewAdvancedBorderStyle advancedBor
290290

291291
#region Private Methods
292292

293-
private static void PaintCell(ILogPaintContext logPaintCtx, DataGridViewCellPaintingEventArgs e, BufferedDataGridView gridView, bool noBackgroundFill, HilightEntry groundEntry)
293+
private static void PaintCell(ILogPaintContext logPaintCtx, DataGridViewCellPaintingEventArgs e, BufferedDataGridView gridView, bool noBackgroundFill, HighlightEntry groundEntry)
294294
{
295295
PaintHighlightedCell(logPaintCtx, e, gridView, noBackgroundFill, groundEntry);
296296
}
297297

298-
private static void PaintHighlightedCell(ILogPaintContext logPaintCtx, DataGridViewCellPaintingEventArgs e, BufferedDataGridView gridView, bool noBackgroundFill, HilightEntry groundEntry)
298+
private static void PaintHighlightedCell(ILogPaintContext logPaintCtx, DataGridViewCellPaintingEventArgs e, BufferedDataGridView gridView, bool noBackgroundFill, HighlightEntry groundEntry)
299299
{
300300
object value = e.Value ?? string.Empty;
301301

@@ -314,7 +314,7 @@ private static void PaintHighlightedCell(ILogPaintContext logPaintCtx, DataGridV
314314
hme.StartPos = 0;
315315
hme.Length = column.FullValue.Length;
316316

317-
var he = new HilightEntry
317+
var he = new HighlightEntry
318318
{
319319
SearchText = column.FullValue,
320320
ForegroundColor = groundEntry?.ForegroundColor ?? ColorMode.ForeColor,
@@ -427,7 +427,7 @@ private static void PaintHighlightedCell(ILogPaintContext logPaintCtx, DataGridV
427427
private static IList<HilightMatchEntry> MergeHighlightMatchEntries(IList<HilightMatchEntry> matchList, HilightMatchEntry groundEntry)
428428
{
429429
// Fill an area with lenth of whole text with a default hilight entry
430-
HilightEntry[] entryArray = new HilightEntry[groundEntry.Length];
430+
HighlightEntry[] entryArray = new HighlightEntry[groundEntry.Length];
431431
for (int i = 0; i < entryArray.Length; ++i)
432432
{
433433
entryArray[i] = groundEntry.HilightEntry;
@@ -455,7 +455,7 @@ private static IList<HilightMatchEntry> MergeHighlightMatchEntries(IList<Hilight
455455
IList<HilightMatchEntry> mergedList = new List<HilightMatchEntry>();
456456
if (entryArray.Length > 0)
457457
{
458-
HilightEntry currentEntry = entryArray[0];
458+
HighlightEntry currentEntry = entryArray[0];
459459
int lastStartPos = 0;
460460
int pos = 0;
461461
for (; pos < entryArray.Length; ++pos)

src/LogExpert/Config/ConfigManager.cs

Lines changed: 60 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,23 @@ public static void Export(FileInfo fileInfo)
8989
Instance.Save(fileInfo, Settings);
9090
}
9191

92+
public static void Export(FileInfo fileInfo, SettingsFlags flags)
93+
{
94+
Instance.Save(fileInfo, Settings, flags);
95+
}
96+
9297
public static void Import(FileInfo fileInfo, ExportImportFlags flags)
9398
{
9499
Instance._settings = Instance.Import(Instance._settings, fileInfo, flags);
95100
Save(SettingsFlags.All);
96101
}
97102

103+
public static void ImportHighlightSettings(FileInfo fileInfo, ExportImportFlags flags)
104+
{
105+
Instance._settings.Preferences.HighlightGroupList = Instance.Import(Instance._settings.Preferences.HighlightGroupList, fileInfo, flags);
106+
Save(SettingsFlags.All);
107+
}
108+
98109
#endregion
99110

100111
#region Private Methods
@@ -144,7 +155,7 @@ private Settings Load()
144155
/// </summary>
145156
/// <param name="fileInfo">file that has settings saved</param>
146157
/// <returns>loaded or created settings</returns>
147-
private Settings LoadOrCreateNew(FileSystemInfo fileInfo)
158+
private Settings LoadOrCreateNew(FileInfo fileInfo)
148159
{
149160
lock (_loadSaveLock)
150161
{
@@ -212,17 +223,9 @@ private Settings LoadOrCreateNew(FileSystemInfo fileInfo)
212223
filterParams.Init();
213224
}
214225

215-
if (settings.hilightGroupList == null)
226+
if (settings.Preferences.HighlightGroupList == null)
216227
{
217-
settings.hilightGroupList = [];
218-
// migrate old non-grouped entries
219-
HilightGroup defaultGroup = new()
220-
{
221-
GroupName = "[Default]",
222-
HilightEntryList = settings.hilightEntryList
223-
};
224-
225-
settings.hilightGroupList.Add(defaultGroup);
228+
settings.Preferences.HighlightGroupList = [];
226229
}
227230

228231
settings.Preferences.highlightMaskList ??= [];
@@ -248,8 +251,6 @@ private Settings LoadOrCreateNew(FileSystemInfo fileInfo)
248251

249252
SetBoundsWithinVirtualScreen(settings);
250253

251-
ConvertSettings(settings);
252-
253254
return settings;
254255
}
255256
}
@@ -292,7 +293,19 @@ private void Save(FileInfo fileInfo, Settings settings)
292293
SaveAsJSON(fileInfo, settings);
293294
}
294295

295-
private void SaveAsJSON(FileInfo fileInfo, Settings settings)
296+
private void Save(FileInfo fileInfo, Settings settings, SettingsFlags flags)
297+
{
298+
switch (flags)
299+
{
300+
case SettingsFlags.HighlightSettings:
301+
SaveHighlightgroupsAsJSON(fileInfo, settings.Preferences.HighlightGroupList);
302+
break;
303+
}
304+
305+
OnConfigChanged(flags);
306+
}
307+
308+
private static void SaveAsJSON(FileInfo fileInfo, Settings settings)
296309
{
297310
settings.versionBuild = Assembly.GetExecutingAssembly().GetName().Version.Build;
298311

@@ -301,55 +314,39 @@ private void SaveAsJSON(FileInfo fileInfo, Settings settings)
301314
serializer.Serialize(sw, settings);
302315
}
303316

304-
/// <summary>
305-
/// Convert settings loaded from previous versions.
306-
/// </summary>
307-
/// <param name="settings"></param>
308-
private void ConvertSettings(Settings settings)
317+
private static void SaveHighlightgroupsAsJSON(FileInfo fileInfo, List<HighlightGroup> groups)
309318
{
310-
//int oldBuildNumber = settings.versionBuild;
311-
312-
//// All Versions before 3583
313-
//if (oldBuildNumber < 3584)
314-
//{
315-
// // External tools
316-
// List<ToolEntry> newList = [];
317-
// foreach (ToolEntry tool in settings.preferences.toolEntries)
318-
// {
319-
// // set favourite to true only when name is empty, because there are always version released without this conversion fx
320-
// // remove empty tool entries (there were always 3 entries before, which can be empty if not used)
321-
// if (Util.IsNull(tool.name))
322-
// {
323-
// if (!Util.IsNull(tool.cmd))
324-
// {
325-
// tool.name = tool.cmd;
326-
// tool.isFavourite = true;
327-
// newList.Add(tool);
328-
// }
329-
// }
330-
// else
331-
// {
332-
// newList.Add(tool);
333-
// }
334-
// if (Util.IsNull(tool.iconFile))
335-
// {
336-
// tool.iconFile = tool.cmd;
337-
// tool.iconIndex = 0;
338-
// }
339-
// }
340-
// settings.preferences.toolEntries = newList;
341-
//}
342-
343-
//if (oldBuildNumber < 3584)
344-
//{
345-
// // Set the color for the FilterList entries to default (black)
346-
// foreach (FilterParams filterParam in settings.filterList)
347-
// {
348-
// filterParam.color = Color.FromKnownColor(KnownColor.Black);
349-
// }
350-
//}
319+
using StreamWriter sw = new(fileInfo.Create());
320+
JsonSerializer serializer = new();
321+
serializer.Serialize(sw, groups);
351322
}
352323

324+
private List<HighlightGroup> Import(List<HighlightGroup> currentGroups, FileInfo fileInfo, ExportImportFlags flags)
325+
{
326+
List<HighlightGroup> newGroups;
327+
328+
try
329+
{
330+
newGroups = JsonConvert.DeserializeObject<List<HighlightGroup>>(File.ReadAllText($"{fileInfo.FullName}"));
331+
}
332+
catch (Exception e)
333+
{
334+
_logger.Error($"Error while deserializing config data: {e}");
335+
newGroups = [];
336+
}
337+
338+
if (flags.HasFlag(ExportImportFlags.KeepExisting))
339+
{
340+
currentGroups.AddRange(newGroups);
341+
}
342+
else
343+
{
344+
currentGroups.Clear();
345+
currentGroups.AddRange(newGroups);
346+
}
347+
348+
return currentGroups;
349+
}
353350

354351
/// <summary>
355352
/// Imports all or some of the settings/prefs stored in the input stream.
@@ -371,7 +368,7 @@ private Settings Import(Settings currentSettings, FileInfo fileInfo, ExportImpor
371368
newSettings.Preferences = ObjectClone.Clone(importSettings.Preferences);
372369
newSettings.Preferences.columnizerMaskList = ownSettings.Preferences.columnizerMaskList;
373370
newSettings.Preferences.highlightMaskList = ownSettings.Preferences.highlightMaskList;
374-
newSettings.hilightGroupList = ownSettings.hilightGroupList;
371+
newSettings.Preferences.HighlightGroupList = ownSettings.Preferences.HighlightGroupList;
375372
newSettings.Preferences.toolEntries = ownSettings.Preferences.toolEntries;
376373
}
377374
else
@@ -389,7 +386,7 @@ private Settings Import(Settings currentSettings, FileInfo fileInfo, ExportImpor
389386
}
390387
if ((flags & ExportImportFlags.HighlightSettings) == ExportImportFlags.HighlightSettings)
391388
{
392-
newSettings.hilightGroupList = ReplaceOrKeepExisting(flags, ownSettings.hilightGroupList, importSettings.hilightGroupList);
389+
newSettings.Preferences.HighlightGroupList = ReplaceOrKeepExisting(flags, ownSettings.Preferences.HighlightGroupList, importSettings.Preferences.HighlightGroupList);
393390
}
394391
if ((flags & ExportImportFlags.ToolEntries) == ExportImportFlags.ToolEntries)
395392
{
@@ -424,13 +421,7 @@ private void SetBoundsWithinVirtualScreen(Settings settings)
424421

425422
protected void OnConfigChanged(SettingsFlags flags)
426423
{
427-
ConfigChangedEventHandler handler = ConfigChanged;
428-
429-
if (handler != null)
430-
{
431-
_logger.Info("Fire config changed event");
432-
handler(this, new ConfigChangedEventArgs(flags));
433-
}
424+
ConfigChanged?.Invoke(this, new ConfigChangedEventArgs(flags));
434425
}
435426

436427
public delegate void ConfigChangedEventHandler(object sender, ConfigChangedEventArgs e);

src/LogExpert/Controls/LogTabWindow/LogTabWindow.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public LogTabWindow(string[] fileNames, int instanceNumber, bool showInstanceNum
8989
Load += OnLogTabWindowLoad;
9090

9191
ConfigManager.Instance.ConfigChanged += OnConfigChanged;
92-
HilightGroupList = ConfigManager.Settings.hilightGroupList;
92+
HighlightGroupList = ConfigManager.Settings.Preferences.HighlightGroupList;
9393

9494
Rectangle led = new(0, 0, 8, 2);
9595

@@ -297,7 +297,7 @@ public LogWindow.LogWindow CurrentLogWindow
297297

298298
public Preferences Preferences => ConfigManager.Settings.Preferences;
299299

300-
public List<HilightGroup> HilightGroupList { get; private set; } = [];
300+
public List<HighlightGroup> HighlightGroupList { get; private set; } = [];
301301

302302
//public Settings Settings
303303
//{
@@ -312,11 +312,11 @@ public LogWindow.LogWindow CurrentLogWindow
312312

313313
#region Internals
314314

315-
internal HilightGroup FindHighlightGroup(string groupName)
315+
internal HighlightGroup FindHighlightGroup(string groupName)
316316
{
317-
lock (HilightGroupList)
317+
lock (HighlightGroupList)
318318
{
319-
foreach (HilightGroup group in HilightGroupList)
319+
foreach (HighlightGroup group in HighlightGroupList)
320320
{
321321
if (group.GroupName.Equals(groupName))
322322
{

src/LogExpert/Controls/LogTabWindow/LogTabWindowEventHandlers.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,7 @@ private void OnLogWindowFilterListChanged(object sender, FilterListChangedEventA
481481
private void OnLogWindowCurrentHighlightGroupChanged(object sender, CurrentHighlightGroupChangedEventArgs e)
482482
{
483483
OnHighlightSettingsChanged();
484-
ConfigManager.Settings.hilightGroupList = HilightGroupList;
484+
ConfigManager.Settings.Preferences.HighlightGroupList = HighlightGroupList;
485485
ConfigManager.Save(SettingsFlags.HighlightSettings);
486486
}
487487

src/LogExpert/Controls/LogTabWindow/LogTabWindowPrivate.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -341,17 +341,17 @@ private void ShowHighlightSettingsDialog()
341341
KeywordActionList = PluginRegistry.PluginRegistry.Instance.RegisteredKeywordActions,
342342
Owner = this,
343343
TopMost = TopMost,
344-
HighlightGroupList = HilightGroupList,
344+
HighlightGroupList = HighlightGroupList,
345345
PreSelectedGroupName = groupsComboBoxHighlightGroups.Text
346346
};
347347

348348
DialogResult res = dlg.ShowDialog();
349349

350350
if (res == DialogResult.OK)
351351
{
352-
HilightGroupList = dlg.HighlightGroupList;
352+
HighlightGroupList = dlg.HighlightGroupList;
353353
FillHighlightComboBox();
354-
ConfigManager.Settings.hilightGroupList = HilightGroupList;
354+
ConfigManager.Settings.Preferences.HighlightGroupList = HighlightGroupList;
355355
ConfigManager.Save(SettingsFlags.HighlightSettings);
356356
OnHighlightSettingsChanged();
357357
}
@@ -361,7 +361,7 @@ private void FillHighlightComboBox()
361361
{
362362
string currentGroupName = groupsComboBoxHighlightGroups.Text;
363363
groupsComboBoxHighlightGroups.Items.Clear();
364-
foreach (HilightGroup group in HilightGroupList)
364+
foreach (HighlightGroup group in HighlightGroupList)
365365
{
366366
groupsComboBoxHighlightGroups.Items.Add(group.GroupName);
367367
if (group.GroupName.Equals(currentGroupName))
@@ -956,7 +956,7 @@ private void NotifyWindowsForChangedPrefs(SettingsFlags flags)
956956

957957
_bookmarkWindow.PreferencesChanged(ConfigManager.Settings.Preferences, false, flags);
958958

959-
HilightGroupList = ConfigManager.Settings.hilightGroupList;
959+
HighlightGroupList = ConfigManager.Settings.Preferences.HighlightGroupList;
960960
if ((flags & SettingsFlags.HighlightSettings) == SettingsFlags.HighlightSettings)
961961
{
962962
OnHighlightSettingsChanged();

src/LogExpert/Controls/LogTabWindow/LogTabWindowPublic.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ public ILogLineColumnizer FindColumnizerByFileMask(string fileName)
259259
return null;
260260
}
261261

262-
public HilightGroup FindHighlightGroupByFileMask(string fileName)
262+
public HighlightGroup FindHighlightGroupByFileMask(string fileName)
263263
{
264264
foreach (HighlightMaskEntry entry in ConfigManager.Settings.Preferences.highlightMaskList)
265265
{
@@ -269,7 +269,7 @@ public HilightGroup FindHighlightGroupByFileMask(string fileName)
269269
{
270270
if (Regex.IsMatch(fileName, entry.mask))
271271
{
272-
HilightGroup group = FindHighlightGroup(entry.highlightGroupName);
272+
HighlightGroup group = FindHighlightGroup(entry.highlightGroupName);
273273
return group;
274274
}
275275
}
@@ -333,9 +333,9 @@ public void FollowTailChanged(LogWindow.LogWindow logWindow, bool isEnabled, boo
333333
}
334334
}
335335

336-
public void NotifySettingsChanged(object cookie, SettingsFlags flags)
336+
public void NotifySettingsChanged(object sender, SettingsFlags flags)
337337
{
338-
if (cookie != this)
338+
if (sender != this)
339339
{
340340
NotifyWindowsForChangedPrefs(flags);
341341
}

src/LogExpert/Controls/LogWindow/LogWindow.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public partial class LogWindow : DockContent, ILogPaintContext, ILogView
9292
private ILogLineColumnizer _currentColumnizer;
9393

9494
//List<HilightEntry> currentHilightEntryList = new List<HilightEntry>();
95-
private HilightGroup _currentHighlightGroup = new();
95+
private HighlightGroup _currentHighlightGroup = new();
9696

9797
private SearchParams _currentSearchParams;
9898

@@ -131,7 +131,7 @@ public partial class LogWindow : DockContent, ILogPaintContext, ILogView
131131
private bool _shouldCancel;
132132
private bool _shouldTimestampDisplaySyncingCancel;
133133
private bool _showAdvanced;
134-
private List<HilightEntry> _tempHighlightEntryList = [];
134+
private List<HighlightEntry> _tempHighlightEntryList = [];
135135
private int _timeShiftSyncLine = 0;
136136

137137
private bool _waitingForClose;

0 commit comments

Comments
 (0)