[2853] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[3742] | 3 | * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[2853] | 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
[4068] | 24 | using System.Drawing;
|
---|
[2853] | 25 | using System.Linq;
|
---|
[4068] | 26 | using HeuristicLab.Collections;
|
---|
| 27 | using HeuristicLab.Common;
|
---|
[2853] | 28 | using HeuristicLab.Core;
|
---|
[2934] | 29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[2853] | 30 |
|
---|
| 31 | namespace HeuristicLab.Operators.Views.GraphVisualization {
|
---|
[3017] | 32 | [StorableClass]
|
---|
[3386] | 33 | public sealed class OperatorGraphVisualizationInfo : GraphVisualizationInfo {
|
---|
| 34 | [Storable]
|
---|
[2934] | 35 | private BidirectionalLookup<IOperator, IOperatorShapeInfo> operatorShapeInfoMapping;
|
---|
[3393] | 36 | private BidirectionalLookup<IOperator, IKeyedItemCollection<string, IParameter>> operatorParameterCollectionMapping;
|
---|
[2934] | 37 | private Dictionary<IParameter, IOperator> parameterOperatorMapping;
|
---|
[2853] | 38 |
|
---|
[3386] | 39 | private OperatorGraphVisualizationInfo()
|
---|
| 40 | : base() {
|
---|
[2934] | 41 | this.operatorShapeInfoMapping = new BidirectionalLookup<IOperator, IOperatorShapeInfo>();
|
---|
[3393] | 42 | this.operatorParameterCollectionMapping = new BidirectionalLookup<IOperator, IKeyedItemCollection<string, IParameter>>();
|
---|
[2934] | 43 | this.parameterOperatorMapping = new Dictionary<IParameter, IOperator>();
|
---|
[3386] | 44 | }
|
---|
[2861] | 45 |
|
---|
[3386] | 46 | [StorableConstructor]
|
---|
| 47 | private OperatorGraphVisualizationInfo(bool deserializing)
|
---|
[4722] | 48 | : base(deserializing) {
|
---|
[3393] | 49 | this.operatorParameterCollectionMapping = new BidirectionalLookup<IOperator, IKeyedItemCollection<string, IParameter>>();
|
---|
[3386] | 50 | this.parameterOperatorMapping = new Dictionary<IParameter, IOperator>();
|
---|
[2861] | 51 | }
|
---|
[4722] | 52 | private OperatorGraphVisualizationInfo(OperatorGraphVisualizationInfo original, Cloner cloner)
|
---|
| 53 | : base(original, cloner) {
|
---|
| 54 | operatorShapeInfoMapping = new BidirectionalLookup<IOperator, IOperatorShapeInfo>();
|
---|
| 55 | operatorParameterCollectionMapping = new BidirectionalLookup<IOperator, IKeyedItemCollection<string, IParameter>>();
|
---|
| 56 | parameterOperatorMapping = new Dictionary<IParameter, IOperator>();
|
---|
[2853] | 57 |
|
---|
[4722] | 58 | operatorGraph = cloner.Clone(original.operatorGraph);
|
---|
| 59 | RegisterOperatorGraphEvents();
|
---|
| 60 | oldInitialShape = cloner.Clone(original.oldInitialShape);
|
---|
| 61 | oldInitialShapeColor = original.oldInitialShapeColor;
|
---|
| 62 |
|
---|
| 63 | foreach (KeyValuePair<IOperator, IOperatorShapeInfo> pair in original.operatorShapeInfoMapping.FirstEnumerable) {
|
---|
| 64 | IOperator op = cloner.Clone(pair.Key);
|
---|
| 65 | IOperatorShapeInfo shapeInfo = cloner.Clone(pair.Value);
|
---|
| 66 | RegisterOperatorEvents(op);
|
---|
| 67 | operatorParameterCollectionMapping.Add(op, pair.Key.Parameters);
|
---|
| 68 | operatorShapeInfoMapping.Add(op, shapeInfo);
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | foreach (IOperator oper in operatorShapeInfoMapping.FirstValues) {
|
---|
| 72 | foreach (IParameter param in oper.Parameters) {
|
---|
| 73 | parameterOperatorMapping.Add(param, oper);
|
---|
| 74 | IValueParameter opParam = param as IValueParameter;
|
---|
| 75 | if (opParam != null && typeof(IOperator).IsAssignableFrom(param.DataType))
|
---|
| 76 | RegisterOperatorParameterEvents(opParam);
|
---|
| 77 | else
|
---|
| 78 | RegisterParameterEvents(param);
|
---|
| 79 | }
|
---|
| 80 | }
|
---|
| 81 | if (original.operatorGraph.InitialOperator != null) {
|
---|
| 82 | IOperatorShapeInfo newInitialShapeInfo = original.operatorShapeInfoMapping.GetByFirst(original.operatorGraph.InitialOperator);
|
---|
| 83 | if (newInitialShapeInfo != null) {
|
---|
| 84 | oldInitialShapeColor = newInitialShapeInfo.Color;
|
---|
| 85 | newInitialShapeInfo.Color = Color.LightGreen;
|
---|
| 86 | }
|
---|
| 87 | oldInitialShape = InitialShape;
|
---|
| 88 | OnInitialShapeChanged();
|
---|
| 89 | }
|
---|
| 90 | }
|
---|
| 91 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 92 | return new OperatorGraphVisualizationInfo(this, cloner);
|
---|
| 93 | }
|
---|
| 94 |
|
---|
[3386] | 95 | public OperatorGraphVisualizationInfo(OperatorGraph operatorGraph)
|
---|
[2861] | 96 | : this() {
|
---|
[3386] | 97 | this.operatorGraph = operatorGraph;
|
---|
| 98 | this.RegisterOperatorGraphEvents();
|
---|
[2875] | 99 |
|
---|
[2853] | 100 | foreach (IOperator op in operatorGraph.Operators)
|
---|
[3386] | 101 | if (!this.operatorShapeInfoMapping.ContainsFirst(op)) //could be added by referencing parameters
|
---|
[2868] | 102 | this.AddOperator(op);
|
---|
[2853] | 103 |
|
---|
[2875] | 104 | this.UpdateInitialShape();
|
---|
[2853] | 105 | }
|
---|
| 106 |
|
---|
[3386] | 107 | [StorableHook(HookType.AfterDeserialization)]
|
---|
[4722] | 108 | private void AfterDeserialization() {
|
---|
[3386] | 109 | this.operatorGraph.DeserializationFinished += new EventHandler(operatorGraph_DeserializationFinished);
|
---|
| 110 |
|
---|
| 111 | IOperator op;
|
---|
| 112 | IOperatorShapeInfo shapeInfo;
|
---|
| 113 | foreach (KeyValuePair<IOperator, IOperatorShapeInfo> pair in this.operatorShapeInfoMapping.FirstEnumerable) {
|
---|
| 114 | op = pair.Key;
|
---|
| 115 | shapeInfo = pair.Value;
|
---|
| 116 | shapeInfo.Icon = new Bitmap(op.ItemImage);
|
---|
| 117 | this.RegisterOperatorEvents(op);
|
---|
| 118 | this.operatorParameterCollectionMapping.Add(op, op.Parameters);
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | foreach (IOperator oper in this.operatorShapeInfoMapping.FirstValues) {
|
---|
| 122 | foreach (IParameter param in oper.Parameters) {
|
---|
[3422] | 123 | IValueParameter opParam = param as IValueParameter;
|
---|
[3386] | 124 | this.parameterOperatorMapping.Add(param, oper);
|
---|
[3422] | 125 | if (opParam != null && typeof(IOperator).IsAssignableFrom(param.DataType))
|
---|
[3386] | 126 | this.RegisterOperatorParameterEvents(opParam);
|
---|
| 127 | else
|
---|
| 128 | this.RegisterParameterEvents(param);
|
---|
| 129 | }
|
---|
| 130 | }
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 | private void operatorGraph_DeserializationFinished(object sender, EventArgs e) {
|
---|
| 134 | this.RegisterOperatorGraphEvents();
|
---|
| 135 | if (this.operatorGraph.InitialOperator != null) {
|
---|
| 136 | IOperatorShapeInfo newInitialShapeInfo = this.operatorShapeInfoMapping.GetByFirst(this.operatorGraph.InitialOperator);
|
---|
| 137 | if (newInitialShapeInfo != null) {
|
---|
| 138 | oldInitialShapeColor = newInitialShapeInfo.Color;
|
---|
| 139 | newInitialShapeInfo.Color = Color.LightGreen;
|
---|
| 140 | }
|
---|
| 141 | oldInitialShape = this.InitialShape;
|
---|
| 142 | this.OnInitialShapeChanged();
|
---|
| 143 | }
|
---|
| 144 | this.operatorGraph.DeserializationFinished -= new EventHandler(operatorGraph_DeserializationFinished);
|
---|
| 145 | }
|
---|
| 146 |
|
---|
| 147 | internal IOperator GetOperatorForShapeInfo(IOperatorShapeInfo shapeInfo) {
|
---|
| 148 | return this.operatorShapeInfoMapping.GetBySecond(shapeInfo);
|
---|
| 149 | }
|
---|
| 150 |
|
---|
[2861] | 151 | private void operatorGraph_InitialOperatorChanged(object sender, EventArgs e) {
|
---|
[2875] | 152 | this.UpdateInitialShape();
|
---|
| 153 | }
|
---|
| 154 |
|
---|
| 155 | private void UpdateInitialShape() {
|
---|
[2934] | 156 | IOperatorShapeInfo old = this.oldInitialShape as OperatorShapeInfo;
|
---|
[2875] | 157 | if (old != null)
|
---|
[2934] | 158 | old.Color = oldInitialShapeColor;
|
---|
[2875] | 159 |
|
---|
| 160 | OperatorShapeInfo newInitialShapeInfo = this.InitialShape as OperatorShapeInfo;
|
---|
| 161 | if (newInitialShapeInfo != null) {
|
---|
[2934] | 162 | oldInitialShapeColor = newInitialShapeInfo.Color;
|
---|
| 163 | newInitialShapeInfo.Color = Color.LightGreen;
|
---|
[2875] | 164 | }
|
---|
| 165 |
|
---|
| 166 | oldInitialShape = this.InitialShape;
|
---|
[3386] | 167 | this.OnInitialShapeChanged();
|
---|
[2861] | 168 | }
|
---|
| 169 |
|
---|
[3386] | 170 | private IShapeInfo oldInitialShape;
|
---|
[2875] | 171 | private Color oldInitialShapeColor;
|
---|
[3386] | 172 | public override IShapeInfo InitialShape {
|
---|
[2861] | 173 | get {
|
---|
| 174 | IOperator op = this.operatorGraph.InitialOperator;
|
---|
| 175 | if (op == null)
|
---|
| 176 | return null;
|
---|
[2934] | 177 | return this.operatorShapeInfoMapping.GetByFirst(op);
|
---|
[2861] | 178 | }
|
---|
[2893] | 179 | set {
|
---|
| 180 | if (value == null)
|
---|
| 181 | this.OperatorGraph.InitialOperator = null;
|
---|
[3386] | 182 | else {
|
---|
| 183 | IOperatorShapeInfo shapeInfo = (IOperatorShapeInfo)value;
|
---|
| 184 | this.OperatorGraph.InitialOperator = this.operatorShapeInfoMapping.GetBySecond(shapeInfo);
|
---|
| 185 | }
|
---|
[2893] | 186 | }
|
---|
[2861] | 187 | }
|
---|
| 188 |
|
---|
[3386] | 189 | [Storable]
|
---|
[2942] | 190 | private OperatorGraph operatorGraph;
|
---|
[2853] | 191 | public OperatorGraph OperatorGraph {
|
---|
| 192 | get { return this.operatorGraph; }
|
---|
| 193 | }
|
---|
| 194 |
|
---|
[3386] | 195 | private void RegisterOperatorGraphEvents() {
|
---|
| 196 | this.operatorGraph.InitialOperatorChanged += new EventHandler(operatorGraph_InitialOperatorChanged);
|
---|
| 197 | this.operatorGraph.Operators.ItemsAdded += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IOperator>(Operators_ItemsAdded);
|
---|
| 198 | this.operatorGraph.Operators.ItemsRemoved += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IOperator>(Operators_ItemsRemoved);
|
---|
| 199 | this.operatorGraph.Operators.CollectionReset += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IOperator>(Operators_CollectionReset);
|
---|
[2853] | 200 | }
|
---|
| 201 |
|
---|
[3386] | 202 | private void DeregisterOperatorGraphEvents() {
|
---|
| 203 | this.operatorGraph.InitialOperatorChanged -= new EventHandler(operatorGraph_InitialOperatorChanged);
|
---|
| 204 | this.operatorGraph.Operators.ItemsAdded -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IOperator>(Operators_ItemsAdded);
|
---|
| 205 | this.operatorGraph.Operators.ItemsRemoved -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IOperator>(Operators_ItemsRemoved);
|
---|
| 206 | this.operatorGraph.Operators.CollectionReset -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IOperator>(Operators_CollectionReset);
|
---|
[2868] | 207 | }
|
---|
| 208 |
|
---|
| 209 | #region methods to manipulate operatorgraph by the shape info
|
---|
[2934] | 210 | internal void AddShapeInfo(IOperator op, IOperatorShapeInfo shapeInfo) {
|
---|
[2853] | 211 | this.RegisterOperatorEvents(op);
|
---|
[2861] | 212 | this.operatorParameterCollectionMapping.Add(op, op.Parameters);
|
---|
[2934] | 213 | this.operatorShapeInfoMapping.Add(op, shapeInfo);
|
---|
[2853] | 214 | this.shapeInfos.Add(shapeInfo);
|
---|
| 215 |
|
---|
[2868] | 216 | foreach (IParameter param in op.Parameters)
|
---|
| 217 | this.AddParameter(op, param);
|
---|
| 218 |
|
---|
[2942] | 219 | this.operatorGraph.Operators.Add(op);
|
---|
[2853] | 220 | }
|
---|
| 221 |
|
---|
[3386] | 222 | public override void RemoveShapeInfo(IShapeInfo shapeInfo) {
|
---|
| 223 | IOperatorShapeInfo opShapeInfo = (IOperatorShapeInfo)shapeInfo;
|
---|
| 224 | if (this.operatorShapeInfoMapping.ContainsSecond(opShapeInfo)) {
|
---|
| 225 | IOperator op = this.operatorShapeInfoMapping.GetBySecond(opShapeInfo);
|
---|
| 226 | this.operatorGraph.Operators.Remove(op);
|
---|
| 227 | }
|
---|
[2853] | 228 | }
|
---|
| 229 |
|
---|
[3386] | 230 | public override void RemoveConnectionInfo(IConnectionInfo connectionInfo) {
|
---|
| 231 | IOperatorShapeInfo shapeInfo = (IOperatorShapeInfo)connectionInfo.From;
|
---|
| 232 | IOperator op = this.operatorShapeInfoMapping.GetBySecond(shapeInfo);
|
---|
[3422] | 233 | IValueParameter param = (IValueParameter)op.Parameters[connectionInfo.ConnectorFrom];
|
---|
[2868] | 234 | param.Value = null;
|
---|
| 235 | }
|
---|
[3422] | 236 |
|
---|
| 237 | public override void AddConnectionInfo(IConnectionInfo connectionInfo) {
|
---|
| 238 | IOperatorShapeInfo shapeInfo = (IOperatorShapeInfo)connectionInfo.From;
|
---|
| 239 | IOperator op = this.operatorShapeInfoMapping.GetBySecond(shapeInfo);
|
---|
| 240 | IOperatorShapeInfo shapeInfoTo = (IOperatorShapeInfo)connectionInfo.To;
|
---|
| 241 | IOperator opTo = this.operatorShapeInfoMapping.GetBySecond(shapeInfoTo);
|
---|
[3514] | 242 | IValueParameter param = (IValueParameter)op.Parameters.Where(p => p.Name == connectionInfo.ConnectorFrom).SingleOrDefault();
|
---|
| 243 | if (param != null)
|
---|
| 244 | param.Value = opTo;
|
---|
[3422] | 245 | }
|
---|
[2868] | 246 | #endregion
|
---|
| 247 |
|
---|
[2853] | 248 | #region operator events
|
---|
| 249 | private void AddOperator(IOperator op) {
|
---|
[2934] | 250 | if (!this.operatorShapeInfoMapping.ContainsFirst(op)) {
|
---|
[2868] | 251 | this.RegisterOperatorEvents(op);
|
---|
[3386] | 252 | IOperatorShapeInfo shapeInfo = OperatorShapeInfoFactory.CreateOperatorShapeInfo(op);
|
---|
[2868] | 253 | this.operatorParameterCollectionMapping.Add(op, op.Parameters);
|
---|
[2934] | 254 | this.operatorShapeInfoMapping.Add(op, shapeInfo);
|
---|
[2942] | 255 | this.shapeInfos.Add(shapeInfo);
|
---|
[2868] | 256 | foreach (IParameter param in op.Parameters)
|
---|
| 257 | this.AddParameter(op, param);
|
---|
| 258 | }
|
---|
[2853] | 259 | }
|
---|
| 260 | private void RemoveOperator(IOperator op) {
|
---|
| 261 | this.DeregisterOperatorEvents(op);
|
---|
[2861] | 262 | foreach (IParameter param in op.Parameters)
|
---|
| 263 | this.RemoveParameter(op, param);
|
---|
[2853] | 264 |
|
---|
[2934] | 265 | IOperatorShapeInfo shapeInfo = this.operatorShapeInfoMapping.GetByFirst(op);
|
---|
[2861] | 266 | this.operatorParameterCollectionMapping.RemoveByFirst(op);
|
---|
[2934] | 267 | this.operatorShapeInfoMapping.RemoveByFirst(op);
|
---|
[2853] | 268 | this.shapeInfos.Remove(shapeInfo);
|
---|
| 269 | }
|
---|
| 270 |
|
---|
[2935] | 271 | private void OperatorBreakpointChanged(object sender, EventArgs e) {
|
---|
| 272 | IOperator op = (IOperator)sender;
|
---|
| 273 | IOperatorShapeInfo operatorShapeInfo = this.operatorShapeInfoMapping.GetByFirst(op);
|
---|
| 274 | if (op.Breakpoint) {
|
---|
| 275 | operatorShapeInfo.LineColor = Color.Red;
|
---|
| 276 | operatorShapeInfo.LineWidth = 2;
|
---|
| 277 | } else {
|
---|
| 278 | operatorShapeInfo.LineColor = Color.Black;
|
---|
| 279 | operatorShapeInfo.LineWidth = 1;
|
---|
| 280 | }
|
---|
| 281 | }
|
---|
| 282 |
|
---|
[3344] | 283 | private void OperatorItemImageChanged(object sender, EventArgs e) {
|
---|
| 284 | IOperator op = (IOperator)sender;
|
---|
| 285 | IOperatorShapeInfo operatorShapeInfo = this.operatorShapeInfoMapping.GetByFirst(op);
|
---|
| 286 | operatorShapeInfo.Icon = new Bitmap(op.ItemImage);
|
---|
| 287 | }
|
---|
| 288 |
|
---|
[2896] | 289 | private void OperatorNameChanged(object sender, EventArgs e) {
|
---|
| 290 | IOperator op = (IOperator)sender;
|
---|
[2934] | 291 | IOperatorShapeInfo operatorShapeInfo = this.operatorShapeInfoMapping.GetByFirst(op);
|
---|
| 292 | operatorShapeInfo.Title = op.Name;
|
---|
[2896] | 293 | }
|
---|
| 294 |
|
---|
[2853] | 295 | private void Operators_ItemsAdded(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IOperator> e) {
|
---|
| 296 | foreach (IOperator op in e.Items)
|
---|
| 297 | this.AddOperator(op);
|
---|
| 298 | }
|
---|
| 299 | private void Operators_ItemsRemoved(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IOperator> e) {
|
---|
| 300 | foreach (IOperator op in e.Items)
|
---|
| 301 | this.RemoveOperator(op);
|
---|
| 302 | }
|
---|
| 303 | private void Operators_CollectionReset(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IOperator> e) {
|
---|
| 304 | foreach (IOperator op in e.OldItems)
|
---|
| 305 | this.RemoveOperator(op);
|
---|
| 306 | foreach (IOperator op in e.Items)
|
---|
| 307 | this.AddOperator(op);
|
---|
| 308 | }
|
---|
| 309 |
|
---|
| 310 | private void RegisterOperatorEvents(IOperator op) {
|
---|
| 311 | op.Parameters.ItemsAdded += new CollectionItemsChangedEventHandler<IParameter>(Parameters_ItemsAdded);
|
---|
| 312 | op.Parameters.ItemsRemoved += new CollectionItemsChangedEventHandler<IParameter>(Parameters_ItemsRemoved);
|
---|
| 313 | op.Parameters.ItemsReplaced += new CollectionItemsChangedEventHandler<IParameter>(Parameters_ItemsReplaced);
|
---|
| 314 | op.Parameters.CollectionReset += new CollectionItemsChangedEventHandler<IParameter>(Parameters_CollectionReset);
|
---|
[2896] | 315 | op.NameChanged += new EventHandler(OperatorNameChanged);
|
---|
[3344] | 316 | op.ItemImageChanged += new EventHandler(OperatorItemImageChanged);
|
---|
[2935] | 317 | op.BreakpointChanged += new EventHandler(OperatorBreakpointChanged);
|
---|
[2853] | 318 | }
|
---|
[2896] | 319 |
|
---|
[2853] | 320 | private void DeregisterOperatorEvents(IOperator op) {
|
---|
| 321 | op.Parameters.ItemsAdded -= new CollectionItemsChangedEventHandler<IParameter>(Parameters_ItemsAdded);
|
---|
| 322 | op.Parameters.ItemsRemoved -= new CollectionItemsChangedEventHandler<IParameter>(Parameters_ItemsRemoved);
|
---|
| 323 | op.Parameters.ItemsReplaced -= new CollectionItemsChangedEventHandler<IParameter>(Parameters_ItemsReplaced);
|
---|
| 324 | op.Parameters.CollectionReset -= new CollectionItemsChangedEventHandler<IParameter>(Parameters_CollectionReset);
|
---|
[2896] | 325 | op.NameChanged -= new EventHandler(OperatorNameChanged);
|
---|
[3344] | 326 | op.ItemImageChanged -= new EventHandler(OperatorItemImageChanged);
|
---|
[2935] | 327 | op.BreakpointChanged -= new EventHandler(OperatorBreakpointChanged);
|
---|
[2853] | 328 | }
|
---|
| 329 | #endregion
|
---|
| 330 |
|
---|
| 331 | #region parameter events
|
---|
[2861] | 332 | private void AddParameter(IOperator op, IParameter param) {
|
---|
[2934] | 333 | this.parameterOperatorMapping.Add(param, op);
|
---|
[3422] | 334 | IValueParameter opParam = param as IValueParameter;
|
---|
| 335 | if (opParam != null && typeof(IOperator).IsAssignableFrom(param.DataType)) {
|
---|
[2861] | 336 | this.RegisterOperatorParameterEvents(opParam);
|
---|
[3386] | 337 | IOperatorShapeInfo shapeInfoFrom = this.operatorShapeInfoMapping.GetByFirst(op);
|
---|
| 338 | shapeInfoFrom.AddConnector(param.Name);
|
---|
[2861] | 339 |
|
---|
| 340 | if (opParam.Value != null) {
|
---|
[3422] | 341 | if (!this.operatorShapeInfoMapping.ContainsFirst((IOperator)opParam.Value))
|
---|
| 342 | this.AddOperator((IOperator)opParam.Value);
|
---|
| 343 | IOperatorShapeInfo shapeInfoTo = this.operatorShapeInfoMapping.GetByFirst((IOperator)opParam.Value);
|
---|
[3386] | 344 | this.connectionInfos.Add(new ConnectionInfo(shapeInfoFrom, param.Name, shapeInfoTo, OperatorShapeInfoFactory.PredecessorConnector));
|
---|
[2861] | 345 | }
|
---|
[2934] | 346 | } else
|
---|
| 347 | this.RegisterParameterEvents(param);
|
---|
[2853] | 348 | }
|
---|
[2868] | 349 |
|
---|
[2861] | 350 | private void RemoveParameter(IOperator op, IParameter param) {
|
---|
[3422] | 351 | IValueParameter opParam = param as IValueParameter;
|
---|
| 352 | if (opParam != null && typeof(IOperator).IsAssignableFrom(param.DataType)) {
|
---|
[2861] | 353 | this.DeregisterOperatorParameterEvents(opParam);
|
---|
[2934] | 354 | IOperatorShapeInfo shapeInfo = this.operatorShapeInfoMapping.GetByFirst(op);
|
---|
[3386] | 355 | this.connectionInfos.RemoveWhere(c => c.From == shapeInfo && c.ConnectorFrom == param.Name);
|
---|
| 356 | this.connectionInfos.RemoveWhere(c => c.To == shapeInfo && c.ConnectorTo == param.Name);
|
---|
[2934] | 357 | shapeInfo.RemoveConnector(param.Name);
|
---|
| 358 | } else
|
---|
| 359 | this.DeregisterParameterEvents(param);
|
---|
[2868] | 360 |
|
---|
[2934] | 361 | this.parameterOperatorMapping.Remove(param);
|
---|
[2853] | 362 | }
|
---|
| 363 |
|
---|
| 364 | private void opParam_ValueChanged(object sender, EventArgs e) {
|
---|
[3422] | 365 | IValueParameter opParam = (IValueParameter)sender;
|
---|
[3729] | 366 | if (this.parameterOperatorMapping.ContainsKey(opParam)) {
|
---|
| 367 | IOperator op = this.parameterOperatorMapping[opParam];
|
---|
| 368 | IOperatorShapeInfo shapeInfoFrom = this.operatorShapeInfoMapping.GetByFirst(op);
|
---|
| 369 | KeyValuePair<IOperatorShapeInfo, string> connectionFrom = new KeyValuePair<IOperatorShapeInfo, string>(shapeInfoFrom, opParam.Name);
|
---|
[2861] | 370 |
|
---|
[3729] | 371 | this.connectionInfos.RemoveWhere(c => c.From == shapeInfoFrom && c.ConnectorFrom == opParam.Name);
|
---|
| 372 | if (opParam.Value != null) {
|
---|
| 373 | if (!this.operatorShapeInfoMapping.ContainsFirst((IOperator)opParam.Value))
|
---|
| 374 | this.AddOperator((IOperator)opParam.Value);
|
---|
| 375 | IOperatorShapeInfo shapeInfoTo = this.operatorShapeInfoMapping.GetByFirst((IOperator)opParam.Value);
|
---|
| 376 | base.AddConnectionInfo(new ConnectionInfo(shapeInfoFrom, opParam.Name, shapeInfoTo, OperatorShapeInfoFactory.PredecessorConnector));
|
---|
| 377 | }
|
---|
[2861] | 378 | }
|
---|
[2853] | 379 | }
|
---|
| 380 |
|
---|
| 381 | private void Parameters_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IParameter> e) {
|
---|
[3393] | 382 | IKeyedItemCollection<string, IParameter> parameterCollection = sender as IKeyedItemCollection<string, IParameter>;
|
---|
[2861] | 383 | IOperator op = this.operatorParameterCollectionMapping.GetBySecond(parameterCollection);
|
---|
[2853] | 384 | foreach (IParameter param in e.Items)
|
---|
[2861] | 385 | AddParameter(op, param);
|
---|
[2934] | 386 | this.UpdateParameterLabels(op);
|
---|
[2853] | 387 | }
|
---|
| 388 | private void Parameters_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IParameter> e) {
|
---|
[3393] | 389 | IKeyedItemCollection<string, IParameter> parameterCollection = sender as IKeyedItemCollection<string, IParameter>;
|
---|
[2861] | 390 | IOperator op = this.operatorParameterCollectionMapping.GetBySecond(parameterCollection);
|
---|
[2853] | 391 | foreach (IParameter param in e.Items)
|
---|
[2861] | 392 | RemoveParameter(op, param);
|
---|
[2934] | 393 | this.UpdateParameterLabels(op);
|
---|
[2853] | 394 | }
|
---|
| 395 | private void Parameters_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IParameter> e) {
|
---|
[3393] | 396 | IKeyedItemCollection<string, IParameter> parameterCollection = sender as IKeyedItemCollection<string, IParameter>;
|
---|
[2861] | 397 | IOperator op = this.operatorParameterCollectionMapping.GetBySecond(parameterCollection);
|
---|
[2853] | 398 | foreach (IParameter param in e.OldItems)
|
---|
[2861] | 399 | RemoveParameter(op, param);
|
---|
[2853] | 400 | foreach (IParameter param in e.Items)
|
---|
[2861] | 401 | AddParameter(op, param);
|
---|
[2934] | 402 | this.UpdateParameterLabels(op);
|
---|
[2853] | 403 | }
|
---|
| 404 | private void Parameters_CollectionReset(object sender, CollectionItemsChangedEventArgs<IParameter> e) {
|
---|
[3393] | 405 | IKeyedItemCollection<string, IParameter> parameterCollection = sender as IKeyedItemCollection<string, IParameter>;
|
---|
[2861] | 406 | IOperator op = this.operatorParameterCollectionMapping.GetBySecond(parameterCollection);
|
---|
[2853] | 407 | foreach (IParameter param in e.OldItems)
|
---|
[2861] | 408 | RemoveParameter(op, param);
|
---|
[2853] | 409 | foreach (IParameter param in e.Items)
|
---|
[2861] | 410 | AddParameter(op, param);
|
---|
[2934] | 411 | this.UpdateParameterLabels(op);
|
---|
[2853] | 412 | }
|
---|
| 413 |
|
---|
[3422] | 414 | private void RegisterOperatorParameterEvents(IValueParameter opParam) {
|
---|
[2853] | 415 | opParam.ValueChanged += new EventHandler(opParam_ValueChanged);
|
---|
| 416 | }
|
---|
[3422] | 417 | private void DeregisterOperatorParameterEvents(IValueParameter opParam) {
|
---|
[2853] | 418 | opParam.ValueChanged -= new EventHandler(opParam_ValueChanged);
|
---|
| 419 | }
|
---|
[2934] | 420 | private void RegisterParameterEvents(IParameter param) {
|
---|
| 421 | param.ToStringChanged += new EventHandler(param_ToStringChanged);
|
---|
| 422 | param.NameChanged += new EventHandler(param_NameChanged);
|
---|
| 423 | }
|
---|
| 424 | private void DeregisterParameterEvents(IParameter param) {
|
---|
| 425 | param.ToStringChanged -= new EventHandler(param_ToStringChanged);
|
---|
| 426 | param.NameChanged -= new EventHandler(param_NameChanged);
|
---|
| 427 | }
|
---|
| 428 |
|
---|
| 429 | private void param_NameChanged(object sender, EventArgs e) {
|
---|
| 430 | IParameter param = (IParameter)sender;
|
---|
| 431 | IOperator op = this.parameterOperatorMapping[param];
|
---|
| 432 | this.UpdateParameterLabels(op);
|
---|
| 433 | }
|
---|
| 434 | private void param_ToStringChanged(object sender, EventArgs e) {
|
---|
| 435 | IParameter param = (IParameter)sender;
|
---|
| 436 | IOperator op = this.parameterOperatorMapping[param];
|
---|
| 437 | this.UpdateParameterLabels(op);
|
---|
| 438 | }
|
---|
| 439 |
|
---|
| 440 | private void UpdateParameterLabels(IOperator op) {
|
---|
[3422] | 441 | IEnumerable<IParameter> parameters = op.Parameters.Where(p => !(p is IValueParameter && typeof(IOperator).IsAssignableFrom(p.DataType)));
|
---|
[2934] | 442 | IOperatorShapeInfo operatorShapeInfo = this.operatorShapeInfoMapping.GetByFirst(op);
|
---|
| 443 | if (parameters.Count() > 0)
|
---|
| 444 | operatorShapeInfo.UpdateLabels(parameters.Select(p => p.ToString()));
|
---|
| 445 | else
|
---|
| 446 | operatorShapeInfo.UpdateLabels(new List<string>());
|
---|
| 447 | }
|
---|
[2853] | 448 | #endregion
|
---|
| 449 | }
|
---|
[2893] | 450 | } |
---|