OptionalExecutablePanel.xaml.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. private Button applyButton;
  29. public OptionalExecutablePanel(Setting setting, IEnumerable<string> options, Button applyButton)
  30. {
  31. InitializeComponent();
  32. this.applyButton = applyButton;
  33. this.setting = setting;
  34. if (setting.handler)
  35. SettingUsage.Text = "Use a specific executable for " + setting.name + ":";
  36. else
  37. {
  38. SettingCheckBox.Content = setting.name + " Executable:";
  39. SettingUsage.Text = setting.usage + ":";
  40. }
  41. Regex regex = new Regex(@"^(?:/|--?)" + setting.option.Substring(1) + @"(?:[:=](?<executable>.*))?$");
  42. foreach (string option in options)
  43. {
  44. Match match = regex.Match(option);
  45. if (match.Success)
  46. {
  47. Group group = match.Groups["executable"];
  48. if (group.Success)
  49. {
  50. SettingCheckBox.IsChecked = true;
  51. SettingExecutableBox.Text = group.Value;
  52. }
  53. }
  54. }
  55. }
  56. public bool IsSelected
  57. {
  58. get
  59. {
  60. return setting.handler || SettingCheckBox.IsChecked.Value;
  61. }
  62. }
  63. public string Option
  64. {
  65. get
  66. {
  67. if (SettingCheckBox.IsChecked.Value)
  68. {
  69. string executable = SettingExecutableBox.Text;
  70. if (File.GetAttributes(executable).HasFlag(FileAttributes.Directory))
  71. throw new Exception("'" + executable + "' is not a file.");
  72. return setting.option + ":" + executable;
  73. }
  74. else
  75. return setting.option;
  76. }
  77. }
  78. private void SettingCheckBox_Checked(object sender, System.Windows.RoutedEventArgs e)
  79. {
  80. SettingExecutablePanel.IsEnabled = true;
  81. applyButton.IsEnabled = true;
  82. }
  83. private void SettingCheckBox_Unchecked(object sender, System.Windows.RoutedEventArgs e)
  84. {
  85. SettingExecutablePanel.IsEnabled = false;
  86. applyButton.IsEnabled = true;
  87. }
  88. private void SettingExecutableBrowse_Click(object sender, System.Windows.RoutedEventArgs e)
  89. {
  90. SettingPanelHelper.DoExecutableBrowseClick(SettingExecutableBox);
  91. }
  92. private void SettingExecutableBox_Populating(object sender, PopulatingEventArgs e)
  93. {
  94. SettingPanelHelper.DoExecutableBoxPopulating(SettingExecutableBox);
  95. }
  96. private void SettingExecutableBox_TextChanged(object sender, System.Windows.RoutedEventArgs e)
  97. {
  98. applyButton.IsEnabled = true;
  99. }
  100. }