2 using System.Collections.Generic;
4 using System.Reflection;
5 using System.Windows.Forms;
6 using DirectOutput.GlobalConfiguration;
8 namespace DirectOutput.Frontend
12 private bool _Modified =
false;
14 protected bool Modified
16 get {
return _Modified; }
17 set { _Modified = value;
18 this.Text=
"Global configuration editor {0}".Build(_Modified?
" <contains unsaved changes>":
"");
26 public bool SaveConfigData()
31 if (!ValidateConfigData())
33 MessageBox.Show(
this,
"The data specified for the Global Configuration contains some invalid data.\nPlease correct the data before saving.",
"Invalid global configuration data", MessageBoxButtons.OK, MessageBoxIcon.Warning);
39 SaveGlobalConfigDialog.InitialDirectory =
new FileInfo(Assembly.GetExecutingAssembly().Location).Directory.FullName;
40 if (SaveGlobalConfigDialog.ShowDialog(
this) != DialogResult.OK)
52 MessageBox.Show(
this,
"Global config saved to\n{0}.\n\nYou must restart the framework to activate the new global config settings.".Build(
GlobalConfig.
GlobalConfigFilename),
"Global configuration saved", MessageBoxButtons.OK, MessageBoxIcon.Information);
58 MessageBox.Show(
this,
"Could not save global configuration.\nA exception occured:\n{0}".Build(E.Message),
"Global config save error", MessageBoxButtons.OK, MessageBoxIcon.Error);
59 Log.
Exception(
"Could not save global configuration.\nA exception occured", E);
65 #region Validate config data
66 public bool ValidateConfigData()
68 return ValidateLedcontrolFiles()
69 && ValidateFilePatterns(GlobalScriptFilePatterns)
70 && ValidateFilePatterns(CabinetConfigFilePatterns)
72 && ValidateFilePatterns(TableScriptFilePatterns)
73 && ValidateFilePatterns(TableConfigFilePatterns)
74 && ValidateLogFilePattern();
77 private bool ValidateLogFilePattern()
79 if (!LogFilePattern.Text.IsNullOrWhiteSpace())
89 private bool ValidateLedcontrolFiles()
91 return UpdateLedcontrolFileStatus();
94 private bool ValidateFilePatterns(DataGridView Source)
96 for (
int r = 0; r < Source.Rows.Count; r++)
98 if (!
new FilePattern((
string)Source[0, r].Value).IsValid)
109 #region Update global config data
114 if (ValidateConfigData())
116 UpdateLedcontrolFiles(Config);
140 for (
int r = 0; r < LedcontrolFiles.Rows.Count; r++)
147 private void SaveFilePatterns(
FilePatternList Patterns, DataGridView Source)
151 for (
int r = 0; r < Source.Rows.Count; r++)
153 Patterns.Add(
new FilePattern((
string)Source[0, r].Value));
163 #region Config data loading
168 LoadLedcontrolFiles(Config);
186 private void LoadFilePatterns(
FilePatternList Patterns, DataGridView Destination)
190 int RowIndex = Destination.Rows.Add();
191 Destination[0, RowIndex].Value = FP.
Pattern;
192 Destination[1, RowIndex].Value = (FP.
IsValid ?
"OK" :
"Invalid file pattern");
196 Destination.ClearSelection();
197 Destination.Refresh();
205 int RowIndex = LedcontrolFiles.Rows.Add();
207 LedcontrolFiles[0, RowIndex].Value = LCF.
LedWizNumber.ToString();
208 LedcontrolFiles[1, RowIndex].Value = LCF.
Filename;
209 LedcontrolFiles[2, RowIndex].Value = LCF.
Status;
212 LedcontrolFiles.ClearSelection();
213 LedcontrolFiles.Refresh();
229 InitializeComponent();
231 LoadConfigData(Config);
237 private string LastDirectory =
"";
238 private void LedcontrolFiles_CellDoubleClick(
object sender, DataGridViewCellEventArgs e)
240 if (e.ColumnIndex == 1 && e.RowIndex.IsBetween(0, LedcontrolFiles.Rows.Count - 1))
242 if (!LastDirectory.IsNullOrWhiteSpace())
244 SelectLedcontrolFile.InitialDirectory = LastDirectory;
248 SelectLedcontrolFile.InitialDirectory = Assembly.GetExecutingAssembly().Location;
250 if (SelectLedcontrolFile.ShowDialog(
this) == DialogResult.OK)
252 FileInfo F =
new FileInfo(SelectLedcontrolFile.FileName);
253 LastDirectory = F.Directory.FullName;
254 if (!LedcontrolFiles.IsCurrentCellInEditMode)
256 LedcontrolFiles.BeginEdit(
true);
259 LedcontrolFiles.EditingControl.Text = F.FullName;
260 LedcontrolFiles.CommitEdit(DataGridViewDataErrorContexts.Commit);
261 LedcontrolFiles.Refresh();
268 private void LedcontrolFiles_DefaultValuesNeeded(
object sender, DataGridViewRowEventArgs e)
273 List<int> NrList =
new List<int>();
275 for (
int i = 0; i < LedcontrolFiles.Rows.Count; i++)
277 if (Nr < Convert.ToInt32((
string)LedcontrolFiles.Rows[i].Cells[0].Value))
279 Nr = Convert.ToInt32((
string)LedcontrolFiles.Rows[i].Cells[0].Value);
281 NrList.Add(Convert.ToInt32((
string)LedcontrolFiles.Rows[i].Cells[0].Value));
284 if (!Nr.IsBetween(1, 16))
287 for (
int i = 1; i < 17; i++)
289 if (!NrList.Contains(i))
297 e.Row.Cells[0].Value = Nr.ToString();
298 e.Row.Cells[1].Value =
"";
302 private void LedcontrolFiles_UserAddedRow(
object sender, DataGridViewRowEventArgs e)
304 if (LedcontrolFiles.Rows.Count > 16)
306 LedcontrolFiles.AllowUserToAddRows =
false;
308 while (LedcontrolFiles.Rows.Count > 17)
310 LedcontrolFiles.Rows.RemoveAt(LedcontrolFiles.Rows.Count - 1);
311 LedcontrolFiles.Refresh();
317 LedcontrolFiles.AllowUserToAddRows =
true;
321 private void LedcontrolFiles_UserDeletedRow(
object sender, DataGridViewRowEventArgs e)
323 LedcontrolFiles.AllowUserToAddRows = !(LedcontrolFiles.Rows.Count > 16);
326 private void LedcontrolFiles_CellValueChanged(
object sender, DataGridViewCellEventArgs e)
328 UpdateLedcontrolFileStatus();
333 private void LedcontrolFiles_UserDeletingRow(
object sender, DataGridViewRowCancelEventArgs e)
335 if (MessageBox.Show(
"Do you really want to delete this row?",
"Delete row", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.No)
342 private void UpdateFilePatternStatus(DataGridView DataGridView,
int RowIndex)
344 if (RowIndex.IsBetween(0, DataGridView.Rows.Count - 1))
347 DataGridView[1, RowIndex].Value = (F.
IsValid ?
"OK" :
"Invalid file pattern");
351 private void TableConfigFilePatterns_CellValueChanged(
object sender, DataGridViewCellEventArgs e)
353 UpdateFilePatternStatus((DataGridView)sender, e.RowIndex);
357 private void TableScriptFilePatterns_CellValueChanged(
object sender, DataGridViewCellEventArgs e)
359 UpdateFilePatternStatus((DataGridView)sender, e.RowIndex);
363 private void CabinetConfigFilePatterns_CellValueChanged(
object sender, DataGridViewCellEventArgs e)
365 UpdateFilePatternStatus((DataGridView)sender, e.RowIndex);
369 private void CabinetScriptFilePatterns_CellValueChanged(
object sender, DataGridViewCellEventArgs e)
371 UpdateFilePatternStatus((DataGridView)sender, e.RowIndex);
376 private bool UpdateLedcontrolFileStatus()
379 bool AllEntriesValid =
true;
380 Dictionary<int, int> N =
new Dictionary<int, int>();
381 foreach (DataGridViewRow R
in LedcontrolFiles.Rows)
383 int Nr = Convert.ToInt32(R.Cells[0].Value);
384 if (N.ContainsKey(Nr))
394 foreach (DataGridViewRow R
in LedcontrolFiles.Rows)
397 string F = (string)R.Cells[1].Value;
398 if (F.IsNullOrWhiteSpace())
400 S =
"No filename set";
401 AllEntriesValid =
false;
407 if (F.IndexOfAny(Path.GetInvalidPathChars()) == -1)
410 FileInfo FI =
new FileInfo(F);
413 if (N[Convert.ToInt32(R.Cells[0].Value)] > 1)
415 S =
"Duplicate Ledwiz number";
416 AllEntriesValid =
false;
425 S =
"File does not exist";
430 S =
"Invalid filename";
431 AllEntriesValid =
false;
436 S =
"Invalid filename";
438 AllEntriesValid =
false;
440 R.Cells[2].Value = S;
442 return AllEntriesValid;
445 private void LogFilePattern_TextChanged(
object sender, EventArgs e)
447 LogFilePatternStatus.Text = (
new FilePattern(LogFilePattern.Text).
IsValid ?
"OK" :
"Invalid file pattern");
451 string LastLogDirectory =
"";
452 private void ShowLogFileDialog_Click(
object sender, EventArgs e)
454 if (LastLogDirectory.IsNullOrWhiteSpace())
456 SelectLogFile.InitialDirectory = Assembly.GetExecutingAssembly().Location;
460 SelectLogFile.InitialDirectory = LastLogDirectory;
462 if (SelectLogFile.ShowDialog(
this) == DialogResult.OK)
464 LogFilePattern.Text = SelectLogFile.FileName;
465 LastLogDirectory =
new FileInfo(SelectLogFile.FileName).Directory.FullName;
466 LogFilePatternStatus.Text = (
new FilePattern(LogFilePattern.Text).
IsValid ?
"OK" :
"Invalid file pattern");
471 private void UpdateTimerIntervalMs_ValueChanged(
object sender, EventArgs e)
476 private void EnableLogging_CheckedChanged(
object sender, EventArgs e)
481 private void GlobalScriptFilePatterns_CellValueChanged(
object sender, DataGridViewCellEventArgs e)
483 UpdateFilePatternStatus((DataGridView)sender, e.RowIndex);
487 private void SaveButton_Click(
object sender, EventArgs e)
492 private void SaveExitButton_Click(
object sender, EventArgs e)
494 if (SaveConfigData())
500 private void ExitButton_Click(
object sender, EventArgs e)
509 private void GlobalConfigEdit_FormClosing(
object sender, FormClosingEventArgs e)
513 switch (MessageBox.Show(
this,
"The global config editor contains unsaved changes.\nDo you want to save before closing the window?",
"Close global config editor", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button3))
515 case DialogResult.Cancel:
518 case DialogResult.No:
520 case DialogResult.Yes:
521 if (!SaveConfigData())