OptionalExecutablePanel.xaml.cs 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // Optional Executable Panel
  2. //
  3. // Douglas Thrift
  4. //
  5. // OptionalExecutablePanel.xaml.cs
  6. /* Copyright 2014 Douglas Thrift
  7. *
  8. * Licensed under the Apache License, Version 2.0 (the "License");
  9. * you may not use this file except in compliance with the License.
  10. * You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS,
  16. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. */
  20. using System;
  21. using System.Collections.Generic;
  22. using System.IO;
  23. using System.Text.RegularExpressions;
  24. using System.Windows.Controls;
  25. public partial class OptionalExecutablePanel : StackPanel, SettingPanel
  26. {
  27. private Setting setting;
  28. public OptionalExecutablePanel(Setting setting, IEnumerable<string> options)
  29. {
  30. InitializeComponent();
  31. this.setting = setting;
  32. if (setting.handler)
  33. SettingUsage.Text = "Use a specific executable for " + setting.name + ":";
  34. else
  35. {
  36. SettingCheckBox.Content = setting.name + " Executable:";
  37. SettingUsage.Text = setting.usage + ":";
  38. }
  39. Regex regex = new Regex(@"^(?:/|--?)" + setting.option.Substring(1) + @"(?:[:=](?<executable>.*))?$");
  40. foreach (string option in options)
  41. {
  42. Match match = regex.Match(option);
  43. if (match.Success)
  44. {
  45. Group group = match.Groups["executable"];
  46. if (group.Success)
  47. {
  48. SettingCheckBox.IsChecked = true;
  49. SettingExecutableBox.Text = group.Value;
  50. }
  51. }
  52. }
  53. }
  54. public bool IsSelected
  55. {
  56. get
  57. {
  58. return setting.handler || SettingCheckBox.IsChecked.Value;
  59. }
  60. }
  61. public string Option
  62. {
  63. get
  64. {
  65. if (SettingCheckBox.IsChecked.Value)
  66. {
  67. string executable = SettingExecutableBox.Text;
  68. if (File.GetAttributes(executable).HasFlag(FileAttributes.Directory))
  69. throw new Exception("'" + executable + "' is not a file.");
  70. return setting.option + ":" + executable;
  71. }
  72. else
  73. return setting.option;
  74. }
  75. }
  76. private void SettingCheckBox_Checked(object sender, System.Windows.RoutedEventArgs e)
  77. {
  78. SettingExecutablePanel.IsEnabled = true;
  79. }
  80. private void SettingCheckBox_Unchecked(object sender, System.Windows.RoutedEventArgs e)
  81. {
  82. SettingExecutablePanel.IsEnabled = false;
  83. }
  84. }