diff --git a/VECTO3GUI/App.config b/VECTO3GUI/App.config index 3d1145dfc2695645de184f2dcd268328d918b0c1..43a59994b753ba74a4a681fe158167c841fdcbf8 100644 --- a/VECTO3GUI/App.config +++ b/VECTO3GUI/App.config @@ -1,7 +1,7 @@ <?xml version="1.0" encoding="utf-8"?> <configuration> <configSections> - <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" > + <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> <section name="VECTO3GUI.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" /> </sectionGroup> </configSections> diff --git a/VECTO3GUI/Helper/FileDialogHelper.cs b/VECTO3GUI/Helper/FileDialogHelper.cs index 8baf328f121cb10fb52c8e67c7e5eb8860b7c97a..bf0fac214d92d234a51e5d3738970bd06292fc29 100644 --- a/VECTO3GUI/Helper/FileDialogHelper.cs +++ b/VECTO3GUI/Helper/FileDialogHelper.cs @@ -14,6 +14,7 @@ namespace VECTO3GUI.Helper public static class FileDialogHelper { private const string XMLFilter = "XML Files (*.xml)|*.xml|All Files (*.*)|*.*"; + private const string JobFilter = "Job Files (*.vectojob|*.vectojob|All Files (*.*)|*.*"; public static string[] ShowSelectFilesDialog(bool multiselect, string initialDirectory = null) @@ -52,6 +53,16 @@ namespace VECTO3GUI.Helper return null; } + public static string SaveJobFileToDialog(string initialDirectory = null) + { + var saveFileDialog = new SaveFileDialog { + Filter = JobFilter + }; + + if (initialDirectory != null) + saveFileDialog.InitialDirectory = initialDirectory; + return saveFileDialog.ShowDialog() == true ? saveFileDialog.FileName : null; + } } } diff --git a/VECTO3GUI/Helper/SerializeHelper.cs b/VECTO3GUI/Helper/SerializeHelper.cs new file mode 100644 index 0000000000000000000000000000000000000000..fea12d4803612c114efe7b2035a3b9a0ec335542 --- /dev/null +++ b/VECTO3GUI/Helper/SerializeHelper.cs @@ -0,0 +1,36 @@ +using System; +using System.Collections.Generic; +using System.IO; +using Castle.Core.Internal; +using Newtonsoft.Json; + +namespace VECTO3GUI.Helper +{ + public static class SerializeHelper + { + public static void SerializeToFile<T>(string filePath, T file) + { + if (filePath.IsNullOrEmpty() || file == null) + return; + + var serializedObject = JsonConvert.SerializeObject(file, Formatting.Indented); + File.WriteAllText(filePath, serializedObject); + } + + public static T DeserializeToObject<T>(string filePath) + { + if (filePath.IsNullOrEmpty()) + return default(T); + + using (var file = File.OpenText(filePath)) { + var serializer = new JsonSerializer(); + var result = serializer.Deserialize(file,typeof(T)); + if (result == null) + return default(T); + + return (T)result; + } + } + + } +} diff --git a/VECTO3GUI/VECTO3GUI.csproj b/VECTO3GUI/VECTO3GUI.csproj index e0f1b6ba4cc3fcdc12e6fd42f2d14a6d60b58594..1e1f5baaa55ed01cfe8dc05828b3dd64c49c6d58 100644 --- a/VECTO3GUI/VECTO3GUI.csproj +++ b/VECTO3GUI/VECTO3GUI.csproj @@ -123,6 +123,9 @@ <Reference Include="Microsoft.WindowsAPICodePack.Shell, Version=1.1.0.0, Culture=neutral, processorArchitecture=MSIL"> <HintPath>..\packages\WindowsAPICodePack-Shell.1.1.1\lib\Microsoft.WindowsAPICodePack.Shell.dll</HintPath> </Reference> + <Reference Include="Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL"> + <HintPath>..\packages\Newtonsoft.Json.12.0.3\lib\net45\Newtonsoft.Json.dll</HintPath> + </Reference> <Reference Include="Ninject, Version=3.3.4.0, Culture=neutral, PublicKeyToken=c7192dc5380945e7, processorArchitecture=MSIL"> <HintPath>..\packages\Ninject.3.3.4\lib\net45\Ninject.dll</HintPath> <Private>True</Private> @@ -165,6 +168,7 @@ <Compile Include="Helper\FileDialogHelper.cs" /> <Compile Include="Helper\OutputWindowHelper.cs" /> <Compile Include="Helper\Converter\SaveButtonLabelConverter.cs" /> + <Compile Include="Helper\SerializeHelper.cs" /> <Compile Include="Helper\ViewModelBase.cs" /> <Compile Include="Model\InterfacesImpl.cs" /> <Compile Include="Model\SettingsModel.cs" /> diff --git a/VECTO3GUI/ViewModel/Impl/AbstractBusJobViewModel.cs b/VECTO3GUI/ViewModel/Impl/AbstractBusJobViewModel.cs index 3783ad384e3cc1c4c6efa95f66964024ad71e979..89b9a377a7a638cdff614710c5fa7c2450d58d2a 100644 --- a/VECTO3GUI/ViewModel/Impl/AbstractBusJobViewModel.cs +++ b/VECTO3GUI/ViewModel/Impl/AbstractBusJobViewModel.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using System.ComponentModel; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -8,7 +7,6 @@ using System.Windows; using System.Windows.Input; using System.Xml; using Castle.Core.Internal; -using Microsoft.WindowsAPICodePack.Shell; using Ninject; using TUGraz.VectoCommon.InputData; using TUGraz.VectoCore.InputData.FileIO.XML; @@ -74,6 +72,8 @@ namespace VECTO3GUI.ViewModel.Impl #region Properties + public JobEntry SavedJobEntry { get; private set; } + public string FirstFilePath { get { return _firstFilePath; } @@ -169,19 +169,39 @@ namespace VECTO3GUI.ViewModel.Impl public ICommand SaveCommand { - get { return _saveCommand ?? (_saveCommand = new RelayCommand(DoSaveCommand, CanSaveCommand)); } + get { return _saveCommand ?? (_saveCommand = new RelayCommand<Window>(DoSaveCommand, CanSaveCommand)); } } - private bool CanSaveCommand() + private bool CanSaveCommand(Window window) { return !HasErrors && !FirstFilePath.IsNullOrEmpty() && !SecondFilePath.IsNullOrEmpty(); } - private void DoSaveCommand() + private void DoSaveCommand(Window window) { - + SaveJob(window); } #endregion + private void SaveJob(Window window) + { + var jobFilePath = FileDialogHelper.SaveJobFileToDialog(Settings.XmlFilePathFolder); + if (jobFilePath != null) { + + var job = new JobEntry { + JobEntryFilePath = jobFilePath, + FirstFilePath = FirstFilePath, + SecondFilePath = SecondFilePath, + JobType = JobType + }; + + SerializeHelper.SerializeToFile(jobFilePath, job); + SavedJobEntry = job; + DoCancelCommand(window); + } + } + + + private string OpenFileSelector(JobFileType jobFileType, string textPropertyName) { var dialogResult = FileDialogHelper.ShowSelectFilesDialog(false, Settings.XmlFilePathFolder); diff --git a/VECTO3GUI/ViewModel/Impl/JobEntry.cs b/VECTO3GUI/ViewModel/Impl/JobEntry.cs index 9bcad924f9d94621334272c974f23502ed5752af..91d9898d8fbe07c50a64679875c11872fea9016d 100644 --- a/VECTO3GUI/ViewModel/Impl/JobEntry.cs +++ b/VECTO3GUI/ViewModel/Impl/JobEntry.cs @@ -1,9 +1,8 @@ using System; -using System.Security.RightsManagement; -using System.Windows.Forms; - -namespace VECTO3GUI.ViewModel.Impl { +using Newtonsoft.Json; +namespace VECTO3GUI.ViewModel.Impl +{ public enum JobType { SingleBusJob, @@ -13,13 +12,18 @@ namespace VECTO3GUI.ViewModel.Impl { public static class JobTypeHelper { + private const string SingleBusJobLabel = "Single Bus Job"; + private const string CompletedBusJobLabel = "Completed Bus Job"; + + public static string GetLabel(this JobType jobType) { - switch (jobType) { + switch (jobType) + { case JobType.SingleBusJob: - return "Single Bus Job"; + return SingleBusJobLabel; case JobType.CompletedBusJob: - return "Completed Bus Job"; + return CompletedBusJobLabel; default: return string.Empty; } @@ -27,31 +31,70 @@ namespace VECTO3GUI.ViewModel.Impl { public static JobType Parse(this string jobTypeName) { - if (JobType.SingleBusJob.GetLabel() == jobTypeName) + if (SingleBusJobLabel == jobTypeName) return JobType.SingleBusJob; - if (JobType.CompletedBusJob.GetLabel() == jobTypeName) + if (CompletedBusJobLabel == jobTypeName) return JobType.CompletedBusJob; return JobType.Unknown; } } - + + [JsonObject(ItemNullValueHandling = NullValueHandling.Ignore)] public class JobEntry : ObservableObject { + private JobType _jobType; + private string _jobTypeName; private bool _selected; - private string _filename; + private string _jobEntryFilePath; + private string _firstFilePath; + private string _secondFilePath; + [JsonIgnore] public int Sorting; + [JsonIgnore] + public JobType JobType + { + get { return _jobType;} + set + { + _jobType = value; + _jobTypeName = _jobType.GetLabel(); + } + } + + public string JobTypeName + { + get { return _jobTypeName; } + set + { + _jobTypeName = value; + _jobType = _jobTypeName.Parse(); + } + } + + [JsonIgnore] public bool Selected { get { return _selected; } set { SetProperty(ref _selected, value); } } - public string Filename + public string JobEntryFilePath + { + get { return _jobEntryFilePath; } + set { SetProperty(ref _jobEntryFilePath, value); } + } + public string FirstFilePath + { + get { return _firstFilePath; } + set { SetProperty(ref _firstFilePath, value); } + } + + public string SecondFilePath { - get { return _filename; } - set { SetProperty(ref _filename, value); } + get { return _secondFilePath; } + set { SetProperty(ref _secondFilePath, value); } } } } \ No newline at end of file diff --git a/VECTO3GUI/ViewModel/Impl/JoblistViewModel.cs b/VECTO3GUI/ViewModel/Impl/JoblistViewModel.cs index 1d3a9a2ad96c7a152b48e2aeb9639b8e9244163c..20b071475785de74d7822d5ea1acf4819abfd1cd 100644 --- a/VECTO3GUI/ViewModel/Impl/JoblistViewModel.cs +++ b/VECTO3GUI/ViewModel/Impl/JoblistViewModel.cs @@ -98,7 +98,7 @@ namespace VECTO3GUI.ViewModel.Impl { _jobs.Add(new JobEntry() { - Filename = jobFile, + FirstFilePath = jobFile, Selected = false, Sorting = _jobs.Count }); @@ -154,7 +154,7 @@ namespace VECTO3GUI.ViewModel.Impl var entry = SelectedJobEntry; try { - var jobEditView = ReadJob(entry.Filename); //Kernel.Get<IJobEditViewModel>(); + var jobEditView = ReadJob(entry.FirstFilePath); //Kernel.Get<IJobEditViewModel>(); if (jobEditView == null) return; @@ -203,7 +203,7 @@ namespace VECTO3GUI.ViewModel.Impl if (SelectedJobEntry == null) return; - var xmlViewModel = new XMLViewModel(SelectedJobEntry.Filename); + var xmlViewModel = new XMLViewModel(SelectedJobEntry.FirstFilePath); var window = OutputWindowHelper.CreateOutputWindow(Kernel, xmlViewModel, xmlViewModel.FileName); window.Show(); } @@ -223,7 +223,7 @@ namespace VECTO3GUI.ViewModel.Impl { _jobs.Add(new JobEntry() { - Filename = filePath.First(), + FirstFilePath = filePath.First(), Selected = false, Sorting = _jobs.Count }); @@ -281,8 +281,12 @@ namespace VECTO3GUI.ViewModel.Impl break; } - var window = OutputWindowHelper.CreateOutputWindow(Kernel, viewModel, $"Create {jobtype.GetLabel()}", 460, 200, ResizeMode.NoResize); + var window = OutputWindowHelper.CreateOutputWindow(Kernel, viewModel, $"Create {jobtype.GetLabel()}", + 460, 200, ResizeMode.NoResize); window.ShowDialog(); + + + AddBusJobEntry(((IBusJobViewModel)viewModel)?.SavedJobEntry); } @@ -295,6 +299,14 @@ namespace VECTO3GUI.ViewModel.Impl #endregion + private void AddBusJobEntry(JobEntry jobEntry) + { + if (jobEntry == null) + return; + + _jobs.Add(jobEntry); + } + private IJobEditViewModel ReadJob(string jobFile) { if (jobFile == null) @@ -333,8 +345,6 @@ namespace VECTO3GUI.ViewModel.Impl private IJobEditViewModel CreateCompleteBusVehicleViewModel(IDeclarationInputDataProvider dataProvider) { - - _messages.Add(new MessageEntry { Message = "Edit File" }); @@ -351,57 +361,6 @@ namespace VECTO3GUI.ViewModel.Impl - #region Legacy - - //IInputDataProvider inputData = null; - //var ext = Path.GetExtension(jobFile); - //switch (ext) { - // case Constants.FileExtensions.VectoJobFile: - // inputData = JSONInputDataFactory.ReadJsonJob(jobFile); - // break; - // case Constants.FileExtensions.VectoXMLDeclarationFile: - // //ToDo - // //case Constants.FileExtensions.VectoXMLJobFile: - // inputData = Kernel.Get<IXMLInputDataReader>().CreateDeclaration(jobFile); - // break; - // default: - // throw new UnsupportedFileVersionException(jobFile); - //} - - //var retVal = CreateJobEditViewModel(inputData); - - //if (retVal == null) { - // throw new Exception("Unsupported job type"); - //} - //return retVal; - - - - //private IJobEditViewModel CreateJobEditViewModel(IInputDataProvider inputData) - //{ - // IJobEditViewModel retVal = null; - // if (inputData is JSONInputDataV2) { - // var jsoninputData = inputData as JSONInputDataV2; - // if (jsoninputData.SavedInDeclarationMode) { - // retVal = new DeclarationJobViewModel(Kernel, jsoninputData); - // } else { - // if (jsoninputData.EngineOnlyMode) { - // retVal = new EngineOnlyJobViewModel(Kernel, jsoninputData); - // } else { - // // TODO! - // } - // } - // } - // //ToDo - // //if (inputData is XMLDeclarationInputDataProvider) { - // // var declInput = inputData as IDeclarationInputDataProvider; - // // retVal = new DeclarationJobViewModel(Kernel, declInput); - // //} - // return retVal; - //} - - - #endregion } diff --git a/VECTO3GUI/ViewModel/Interfaces/IBusJobViewModel.cs b/VECTO3GUI/ViewModel/Interfaces/IBusJobViewModel.cs index 5c6413f16bc1ced38cd68e7158336cbabb26301b..2af1764f52943d83f7c31eadd0f36528915f6aff 100644 --- a/VECTO3GUI/ViewModel/Interfaces/IBusJobViewModel.cs +++ b/VECTO3GUI/ViewModel/Interfaces/IBusJobViewModel.cs @@ -16,6 +16,7 @@ namespace VECTO3GUI.ViewModel.Interfaces string SecondFilePath { get; } string FirstLabelText { get; } string SecondLabelText { get; } + JobEntry SavedJobEntry { get; } ICommand SelectFirstFileCommand { get; } ICommand SelectSecondFileCommand { get; } diff --git a/VECTO3GUI/Views/BusJobView.xaml b/VECTO3GUI/Views/BusJobView.xaml index fd6c2eda2318a901fb0e093b672dce96d7e3770b..5aeebfb8fa65873aaa94bb852f5a98c419ee3066 100644 --- a/VECTO3GUI/Views/BusJobView.xaml +++ b/VECTO3GUI/Views/BusJobView.xaml @@ -92,7 +92,8 @@ <StackPanel Orientation="Horizontal" HorizontalAlignment="Right"> <Button Content="Save" HorizontalAlignment="Right" Margin="5,0,0,0" VerticalAlignment="Center" Width="70" - Command="{Binding SaveCommand}"/> + Command="{Binding SaveCommand}" + CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}"/> <Button Content="Cancel" HorizontalAlignment="Right" Margin="5,0,10,0" Width="70" Command="{Binding CancelCommand}" CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}"/> diff --git a/VECTO3GUI/Views/JoblistView.xaml b/VECTO3GUI/Views/JoblistView.xaml index 10c1631723f62eadedac91bf72e31fc891aa5321..2ed97f2810b85c7ad9d57530bb94c2eebe5daf6b 100644 --- a/VECTO3GUI/Views/JoblistView.xaml +++ b/VECTO3GUI/Views/JoblistView.xaml @@ -61,7 +61,7 @@ </DataGrid.ContextMenu> <DataGrid.Columns> - <DataGridTextColumn Header="File Path" Binding="{Binding Filename}" > + <DataGridTextColumn Header="File Path" Binding="{Binding FirstFilePath}" > <DataGridTextColumn.CellStyle> <Style TargetType= "{x:Type DataGridCell}" BasedOn="{StaticResource MetroDataGridCell}" > <Setter Property="ContextMenu" Value="{StaticResource RowMenu}"/> diff --git a/VECTO3GUI/packages.config b/VECTO3GUI/packages.config index bc9ede5301f91b9952915cf8cc74cb8edf431896..2cb8134ec1dc64efe357691771c6a8486048fce7 100644 --- a/VECTO3GUI/packages.config +++ b/VECTO3GUI/packages.config @@ -5,6 +5,7 @@ <package id="ControlzEx" version="3.0.2.4" targetFramework="net452" /> <package id="MahApps.Metro" version="1.6.5" targetFramework="net452" /> <package id="MahApps.Metro.IconPacks" version="3.7.0" targetFramework="net452" /> + <package id="Newtonsoft.Json" version="12.0.3" targetFramework="net452" /> <package id="Ninject" version="3.3.4" targetFramework="net452" /> <package id="Ninject.Extensions.Factory" version="3.3.2" targetFramework="net452" /> <package id="System.Windows.Interactivity.WPF" version="2.0.20525" targetFramework="net452" />