OptionalYesNoExecutablePanel.xaml.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // Optional Yes/No/Executable Panel
  2. //
  3. // Douglas Thrift
  4. //
  5. // OptionalYesNoExecutablePanel.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 OptionalYesNoExecutablePanel : StackPanel, SettingPanel
  26. {
  27. private Setting setting;
  28. private Button applyButton;
  29. public OptionalYesNoExecutablePanel(Setting setting, IEnumerable<string> options, Button applyButton)
  30. {
  31. InitializeComponent();
  32. this.applyButton = applyButton;
  33. this.setting = setting;
  34. SettingCheckBox.Content = setting.name + ":";
  35. SettingUsage.Text = setting.usage + ":";
  36. Regex regex = new Regex(@"^(?:/|--?)" + setting.option.Substring(1) + @"(?:[:=](?<executable>.*))?$");
  37. foreach (string option in options)
  38. {
  39. Match match = regex.Match(option);
  40. if (match.Success)
  41. {
  42. Group group = match.Groups["executable"];
  43. if (group.Success)
  44. switch (group.Value)
  45. {
  46. case "yes":
  47. SettingYes.IsChecked = true;
  48. break;
  49. case "no":
  50. SettingNo.IsChecked = true;
  51. break;
  52. default:
  53. SettingExecutable.IsChecked = true;
  54. SettingExecutableBox.Text = group.Value;
  55. break;
  56. }
  57. SettingCheckBox.IsChecked = true;
  58. }
  59. }
  60. }
  61. public bool IsSelected
  62. {
  63. get
  64. {
  65. return SettingCheckBox.IsChecked.Value;
  66. }
  67. }
  68. public string Option
  69. {
  70. get
  71. {
  72. if (SettingYes.IsChecked.Value)
  73. return setting.option + ":yes";
  74. else if (SettingNo.IsChecked.Value)
  75. return setting.option + ":no";
  76. else
  77. {
  78. string executable = SettingExecutableBox.Text;
  79. if (File.GetAttributes(executable).HasFlag(FileAttributes.Directory))
  80. throw new Exception("'" + executable + "' is not a file.");
  81. return setting.option + ":" + executable;
  82. }
  83. }
  84. }
  85. private void SettingCheckBox_Checked(object sender, System.Windows.RoutedEventArgs e)
  86. {
  87. SettingRadioPanel.IsEnabled = true;
  88. applyButton.IsEnabled = true;
  89. }
  90. private void SettingCheckBox_Unchecked(object sender, System.Windows.RoutedEventArgs e)
  91. {
  92. SettingRadioPanel.IsEnabled = false;
  93. applyButton.IsEnabled = true;
  94. }
  95. private void SettingYes_Unchecked(object sender, System.Windows.RoutedEventArgs e)
  96. {
  97. applyButton.IsEnabled = true;
  98. }
  99. private void SettingNo_Unchecked(object sender, System.Windows.RoutedEventArgs e)
  100. {
  101. applyButton.IsEnabled = true;
  102. }
  103. private void SettingExecutable_Checked(object sender, System.Windows.RoutedEventArgs e)
  104. {
  105. SettingExecutablePanel.IsEnabled = true;
  106. }
  107. private void SettingExecutable_Unchecked(object sender, System.Windows.RoutedEventArgs e)
  108. {
  109. SettingExecutablePanel.IsEnabled = false;
  110. applyButton.IsEnabled = true;
  111. }
  112. private void SettingExecutableBrowse_Click(object sender, System.Windows.RoutedEventArgs e)
  113. {
  114. SettingPanelHelper.DoExecutableBrowseClick(SettingExecutableBox);
  115. }
  116. private void SettingExecutableBox_Populating(object sender, PopulatingEventArgs e)
  117. {
  118. SettingPanelHelper.DoExecutableBoxPopulating(SettingExecutableBox);
  119. }
  120. private void SettingExecutableBox_TextChanged(object sender, System.Windows.RoutedEventArgs e)
  121. {
  122. applyButton.IsEnabled = true;
  123. }
  124. }