Code development platform for open source projects from the European Union institutions

Skip to content
Snippets Groups Projects
Commit 583780d0 authored by Franz KOBER josef's avatar Franz KOBER josef
Browse files

added serialization for single and completed bus job

parent dcddbd81
No related branches found
No related tags found
No related merge requests found
<?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>
......
......@@ -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;
}
}
}
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;
}
}
}
}
......@@ -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" />
......
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);
......
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
......@@ -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
}
......@@ -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; }
......
......@@ -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}}}"/>
......
......@@ -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}"/>
......
......@@ -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" />
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment