#region License Information /* HeuristicLab * Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using HeuristicLab.Common; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; using System; using System.Collections.Generic; using System.Linq; namespace HeuristicLab.Core.Networks { [Item("ParameterizedPort", "An abstract base class for parameterized ports of optimization network nodes.")] [StorableClass] public abstract class ParameterizedPort : Port, IParameterizedPort { public override IEnumerable Children { get { return base.Children.Concat(Parameters.AsEnumerable()); } } [Storable] private PortParameterCollection parameters; protected PortParameterCollection Parameters { get { return parameters; } } private ReadOnlyKeyedItemCollection readOnlyParameters; IKeyedItemCollection IParameterizedPort.Parameters { get { if (readOnlyParameters == null) readOnlyParameters = parameters.AsReadOnly(); return readOnlyParameters; } } [StorableConstructor] protected ParameterizedPort(bool deserializing) : base(deserializing) { } protected ParameterizedPort(ParameterizedPort original, Cloner cloner) : base(original, cloner) { parameters = cloner.Clone(original.parameters); foreach (var p in Parameters) p.Parent = this; readOnlyParameters = null; RegisterParametersEvents(); } protected ParameterizedPort() : base("ParameterizedPort") { parameters = new PortParameterCollection(); readOnlyParameters = null; RegisterParametersEvents(); } protected ParameterizedPort(string name) : base(name) { parameters = new PortParameterCollection(); readOnlyParameters = null; RegisterParametersEvents(); } protected ParameterizedPort(string name, string description) : base(name, description) { parameters = new PortParameterCollection(); readOnlyParameters = null; RegisterParametersEvents(); } [StorableHook(HookType.AfterDeserialization)] private void AfterDeserialization() { foreach (var p in Parameters) p.Parent = this; RegisterParametersEvents(); } public event EventHandler InterfaceChanged; protected virtual void OnInterfaceChanged() { var handler = InterfaceChanged; if (handler != null) handler(this, EventArgs.Empty); } #region Parameters Events protected virtual void RegisterParametersEvents() { parameters.ItemsAdded += Parameters_ItemsAdded; parameters.ItemsRemoved += Parameters_ItemsRemoved; parameters.ItemsReplaced += Parameters_ItemsReplaced; parameters.CollectionReset += Parameters_CollectionReset; foreach (var p in parameters) RegisterParameterEvents(p); } protected virtual void Parameters_ItemsAdded(object sender, Collections.CollectionItemsChangedEventArgs e) { foreach (var p in e.Items) { p.Parent = this; RegisterParameterEvents(p); } OnInterfaceChanged(); } protected virtual void Parameters_ItemsRemoved(object sender, Collections.CollectionItemsChangedEventArgs e) { foreach (var p in e.Items) { p.Parent = null; DeregisterParameterEvents(p); } OnInterfaceChanged(); } protected virtual void Parameters_ItemsReplaced(object sender, Collections.CollectionItemsChangedEventArgs e) { foreach (var p in e.OldItems) { p.Parent = null; DeregisterParameterEvents(p); } foreach (var p in e.Items) { p.Parent = this; RegisterParameterEvents(p); } OnInterfaceChanged(); } protected virtual void Parameters_CollectionReset(object sender, Collections.CollectionItemsChangedEventArgs e) { foreach (var p in e.OldItems) { p.Parent = null; DeregisterParameterEvents(p); } foreach (var p in e.Items) { p.Parent = this; RegisterParameterEvents(p); } OnInterfaceChanged(); } #endregion #region Parameter Events protected virtual void RegisterParameterEvents(IPortParameter parameter) { parameter.NameChanged += Parameter_NameChanged; parameter.TypeChanged += Parameter_TypeChanged; } protected virtual void DeregisterParameterEvents(IPortParameter parameter) { parameter.NameChanged -= Parameter_NameChanged; parameter.TypeChanged -= Parameter_TypeChanged; } protected virtual void Parameter_NameChanged(object sender, EventArgs e) { OnInterfaceChanged(); } protected virtual void Parameter_TypeChanged(object sender, EventArgs e) { OnInterfaceChanged(); } #endregion } }