|
| 1 | +using Microsoft.Win32; |
| 2 | +using System; |
| 3 | +using System.Activities; |
| 4 | +using System.Activities.Presentation.Model; |
| 5 | +using System.IO; |
| 6 | +using System.Windows; |
| 7 | + |
| 8 | +namespace Utils.Controls |
| 9 | +{ |
| 10 | + /// <summary> |
| 11 | + /// Interaction logic for ExpressionFilePathControl.xaml |
| 12 | + /// </summary> |
| 13 | + public partial class FilePathControl : System.Windows.Controls.UserControl |
| 14 | + { |
| 15 | + public static readonly DependencyProperty ModelItemProperty = DependencyProperty.Register("ModelItem", typeof(ModelItem), typeof(FilePathControl)); |
| 16 | + |
| 17 | + public ModelItem ModelItem |
| 18 | + { |
| 19 | + get { return GetValue(ModelItemProperty) as ModelItem; } |
| 20 | + set { SetValue(ModelItemProperty, value); } |
| 21 | + } |
| 22 | + |
| 23 | + public static readonly DependencyProperty ExpressionProperty = DependencyProperty.Register("Expression", typeof(ModelItem), typeof(FilePathControl)); |
| 24 | + |
| 25 | + public ModelItem Expression |
| 26 | + { |
| 27 | + get { return GetValue(ExpressionProperty) as ModelItem; } |
| 28 | + set { SetValue(ExpressionProperty, value); } |
| 29 | + } |
| 30 | + |
| 31 | + public static readonly DependencyProperty PropertyNameProperty = DependencyProperty.Register("PropertyName", typeof(string), typeof(FilePathControl)); |
| 32 | + |
| 33 | + public string PropertyName |
| 34 | + { |
| 35 | + get { return GetValue(PropertyNameProperty) as string; } |
| 36 | + set { SetValue(PropertyNameProperty, value); } |
| 37 | + } |
| 38 | + |
| 39 | + public static readonly DependencyProperty HintTextProperty = DependencyProperty.Register("HintText", typeof(string), typeof(FilePathControl), new PropertyMetadata("Text must be qouted")); |
| 40 | + public string HintText |
| 41 | + { |
| 42 | + get { return GetValue(HintTextProperty) as string; } |
| 43 | + set { SetValue(HintTextProperty, value); } |
| 44 | + } |
| 45 | + public static readonly DependencyProperty FileNameProperty = DependencyProperty.Register("FileName", typeof(string), typeof(FilePathControl)); |
| 46 | + public string FileName |
| 47 | + { |
| 48 | + get { return GetValue(FileNameProperty) as string; } |
| 49 | + set { SetValue(FileNameProperty, value); } |
| 50 | + } |
| 51 | + public static readonly DependencyProperty FilterProperty = DependencyProperty.Register("Filter", typeof(string), typeof(FilePathControl)); |
| 52 | + public string Filter |
| 53 | + { |
| 54 | + get { return GetValue(FilterProperty) as string; } |
| 55 | + set { SetValue(FilterProperty, value); } |
| 56 | + } |
| 57 | + public static readonly DependencyProperty TitleProperty = DependencyProperty.Register("Title", typeof(string), typeof(FilePathControl)); |
| 58 | + public string Title |
| 59 | + { |
| 60 | + get { return GetValue(TitleProperty) as string; } |
| 61 | + set { SetValue(TitleProperty, value); } |
| 62 | + } |
| 63 | + public static readonly RoutedEvent OpenEvent = EventManager.RegisterRoutedEvent("Open", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(FilePathControl)); |
| 64 | + public event RoutedEventHandler Open |
| 65 | + { |
| 66 | + add { AddHandler(OpenEvent, value); } |
| 67 | + remove { RemoveHandler(OpenEvent, value); } |
| 68 | + } |
| 69 | + public static readonly RoutedEvent FileSelectedEvent = EventManager.RegisterRoutedEvent("FileSelected", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(FilePathControl)); |
| 70 | + public event RoutedEventHandler FileSelected |
| 71 | + { |
| 72 | + add { AddHandler(FileSelectedEvent, value); } |
| 73 | + remove { RemoveHandler(FileSelectedEvent, value); } |
| 74 | + } |
| 75 | + public bool IsSaveDialog { get; set; } |
| 76 | + public bool? ValidateNames { get; set; } |
| 77 | + public bool? CheckFileExists { get; set; } |
| 78 | + public bool? CheckPathExists { get; set; } |
| 79 | + public FilePathControl() |
| 80 | + { |
| 81 | + Title = "Select File"; |
| 82 | + this.Loaded += (s, e) => |
| 83 | + { |
| 84 | + FileNameTextBox.HintText = HintText; |
| 85 | + }; |
| 86 | + InitializeComponent(); |
| 87 | + } |
| 88 | + private void LoadButton_Click(object sender, RoutedEventArgs e) |
| 89 | + { |
| 90 | + RoutedEventArgs args = new RoutedEventArgs(OpenEvent); |
| 91 | + args.Source = LoadButton; |
| 92 | + RaiseEvent(args); |
| 93 | + if (args.Handled) return; |
| 94 | + |
| 95 | + FileDialog fileDialog = null; |
| 96 | + |
| 97 | + if (IsSaveDialog) |
| 98 | + { |
| 99 | + fileDialog = new SaveFileDialog() { OverwritePrompt = false }; |
| 100 | + } |
| 101 | + else |
| 102 | + { |
| 103 | + fileDialog = new OpenFileDialog(); |
| 104 | + } |
| 105 | + |
| 106 | + if (Filter == null) |
| 107 | + { |
| 108 | + Filter = "All files (*.*)|*.*"; |
| 109 | + } |
| 110 | + |
| 111 | + fileDialog.Filter = Filter; |
| 112 | + fileDialog.FileName = FileName; |
| 113 | + fileDialog.Title = Title; |
| 114 | + |
| 115 | + if (ValidateNames != null) |
| 116 | + { |
| 117 | + fileDialog.ValidateNames = ValidateNames.Value; |
| 118 | + } |
| 119 | + |
| 120 | + if (CheckFileExists != null) |
| 121 | + { |
| 122 | + fileDialog.CheckFileExists = CheckFileExists.Value; |
| 123 | + } |
| 124 | + |
| 125 | + if (CheckPathExists != null) |
| 126 | + { |
| 127 | + fileDialog.CheckPathExists = CheckPathExists.Value; |
| 128 | + } |
| 129 | + |
| 130 | + var workspacePath = Directory.GetCurrentDirectory(); |
| 131 | + fileDialog.InitialDirectory = workspacePath; |
| 132 | + |
| 133 | + var result = fileDialog.ShowDialog(); |
| 134 | + if (result.HasValue && result.Value == true) |
| 135 | + { |
| 136 | + var workflowRelativePath = GetRelativePath(workspacePath, fileDialog.FileName); |
| 137 | + |
| 138 | + RoutedEventArgs fileArgs = new FileSelectedRoutedEventArgs(FileSelectedEvent, workflowRelativePath); |
| 139 | + fileArgs.Source = LoadButton; |
| 140 | + RaiseEvent(fileArgs); |
| 141 | + |
| 142 | + if (!fileArgs.Handled && PropertyName != null) |
| 143 | + { |
| 144 | + ModelItem.Properties[PropertyName].SetValue(new InArgument<string>(workflowRelativePath)); |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + public static string GetRelativePath(string basePath, string path) |
| 150 | + { |
| 151 | + if (string.IsNullOrWhiteSpace(basePath)) return path; |
| 152 | + |
| 153 | + var workflowRelativePath = path; |
| 154 | + try |
| 155 | + { |
| 156 | + var workspaceUri = new Uri(Path.Combine(basePath, Path.PathSeparator.ToString())); |
| 157 | + var workflowUri = new Uri(path); |
| 158 | + |
| 159 | + // the workflow is in the current workspace |
| 160 | + if (workspaceUri.IsBaseOf(workflowUri)) |
| 161 | + { |
| 162 | + workflowRelativePath = Uri.UnescapeDataString(workspaceUri.MakeRelativeUri(workflowUri).OriginalString).Replace('/', Path.DirectorySeparatorChar); |
| 163 | + } |
| 164 | + } |
| 165 | + catch { } |
| 166 | + |
| 167 | + return workflowRelativePath; |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + public class FileSelectedRoutedEventArgs : RoutedEventArgs |
| 172 | + { |
| 173 | + public string Path { get; internal set; } |
| 174 | + |
| 175 | + public FileSelectedRoutedEventArgs(RoutedEvent routedEvent, string path) |
| 176 | + : base(routedEvent) |
| 177 | + { |
| 178 | + this.Path = path; |
| 179 | + } |
| 180 | + } |
| 181 | +} |
0 commit comments