From 606c12a4820474a202df2a6ce050945d887a0a46 Mon Sep 17 00:00:00 2001
From: "VKMTHD\\franzjosefkober" <franz.josef.kober@ivt.tugraz.at>
Date: Wed, 6 May 2020 09:15:41 +0200
Subject: [PATCH] added double click, changes correct folder at job edit

---
 VECTO3GUI/Helper/FileDialogHelper.cs          |  2 +-
 .../ViewModel/Impl/AbstractBusJobViewModel.cs | 56 ++++++++++++++-----
 VECTO3GUI/ViewModel/Impl/JoblistViewModel.cs  | 14 +++++
 .../ViewModel/Interfaces/IJoblistViewModel.cs |  2 +
 VECTO3GUI/Views/JoblistTabView.xaml           |  2 +-
 VECTO3GUI/Views/JoblistView.xaml              | 11 +++-
 6 files changed, 69 insertions(+), 18 deletions(-)

diff --git a/VECTO3GUI/Helper/FileDialogHelper.cs b/VECTO3GUI/Helper/FileDialogHelper.cs
index e8bfe59e80..9ca61b1692 100644
--- a/VECTO3GUI/Helper/FileDialogHelper.cs
+++ b/VECTO3GUI/Helper/FileDialogHelper.cs
@@ -14,7 +14,7 @@ namespace VECTO3GUI.Helper
 	public static class FileDialogHelper
 	{
 		public const string XMLFilter = "XML Files (*.xml)|*.xml|All Files (*.*)|*.*";
-		public const string JobFilter = "Job Files (*.vectojob|*.vectojob|All Files (*.*)|*.*";
+		public const string JobFilter = "Vecto Files (*.vecto|*.vecto|All Files (*.*)|*.*";
 
 		public static string[] ShowSelectFilesDialog(bool multiselect)
 		{
diff --git a/VECTO3GUI/ViewModel/Impl/AbstractBusJobViewModel.cs b/VECTO3GUI/ViewModel/Impl/AbstractBusJobViewModel.cs
index ead3ff00bf..7ff301c9b7 100644
--- a/VECTO3GUI/ViewModel/Impl/AbstractBusJobViewModel.cs
+++ b/VECTO3GUI/ViewModel/Impl/AbstractBusJobViewModel.cs
@@ -1,5 +1,5 @@
 using System;
-using System.Collections.Generic;
+using System.IO;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
@@ -23,7 +23,7 @@ namespace VECTO3GUI.ViewModel.Impl
 	{
 		CompletedBusFile,
 		PrimaryBusFile,
-		PIFBusFile
+		PIFBusFile,
 	}
 
 	public static class JobFileTypeHelper
@@ -67,6 +67,7 @@ namespace VECTO3GUI.ViewModel.Impl
 		protected SettingsModel Settings { get; private set; }
 
 		protected JobType JobType;
+		protected JobEntry JobEntry;
 		private readonly bool _editJob;
 
 		#endregion
@@ -147,6 +148,8 @@ namespace VECTO3GUI.ViewModel.Impl
 
 		private void SetJobEntryData(JobEntry jobEntry)
 		{
+			JobEntry = jobEntry;
+
 			FirstFilePath = jobEntry.Header.JobType == JobType.SingleBusJob
 				? jobEntry.Body.PrimaryVehicle
 				: jobEntry.Body.PrimaryVehicleResults;
@@ -167,7 +170,7 @@ namespace VECTO3GUI.ViewModel.Impl
 		}
 		private void DoSelectFirstFileCommand(JobFileType jobFileType)
 		{
-			FirstFilePath = OpenFileSelector(jobFileType, nameof(FirstFilePath));
+			FirstFilePath = OpenFileSelector(jobFileType, nameof(FirstFilePath), FirstFilePath);
 		}
 
 		public ICommand SelectSecondFileCommand
@@ -180,7 +183,7 @@ namespace VECTO3GUI.ViewModel.Impl
 		}
 		private void DoSelectSecondFileCommand(JobFileType jobFileType)
 		{
-			SecondFilePath = OpenFileSelector(jobFileType, nameof(SecondFilePath));
+			SecondFilePath = OpenFileSelector(jobFileType, nameof(SecondFilePath), SecondFilePath);
 		}
 
 		public ICommand CancelCommand
@@ -253,23 +256,46 @@ namespace VECTO3GUI.ViewModel.Impl
 		}
 
 
-		private string OpenFileSelector(JobFileType jobFileType, string textPropertyName)
+		private string OpenFileSelector(JobFileType jobFileType, string textPropertyName, string filePath)
 		{
-			var dialogResult = FileDialogHelper.ShowSelectFilesDialog(false, FileDialogHelper.XMLFilter, Settings.XmlFilePathFolder);
-			if (dialogResult == null)
-				return null;
+			var folderPath = GetFolderPath(filePath);
 
-			var filePath = dialogResult.FirstOrDefault();
-			var validationResult = IsValideXml(jobFileType, filePath);
+			var dialogResult = FileDialogHelper.ShowSelectFilesDialog(false, FileDialogHelper.XMLFilter, folderPath);
+			if (dialogResult != null) {
 
-			if (!validationResult)
-				AddPropertyError(textPropertyName, $"Selected XML-File is not a valid {jobFileType.GetLable()}!");
-			else
-				RemovePropertyError(textPropertyName);
+				filePath = dialogResult.FirstOrDefault();
+				var validationResult = IsValideXml(jobFileType, filePath);
+
+				if (!validationResult)
+					AddPropertyError(textPropertyName, $"Selected XML-File is not a valid {jobFileType.GetLable()}!");
+				else
+					RemovePropertyError(textPropertyName);
 
-			return !validationResult ? null : filePath;
+				return !validationResult ? null : filePath;
+			}
+			
+			return filePath;
 		}
 
