Make some classes ICloneable; implement SettingsRepositoryViewModel.

This commit is contained in:
Daniel Kraus 2015-08-01 21:23:56 +02:00
parent 5197c294b9
commit d0be7524d3
15 changed files with 478 additions and 67 deletions

View File

@ -56,27 +56,5 @@ namespace Tests.Controller
Assert.AreEqual(1, sr.SettingsList.Count); Assert.AreEqual(1, sr.SettingsList.Count);
Assert.AreEqual(testName, sr.SettingsList[0].Name); Assert.AreEqual(testName, sr.SettingsList[0].Name);
} }
[Test]
public void CreateDefaultSettings()
{
SettingsRepository sr = new SettingsRepository();
sr.SettingsList.Add(new Settings("test1", null));
sr.SettingsList.Add(new Settings("test2", null));
sr.SettingsList.Add(new Settings("test3", null));
sr.ResetDefault();
// Assert that there are now only the 2 default settings
Assert.AreEqual(2, sr.SettingsList.Count);
Assert.AreEqual(
zaaReloaded2.Properties.Settings.Default.SettingsNameWard,
sr.SettingsList[0].Name,
"Settings for ward expected as #1 (index 0) in list");
Assert.AreEqual(
zaaReloaded2.Properties.Settings.Default.SettingsNameClinic,
sr.SettingsList[1].Name,
"Settings for outpatient clinic expected as #2 (index 1) in list");
}
} }
} }

View File

