zaaReloaded2/zaaReloaded2/ViewModels/SettingsViewModel.cs

301 lines
8.5 KiB
C#
Executable File

/* SettingsViewModel.cs
* part of zaaReloaded2
*
* Copyright 2015 Daniel Kraus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using Bovender.Mvvm;
using Bovender.Mvvm.ViewModels;
using Bovender.Mvvm.Messaging;
using zaaReloaded2.Controller;
using zaaReloaded2.Controller.Elements;
using zaaReloaded2.Formatter;
namespace zaaReloaded2.ViewModels
{
/// <summary>
/// View model for the zaaReloaded2.Controller.Settings class.
/// </summary>
public class SettingsViewModel : ViewModelBase, ICloneable
{
#region Properties
/// <summary>
/// Gets or sets the name of the Settings
/// </summary>
public string Name
{
[DebuggerStepThrough]
get
{
return _settings.Name;
}
[DebuggerStepThrough]
set
{
_settings.Name = value;
OnPropertyChanged("Name");
}
}
/// <summary>
/// Is true if the settings' name is editable.
/// If the settings' name is a default, preconfigured name,
/// this will be false.
/// </summary>
public bool IsNameEnabled
{
get
{
return (Name != Properties.Settings.Default.SettingsNameClinic) &&
(Name != Properties.Settings.Default.SettingsNameWard);
}
}
/// <summary>
/// Gets a list of element view models.
/// </summary>
public IList<ElementViewModel> Elements
{
get
{
if (_elements == null) { _elements = new List<ElementViewModel>(); }
return _elements;
}
}
/// <summary>
/// Gets or sets the currently selected element.
/// </summary>
public ElementViewModel SelectedElement
{
get { return _selectedElement; }
set
{
_selectedElement = value;
OnPropertyChanged("SelectedElement");
}
}
/// <summary>
/// Returns an EnumProvider object for the ReferenceStyle
/// </summary>
public EnumProvider<ReferenceStyle> ReferenceStyle
{
get
{
if (_referenceStyle == null)
{
_referenceStyle = new EnumProvider<ReferenceStyle>(_settings.ReferenceStyle);
}
return _referenceStyle;
}
}
#endregion
#region Constructors
public SettingsViewModel()
: this(new Settings())
{ }
public SettingsViewModel(Settings settings)
: base()
{
_settings = settings;
_elements = new List<ElementViewModel>();
foreach (ElementBase element in settings.Elements)
{
ElementViewModel vm;
if (element is FormatElementBase)
{
vm = new FormatElementViewModel(element as FormatElementBase);
}
else if (element is ControlElementBase)
{
vm = new ControlElementViewModel(element as ControlElementBase);
}
else
{
throw new InvalidOperationException(
"Cannot create ViewModel for " + element.GetType().ToString());
}
AddElementViewModel(vm);
}
}
#endregion
#region Messages
#endregion
#region Commands
public DelegatingCommand AddElementCommand
{
get
{
if (_addElementCommand == null)
{
_addElementCommand = new DelegatingCommand(
param => DoAddElement());
}
return _addElementCommand;
}
}
public DelegatingCommand AddChildElementCommand
{
get
{
if (_addChildElementCommand == null)
{
_addChildElementCommand = new DelegatingCommand(
param => DoAddChildElement(),
param => CanAddChildElement());
}
return _addChildElementCommand;
}
}
public DelegatingCommand DeleteElementCommand
{
get
{
if (_deleteElementCommand == null)
{
_deleteElementCommand = new DelegatingCommand(
param => DoDeleteElement(),
param => CanDeleteElement());
}
return _deleteElementCommand;
}
}
public DelegatingCommand CopyElementCommand
{
get
{
if (_copyElementCommand == null)
{
_copyElementCommand = new DelegatingCommand(
param => DoCopyElement(),
param => CanCopyElement());
}
return _copyElementCommand;
}
}
#endregion
#region Public methods
/// <summary>
/// Wires the OnProperty changed event of an ElementViewModel's
/// wrapped model and adds the view model to the Elements collection.
/// </summary>
public void AddElementViewModel(ElementViewModel elementViewModel)
{
elementViewModel.PropertyChanged += ElementViewModel_PropertyChanged;
Elements.Add(elementViewModel);
}
/// <summary>
/// Wires the OnProperty changed event of an ElementViewModel's
/// wrapped model and adds the view model as a child of another
/// view model.
/// </summary>
public void AddChildElementViewModel(ControlElementViewModel parent, FormatElementViewModel child)
{
child.PropertyChanged += ElementViewModel_PropertyChanged;
parent.AddChildElement(child);
}
#endregion
#region Private methods
void DoAddElement() { }
void DoAddChildElement() { }
bool CanAddChildElement()
{
return SelectedElement is ControlElementViewModel;
}
void DoDeleteElement() { }
bool CanDeleteElement() { return _selectedElement != null; }
void DoCopyElement() { }
bool CanCopyElement() { return _selectedElement != null; }
/// <summary>
/// Sets or unsets the SelectedElement property whenever the IsSelected
/// property of an ElementViewModel changes.
/// </summary>
void ElementViewModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
ElementViewModel vm = sender as ElementViewModel;
if (vm != null && e.PropertyName == "IsSelected")
{
SelectedElement = vm.IsSelected ? vm : null;
}
}
#endregion
#region Implementation of ViewModelBase
public override object RevealModelObject()
{
return _settings;
}
#endregion
#region Implementation of ICloneable
public object Clone()
{
return new SettingsViewModel(_settings.Clone() as Settings);
}
#endregion
#region Fields
Settings _settings;
DelegatingCommand _addElementCommand;
DelegatingCommand _addChildElementCommand;
DelegatingCommand _deleteElementCommand;
DelegatingCommand _copyElementCommand;
List<ElementViewModel> _elements;
ElementViewModel _selectedElement;
EnumProvider<ReferenceStyle> _referenceStyle;
#endregion
}
}