+		private string GetFolderPath(string filePath)
+		{
+			if (!_editJob || filePath.IsNullOrEmpty())
+				return Settings.XmlFilePathFolder;
+			
+			if (IsFileName(filePath)) {
+				return !JobEntry.JobEntryFilePath.IsNullOrEmpty()
+					? Path.GetDirectoryName(JobEntry.JobEntryFilePath)
+					: Path.GetDirectoryName(Settings.XmlFilePathFolder);
+			}
+
+			return filePath;
+		}
+		
+		private bool IsFileName( string filePath)
+		{
+			return !Directory.Exists(filePath);
+		}
+		
 		private bool IsValideXml(JobFileType jobFileType, string filePath)
 		{
 			if (filePath.IsNullOrEmpty())
diff --git a/VECTO3GUI/ViewModel/Impl/JoblistViewModel.cs b/VECTO3GUI/ViewModel/Impl/JoblistViewModel.cs
index b88563191e..88f82773cd 100644
--- a/VECTO3GUI/ViewModel/Impl/JoblistViewModel.cs
+++ b/VECTO3GUI/ViewModel/Impl/JoblistViewModel.cs
@@ -48,6 +48,7 @@ namespace VECTO3GUI.ViewModel.Impl
 		private ICommand _moveJobDownCommand;
 		private ICommand _startSimulationCommand;
 		private ICommand _openInFolderCommand;
+		private ICommand _doubleClickCommand;
 
 		#endregion
 
@@ -125,6 +126,19 @@ namespace VECTO3GUI.ViewModel.Impl
 
 		#region Commands
 
+
+		public ICommand DoubleClickCommand
+		{
+			get { return _doubleClickCommand ?? (_doubleClickCommand = new RelayCommand<JobEntry>(DoDoubleClick)); }
+		}
+		private void DoDoubleClick(JobEntry jobEntry)
+		{
+			if (!CanEditCompletdFile(jobEntry))
+				return;
+			DoEditJob(jobEntry);
+		}
+
+
 		public ICommand RemoveJob
 		{
 			get
diff --git a/VECTO3GUI/ViewModel/Interfaces/IJoblistViewModel.cs b/VECTO3GUI/ViewModel/Interfaces/IJoblistViewModel.cs
index 09bcf5e9d9..0eb145dae9 100644
--- a/VECTO3GUI/ViewModel/Interfaces/IJoblistViewModel.cs
+++ b/VECTO3GUI/ViewModel/Interfaces/IJoblistViewModel.cs
@@ -23,5 +23,7 @@ namespace VECTO3GUI.ViewModel.Interfaces
 		ICommand OpenSettings { get; }
 		ICommand ExitMainCommand { get; }
 		ICommand OpenInFolder { get; }
+		ICommand DoubleClickCommand { get; }
+
 	}
 }
diff --git a/VECTO3GUI/Views/JoblistTabView.xaml b/VECTO3GUI/Views/JoblistTabView.xaml
index f5391b0b56..54a665f085 100644
--- a/VECTO3GUI/Views/JoblistTabView.xaml
+++ b/VECTO3GUI/Views/JoblistTabView.xaml
@@ -34,7 +34,7 @@
                                   CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}"/>
                         <MenuItem Header="Save to" Command="{Binding SaveToJob}"/>
                         <Separator HorizontalAlignment="Stretch" Background="Gray"/>
-                        <MenuItem Header="Exit" 
+                        <MenuItem Header="Close" 
                                   Command="{Binding CloseJob}" 
                                   CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}"/>
                     </MenuItem>
diff --git a/VECTO3GUI/Views/JoblistView.xaml b/VECTO3GUI/Views/JoblistView.xaml
index 8cc63c9467..aef6a140c9 100644
--- a/VECTO3GUI/Views/JoblistView.xaml
+++ b/VECTO3GUI/Views/JoblistView.xaml
@@ -64,7 +64,16 @@
             <DataGrid x:Name="JobList" ItemsSource="{Binding Jobs}" 
                       SelectedValue="{Binding DataContext.SelectedJobEntry, RelativeSource={RelativeSource AncestorType=views:JoblistView}}"
                       Style="{DynamicResource AzureDataGrid}" BorderThickness="1" CanUserAddRows="False" AutoGenerateColumns="False" SelectionUnit="FullRow" 
-                      IsReadOnly="False" HeadersVisibility="All"  RowHeaderWidth="5" >
+                      IsReadOnly="False" HeadersVisibility="All"  RowHeaderWidth="5" IsSynchronizedWithCurrentItem="True" >
+
+                <DataGrid.InputBindings>
+                    <MouseBinding
+                        MouseAction="LeftDoubleClick"
+                        Command="{Binding DoubleClickCommand}"
+                        CommandParameter="{Binding Jobs/}"/>
+                </DataGrid.InputBindings>
+
+
 
                 <DataGrid.Resources>
                     <ContextMenu x:Key="RowMenu">
-- 
GitLab