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

Skip to content
Snippets Groups Projects
Commit 02a80c77 authored by Harald Martini's avatar Harald Martini
Browse files

Added commands for saving to CreateVifViewModel

parent 8c583882
No related branches found
No related tags found
No related merge requests found
using System;
using System.Diagnostics;
using System.IO;
using System.Windows;
using System.Windows.Input;
using InteractiveDataDisplay.WPF;
using Microsoft.Toolkit.Mvvm.Input;
using Microsoft.WindowsAPICodePack.Shell.Interop;
using Newtonsoft.Json;
using TUGraz.VectoCommon.InputData;
using TUGraz.VectoCommon.Models;
using TUGraz.VectoCore.InputData.FileIO.JSON;
using TUGraz.VectoCore.InputData.FileIO.XML;
using TUGraz.VectoCore.InputData.FileIO.XML.Declaration;
using TUGraz.VectoCore.InputData.FileIO.XML.Declaration.DataProvider;
......@@ -26,6 +30,7 @@ namespace VECTO3GUI2020.ViewModel.MultiStage.Implementation
bool? StageInputExempted { get; set; }
string PrimaryInputPath { get; set; }
string StageInputPath { get; set; }
void SaveJob(string path);
}
public class CreateVifViewModel : ViewModelBase, ICreateVifViewModel
{
......@@ -34,7 +39,6 @@ namespace VECTO3GUI2020.ViewModel.MultiStage.Implementation
private readonly IDialogHelper _dialogHelper;
private readonly IXMLInputDataReader _inputDataReader;
private static uint _newVifCounter = 0;
private readonly JSONJob _jsonJob;
private bool? _exemptedPrimary;
......@@ -52,9 +56,10 @@ namespace VECTO3GUI2020.ViewModel.MultiStage.Implementation
set => SetProperty(ref _stageInputExempted, value);
}
public CreateVifViewModel(IDialogHelper dialogHelper, IXMLInputDataReader inputDataReader, IAdditionalJobInfoViewModel additionalJobInfo, JSONJob jsonJob)
public CreateVifViewModel(IDialogHelper dialogHelper,
IXMLInputDataReader inputDataReader,
IAdditionalJobInfoViewModel additionalJobInfo)
{
_jsonJob = jsonJob;
_dialogHelper = dialogHelper;
_inputDataReader = inputDataReader;
Title = "Create VIF";
......@@ -62,6 +67,25 @@ namespace VECTO3GUI2020.ViewModel.MultiStage.Implementation
_documentName = $"New Vif {++_newVifCounter}";
}
public CreateVifViewModel(IDeclarationInputDataProvider inputData,
IDialogHelper dialogHelper,
IXMLInputDataReader inputDataReader,
IAdditionalJobInfoViewModel additionalJobInfo) : this(dialogHelper, inputDataReader, additionalJobInfo)
{
SetInputData(inputData);
}
private void SetInputData(IInputDataProvider inputData)
{
var inputDataProvider = inputData as JSONInputDataV10_PrimaryAndInterimBus;
Debug.Assert(inputDataProvider != null);
DataSource = inputData.DataSource;
Title += $"- {Path.GetFileName(_dataSource.SourceFile)}";
DocumentName = Path.GetFileNameWithoutExtension(_dataSource.SourceFile);
}
public string PrimaryInputPath
{
......@@ -109,18 +133,67 @@ namespace VECTO3GUI2020.ViewModel.MultiStage.Implementation
}));
}
private ICommand _saveJobCommand;
private IRelayCommand _saveJobCommand;
public ICommand SaveJobCommand
{
get => _saveJobCommand ?? (_saveJobCommand = new RelayCommand(() => { return; }));
get => _saveJobCommand ?? (_saveJobCommand = new RelayCommand(() => {
if (_dataSource.SourceFile != null) {
if (CanBeSaved()) {
SaveJob(_dataSource.SourceFile);
}
}
}, () => DataSource != null));
}
private bool CanBeSaved()
{
if (_primaryInputPath == null) {
_dialogHelper.ShowMessageBox("At least Primary Vehicle has to be provided", "Info", MessageBoxButton.OK,
MessageBoxImage.Information);
return false;
}
return true;
}
public void SaveJob(string path)
{
if (path == null) {
return;
}
var jsonJob = new JSONJob() {
Header = new JSONJobHeader() {
AppVersion = "Vecto3GUI2020",
CreatedBy = Environment.UserName,
Date = DateTime.Today,
FileVersion = JSONJobHeader.PrimaryAndInterimVersion
},
Body = new JSONJobBody() {
PrimaryVehicle = PrimaryInputPath,
InterimStage = StageInputPath
}
};
string jsonString = JsonConvert.SerializeObject(jsonJob, Formatting.Indented);
Debug.WriteLine(jsonString);
File.WriteAllText(path, jsonString);
SetInputData(JSONInputDataFactory.ReadJsonJob(path));
}
private ICommand _saveJobAsCommand;
public ICommand SaveJobAsCommand
{
get => _saveJobAsCommand ?? (_saveJobAsCommand = new RelayCommand(() => { return; }));
get => _saveJobAsCommand ?? (_saveJobAsCommand = new RelayCommand(() => {
if (CanBeSaved()) {
var path = _dialogHelper.SaveToJsonDialog();
SaveJob(path);
}
}));
}
public bool LoadStageInput(string fileName)
......@@ -251,6 +324,8 @@ namespace VECTO3GUI2020.ViewModel.MultiStage.Implementation
#region Implementation of IDocumentViewModel
private bool _selected;
private string _documentName;
private DataSource _dataSource;
public string DocumentName
{
get => _documentName;
......@@ -260,7 +335,16 @@ namespace VECTO3GUI2020.ViewModel.MultiStage.Implementation
public XmlDocumentType DocumentType => throw new NotImplementedException();
public DataSource DataSource => null;
public DataSource DataSource
{
get => _dataSource;
set
{
if (SetProperty(ref _dataSource, value)) {
_saveJobCommand.NotifyCanExecuteChanged();
}
}
}
public IEditViewModel EditViewModel => this;
......
......@@ -39,8 +39,8 @@
<DockPanel DockPanel.Dock="Bottom" LastChildFill="False">
<UniformGrid DockPanel.Dock="Right" Rows="1" Width="500" HorizontalAlignment="Right">
<Button Style="{StaticResource MultiStageButtonStyle1}">Create new VIF</Button>
<Button Style="{StaticResource MultiStageButtonStyle1}">Save Job as ...</Button>
<Button Style="{StaticResource MultiStageButtonStyle1}">Save Job</Button>
<Button Style="{StaticResource MultiStageButtonStyle1}" Command="{Binding SaveJobAsCommand}">Save Job as ...</Button>
<Button Style="{StaticResource MultiStageButtonStyle1}" Command="{Binding SaveJobCommand}">Save Job</Button>
<Button Style="{StaticResource MultiStageButtonStyle1}"
Command="{Binding CloseWindowCommand}"
CommandParameter="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}">Close</Button>
......
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