From 89fb7da24d43fd0ddfdc0351eac9cbfdb9a725a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wies=C5=82aw=20=C5=A0olt=C3=A9s?= Date: Fri, 15 Jun 2018 20:12:47 +0200 Subject: [PATCH] Refactored main window --- src/MicroStationTagExplorer/MainWindow.xaml | 9 +- .../MainWindow.xaml.cs | 164 ++++++++++-------- 2 files changed, 94 insertions(+), 79 deletions(-) diff --git a/src/MicroStationTagExplorer/MainWindow.xaml b/src/MicroStationTagExplorer/MainWindow.xaml index 0efbb17..d1fd872 100644 --- a/src/MicroStationTagExplorer/MainWindow.xaml +++ b/src/MicroStationTagExplorer/MainWindow.xaml @@ -21,12 +21,13 @@ + + + - + - - - + GetFiles() + { + IList files = null; + + if (DataGridFiles.DataContext != null) + { + files = DataGridFiles.DataContext as IList; + } + else + { + files = new ObservableCollection(); + DataGridFiles.DataContext = files; + } + + return files; + } + private void AddFiles() { var dlg = new OpenFileDialog @@ -32,16 +49,7 @@ namespace MicroStationTagExplorer var result = dlg.ShowDialog(this); if (result == true) { - IList files = null; - if (DataGridFiles.DataContext != null) - { - files = DataGridFiles.DataContext as IList; - } - else - { - files = new ObservableCollection(); - DataGridFiles.DataContext = files; - } + IList files = GetFiles(); foreach (var fileName in dlg.FileNames) { @@ -54,24 +62,54 @@ namespace MicroStationTagExplorer } } + private void AddPaths(string[] paths) + { + IList files = GetFiles(); + + foreach (var path in paths) + { + System.IO.FileAttributes attributes = System.IO.File.GetAttributes(path); + if (attributes.HasFlag(System.IO.FileAttributes.Directory)) + { + var fileNames = System.IO.Directory.EnumerateFiles(path, "*.*", System.IO.SearchOption.AllDirectories) + .Where(s => s.EndsWith(".dwg") || s.EndsWith(".dgn")); ; + foreach (var fileName in fileNames) + { + var file = new File() + { + Path = fileName + }; + files.Add(file); + } + } + else + { + if (path.EndsWith(".dwg") || path.EndsWith(".dgn")) + { + var file = new File() + { + Path = path + }; + files.Add(file); + } + } + } + } + private void GetTags() { - if (DataGridFiles.DataContext == null) - return; - if (IsRunning == false) { - int selectedIndex = DataGridFiles.SelectedIndex; - IList files = DataGridFiles.DataContext as IList; - if (files == null) - return; - IsRunning = true; - FileGetTags.Header = "S_top"; - TextBoxStatus.Text = ""; + + DisableWindow(); + UpdateStatus(""); + TokenSource = new CancellationTokenSource(); Token = TokenSource.Token; + IList files = GetFiles(); + Task.Factory.StartNew(() => { try @@ -90,7 +128,7 @@ namespace MicroStationTagExplorer Dispatcher.Invoke(() => { - TextBoxStatus.Text = "[" + i + "/" + count + "] " + System.IO.Path.GetFileName(file.Path); + UpdateStatus("[" + i + "/" + count + "] " + System.IO.Path.GetFileName(file.Path)); }); using (var microstation = new MicrostationInterop(file.Path)) @@ -109,8 +147,8 @@ namespace MicroStationTagExplorer Dispatcher.Invoke(() => { - FileGetTags.Header = "_Get Tags"; - TextBoxStatus.Text = ""; + EnableWindow(); + UpdateStatus(""); }); TokenSource.Dispose(); @@ -121,11 +159,32 @@ namespace MicroStationTagExplorer { TokenSource.Cancel(); TokenSource.Dispose(); - FileGetTags.Header = "_Get Tags"; - TextBoxStatus.Text = ""; + EnableWindow(); + UpdateStatus(""); } } + private void UpdateStatus(string message) + { + TextBoxStatus.Text = message; + } + + private void EnableWindow() + { + FileMenu.IsEnabled = true; + TagsImport.IsEnabled = true; + TagsExport.IsEnabled = true; + TagsGet.Header = "_Get"; + } + + private void DisableWindow() + { + FileMenu.IsEnabled = false; + TagsImport.IsEnabled = false; + TagsExport.IsEnabled = false; + TagsGet.Header = "S_top"; + } + private void ImportTags() { } @@ -153,15 +212,9 @@ namespace MicroStationTagExplorer private void ExportTags() { - if (DataGridFiles.DataContext == null) - return; - if (IsRunning == false) { - int selectedIndex = DataGridFiles.SelectedIndex; - IList files = DataGridFiles.DataContext as IList; - if (files == null) - return; + IList files = GetFiles(); try { @@ -216,46 +269,7 @@ namespace MicroStationTagExplorer var data = e.Data.GetData(DataFormats.FileDrop); if (data != null && data is string[]) { - IList files = null; - if (DataGridFiles.DataContext != null) - { - files = DataGridFiles.DataContext as IList; - } - else - { - files = new ObservableCollection(); - DataGridFiles.DataContext = files; - } - - var droppedFiles = data as string[]; - foreach (var droppedFile in droppedFiles) - { - System.IO.FileAttributes attributes = System.IO.File.GetAttributes(droppedFile); - if (attributes.HasFlag(System.IO.FileAttributes.Directory)) - { - var allFiles = System.IO.Directory.EnumerateFiles(droppedFile, "*.*", System.IO.SearchOption.AllDirectories) - .Where(s => s.EndsWith(".dwg") || s.EndsWith(".dgn")); ; - foreach (var allFile in allFiles) - { - var file = new File() - { - Path = allFile - }; - files.Add(file); - } - } - else - { - if (droppedFile.EndsWith(".dwg") || droppedFile.EndsWith(".dgn")) - { - var file = new File() - { - Path = droppedFile - }; - files.Add(file); - } - } - } + AddPaths(data as string[]); } } } @@ -273,17 +287,17 @@ namespace MicroStationTagExplorer AddFiles(); } - private void FileGetTags_Click(object sender, RoutedEventArgs e) + private void TagsGet_Click(object sender, RoutedEventArgs e) { GetTags(); } - private void FileImportTags_Click(object sender, RoutedEventArgs e) + private void TagsImport_Click(object sender, RoutedEventArgs e) { ImportTags(); } - private void FileExportTags_Click(object sender, RoutedEventArgs e) + private void TagsExport_Click(object sender, RoutedEventArgs e) { ExportTags(); }