Filtration/Filtration.ThemeEditor/ViewModels/ThemeViewModel.cs

143 lines
4.2 KiB
C#
Raw Normal View History

2015-06-26 12:42:20 -04:00
using System;
using System.Collections.ObjectModel;
using System.IO;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Media.Imaging;
using Filtration.Common.Services;
2015-06-26 12:42:20 -04:00
using Filtration.Common.ViewModels;
using Filtration.Interface;
using Filtration.ThemeEditor.Providers;
2015-07-03 17:16:17 -04:00
using NLog;
2015-06-26 12:42:20 -04:00
using MessageBox = System.Windows.MessageBox;
namespace Filtration.ThemeEditor.ViewModels
{
public interface IThemeViewModel : IEditableDocument
{
void Initialise(ObservableCollection<ThemeComponentViewModel> themeComponentViewModels, bool newTheme);
string Title { get; }
string FilePath { get; set; }
string Filename { get; }
string Name { get; set; }
ObservableCollection<ThemeComponentViewModel> Components { get; set; }
}
public class ThemeViewModel : PaneViewModel, IThemeViewModel
{
2015-07-03 17:16:17 -04:00
private static readonly Logger _logger = LogManager.GetCurrentClassLogger();
2015-06-26 12:42:20 -04:00
private readonly IThemeProvider _themeProvider;
private readonly IMessageBoxService _messageBoxService;
2015-06-26 12:42:20 -04:00
private bool _filenameIsFake;
private string _filePath;
public ThemeViewModel(IThemeProvider themeProvider,
IMessageBoxService messageBoxService)
2015-06-26 12:42:20 -04:00
{
_themeProvider = themeProvider;
_messageBoxService = messageBoxService;
2015-06-26 12:42:20 -04:00
Components = new ObservableCollection<ThemeComponentViewModel>();
var icon = new BitmapImage();
icon.BeginInit();
icon.UriSource = new Uri("pack://application:,,,/Filtration;component/Resources/Icons/Theme.ico");
2015-06-26 12:42:20 -04:00
icon.EndInit();
IconSource = icon;
}
public void Initialise(ObservableCollection<ThemeComponentViewModel> themeComponentViewModels, bool newTheme)
{
Components = themeComponentViewModels;
_filenameIsFake = newTheme;
}
public bool IsScript { get { return false; } }
public bool IsTheme { get { return true; } }
public bool IsDirty { get; private set; }
public string FilePath
{
get { return _filePath; }
set
{
_filePath = value;
Title = Filename;
}
}
public string Filename
{
get { return Path.GetFileName(FilePath); }
}
public string Name { get; set; }
public ObservableCollection<ThemeComponentViewModel> Components { get; set; }
public void Save()
{
if (_filenameIsFake)
{
SaveAs();
return;
}
try
{
_themeProvider.SaveTheme(this, FilePath);
//RemoveDirtyFlag();
}
catch (Exception e)
{
2015-07-03 17:16:17 -04:00
if (_logger.IsErrorEnabled)
{
_logger.Error(e);
}
_messageBoxService.Show("Save Error", "Error saving filter theme - " + e.Message, MessageBoxButton.OK, MessageBoxImage.Error);
2015-06-26 12:42:20 -04:00
}
}
public void SaveAs()
{
var saveDialog = new SaveFileDialog
{
DefaultExt = ".filter",
Filter = @"Filter Theme Files (*.filtertheme)|*.filtertheme|All Files (*.*)|*.*"
};
var result = saveDialog.ShowDialog();
if (result != DialogResult.OK) return;
var previousFilePath = FilePath;
2015-07-03 17:16:17 -04:00
try
{
2015-06-26 12:42:20 -04:00
FilePath = saveDialog.FileName;
_themeProvider.SaveTheme(this, FilePath);
_filenameIsFake = false;
//RemoveDirtyFlag();
2015-07-03 17:16:17 -04:00
}
catch (Exception e)
{
if (_logger.IsErrorEnabled)
{
_logger.Error(e);
}
_messageBoxService.Show("Save Error", "Error saving theme file - " + e.Message, MessageBoxButton.OK,
2015-07-03 17:16:17 -04:00
MessageBoxImage.Error);
FilePath = previousFilePath;
}
2015-06-26 12:42:20 -04:00
}
public void Close()
{
throw new NotImplementedException();
}
}
}