Browse Source

Implement browse dialogs and autocomplete boxes!

Douglas Thrift 9 years ago
parent
commit
dcfbdc385f

+ 2 - 0
ssh-handler/OptionalExecutablePanel.xaml

@@ -27,10 +27,12 @@
             >
             <toolkit:AutoCompleteBox
                 Name="SettingExecutableBox"
+                Populating="SettingExecutableBox_Populating"
                 />
             <Button
                 Name="SettingExecutableBrowse"
                 Content="Browse..."
+                Click="SettingExecutableBrowse_Click"
                 />
         </StackPanel>
     </StackPanel>

+ 10 - 0
ssh-handler/OptionalExecutablePanel.xaml.cs

@@ -95,4 +95,14 @@ public partial class OptionalExecutablePanel : StackPanel, SettingPanel
     {
         SettingExecutablePanel.IsEnabled = false;
     }
+
+    private void SettingExecutableBrowse_Click(object sender, System.Windows.RoutedEventArgs e)
+    {
+        SettingPanelHelper.DoExecutableBrowseClick(SettingExecutableBox);
+    }
+
+    private void SettingExecutableBox_Populating(object sender, PopulatingEventArgs e)
+    {
+        SettingPanelHelper.DoExecutableBoxPopulating(SettingExecutableBox);
+    }
 }

+ 2 - 0
ssh-handler/OptionalYesNoDirectoryPanel.xaml

@@ -47,10 +47,12 @@
                 >
                 <toolkit:AutoCompleteBox
                     Name="SettingDirectoryBox"
+                    Populating="SettingDirectoryBox_Populating"
                     />
                 <Button
                     Name="SettingDirectoryBrowse"
                     Content="Browse..."
+                    Click="SettingDirectoryBrowse_Click"
                     />
             </StackPanel>
         </StackPanel>

+ 10 - 0
ssh-handler/OptionalYesNoDirectoryPanel.xaml.cs

@@ -112,4 +112,14 @@ public partial class OptionalYesNoDirectoryPanel : StackPanel, SettingPanel
     {
         SettingDirectoryPanel.IsEnabled = false;
     }
+
+    private void SettingDirectoryBrowse_Click(object sender, System.Windows.RoutedEventArgs e)
+    {
+        SettingPanelHelper.DoDirectoryBrowseClick(SettingDirectoryBox);
+    }
+
+    private void SettingDirectoryBox_Populating(object sender, PopulatingEventArgs e)
+    {
+        SettingPanelHelper.DoDirectoryBoxPopulating(SettingDirectoryBox);
+    }
 }

+ 2 - 0
ssh-handler/OptionalYesNoExecutablePanel.xaml

@@ -47,10 +47,12 @@
                 >
                 <toolkit:AutoCompleteBox
                     Name="SettingExecutableBox"
+                    Populating="SettingExecutableBox_Populating"
                     />
                 <Button
                     Name="SettingExecutableBrowse"
                     Content="Browse..."
+                    Click="SettingExecutableBrowse_Click"
                     />
             </StackPanel>
         </StackPanel>

+ 10 - 0
ssh-handler/OptionalYesNoExecutablePanel.xaml.cs

@@ -112,4 +112,14 @@ public partial class OptionalYesNoExecutablePanel : StackPanel, SettingPanel
     {
         SettingExecutablePanel.IsEnabled = false;
     }
+
+    private void SettingExecutableBrowse_Click(object sender, System.Windows.RoutedEventArgs e)
+    {
+        SettingPanelHelper.DoExecutableBrowseClick(SettingExecutableBox);
+    }
+
+    private void SettingExecutableBox_Populating(object sender, PopulatingEventArgs e)
+    {
+        SettingPanelHelper.DoExecutableBoxPopulating(SettingExecutableBox);
+    }
 }

+ 119 - 0
ssh-handler/SettingPanelHelper.cs

