Filtration/Filtration.ThemeEditor/ViewModels/ThemeEditorViewModel.cs

213 lines
6.7 KiB
C#
Raw Normal View History

2015-06-26 12:42:20 -04:00
using System;
using System.IO;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Media;
2015-06-26 12:42:20 -04:00
using System.Windows.Media.Imaging;
using Filtration.Common.Services;
2015-06-26 12:42:20 -04:00
using Filtration.Common.ViewModels;
using Filtration.Interface;
2015-07-06 08:41:46 -04:00
using Filtration.ObjectModel;
using Filtration.ObjectModel.Enums;
using Filtration.ObjectModel.ThemeEditor;
2015-07-06 08:41:46 -04:00
using Filtration.ThemeEditor.Messages;
2015-06-26 12:42:20 -04:00
using Filtration.ThemeEditor.Providers;
using GalaSoft.MvvmLight.CommandWpf;
2015-07-06 08:41:46 -04:00
using GalaSoft.MvvmLight.Messaging;
2015-07-03 17:16:17 -04:00
using NLog;
2015-06-26 12:42:20 -04:00
namespace Filtration.ThemeEditor.ViewModels
{
public interface IThemeEditorViewModel : IEditableDocument
2015-06-26 12:42:20 -04:00
{
RelayCommand<ThemeComponentType> AddThemeComponentCommand { get; }
RelayCommand<ThemeComponent> DeleteThemeComponentCommand { get; }
2015-07-06 08:41:46 -04:00
RelayCommand CloseCommand { get; }
2015-07-06 08:41:46 -04:00
void InitialiseForNewTheme(ThemeComponentCollection themeComponentCollection);
void InitialiseForMasterTheme(ItemFilterScript script);
bool IsMasterTheme { get; }
ItemFilterScript IsMasterThemeForScript { get; }
2015-06-26 12:42:20 -04:00
string Title { get; }
string FilePath { get; set; }
string Filename { get; }
string Name { get; set; }
ThemeComponentCollection Components { get; set; }
ThemeComponent SelectedThemeComponent { get; }
2015-06-26 12:42:20 -04:00
}
public class ThemeEditorViewModel : PaneViewModel, IThemeEditorViewModel
2015-06-26 12:42:20 -04:00
{
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;
private ThemeComponent _selectedThemeComponent;
2015-06-26 12:42:20 -04:00
public ThemeEditorViewModel(IThemeProvider themeProvider,
IMessageBoxService messageBoxService)
2015-06-26 12:42:20 -04:00
{
_themeProvider = themeProvider;
_messageBoxService = messageBoxService;
2015-06-26 12:42:20 -04:00
2015-07-06 08:41:46 -04:00
AddThemeComponentCommand = new RelayCommand<ThemeComponentType>(OnAddThemeComponentCommand, t => IsMasterTheme);
DeleteThemeComponentCommand = new RelayCommand<ThemeComponent>(OnDeleteThemeComponentCommand,
2015-07-06 08:41:46 -04:00
t => IsMasterTheme && SelectedThemeComponent != null);
CloseCommand = new RelayCommand(OnCloseCommand);
2015-06-26 12:42:20 -04:00
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;
2015-07-06 08:41:46 -04:00
2015-06-26 12:42:20 -04:00
}
public RelayCommand<ThemeComponentType> AddThemeComponentCommand { get; private set; }
public RelayCommand<ThemeComponent> DeleteThemeComponentCommand { get; private set; }
2015-07-06 08:41:46 -04:00
public RelayCommand CloseCommand { get; private set; }
2015-07-06 08:41:46 -04:00
public bool IsMasterTheme
{
get { return Components.IsMasterCollection; }
}
2015-07-06 08:41:46 -04:00
public ItemFilterScript IsMasterThemeForScript { get; private set; }
public void InitialiseForNewTheme(ThemeComponentCollection themeComponentCollection)
2015-06-26 12:42:20 -04:00
{
Components = themeComponentCollection;
2015-07-06 08:41:46 -04:00
_filenameIsFake = true;
}
public void InitialiseForMasterTheme(ItemFilterScript script)
{
Components = script.ThemeComponents;
IsMasterThemeForScript = script;
_filenameIsFake = true;
2015-06-26 12:42:20 -04:00
}
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 _filenameIsFake ? FilePath : Path.GetFileName(FilePath); }
2015-06-26 12:42:20 -04:00
}
public string Name { get; set; }
public ThemeComponentCollection Components { get; set; }
public ThemeComponent SelectedThemeComponent
{
get { return _selectedThemeComponent; }
set
{
_selectedThemeComponent = value;
RaisePropertyChanged();
}
}
2015-06-26 12:42:20 -04:00
public void Save()
{
2015-07-06 08:41:46 -04:00
if (IsMasterTheme) return;
2015-06-26 12:42:20 -04:00
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()
{
2015-07-06 08:41:46 -04:00
if (IsMasterTheme) return;
2015-06-26 12:42:20 -04:00
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;
Title = Filename;
2015-06-26 12:42:20 -04:00
//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
}
2015-07-06 08:41:46 -04:00
public void OnCloseCommand()
2015-06-26 12:42:20 -04:00
{
2015-07-06 08:41:46 -04:00
Close();
2015-06-26 12:42:20 -04:00
}
2015-07-06 08:41:46 -04:00
public void Close()
{
Messenger.Default.Send(new ThemeClosedMessage { ClosedViewModel = this });
}
private void OnAddThemeComponentCommand(ThemeComponentType themeComponentType)
{
Components.Add(new ThemeComponent(themeComponentType, "Untitled Component",
new Color {A = 255, R = 255, G = 255, B = 255}));
}
private void OnDeleteThemeComponentCommand(ThemeComponent themeComponent)
{
if (themeComponent == null) return;
themeComponent.TerminateComponent();
Components.Remove(themeComponent);
}
2015-06-26 12:42:20 -04:00
}
}