Changeset 2861 for trunk/sources/HeuristicLab.Operators.Views.GraphVisualization/3.3/OperatorGraphView.cs
- Timestamp:
- 02/24/10 17:58:03 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Operators.Views.GraphVisualization/3.3/OperatorGraphView.cs
r2853 r2861 34 34 using HeuristicLab.Parameters; 35 35 using HeuristicLab.MainForm.WindowsForms; 36 using HeuristicLab.Collections; 36 37 37 38 namespace HeuristicLab.Operators.Views.GraphVisualization { 39 38 40 [Content(typeof(OperatorGraph), false)] 39 41 public partial class OperatorGraphView : ContentView { 40 private BidirectionalLookup<IShapeInfo, IShape> shapeMapping; 41 private BidirectionalLookup<IConnectionInfo, IConnection> connectionMapping; 42 private BidirectionalLookup<IShapeInfo, IShape> shapeInfoShapeMapping; 43 private BidirectionalLookup<IShapeInfo, INotifyObservableDictionaryItemsChanged<string, IShapeInfo>> shapeInfoConnectionsMapping; 44 private Dictionary<IConnector, IShape> connectorShapeMapping; 45 private BidirectionalLookup<IConnection, KeyValuePair<IConnector, IConnector>> connectionConnectorsMapping; 42 46 43 47 private bool causedUpdateOfShapeInfo; … … 46 50 this.causedUpdateOfShapeInfo = false; 47 51 Caption = "Operator Graph"; 48 this.shapeMapping = new BidirectionalLookup<IShapeInfo, IShape>(); 49 this.connectionMapping = new BidirectionalLookup<IConnectionInfo, IConnection>(); 52 this.shapeInfoShapeMapping = new BidirectionalLookup<IShapeInfo, IShape>(); 53 this.shapeInfoConnectionsMapping = new BidirectionalLookup<IShapeInfo, INotifyObservableDictionaryItemsChanged<string, IShapeInfo>>(); 54 this.connectionConnectorsMapping = new BidirectionalLookup<IConnection, KeyValuePair<IConnector, IConnector>>(); 55 this.connectorShapeMapping = new Dictionary<IConnector, IShape>(); 50 56 } 51 57 … … 72 78 73 79 private void UpdateVisualizationInfo() { 74 foreach (IShapeInfo shapeInfo in this.shape Mapping.FirstValues)80 foreach (IShapeInfo shapeInfo in this.shapeInfoShapeMapping.FirstValues) 75 81 this.RemoveShapeInfo(shapeInfo); 76 this.shapeMapping.Clear(); 82 83 this.shapeInfoShapeMapping.Clear(); 84 this.shapeInfoConnectionsMapping.Clear(); 85 this.connectorShapeMapping.Clear(); 86 this.connectionConnectorsMapping.Clear(); 77 87 78 88 foreach (IShapeInfo shapeInfo in this.VisualizationInfo.ShapeInfos) 79 89 this.AddShapeInfo(shapeInfo); 90 91 this.UpdateLayoutRoot(); 92 } 93 94 private void UpdateLayoutRoot() { 95 IShapeInfo shapeInfo = this.VisualizationInfo.InitialShape; 96 if (shapeInfo != null) 97 this.graphVisualization.Controller.Model.LayoutRoot = this.shapeInfoShapeMapping.GetByFirst(shapeInfo); 98 else 99 this.graphVisualization.Controller.Model.LayoutRoot = null; 100 } 101 102 private void VisualizationInfo_InitialShapeChanged(object sender, EventArgs e) { 103 this.UpdateLayoutRoot(); 80 104 } 81 105 82 106 protected override void RegisterContentEvents() { 83 107 base.RegisterContentEvents(); 108 this.VisualizationInfo.InitialShapeChanged += new EventHandler(VisualizationInfo_InitialShapeChanged); 84 109 this.VisualizationInfo.ObserveableShapeInfos.ItemsAdded += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IShapeInfo>(ShapeInfos_ItemsAdded); 85 110 this.VisualizationInfo.ObserveableShapeInfos.ItemsRemoved += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IShapeInfo>(ShapeInfos_ItemsRemoved); … … 89 114 protected override void DeregisterContentEvents() { 90 115 base.DeregisterContentEvents(); 116 this.VisualizationInfo.InitialShapeChanged -= new EventHandler(VisualizationInfo_InitialShapeChanged); 91 117 this.VisualizationInfo.ObserveableShapeInfos.ItemsAdded -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IShapeInfo>(ShapeInfos_ItemsAdded); 92 118 this.VisualizationInfo.ObserveableShapeInfos.ItemsRemoved -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IShapeInfo>(ShapeInfos_ItemsRemoved); 93 119 this.VisualizationInfo.ObserveableShapeInfos.CollectionReset -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IShapeInfo>(ShapeInfos_CollectionReset); 94 120 } 95 96 121 97 122 private void ShapeInfos_CollectionReset(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IShapeInfo> e) { … … 100 125 foreach (IShapeInfo shapeInfo in e.Items) 101 126 this.AddShapeInfo(shapeInfo); 102 this.graphVisualization.Invalidate();103 127 } 104 128 … … 106 130 foreach (IShapeInfo shapeInfo in e.Items) 107 131 this.AddShapeInfo(shapeInfo); 108 this.graphVisualization.Invalidate();109 132 } 110 133 … … 112 135 foreach (IShapeInfo shapeInfo in e.Items) 113 136 this.RemoveShapeInfo(shapeInfo); 114 this.graphVisualization.Invalidate();115 137 } 116 138 117 139 private void AddShapeInfo(IShapeInfo shapeInfo) { 140 this.RegisterShapeInfoEvents(shapeInfo); 141 118 142 IShape shape = shapeInfo.CreateShape(); 143 shape.OnEntityChange += new EventHandler<EntityEventArgs>(shape_OnEntityChange); 144 this.shapeInfoShapeMapping.Add(shapeInfo, shape); 145 146 foreach (IConnector connector in shape.Connectors) 147 this.connectorShapeMapping.Add(connector, shape); 148 119 149 this.graphVisualization.Controller.Model.AddShape(shape); 120 this.shapeMapping.Add(shapeInfo, shape); 121 122 shape.OnEntityChange += new EventHandler<EntityEventArgs>(shape_OnEntityChange); 123 shapeInfo.Changed += new ChangedEventHandler(shapeInfo_Changed); 150 151 foreach (KeyValuePair<string, IShapeInfo> pair in shapeInfo.Connections) { 152 if (!this.shapeInfoShapeMapping.ContainsFirst(pair.Value)) 153 this.AddShapeInfo(pair.Value); 154 this.AddConnection(shapeInfo, pair.Key, pair.Value); 155 } 124 156 } 125 157 126 158 private void RemoveShapeInfo(IShapeInfo shapeInfo) { 127 IShape shape = this.shapeMapping.GetByFirst(shapeInfo); 159 this.DeregisterShapeInfoEvents(shapeInfo); 160 IShape shape = this.shapeInfoShapeMapping.GetByFirst(shapeInfo); 128 161 shape.OnEntityChange -= new EventHandler<EntityEventArgs>(shape_OnEntityChange); 129 162 shapeInfo.Changed -= new ChangedEventHandler(shapeInfo_Changed); 130 163 131 this.shapeMapping.RemoveByFirst(shapeInfo); 132 if (this.graphVisualization.Controller.Model.Shapes.Contains(shape)) 164 IConnection connection; 165 foreach (IConnector connector in shape.Connectors) { 166 connection = this.GetConnection(shapeInfo, connector.Name); 167 this.RemoveConnection(connection); 168 } 169 170 this.shapeInfoShapeMapping.RemoveByFirst(shapeInfo); 171 172 if (this.graphVisualization.Controller.Model.Shapes.Contains(shape)) { 133 173 this.graphVisualization.Controller.Model.RemoveShape(shape); 134 } 174 } 175 } 176 177 private void RegisterShapeInfoEvents(IShapeInfo shapeInfo) { 178 shapeInfo.Changed += new ChangedEventHandler(shapeInfo_Changed); 179 180 this.shapeInfoConnectionsMapping.Add(shapeInfo, shapeInfo.ObservableConnections); 181 shapeInfo.ObservableConnections.ItemsAdded += new CollectionItemsChangedEventHandler<KeyValuePair<string, IShapeInfo>>(Connections_ItemsAdded); 182 shapeInfo.ObservableConnections.ItemsRemoved += new CollectionItemsChangedEventHandler<KeyValuePair<string, IShapeInfo>>(Connections_ItemsRemoved); 183 shapeInfo.ObservableConnections.ItemsReplaced += new CollectionItemsChangedEventHandler<KeyValuePair<string, IShapeInfo>>(Connections_ItemsReplaced); 184 shapeInfo.ObservableConnections.CollectionReset += new CollectionItemsChangedEventHandler<KeyValuePair<string, IShapeInfo>>(Connections_CollectionReset); 185 } 186 187 private void DeregisterShapeInfoEvents(IShapeInfo shapeInfo) { 188 shapeInfo.Changed -= new ChangedEventHandler(shapeInfo_Changed); 189 190 this.shapeInfoConnectionsMapping.RemoveByFirst(shapeInfo); 191 shapeInfo.ObservableConnections.ItemsAdded -= new CollectionItemsChangedEventHandler<KeyValuePair<string, IShapeInfo>>(Connections_ItemsAdded); 192 shapeInfo.ObservableConnections.ItemsRemoved -= new CollectionItemsChangedEventHandler<KeyValuePair<string, IShapeInfo>>(Connections_ItemsRemoved); 193 shapeInfo.ObservableConnections.ItemsReplaced -= new CollectionItemsChangedEventHandler<KeyValuePair<string, IShapeInfo>>(Connections_ItemsReplaced); 194 shapeInfo.ObservableConnections.CollectionReset -= new CollectionItemsChangedEventHandler<KeyValuePair<string, IShapeInfo>>(Connections_CollectionReset); 195 } 196 197 private void Connections_CollectionReset(object sender, CollectionItemsChangedEventArgs<KeyValuePair<string, IShapeInfo>> e) { 198 IShapeInfo shapeInfo = this.shapeInfoConnectionsMapping.GetBySecond((INotifyObservableDictionaryItemsChanged<string, IShapeInfo>)sender); 199 IConnection connection; 200 foreach (KeyValuePair<string, IShapeInfo> pair in e.Items) { 201 connection = this.GetConnection(shapeInfo, pair.Key); 202 this.RemoveConnection(connection); 203 } 204 foreach (KeyValuePair<string, IShapeInfo> pair in e.Items) 205 this.AddConnection(shapeInfo, pair.Key, pair.Value); 206 } 207 208 private void Connections_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<KeyValuePair<string, IShapeInfo>> e) { 209 IShapeInfo shapeInfo = this.shapeInfoConnectionsMapping.GetBySecond((INotifyObservableDictionaryItemsChanged<string, IShapeInfo>)sender); 210 IConnection connection; 211 foreach (KeyValuePair<string, IShapeInfo> pair in e.Items) { 212 connection = this.GetConnection(shapeInfo, pair.Key); 213 this.RemoveConnection(connection); 214 } 215 foreach (KeyValuePair<string, IShapeInfo> pair in e.Items) 216 this.AddConnection(shapeInfo, pair.Key, pair.Value); 217 } 218 219 private void Connections_ItemsAdded(object sender, CollectionItemsChangedEventArgs<KeyValuePair<string, IShapeInfo>> e) { 220 IShapeInfo shapeInfo = this.shapeInfoConnectionsMapping.GetBySecond((INotifyObservableDictionaryItemsChanged<string, IShapeInfo>)sender); 221 foreach (KeyValuePair<string, IShapeInfo> pair in e.Items) 222 this.AddConnection(shapeInfo, pair.Key, pair.Value); 223 } 224 225 private void Connections_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<KeyValuePair<string, IShapeInfo>> e) { 226 IShapeInfo shapeInfo = this.shapeInfoConnectionsMapping.GetBySecond((INotifyObservableDictionaryItemsChanged<string, IShapeInfo>)sender); 227 IConnection connection; 228 foreach (KeyValuePair<string, IShapeInfo> pair in e.Items) { 229 connection = this.GetConnection(shapeInfo, pair.Key); 230 this.RemoveConnection(connection); 231 } 232 } 233 234 private void AddConnection(IShapeInfo shapeInfoFrom, string connectorName, IShapeInfo shapeInfoTo) { 235 IShape shapeFrom = this.shapeInfoShapeMapping.GetByFirst(shapeInfoFrom); 236 IShape shapeTo = this.shapeInfoShapeMapping.GetByFirst(shapeInfoTo); 237 238 IConnector connectorFrom = shapeFrom.Connectors.Where(c => c.Name == connectorName).First(); 239 IConnector connectorTo = shapeTo.Connectors.Where(c => c.Name == "Predecessor").FirstOrDefault(); 240 241 IConnection connection = Factory.CreateConnection(connectorFrom, connectorTo); 242 this.connectionConnectorsMapping.Add(connection, new KeyValuePair<IConnector, IConnector>(connectorFrom, connectorTo)); 243 this.graphVisualization.Controller.Model.AddConnection(connection); 244 this.graphVisualization.Invalidate(); 245 } 246 247 private IConnection GetConnection(IShapeInfo shapeInfoFrom, string connectorName) { 248 IShape shape = this.shapeInfoShapeMapping.GetByFirst(shapeInfoFrom); 249 IConnector connector = shape.Connectors.Where(c => c.Name == connectorName).First(); 250 251 if (!this.connectionConnectorsMapping.SecondValues.Any(p => p.Key == connector)) 252 return null; 253 254 KeyValuePair<IConnector, IConnector> connectorPair = this.connectionConnectorsMapping.SecondValues.Where(p => p.Key == connector).FirstOrDefault(); 255 return this.connectionConnectorsMapping.GetBySecond(connectorPair); 256 } 257 258 private void ChangeConnection(IShapeInfo shapeInfoFrom, string connectorName, IShapeInfo shapeInfoTo) { 259 IConnection connection = this.GetConnection(shapeInfoFrom, connectorName); 260 IShape shapeTo = this.shapeInfoShapeMapping.GetByFirst(shapeInfoFrom); 261 IConnector connectorTo = shapeTo.Connectors.Where(c => c.Name == "Predecessor").FirstOrDefault(); 262 263 connection.To.DetachFromParent(); 264 connection.To.AttachTo(connectorTo); 265 this.graphVisualization.Invalidate(); 266 } 267 268 private void RemoveConnection(IConnection connection) { 269 if (connection == null) 270 return; 271 272 if (connection.From.AttachedTo != null) 273 connection.From.DetachFromParent(); 274 if (connection.To.AttachedTo != null) 275 connection.To.DetachFromParent(); 276 277 if (this.connectionConnectorsMapping.ContainsFirst(connection)) 278 this.connectionConnectorsMapping.RemoveByFirst(connection); 279 if (this.graphVisualization.Controller.Model.Connections.Contains(connection)) 280 this.graphVisualization.Controller.Model.Remove(connection); 281 this.graphVisualization.Invalidate(); 282 } 283 135 284 136 285 private void shapeInfo_Changed(object sender, ChangedEventArgs e) { 286 if (this.causedUpdateOfShapeInfo) 287 return; 288 137 289 IShapeInfo shapeInfo = (IShapeInfo)sender; 138 this.UpdateShape(shapeInfo); 139 } 290 IShape shape = this.shapeInfoShapeMapping.GetByFirst(shapeInfo); 291 shapeInfo.UpdateShape(shape); 292 } 293 140 294 141 295 private void shape_OnEntityChange(object sender, EntityEventArgs e) { 142 296 this.causedUpdateOfShapeInfo = true; 143 297 IShape shape = e.Entity as IShape; 144 IShapeInfo shapeInfo = this.shape Mapping.GetBySecond(shape);298 IShapeInfo shapeInfo = this.shapeInfoShapeMapping.GetBySecond(shape); 145 299 146 300 shapeInfo.Location = shape.Location; 301 shapeInfo.Size = new Size(shape.Width, shape.Height); 302 147 303 this.graphVisualization.Invalidate(); 148 304 this.causedUpdateOfShapeInfo = false; 149 305 } 150 306 151 private void UpdateShape(IShapeInfo shapeInfo) { 152 if (!this.causedUpdateOfShapeInfo) { 153 IShape shape = this.shapeMapping.GetByFirst(shapeInfo); 154 shape.Location = shapeInfo.Location; 307 private void graphVisualization_OnEntityAdded(object sender, EntityEventArgs e) { 308 IConnection connection = e.Entity as IConnection; 309 if (connection != null && !this.connectionConnectorsMapping.ContainsFirst(connection)) { 310 if (connection.From.AttachedTo == null || connection.To.AttachedTo == null) 311 this.RemoveConnection(connection); 155 312 } 156 313 } 157 314 158 315 private void graphVisualization_OnEntityRemoved(object sender, EntityEventArgs e) { 159 IShape shape = (IShape)e.Entity;160 if ( this.shapeMapping.ContainsSecond(shape)) {161 IShapeInfo shapeInfo = this.shape Mapping.GetBySecond(shape);316 IShape shape = e.Entity as IShape; 317 if (shape != null && this.shapeInfoShapeMapping.ContainsSecond(shape)) { 318 IShapeInfo shapeInfo = this.shapeInfoShapeMapping.GetBySecond(shape); 162 319 this.VisualizationInfo.RemoveShapeInfo(shapeInfo); 163 320 } 164 } 165 166 167 168 169 //protected override void OnContentChanged() { 170 // base.OnContentChanged(); 171 // this.ClearGraph(); 172 173 // this.CreateGraph(null, new OperatorParameter(string.Empty, this.Content)); 174 // foreach (IShape shape in this.operatorShapeMapping.Values) 175 // this.GraphModel.AddShape(shape); 176 177 // foreach (IConnection connection in this.connectionMapping.Values) 178 // this.GraphModel.AddConnection(connection); 179 180 // if (this.Content == null) 181 // this.graphVisualization.Controller.Model.LayoutRoot = null; 182 // else 183 // this.graphVisualization.Controller.Model.LayoutRoot = this.operatorShapeMapping[this.Content]; 184 // this.RelayoutOperatorGraph(); 185 //} 186 187 //private void opParam_ValueChanged(object sender, EventArgs e) { 188 // if (InvokeRequired) 189 // Invoke(new EventHandler(opParam_ValueChanged), sender, e); 190 // else { 191 // this.OnContentChanged(); 192 // } 193 //} 194 195 //private void ClearGraph() { 196 // this.GraphModel.Clear(); 197 // foreach (IValueParameter<IOperator> opParam in this.parameters) 198 // opParam.ValueChanged -= opParam_ValueChanged; 199 200 // this.operatorShapeMapping.Clear(); 201 // this.parameters.Clear(); 202 // this.connectionMapping.Clear(); 203 //} 204 205 206 207 //private void CreateGraph(IOperator parent, IValueParameter<IOperator> op) { 208 // if (op == null || op.Value == null) 209 // return; 210 211 // IShape shape; 212 // if (!this.operatorShapeMapping.ContainsKey(op.Value)) { 213 // //shape = GraphVisualizationInfo.CreateShape(op.Value); 214 // //this.operatorShapeMapping[op.Value] = shape; 215 216 // foreach (IParameter param in op.Value.Parameters) { 217 // IValueParameter<IOperator> opParam = param as IValueParameter<IOperator>; 218 // if (opParam != null) { 219 // HandleOperatorParameter(opParam); 220 // this.CreateGraph(op.Value, opParam); 221 // } 222 // } 223 // } 224 225 // if (parent != null) { 226 // IShape from = this.operatorShapeMapping[parent]; 227 // IShape to = this.operatorShapeMapping[op.Value]; 228 // //IConnection connection = GraphVisualizationInfo.CreateConnection(from,to); 229 // //this.connectionMapping[new KeyValuePair<IOperator, IValueParameter<IOperator>>(parent, op)] = connection; 230 // } 231 //} 232 233 //private void DeleteGraph(IOperator parent, IValueParameter<IOperator> op) { 234 235 //} 236 237 //private void HandleOperatorParameter(IValueParameter<IOperator> opParam) { 238 // if (opParam == null) 239 // return; 240 // opParam.ValueChanged += new EventHandler(opParam_ValueChanged); 241 // parameters.Add(opParam); 242 //} 243 244 //private void Model_OnEntityRemoved(object sender, EntityEventArgs e) { 245 // IShape shape = e.Entity as IShape; 246 // if (shape != null) { 247 // IOperator op = operatorShapeMapping.Where(os => os.Value == shape).First().Key; 248 // if (op == this.Content) 249 // this.Content = null; 250 // else { 251 // //clear all connections to the removed operator 252 // IEnumerable<IValueParameter<IOperator>> parentOperator = this.connectionMapping.Where(cs => cs.Key.Value.Value == op).Select(x => x.Key.Value); 253 // foreach (IValueParameter<IOperator> opParam in parentOperator.ToArray()) 254 // opParam.Value = null; 255 256 // //remove connections from graph view 257 // IEnumerable<IConnection> connections = this.connectionMapping.Where(cs => cs.Key.Value.Value == op).Select(x => x.Value); 258 // foreach (IConnection connection in connections) 259 // this.GraphModel.Remove(connection); 260 261 // this.graphVisualization.Invalidate(); 262 // } 263 264 265 // } 266 //} 321 322 IConnection connection = e.Entity as IConnection; 323 if (connection != null && this.connectionConnectorsMapping.ContainsFirst(connection)) { 324 IShape parentShape = this.connectorShapeMapping[connection.From.AttachedTo]; 325 IShapeInfo shapeInfo = this.shapeInfoShapeMapping.GetBySecond(parentShape); 326 string parameterName = connection.From.AttachedTo.Name; 327 328 shapeInfo.RemoveConnection(parameterName); 329 } 330 } 267 331 268 332 #region methods for toolbar items 269 333 334 private int layoutCount = 0; 270 335 internal void RelayoutOperatorGraph() { 271 //if (this.operatorShapeMapping.Count > 0 && this.connectionMapping.Count > 0) { //otherwise the layout does not work 272 this.graphVisualization.Controller.RunActivity("Standard TreeLayout"); 273 this.graphVisualization.Invalidate(); 274 //} 336 if (this.shapeInfoShapeMapping.Count > 0 && this.connectionConnectorsMapping.Count > 0) { //otherwise the layout does not work 337 338 string layoutName = string.Empty; 339 switch (this.layoutCount % 6) { 340 case 0: { layoutName = "Random Layout"; break; } 341 case 1: { layoutName = "FruchtermanReingold Layout"; break; } 342 case 2: { layoutName = "Standard TreeLayout"; break; } 343 case 3: { layoutName = "Radial TreeLayout"; break; } 344 case 4: { layoutName = "Balloon TreeLayout"; break; } 345 case 5: { layoutName = "ForceDirected Layout"; break; } 346 } 347 this.graphVisualization.Controller.RunActivity(layoutName); 348 MessageBox.Show(layoutName); 349 this.layoutCount++; 350 this.graphVisualization.Invalidate(); 351 } 352 } 353 354 internal void ActivateConnectionTool() { 355 this.graphVisualization.Controller.ActivateTool(ControllerBase.ConnectionToolName); 275 356 } 276 357 … … 309 390 if (e.Effect != DragDropEffects.None) { 310 391 IOperator op = e.Data.GetData("Value") as IOperator; 311 IShapeInfo shapeInfo = ShapeInfoFactory.CreateShapeInfo(op);392 IShapeInfo shapeInfo = Factory.CreateShapeInfo(op); 312 393 Point controlCoordinates = this.PointToClient(new Point(e.X, e.Y)); 313 394 PointF viewCoordinates = this.graphVisualization.Controller.View.DeviceToView(controlCoordinates);
Note: See TracChangeset
for help on using the changeset viewer.