Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OptimizationNetworks/HeuristicLab.Networks/3.3/### obsolete/ClientServicePort.cs @ 13077

Last change on this file since 13077 was 11500, checked in by swagner, 10 years ago

#2205: Worked on optimization networks

File size: 5.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using HeuristicLab.Common;
23using HeuristicLab.Core;
24using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
25using System;
26using System.Collections.Generic;
27using System.Linq;
28
29namespace HeuristicLab.Optimization.Networks {
30  [Item("ClientServicePort", "An abstract base class for service and client ports of optimization network nodes.")]
31  [StorableClass]
32  public abstract class ClientServicePort : Port, IClientServicePort {
33    public override IEnumerable<IEntity> Children {
34      get { return base.Children.Concat(Parameters.AsEnumerable<IEntity>()); }
35    }
36
37    [Storable]
38    protected ServiceParameterCollection parameters;
39    public ServiceParameterCollection Parameters {
40      get { return parameters; }
41    }
42
43    [StorableConstructor]
44    protected ClientServicePort(bool deserializing) : base(deserializing) { }
45    protected ClientServicePort(ClientServicePort original, Cloner cloner)
46      : base(original, cloner) {
47      parameters = cloner.Clone(original.parameters);
48      foreach (var p in Parameters)
49        p.Parent = this;
50      RegisterParametersEvents();
51    }
52    protected ClientServicePort()
53      : base("ClientServicePort") {
54      parameters = new ServiceParameterCollection();
55      RegisterParametersEvents();
56    }
57    protected ClientServicePort(string name)
58      : base(name) {
59      parameters = new ServiceParameterCollection();
60      RegisterParametersEvents();
61    }
62    protected ClientServicePort(string name, string description)
63      : base(name, description) {
64      parameters = new ServiceParameterCollection();
65      RegisterParametersEvents();
66    }
67
68    [StorableHook(HookType.AfterDeserialization)]
69    private void AfterDeserialization() {
70      foreach (var p in Parameters)
71        p.Parent = this;
72      RegisterParametersEvents();
73    }
74
75    public event EventHandler InterfaceChanged;
76    protected virtual void OnInterfaceChanged() {
77      var handler = InterfaceChanged;
78      if (handler != null) handler(this, EventArgs.Empty);
79    }
80
81    #region Parameters Events
82    protected virtual void RegisterParametersEvents() {
83      parameters.ItemsAdded += Parameters_ItemsAdded;
84      parameters.ItemsRemoved += Parameters_ItemsRemoved;
85      parameters.ItemsReplaced += Parameters_ItemsReplaced;
86      parameters.CollectionReset += Parameters_CollectionReset;
87      foreach (var p in parameters)
88        RegisterParameterEvents(p);
89    }
90    protected virtual void Parameters_ItemsAdded(object sender, Collections.CollectionItemsChangedEventArgs<IServiceParameter> e) {
91      foreach (var p in e.Items) {
92        p.Parent = this;
93        RegisterParameterEvents(p);
94      }
95      OnInterfaceChanged();
96    }
97    protected virtual void Parameters_ItemsRemoved(object sender, Collections.CollectionItemsChangedEventArgs<IServiceParameter> e) {
98      foreach (var p in e.Items) {
99        p.Parent = null;
100        DeregisterParameterEvents(p);
101      }
102      OnInterfaceChanged();
103    }
104    protected virtual void Parameters_ItemsReplaced(object sender, Collections.CollectionItemsChangedEventArgs<IServiceParameter> e) {
105      foreach (var p in e.OldItems) {
106        p.Parent = null;
107        DeregisterParameterEvents(p);
108      }
109      foreach (var p in e.Items) {
110        p.Parent = this;
111        RegisterParameterEvents(p);
112      }
113      OnInterfaceChanged();
114    }
115    protected virtual void Parameters_CollectionReset(object sender, Collections.CollectionItemsChangedEventArgs<IServiceParameter> e) {
116      foreach (var p in e.OldItems) {
117        p.Parent = null;
118        DeregisterParameterEvents(p);
119      }
120      foreach (var p in e.Items) {
121        p.Parent = this;
122        RegisterParameterEvents(p);
123      }
124      OnInterfaceChanged();
125    }
126    #endregion
127
128    #region Parameter Events
129    protected virtual void RegisterParameterEvents(IServiceParameter parameter) {
130      parameter.NameChanged += ServiceParameter_NameChanged;
131      parameter.TypeChanged += ServiceParameter_TypeChanged;
132    }
133    protected virtual void DeregisterParameterEvents(IServiceParameter parameter) {
134      parameter.NameChanged -= ServiceParameter_NameChanged;
135      parameter.TypeChanged -= ServiceParameter_TypeChanged;
136    }
137    protected virtual void ServiceParameter_NameChanged(object sender, EventArgs e) {
138      OnInterfaceChanged();
139    }
140    protected virtual void ServiceParameter_TypeChanged(object sender, EventArgs e) {
141      OnInterfaceChanged();
142    }
143    #endregion
144  }
145}
Note: See TracBrowser for help on using the repository browser.