@@ -0,0 +1,119 @@
+// Setting Panel Helper
+//
+// Douglas Thrift
+//
+// SettingPanelHelper.cs
+
+/*  Copyright 2014 Douglas Thrift
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+
+using System;
+using System.Diagnostics;
+using System.IO;
+using System.Linq;
+using System.Security;
+using System.Windows.Controls;
+
+public static class SettingPanelHelper
+{
+    public static void DoExecutableBrowseClick(AutoCompleteBox executableBox)
+    {
+        var dialog = new System.Windows.Forms.OpenFileDialog();
+
+        dialog.AutoUpgradeEnabled = true;
+
+        string pathext = Environment.GetEnvironmentVariable("PathExt");
+
+        if (pathext == null)
+            pathext = ".com;.exe;.bat;.cmd";
+
+        pathext = string.Join(";", pathext.Split(';').Select(extension => "*" + extension.ToLowerInvariant()));
+
+        dialog.Filter = string.Format("Executable files ({0})|{0}|All files (*.*)|*.*", pathext);
+        dialog.FilterIndex = 1;
+        dialog.RestoreDirectory = true;
+
+        if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
+            executableBox.Text = dialog.FileName;
+    }
+
+    public static void DoExecutableBoxPopulating(AutoCompleteBox executableBox)
+    {
+        try
+        {
+            string directoryName = Path.GetDirectoryName(executableBox.Text);
+            if (directoryName == null)
+                directoryName = Path.GetPathRoot(executableBox.Text);
+
+            if (Directory.Exists(directoryName))
+            {
+                var directoryInfo = new DirectoryInfo(directoryName.EndsWith(":") ? directoryName + @"\" : directoryName);
+                executableBox.ItemsSource = directoryInfo.EnumerateFileSystemInfos().Where(info => !info.Attributes.HasFlag(FileAttributes.Hidden)).Select(info => info.FullName);
+                executableBox.PopulateComplete();
+            }
+        }
+        catch (ArgumentException exception)
+        {
+            Debug.WriteLine("{0}: {1}", exception, exception.Message);
+        }
+        catch (PathTooLongException exception)
+        {
+            Debug.WriteLine("{0}: {1}", exception, exception.Message);
+        }
+        catch (SecurityException exception)
+        {
+            Debug.WriteLine("{0}: {1}", exception, exception.Message);
+        }
+    }
+
+    public static void DoDirectoryBrowseClick(AutoCompleteBox directoryBox)
+    {
+        var dialog = new System.Windows.Forms.FolderBrowserDialog();
+
+        dialog.ShowNewFolderButton = false;
+
+        if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
+            directoryBox.Text = dialog.SelectedPath;
+    }
+
+    public static void DoDirectoryBoxPopulating(AutoCompleteBox directoryBox)
+    {
+        try
+        {
+            string directoryName = Path.GetDirectoryName(directoryBox.Text);
+            if (directoryName == null)
+                directoryName = Path.GetPathRoot(directoryBox.Text);
+
+            if (Directory.Exists(directoryName))
+            {
+                var directoryInfo = new DirectoryInfo(directoryName.EndsWith(":") ? directoryName + @"\" : directoryName);
+                directoryBox.ItemsSource = directoryInfo.EnumerateDirectories().Where(info => !info.Attributes.HasFlag(FileAttributes.Hidden)).Select(info => info.FullName);
+                directoryBox.PopulateComplete();
+            }
+        }
+        catch (ArgumentException exception)
+        {
+            Debug.WriteLine("{0}: {1}", exception, exception.Message);
+        }
+        catch (PathTooLongException exception)
+        {
+            Debug.WriteLine("{0}: {1}", exception, exception.Message);
+        }
+        catch (SecurityException exception)
+        {
+            Debug.WriteLine("{0}: {1}", exception, exception.Message);
+        }
+    }
+}

+ 1 - 0
ssh-handler/ssh-handler.csproj

@@ -94,6 +94,7 @@
     <Compile Include="PuttyHandler.cs" />
     <Compile Include="Setting.cs" />
     <Compile Include="SettingPanel.cs" />
+    <Compile Include="SettingPanelHelper.cs" />
     <Compile Include="SettingType.cs" />
     <Compile Include="Shell32.cs" />
     <Compile Include="SshHandler.cs" />