@ -91,7 +91,7 @@
<Compile Include="Thesaurus\TestThesaurus.cs" /> <Compile Include="Thesaurus\TestThesaurus.cs" />
<Compile Include="Importer\ZaaImporter\TimePointTest.cs" /> <Compile Include="Importer\ZaaImporter\TimePointTest.cs" />
<Compile Include="TestHelpers.cs" /> <Compile Include="TestHelpers.cs" />
<Compile Include="ViewModels\SettingsRepositoryViewModel.cs" /> <Compile Include="ViewModels\SettingsRepositoryViewModelTest.cs" />
<Compile Include="ViewModels\SettingsViewModelTest.cs" /> <Compile Include="ViewModels\SettingsViewModelTest.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@ -1,36 +0,0 @@
/* SettingsRepositoryViewModel.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.Linq;
using System.Text;
using NUnit.Framework;
using zaaReloaded2.Controller;
namespace Tests.ViewModels
{
[TestFixture]
class SettingsRepositoryViewModel
{
[Test]
public void CannotDeleteDefaultSettings()
{
}
}
}

View File

@ -0,0 +1,71 @@
/* SettingsRepositoryViewModel.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.Linq;
using System.Text;
using NUnit.Framework;
using zaaReloaded2.Controller;
using zaaReloaded2.ViewModels;
namespace Tests.ViewModels
{
[TestFixture]
class SettingsRepositoryViewModelTest
{
SettingsRepositoryViewModel _vm;
const string TESTNAME = "hello world";
[SetUp]
public void SetUp()
{
SettingsRepository rep = new SettingsRepository();
rep.SettingsList.Add(new Settings(TESTNAME));
_vm = new SettingsRepositoryViewModel(rep);
}
[Test]
public void CanDeleteNonDefaultSettings()
{
_vm.SettingsList[_vm.SettingsList.Count - 1].IsSelected = true;
Assert.IsTrue(_vm.DeleteSettingsCommand.CanExecute(null));
}
[Test]
public void CannotDeleteDefaultSettings()
{
_vm.SettingsList[0].IsSelected = true;
Assert.IsFalse(_vm.DeleteSettingsCommand.CanExecute(null));
}
[Test]
public void CopySettings()
{
SettingsViewModel orig = _vm.SettingsList[0];
orig.IsSelected = true;
_vm.CopySettingsCommand.Execute(null);
SettingsViewModel copy = _vm.SettingsList[_vm.SettingsList.Count-1];
// Make sure we have a new object
Assert.AreNotSame(orig, copy);
Assert.AreNotSame(orig.RevealModelObject(), copy.RevealModelObject());
Assert.AreEqual(String.Format("Kopie von {0}", orig.Name), copy.Name);
}
}
}

View File

@ -36,5 +36,12 @@ namespace zaaReloaded2.Controller.Elements
{ {
formatter.WriteParagraph(Content); formatter.WriteParagraph(Content);
} }
protected override ElementBase CreateInstance()
{
CustomText clone = new CustomText();
clone.Content = Content;
return clone;
}
} }
} }

View File

@ -28,7 +28,7 @@ namespace zaaReloaded2.Controller.Elements
/// Base class for formatting elements. /// Base class for formatting elements.
/// </summary> /// </summary>
[Serializable] [Serializable]
public abstract class ElementBase public abstract class ElementBase : ICloneable
{ {
/// <summary> /// <summary>
/// Returns the label for this formatting element. /// Returns the label for this formatting element.
@ -44,5 +44,16 @@ namespace zaaReloaded2.Controller.Elements
/// Element belongs to. The Formatter object provides access /// Element belongs to. The Formatter object provides access
/// to the current Word document etc.</param> /// to the current Word document etc.</param>
abstract public void Run(zaaReloaded2.Formatter.Formatter formatter); abstract public void Run(zaaReloaded2.Formatter.Formatter formatter);
public object Clone()
{
return CreateInstance();
}
/// <summary>
/// Creates a new instance that can be used for cloning.
/// </summary>
/// <returns>New instance of a derived class.</returns>
protected abstract ElementBase CreateInstance();
} }
} }

View File

@ -43,5 +43,10 @@ namespace zaaReloaded2.Controller.Elements
public SelectEachDay(IList<FormatElementBase> formatElements) public SelectEachDay(IList<FormatElementBase> formatElements)
: base(formatElements) : base(formatElements)
{ } { }
protected override ElementBase CreateInstance()
{
return new SelectEachDay();
}
} }
} }

View File

@ -47,5 +47,10 @@ namespace zaaReloaded2.Controller.Elements
public SelectFirstDay(IList<FormatElementBase> formatElements) public SelectFirstDay(IList<FormatElementBase> formatElements)
: base(formatElements) : base(formatElements)
{ } { }
protected override ElementBase CreateInstance()
{
return new SelectFirstDay();
}
} }
} }

View File

@ -48,5 +48,10 @@ namespace zaaReloaded2.Controller.Elements
public SelectLastDay(IList<FormatElementBase> formatElements) public SelectLastDay(IList<FormatElementBase> formatElements)
: base(formatElements) : base(formatElements)
{ } { }
protected override ElementBase CreateInstance()
{
return new SelectLastDay();
}
} }
} }

View File

@ -28,7 +28,7 @@ namespace zaaReloaded2.Controller
/// Holds settings related to controlling laboratory output. /// Holds settings related to controlling laboratory output.
/// </summary> /// </summary>
[Serializable] [Serializable]
public class Settings public class Settings : ICloneable
{ {
#region Properties #region Properties
@ -49,13 +49,19 @@ namespace zaaReloaded2.Controller
#endregion #endregion
#region Constructor #region Constructors
public Settings() public Settings()
{ {
Elements = new List<ElementBase>(); Elements = new List<ElementBase>();
} }
public Settings(string name)
: this()
{
Name = name;
}
/// <summary> /// <summary>
/// Creates a new Settings object with an initial /// Creates a new Settings object with an initial
/// set of elements. /// set of elements.
@ -94,5 +100,19 @@ namespace zaaReloaded2.Controller
} }
#endregion #endregion
#region Implementation of ICloneable
public object Clone()
{
Settings clone = new Settings(
String.Format("Kopie von {0}", Name),
Elements.Select(e => e.Clone() as ElementBase).ToList()
);
clone.ReferenceStyle = ReferenceStyle;
return clone;
}
#endregion
} }
} }

View File

@ -62,22 +62,27 @@ namespace zaaReloaded2.Controller
#endregion #endregion
#region Constructor #region Constructors
/// <summary>
/// Creates a new instance of a settings repository that will
/// contain a default set of settings.
/// </summary>
public SettingsRepository() public SettingsRepository()
{ {
SettingsList = new List<Settings>(); SettingsList = new List<Settings>();
CreateDefault();
} }
#endregion #endregion
#region Public methods #region Private methods
/// <summary> /// <summary>
/// Resets the Settings contained in this SettingsRepository /// Resets the Settings contained in this SettingsRepository
/// to the default set of settings. /// to the default set of settings.
/// </summary> /// </summary>
public void ResetDefault() private void CreateDefault()
{ {
SettingsList.Clear(); SettingsList.Clear();

View File

@ -26,7 +26,7 @@ using System.Collections.ObjectModel;
namespace zaaReloaded2.ViewModels namespace zaaReloaded2.ViewModels
{ {
abstract class ElementViewModel : ViewModelBase public abstract class ElementViewModel : ViewModelBase
{ {
#region Properties #region Properties

View File

@ -0,0 +1,330 @@
/* SettingsRepositoryViewModel.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.Linq;
using System.Text;
using Bovender.Mvvm;
using Bovender.Mvvm.ViewModels;
using Bovender.Mvvm.Messaging;
using zaaReloaded2.Controller;
namespace zaaReloaded2.ViewModels
{
public class SettingsRepositoryViewModel : ViewModelBase
{
#region Properties
public IList<SettingsViewModel> SettingsList { get; private set; }
public SettingsViewModel Selected { get; private set; }
#endregion
#region Commands
public DelegatingCommand AddSettingsCommand
{
get
{
if (_addSettingsCommand == null)
{
_addSettingsCommand = new DelegatingCommand(
param => DoAddSettings());
}
return _addSettingsCommand;
}
}
public DelegatingCommand EditSettingsCommand
{
get
{
if (_editSettingsCommand == null)
{
_editSettingsCommand = new DelegatingCommand(
param => DoEditSettings(),
param => CanEditSettings());
}
return _editSettingsCommand;
}
}
public DelegatingCommand UseSettingsCommand
{
get
{
if (_useSettingsCommand == null)
{
_useSettingsCommand = new DelegatingCommand(
param => DoUseSettings(),
param => CanUseSettings());
}
return _useSettingsCommand;
}
}
public DelegatingCommand DeleteSettingsCommand
{
get
{
if (_deleteSettingsCommand == null)
{
_deleteSettingsCommand = new DelegatingCommand(
param => DoDeleteSettings(),
param => CanDeleteSettings());
}
return _deleteSettingsCommand;
}
}
public DelegatingCommand CopySettingsCommand
{
get
{
if (_copySettingsCommand == null)
{
_copySettingsCommand = new DelegatingCommand(
param => DoCopySettings());
}
return _copySettingsCommand;
}
}
public DelegatingCommand ResetSettingsCommand
{
get
{
if (_resetSettingsCommand == null)
{
_resetSettingsCommand = new DelegatingCommand(
param => DoResetSettings());
}
return _resetSettingsCommand;
}
}
#endregion
#region Messages
Message<ViewModelMessageContent> EditSettingsMessage
{
get
{
if (_editSettingsMessage == null)
{
_editSettingsMessage = new Message<ViewModelMessageContent>();
}
return _editSettingsMessage;
}
}
Message<ViewModelMessageContent> UseSettingsMessage
{
get
{
if (_useSettingsMessage == null)
{
_useSettingsMessage = new Message<ViewModelMessageContent>();
}
return _useSettingsMessage;
}
}
Message<ViewModelMessageContent> ConfirmDeleteSettingsMessage
{
get
{
if (_confirmDeleteSettingsMessage == null)
{
_confirmDeleteSettingsMessage = new Message<ViewModelMessageContent>();
}
return _confirmDeleteSettingsMessage;
}
}
Message<ViewModelMessageContent> ConfirmResetSettingsMessage
{
get
{
if (_confirmResetSettingsMessage == null)
{
_confirmResetSettingsMessage = new Message<ViewModelMessageContent>();
}
return _confirmResetSettingsMessage;
}
}
#endregion
#region Constructors
public SettingsRepositoryViewModel(SettingsRepository repository)
{
_repository = repository;
SettingsList = new List<SettingsViewModel>();
foreach (Settings s in repository.SettingsList)
{
SettingsViewModel vm = new SettingsViewModel(s);
vm.PropertyChanged += SettingsViewModel_PropertyChanged;
SettingsList.Add(vm);
}
}
#endregion
#region Private methods
void DoEditSettings()
{
if (CanEditSettings())
{
EditSettingsMessage.Send(new ViewModelMessageContent(Selected));
}
}
bool CanEditSettings()
{
return Selected != null && !IsDefaultSettings();
}
void DoUseSettings()
{
UseSettingsMessage.Send(new ViewModelMessageContent(Selected));
}
bool CanUseSettings()
{
return Selected != null;
}
void DoAddSettings()
{
Settings s = new Settings("Neu");
SettingsViewModel vm = new SettingsViewModel(s);
_repository.SettingsList.Add(s);
SettingsList.Add(vm);
vm.IsSelected = true;
}
bool CanDeleteSettings()
{
return Selected != null && !IsDefaultSettings();
}
void DoDeleteSettings()
{
if (CanDeleteSettings())
{
ConfirmDeleteSettingsMessage.Send(
new ViewModelMessageContent(Selected),
param => ConfirmDeleteSettings(param));
}
}
void ConfirmDeleteSettings(ViewModelMessageContent content)
{
SettingsViewModel vm = content.ViewModel as SettingsViewModel;
if (CanDeleteSettings() && content.Confirmed)
{
_repository.SettingsList.Remove(vm.RevealModelObject() as Settings);
SettingsList.Remove(vm);
}
}
void DoResetSettings()
{
ConfirmResetSettingsMessage.Send(
new ViewModelMessageContent(this),
param => ConfirmResetSettings(param));
}
void ConfirmResetSettings(ViewModelMessageContent content)
{
if (content.Confirmed)
{
}
}
void DoCopySettings()
{
if (Selected != null)
{
SettingsViewModel copy = Selected.Clone() as SettingsViewModel;
SettingsList.Add(copy);
_repository.SettingsList.Add(copy.RevealModelObject() as Settings);
}
}
void SettingsViewModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
SettingsViewModel vm = sender as SettingsViewModel;
if (vm != null && e.PropertyName == "IsSelected")
{
Selected = vm.IsSelected ? vm : null;
}
}
/// <summary>
/// Determines whether the selected SettingsViewModel belongs
/// to one of the default settings.
/// </summary>
bool IsDefaultSettings()
{
if (Selected != null)
{
return
(Selected.Name == zaaReloaded2.Properties.Settings.Default.SettingsNameClinic
|| Selected.Name == zaaReloaded2.Properties.Settings.Default.SettingsNameWard);
}
else
{
return false;
}
}
#endregion
#region Implementation of ViewModelBase
public override object RevealModelObject()
{
return _repository;
}
#endregion
#region Fields
SettingsRepository _repository;
DelegatingCommand _useSettingsCommand;
DelegatingCommand _addSettingsCommand;
DelegatingCommand _editSettingsCommand;
DelegatingCommand _deleteSettingsCommand;
DelegatingCommand _resetSettingsCommand;
DelegatingCommand _copySettingsCommand;
Message<ViewModelMessageContent> _confirmDeleteSettingsMessage;
Message<ViewModelMessageContent> _confirmResetSettingsMessage;
Message<ViewModelMessageContent> _editSettingsMessage;
Message<ViewModelMessageContent> _useSettingsMessage;
#endregion
}
}

View File

@ -31,7 +31,7 @@ namespace zaaReloaded2.ViewModels
/// <summary> /// <summary>
/// View model for the zaaReloaded2.Controller.Settings class. /// View model for the zaaReloaded2.Controller.Settings class.
/// </summary> /// </summary>
class SettingsViewModel : ViewModelBase public class SettingsViewModel : ViewModelBase, ICloneable
{ {
#region Properties #region Properties
@ -238,6 +238,15 @@ namespace zaaReloaded2.ViewModels
#endregion #endregion
#region Implementation of ICloneable
public object Clone()
{
return new SettingsViewModel(_settings.Clone() as Settings);
}
#endregion
#region Fields #region Fields
Settings _settings; Settings _settings;

View File

@ -222,6 +222,7 @@
<Compile Include="ViewModels\ElementViewModel.cs" /> <Compile Include="ViewModels\ElementViewModel.cs" />
<Compile Include="ViewModels\FormatElementViewModel.cs" /> <Compile Include="ViewModels\FormatElementViewModel.cs" />
<Compile Include="ViewModels\ControlElementViewModel.cs" /> <Compile Include="ViewModels\ControlElementViewModel.cs" />
<Compile Include="ViewModels\SettingsRepositoryViewModel.cs" />
<Compile Include="Views\AboutView.xaml.cs"> <Compile Include="Views\AboutView.xaml.cs">
<DependentUpon>AboutView.xaml</DependentUpon> <DependentUpon>AboutView.xaml</DependentUpon>
</Compile> </Compile>