/* ElementViewModelBase.cs * part of zaaReloaded2 * * Copyright 2015-2018 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.Linq; using zaaReloaded2.Controller.Elements; using System.Collections.ObjectModel; namespace zaaReloaded2.ViewModels { public class ControlElementViewModel : ElementViewModel { #region Properties /// /// Gets a collection of child ElementViewModels. /// public ObservableCollection Elements { get; protected set; } /// /// Gets information whether this control element view model can /// have child elements; /// public virtual bool CanHaveChildren { get { return ((ControlElementBase)Element).CanHaveChildren; } } public override string ToolTip { get { return Element.Label; } } #endregion #region Constructors public ControlElementViewModel() : this(null) { } public ControlElementViewModel(ControlElementBase controlElement) : base(controlElement) { Elements = new ObservableCollection(); if (controlElement != null) { foreach (FormatElementBase childElement in controlElement.Children) { FormatElementViewModel childVM = new FormatElementViewModel(childElement); Elements.Add(childVM); } } } /// /// Creates a new instance by copying the Elements property from another /// ControlElementViewModel and the Children property from the other /// view model's model. /// /// Other ControlElementViewModel to copy /// from. public ControlElementViewModel( ControlElementBase controlElement, ControlElementViewModel copyFrom) : this(controlElement) { Elements = copyFrom.Elements; ((ControlElementBase)Element).Children = controlElement.Children; } #endregion #region Public methods public void AddChildElement(FormatElementViewModel viewModel) { Elements.Add(viewModel); viewModel.Parent = this; ControlElementBase e = Element as ControlElementBase; e.Children.Add(viewModel.RevealModelObject() as FormatElementBase); } public void RemoveChildElement(FormatElementViewModel viewModel) { if (!Elements.Contains(viewModel)) { throw new InvalidOperationException( "Cannot remove child view model that is not in the collection."); } Elements.Remove(viewModel); ((ControlElementBase)Element).Children .Remove(viewModel.RevealModelObject() as FormatElementBase); viewModel.Parent = null; } #endregion #region Overrides public override object Clone() { ControlElementViewModel clone = new ControlElementViewModel(); clone.Element = Element; clone.Elements = new ObservableCollection( Elements.Select(evm => evm.Clone() as ElementViewModel)); foreach (FormatElementViewModel evm in clone.Elements) { evm.Parent = clone; } return clone; } #endregion } }