Implement ElementPickerViewModel; initial SettingsViewModelTest.

This commit is contained in:
Daniel Kraus 2015-08-02 21:51:58 +02:00
parent 9fd0907310
commit 1e4c3681f2
6 changed files with 230 additions and 12 deletions

View File

@ -22,6 +22,7 @@ using System.Text;
using NUnit.Framework;
using zaaReloaded2.ViewModels;
using zaaReloaded2.Controller;
using zaaReloaded2.Controller.Elements;
namespace Tests.ViewModels
{
@ -51,7 +52,16 @@ namespace Tests.ViewModels
[Test]
public void CannotAddChildElementToFormatElement()
{
ControlElementViewModel parent = new ControlElementViewModel(
new SelectFirstDay());
_settingsVM.AddElementViewModel(parent);
parent.IsSelected = true;
Assert.IsTrue(_settingsVM.AddChildElementCommand.CanExecute(null));
FormatElementViewModel child = new FormatElementViewModel(new Items());
parent.AddChildElement(child);
parent.IsSelected = false;
child.IsSelected = true;
Assert.IsFalse(_settingsVM.AddChildElementCommand.CanExecute(null));
}
[Test]

View File

@ -0,0 +1,46 @@
/* CategoryViewModel.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.ViewModels;
namespace zaaReloaded2.ViewModels
{
/// <summary>
/// A simple view model that can be used to categorize items
/// in a tree view. It has a custom DisplayString and holds
/// a collection of child view model items.
/// </summary>
class CategoryViewModel : ViewModelBase
{
public IEnumerable<ViewModelBase> Children { get; private set; }
public CategoryViewModel(string name, IEnumerable<ViewModelBase> children)
{
DisplayString = name;
Children = children;
}
public override object RevealModelObject()
{
throw new NotImplementedException();
}
}
}

View File

@ -46,5 +46,16 @@ namespace zaaReloaded2.ViewModels
{ }
#endregion
#region Public methods
public void AddChildElement(FormatElementViewModel viewModel)
{
Elements.Add(viewModel);
ControlElementBase e = Element as ControlElementBase;
e.FormatElements.Add(viewModel.RevealModelObject() as FormatElementBase);
}
#endregion
}
}

View File

@ -0,0 +1,122 @@
/* ElementPickerViewModel.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.Messaging;
using Bovender.Mvvm.ViewModels;
using System.Collections.ObjectModel;
using zaaReloaded2.Controller.Elements;
namespace zaaReloaded2.ViewModels
{
/// <summary>
/// View model that presents a list of zaaReloaded2.Contorller.Elements.ElementBase
/// classes to choose from.
/// </summary>
class ElementPickerViewModel : ViewModelBase
{
#region Properties
/// <summary>
/// A two-dimensional tree
/// </summary>
public IList<CategoryViewModel> AvailableElements { get; private set; }
#endregion
#region Constructors
public ElementPickerViewModel(SettingsViewModel caller, bool allowControlElements)
{
_caller = caller;
AvailableElements = new List<CategoryViewModel>();
if (allowControlElements)
{
AvailableElements.Add(
new CategoryViewModel(
"Kontroll-Elemente",
new Collection<ViewModelBase>()
{
CreateControlElementViewModel(new SelectFirstDay()),
CreateControlElementViewModel(new SelectLastDay()),
CreateControlElementViewModel(new SelectEachDay())
}
)
);
}
AvailableElements.Add(
new CategoryViewModel(
"Ausgabe-Elemente",
new Collection<ViewModelBase>()
{
CreateFormatElementViewModel("Laborparameter", new Items()),
CreateFormatElementViewModel("Beliebiger Text", new CustomText()),
}
)
);
}
#endregion
#region Private methods
/// <summary>
/// Creates a new ControlElementViewModel that wraps a ControlElementBase
/// object. The display string of the ControlElementViewModel is taken
/// from the canonical Label of the control element.
/// </summary>
ViewModelBase CreateControlElementViewModel(ControlElementBase element)
{
ControlElementViewModel vm = new ControlElementViewModel(element);
vm.DisplayString = element.Label;
return vm;
}
/// <summary>
/// Creates a new FormatElementViewModel that wraps a FormatElementBase
/// object and has a custom display string. The custom display string
/// is necessary because format elements do not have a canonical label.
/// </summary>
ViewModelBase CreateFormatElementViewModel(string name, FormatElementBase element)
{
FormatElementViewModel vm = new FormatElementViewModel(element);
vm.DisplayString = name;
return vm;
}
#endregion
#region Implementation ov ViewModelBase
public override object RevealModelObject()
{
throw new NotImplementedException();
}
#endregion
#region Fields
SettingsViewModel _caller;
#endregion
}
}

View File

@ -22,6 +22,7 @@ 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;
@ -161,6 +162,20 @@ namespace zaaReloaded2.ViewModels
}
}
public DelegatingCommand AddChildElementCommand
{
get
{
if (_addChildElementCommand == null)
{
_addChildElementCommand = new DelegatingCommand(
param => DoAddChildElement(),
param => CanAddChildElement());
}
return _addChildElementCommand;
}
}
public DelegatingCommand DeleteElementCommand
{
get
@ -191,10 +206,31 @@ namespace zaaReloaded2.ViewModels
#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);
}
#endregion
#region Private methods
void DoAddElement() { }
void DoAddChildElement() { }
bool CanAddChildElement()
{
return SelectedElement is ControlElementViewModel;
}
void DoDeleteElement() { }
bool CanDeleteElement() { return _selectedElement != null; }
@ -203,17 +239,6 @@ namespace zaaReloaded2.ViewModels
bool CanCopyElement() { return _selectedElement != null; }
/// <summary>
/// Internal function that creates wires the OnProperty changed event
/// of an ElementViewModel's wrapped model and adds the view model
/// to the Elements collection.
/// </summary>
void AddElementViewModel(ElementViewModel elementViewModel)
{
elementViewModel.PropertyChanged += ElementViewModel_PropertyChanged;
Elements.Add(elementViewModel);
}
/// <summary>
/// Sets or unsets the SelectedElement property whenever the IsSelected
/// property of an ElementViewModel changes.
@ -251,8 +276,10 @@ namespace zaaReloaded2.ViewModels
Settings _settings;
DelegatingCommand _addElementCommand;
DelegatingCommand _addChildElementCommand;
DelegatingCommand _deleteElementCommand;
DelegatingCommand _copyElementCommand;
List<ElementViewModel> _elements;
ElementViewModel _selectedElement;
EnumProvider<ReferenceStyle> _referenceStyle;

View File

@ -219,6 +219,8 @@
</Compile>
<Compile Include="Updater\Updater.cs" />
<Compile Include="ViewModels\AboutViewModel.cs" />
<Compile Include="ViewModels\CategoryViewModel.cs" />
<Compile Include="ViewModels\ElementPickerViewModel.cs" />
<Compile Include="ViewModels\ElementViewModel.cs" />
<Compile Include="ViewModels\FormatElementViewModel.cs" />
<Compile Include="ViewModels\ControlElementViewModel.cs" />