OptionalYesNoDirectoryPanel.xaml.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // Optional Yes/No/Directory Panel
  2. //
  3. // Douglas Thrift
  4. //
  5. // OptionalYesNoDirectoryPanel.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 OptionalYesNoDirectoryPanel : StackPanel, SettingPanel
  26. {
  27. private Setting setting;
  28. public OptionalYesNoDirectoryPanel(Setting setting, IEnumerable<string> options)
  29. {
  30. InitializeComponent();
  31. this.setting = setting;
  32. SettingCheckBox.Content = setting.name + ":";
  33. SettingUsage.Text = setting.usage + ":";
  34. Regex regex = new Regex(@"^(?:/|--?)" + setting.option.Substring(1) + @"(?:[:=](?<directory>.*))?$");
  35. foreach (string option in options)
  36. {
  37. Match match = regex.Match(option);
  38. if (match.Success)
  39. {
  40. Group group = match.Groups["directory"];
  41. if (group.Success)
  42. switch (group.Value)
  43. {
  44. case "yes":
  45. SettingYes.IsChecked = true;
  46. break;
  47. case "no":
  48. SettingNo.IsChecked = true;
  49. break;
  50. default:
  51. SettingDirectory.IsChecked = true;
  52. SettingDirectoryBox.Text = group.Value;
  53. break;
  54. }
  55. SettingCheckBox.IsChecked = true;
  56. }
  57. }
  58. }
  59. public bool IsSelected
  60. {
  61. get
  62. {
  63. return SettingCheckBox.IsChecked.Value;
  64. }
  65. }
  66. public string Option
  67. {
  68. get
  69. {
  70. if (SettingYes.IsChecked.Value)
  71. return setting.option + ":yes";
  72. else if (SettingNo.IsChecked.Value)
  73. return setting.option + ":no";
  74. else
  75. {
  76. string directory = SettingDirectoryBox.Text;
  77. if (!File.GetAttributes(directory).HasFlag(FileAttributes.Directory))
  78. throw new Exception("'" + directory + "' is not a directory.");
  79. return setting.option + ":" + directory;
  80. }
  81. }
  82. }
  83. private void SettingCheckBox_Checked(object sender, System.Windows.RoutedEventArgs e)
  84. {
  85. SettingRadioPanel.IsEnabled = true;
  86. }
  87. private void SettingCheckBox_Unchecked(object sender, System.Windows.RoutedEventArgs e)
  88. {
  89. SettingRadioPanel.IsEnabled = false;
  90. }
  91. private void SettingDirectory_Checked(object sender, System.Windows.RoutedEventArgs e)
  92. {
  93. SettingDirectoryPanel.IsEnabled = true;
  94. }
  95. private void SettingDirectory_Unchecked(object sender, System.Windows.RoutedEventArgs e)
  96. {
  97. SettingDirectoryPanel.IsEnabled = false;
  98. }
  99. private void SettingDirectoryBrowse_Click(object sender, System.Windows.RoutedEventArgs e)
  100. {
  101. SettingPanelHelper.DoDirectoryBrowseClick(SettingDirectoryBox);
  102. }
  103. private void SettingDirectoryBox_Populating(object sender, PopulatingEventArgs e)
  104. {
  105. SettingPanelHelper.DoDirectoryBoxPopulating(SettingDirectoryBox);
  106. }
  107. }