Changeset 2893 for trunk/sources/HeuristicLab.Operators.Views.GraphVisualization/3.3/OperatorGraphView.cs
- Timestamp:
- 03/01/10 11:45:00 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Operators.Views.GraphVisualization/3.3/OperatorGraphView.cs
r2875 r2893 40 40 [Content(typeof(OperatorGraph), false)] 41 41 public partial class OperatorGraphView : ContentView { 42 private BidirectionalLookup<IShapeInfo, IShape> shapeInfoShapeMapping;43 private BidirectionalLookup<IShapeInfo, INotifyObservableDictionaryItemsChanged<string, IShapeInfo>> shapeInfoConnectionsMapping;44 private BidirectionalLookup<IConnection, KeyValuePair<IConnector, IConnector>> connectionConnectorsMapping;45 46 private bool causedUpdateOfShapeInfo;47 42 public OperatorGraphView() { 48 43 InitializeComponent(); 49 this.causedUpdateOfShapeInfo = false; 50 Caption = "Operator Graph"; 51 this.shapeInfoShapeMapping = new BidirectionalLookup<IShapeInfo, IShape>(); 52 this.shapeInfoConnectionsMapping = new BidirectionalLookup<IShapeInfo, INotifyObservableDictionaryItemsChanged<string, IShapeInfo>>(); 53 this.connectionConnectorsMapping = new BidirectionalLookup<IConnection, KeyValuePair<IConnector, IConnector>>(); 44 Caption = "Operator Graph Visualization"; 45 46 this.graphVisualizationInfoView.Controller.OnShowContextMenu += new EventHandler<EntityMenuEventArgs>(Controller_OnShowContextMenu); 54 47 } 55 48 … … 66 59 protected override void OnContentChanged() { 67 60 if (this.VisualizationInfo == null) 68 this.VisualizationInfo = new OperatorGraphVisualizationInfo(this.Content);69 this. UpdateVisualizationInfo();61 this.VisualizationInfo = new GraphVisualizationInfo(this.Content); 62 this.graphVisualizationInfoView.Content = this.VisualizationInfo; 70 63 } 71 64 72 private OperatorGraphVisualizationInfo VisualizationInfo {73 get { return Content.VisualizationInfo as OperatorGraphVisualizationInfo; }65 private GraphVisualizationInfo VisualizationInfo { 66 get { return Content.VisualizationInfo as GraphVisualizationInfo; } 74 67 set { this.Content.VisualizationInfo = value; } 75 68 } 76 69 77 private void UpdateVisualizationInfo() { 78 foreach (IShapeInfo shapeInfo in this.shapeInfoShapeMapping.FirstValues) 79 this.RemoveShapeInfo(shapeInfo); 80 81 this.shapeInfoShapeMapping.Clear(); 82 this.shapeInfoConnectionsMapping.Clear(); 83 this.connectionConnectorsMapping.Clear(); 84 85 foreach (IShapeInfo shapeInfo in this.VisualizationInfo.ShapeInfos) 86 if (!this.shapeInfoShapeMapping.ContainsFirst(shapeInfo)) 87 this.AddShapeInfo(shapeInfo); 88 89 foreach (KeyValuePair<KeyValuePair<IShapeInfo, string>, IShapeInfo> connection in this.VisualizationInfo.Connections) 90 this.AddConnection(connection.Key.Key, connection.Key.Value, connection.Value); 91 92 this.UpdateLayoutRoot(); 93 } 94 95 private void UpdateLayoutRoot() { 96 IShapeInfo shapeInfo = this.VisualizationInfo.InitialShape; 97 if (shapeInfo != null) 98 this.graphVisualization.Controller.Model.LayoutRoot = this.shapeInfoShapeMapping.GetByFirst(shapeInfo); 99 else 100 this.graphVisualization.Controller.Model.LayoutRoot = null; 101 } 102 103 private void VisualizationInfo_InitialShapeChanged(object sender, EventArgs e) { 104 this.UpdateLayoutRoot(); 105 } 106 107 protected override void RegisterContentEvents() { 108 base.RegisterContentEvents(); 109 this.VisualizationInfo.InitialShapeChanged += new EventHandler(VisualizationInfo_InitialShapeChanged); 110 this.VisualizationInfo.ObserveableShapeInfos.ItemsAdded += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IShapeInfo>(ShapeInfos_ItemsAdded); 111 this.VisualizationInfo.ObserveableShapeInfos.ItemsRemoved += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IShapeInfo>(ShapeInfos_ItemsRemoved); 112 this.VisualizationInfo.ObserveableShapeInfos.CollectionReset += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IShapeInfo>(ShapeInfos_CollectionReset); 113 114 this.VisualizationInfo.ObservableConnections.ItemsAdded += new CollectionItemsChangedEventHandler<KeyValuePair<KeyValuePair<IShapeInfo, string>, IShapeInfo>>(Connections_ItemsAdded); 115 this.VisualizationInfo.ObservableConnections.ItemsRemoved += new CollectionItemsChangedEventHandler<KeyValuePair<KeyValuePair<IShapeInfo, string>, IShapeInfo>>(Connections_ItemsRemoved); 116 this.VisualizationInfo.ObservableConnections.ItemsReplaced += new CollectionItemsChangedEventHandler<KeyValuePair<KeyValuePair<IShapeInfo, string>, IShapeInfo>>(Connections_ItemsReplaced); 117 this.VisualizationInfo.ObservableConnections.CollectionReset += new CollectionItemsChangedEventHandler<KeyValuePair<KeyValuePair<IShapeInfo, string>, IShapeInfo>>(Connections_CollectionReset); 118 } 119 120 protected override void DeregisterContentEvents() { 121 base.DeregisterContentEvents(); 122 this.VisualizationInfo.InitialShapeChanged -= new EventHandler(VisualizationInfo_InitialShapeChanged); 123 this.VisualizationInfo.ObserveableShapeInfos.ItemsAdded -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IShapeInfo>(ShapeInfos_ItemsAdded); 124 this.VisualizationInfo.ObserveableShapeInfos.ItemsRemoved -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IShapeInfo>(ShapeInfos_ItemsRemoved); 125 this.VisualizationInfo.ObserveableShapeInfos.CollectionReset -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IShapeInfo>(ShapeInfos_CollectionReset); 126 127 this.VisualizationInfo.ObservableConnections.ItemsAdded -= new CollectionItemsChangedEventHandler<KeyValuePair<KeyValuePair<IShapeInfo, string>, IShapeInfo>>(Connections_ItemsAdded); 128 this.VisualizationInfo.ObservableConnections.ItemsRemoved -= new CollectionItemsChangedEventHandler<KeyValuePair<KeyValuePair<IShapeInfo, string>, IShapeInfo>>(Connections_ItemsRemoved); 129 this.VisualizationInfo.ObservableConnections.ItemsReplaced -= new CollectionItemsChangedEventHandler<KeyValuePair<KeyValuePair<IShapeInfo, string>, IShapeInfo>>(Connections_ItemsReplaced); 130 this.VisualizationInfo.ObservableConnections.CollectionReset -= new CollectionItemsChangedEventHandler<KeyValuePair<KeyValuePair<IShapeInfo, string>, IShapeInfo>>(Connections_CollectionReset); 131 } 132 133 private void ShapeInfos_CollectionReset(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IShapeInfo> e) { 134 foreach (IShapeInfo shapeInfo in e.OldItems) 135 this.RemoveShapeInfo(shapeInfo); 136 foreach (IShapeInfo shapeInfo in e.Items) 137 this.AddShapeInfo(shapeInfo); 138 } 139 140 private void ShapeInfos_ItemsAdded(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IShapeInfo> e) { 141 foreach (IShapeInfo shapeInfo in e.Items) 142 this.AddShapeInfo(shapeInfo); 143 } 144 145 private void ShapeInfos_ItemsRemoved(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IShapeInfo> e) { 146 foreach (IShapeInfo shapeInfo in e.Items) 147 this.RemoveShapeInfo(shapeInfo); 148 } 149 150 private void AddShapeInfo(IShapeInfo shapeInfo) { 151 this.RegisterShapeInfoEvents(shapeInfo); 152 153 IShape shape = shapeInfo.CreateShape(); 154 shape.OnEntityChange += new EventHandler<EntityEventArgs>(shape_OnEntityChange); 155 this.shapeInfoShapeMapping.Add(shapeInfo, shape); 156 157 this.graphVisualization.Controller.Model.AddShape(shape); 158 this.graphVisualization.Invalidate(); 159 } 160 161 private void RemoveShapeInfo(IShapeInfo shapeInfo) { 162 this.DeregisterShapeInfoEvents(shapeInfo); 163 IShape shape = this.shapeInfoShapeMapping.GetByFirst(shapeInfo); 164 shape.OnEntityChange -= new EventHandler<EntityEventArgs>(shape_OnEntityChange); 165 shapeInfo.Changed -= new ChangedEventHandler(shapeInfo_Changed); 166 167 IConnection connection; 168 foreach (IConnector connector in shape.Connectors) { 169 connection = this.GetConnection(shapeInfo, connector.Name); 170 this.RemoveConnection(connection); 171 } 172 173 this.shapeInfoShapeMapping.RemoveByFirst(shapeInfo); 174 this.shapeInfoConnectionsMapping.RemoveByFirst(shapeInfo); 175 176 if (this.graphVisualization.Controller.Model.Shapes.Contains(shape)) { 177 this.graphVisualization.Controller.Model.RemoveShape(shape); 178 } 179 this.graphVisualization.Invalidate(); 180 } 181 182 private void RegisterShapeInfoEvents(IShapeInfo shapeInfo) { 183 shapeInfo.Changed += new ChangedEventHandler(shapeInfo_Changed); 184 } 185 186 private void DeregisterShapeInfoEvents(IShapeInfo shapeInfo) { 187 shapeInfo.Changed -= new ChangedEventHandler(shapeInfo_Changed); 188 } 189 190 private void Connections_CollectionReset(object sender, CollectionItemsChangedEventArgs<KeyValuePair<KeyValuePair<IShapeInfo, string>, IShapeInfo>> e) { 191 IConnection connection; 192 foreach (KeyValuePair<KeyValuePair<IShapeInfo, string>, IShapeInfo> pair in e.Items) { 193 connection = this.GetConnection(pair.Key.Key, pair.Key.Value); 194 this.RemoveConnection(connection); 195 } 196 foreach (KeyValuePair<KeyValuePair<IShapeInfo, string>, IShapeInfo> pair in e.Items) 197 this.AddConnection(pair.Key.Key, pair.Key.Value, pair.Value); 198 } 199 200 private void Connections_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<KeyValuePair<KeyValuePair<IShapeInfo, string>, IShapeInfo>> e) { 201 IConnection connection; 202 foreach (KeyValuePair<KeyValuePair<IShapeInfo, string>, IShapeInfo> pair in e.Items) { 203 connection = this.GetConnection(pair.Key.Key, pair.Key.Value); 204 this.RemoveConnection(connection); 205 } 206 foreach (KeyValuePair<KeyValuePair<IShapeInfo, string>, IShapeInfo> pair in e.Items) 207 this.AddConnection(pair.Key.Key, pair.Key.Value, pair.Value); 208 } 209 210 private void Connections_ItemsAdded(object sender, CollectionItemsChangedEventArgs<KeyValuePair<KeyValuePair<IShapeInfo, string>, IShapeInfo>> e) { 211 foreach (KeyValuePair<KeyValuePair<IShapeInfo, string>, IShapeInfo> pair in e.Items) 212 this.AddConnection(pair.Key.Key, pair.Key.Value, pair.Value); 213 } 214 215 private void Connections_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<KeyValuePair<KeyValuePair<IShapeInfo, string>, IShapeInfo>> e) { 216 IConnection connection; 217 foreach (KeyValuePair<KeyValuePair<IShapeInfo, string>, IShapeInfo> pair in e.Items) { 218 connection = this.GetConnection(pair.Key.Key, pair.Key.Value); 219 this.RemoveConnection(connection); 70 #region context menu 71 private void Controller_OnShowContextMenu(object sender, EntityMenuEventArgs e) { 72 IShape shape = this.graphVisualizationInfoView.Controller.Model.GetShapeAt(e.MouseEventArgs.Location); 73 if (shape != null) { 74 IShapeInfo shapeInfo = shape.Tag as IShapeInfo; 75 this.shapeContextMenu.Tag = shapeInfo; 76 PointF worldPoint = this.graphVisualizationInfoView.Controller.View.WorldToView(e.MouseEventArgs.Location); 77 this.shapeContextMenu.Show(this, Point.Round(worldPoint)); 220 78 } 221 79 } 222 80 223 private void AddConnection(IShapeInfo shapeInfoFrom, string connectorName, IShapeInfo shapeInfoTo) { 224 IShape shapeFrom = this.shapeInfoShapeMapping.GetByFirst(shapeInfoFrom); 225 IShape shapeTo = this.shapeInfoShapeMapping.GetByFirst(shapeInfoTo); 226 227 IConnector connectorFrom = shapeFrom.Connectors.Where(c => c.Name == connectorName).First(); 228 IConnector connectorTo = shapeTo.Connectors.Where(c => c.Name == "Predecessor").FirstOrDefault(); 229 KeyValuePair<IConnector, IConnector> connectorPair = new KeyValuePair<IConnector, IConnector>(connectorFrom, connectorTo); 230 if (!this.connectionConnectorsMapping.ContainsSecond(connectorPair)) { 231 IConnection connection = Factory.CreateConnection(connectorFrom, connectorTo); 232 this.connectionConnectorsMapping.Add(connection, connectorPair); 233 this.graphVisualization.Controller.Model.AddConnection(connection); 234 this.graphVisualization.Invalidate(); 81 private void shapeContextMenu_Opening(object sender, System.ComponentModel.CancelEventArgs e) { 82 IShapeInfo shapeInfo = this.shapeContextMenu.Tag as ShapeInfo; 83 if (shapeInfo != null) { 84 IOperator op = this.VisualizationInfo.GetOperatorForShapeInfo(shapeInfo); 85 this.initialToolStripMenuItem.Checked = this.Content.InitialOperator == op; 86 this.breakPointToolStripMenuItem.Checked = op.Breakpoint; 235 87 } 236 88 } 237 89 238 private IConnection GetConnection(IShapeInfo shapeInfoFrom, string connectorName) { 239 IShape shape = this.shapeInfoShapeMapping.GetByFirst(shapeInfoFrom); 240 IConnector connector = shape.Connectors.Where(c => c.Name == connectorName).First(); 241 242 if (!this.connectionConnectorsMapping.SecondValues.Any(p => p.Key == connector)) 243 return null; 244 245 KeyValuePair<IConnector, IConnector> connectorPair = this.connectionConnectorsMapping.SecondValues.Where(p => p.Key == connector).FirstOrDefault(); 246 return this.connectionConnectorsMapping.GetBySecond(connectorPair); 247 } 248 249 private void ChangeConnection(IShapeInfo shapeInfoFrom, string connectorName, IShapeInfo shapeInfoTo) { 250 IConnection connection = this.GetConnection(shapeInfoFrom, connectorName); 251 IShape shapeTo = this.shapeInfoShapeMapping.GetByFirst(shapeInfoFrom); 252 IConnector connectorTo = shapeTo.Connectors.Where(c => c.Name == "Predecessor").First(); 253 254 connection.To.DetachFromParent(); 255 connection.To.AttachTo(connectorTo); 256 this.graphVisualization.Invalidate(); 257 } 258 259 private void RemoveConnection(IConnection connection) { 260 if (connection == null) 261 return; 262 263 if (connection.From.AttachedTo != null) 264 connection.From.DetachFromParent(); 265 if (connection.To.AttachedTo != null) 266 connection.To.DetachFromParent(); 267 268 if (this.connectionConnectorsMapping.ContainsFirst(connection)) 269 this.connectionConnectorsMapping.RemoveByFirst(connection); 270 if (this.graphVisualization.Controller.Model.Connections.Contains(connection)) 271 this.graphVisualization.Controller.Model.Remove(connection); 272 this.graphVisualization.Invalidate(); 273 } 274 275 276 private void shapeInfo_Changed(object sender, ChangedEventArgs e) { 277 if (this.causedUpdateOfShapeInfo) 278 return; 279 280 IShapeInfo shapeInfo = (IShapeInfo)sender; 281 IShape shape = this.shapeInfoShapeMapping.GetByFirst(shapeInfo); 282 shapeInfo.UpdateShape(shape); 283 shape.Invalidate(); 284 } 285 286 287 private void shape_OnEntityChange(object sender, EntityEventArgs e) { 288 this.causedUpdateOfShapeInfo = true; 289 IShape shape = e.Entity as IShape; 290 IShapeInfo shapeInfo = this.shapeInfoShapeMapping.GetBySecond(shape); 291 292 shapeInfo.Location = shape.Location; 293 shapeInfo.Size = new Size(shape.Width, shape.Height); 294 295 this.graphVisualization.Invalidate(); 296 this.causedUpdateOfShapeInfo = false; 297 } 298 299 private void graphVisualization_OnEntityAdded(object sender, EntityEventArgs e) { 300 IConnection connection = e.Entity as IConnection; 301 if (connection != null && !this.connectionConnectorsMapping.ContainsFirst(connection)) { 302 IConnector connectorFrom = connection.From.AttachedTo; 303 IConnector connectorTo = connection.To.AttachedTo; 304 this.RemoveConnection(connection); //is added again by the model events 305 306 if (connectorFrom != null && connectorTo != null) { 307 IShape shapeFrom = (IShape)connectorFrom.Parent; 308 IShape shapeTo = (IShape)connectorTo.Parent; 309 IShapeInfo shapeInfoFrom = this.shapeInfoShapeMapping.GetBySecond(shapeFrom); 310 IShapeInfo shapeInfoTo = this.shapeInfoShapeMapping.GetBySecond(shapeTo); 311 string connectorName = connectorFrom.Name; 312 313 this.VisualizationInfo.AddConnection(shapeInfoFrom, connectorName, shapeInfoTo); 314 } 90 private void openViewToolStripMenuItem_Click(object sender, EventArgs e) { 91 IShapeInfo shapeInfo = this.shapeContextMenu.Tag as ShapeInfo; 92 if (shapeInfo != null) { 93 IOperator op = this.VisualizationInfo.GetOperatorForShapeInfo(shapeInfo); 94 MainFormManager.CreateDefaultView(op).Show(); 315 95 } 316 96 } 317 97 318 private void graphVisualization_OnEntityRemoved(object sender, EntityEventArgs e) { 319 IShape shape = e.Entity as IShape; 320 if (shape != null && this.shapeInfoShapeMapping.ContainsSecond(shape)) { 321 IShapeInfo shapeInfo = this.shapeInfoShapeMapping.GetBySecond(shape); 322 this.VisualizationInfo.RemoveShapeInfo(shapeInfo); 323 } 98 private void initialOperatorToolStripMenuItem_Click(object sender, EventArgs e) { 99 IShapeInfo shapeInfo = this.shapeContextMenu.Tag as ShapeInfo; 100 if (this.VisualizationInfo.InitialShape == shapeInfo) 101 this.VisualizationInfo.InitialShape = null; 102 else 103 this.VisualizationInfo.InitialShape = shapeInfo; 104 } 324 105 325 IConnection connection = e.Entity as IConnection; 326 if (connection != null && this.connectionConnectorsMapping.ContainsFirst(connection)) { 327 IShape parentShape = (IShape)connection.From.AttachedTo.Parent; 328 IShapeInfo shapeInfo = this.shapeInfoShapeMapping.GetBySecond(parentShape); 329 string connectorName = connection.From.AttachedTo.Name; 330 331 this.VisualizationInfo.RemoveConnection(shapeInfo, connectorName); 106 private void breakPointToolStripMenuItem_Click(object sender, EventArgs e) { 107 IShapeInfo shapeInfo = this.shapeContextMenu.Tag as ShapeInfo; 108 if (shapeInfo != null) { 109 IOperator op = this.VisualizationInfo.GetOperatorForShapeInfo(shapeInfo); 110 op.Breakpoint = !op.Breakpoint; 332 111 } 333 112 } 113 #endregion 334 114 335 115 #region methods for toolbar items 336 337 116 internal void RelayoutOperatorGraph() { 338 if (this.shapeInfoShapeMapping.Count > 0 339 && this.connectionConnectorsMapping.Count > 0 340 && this.VisualizationInfo.InitialShape != null) { //otherwise the layout does not work 341 string layoutName = "Standard TreeLayout"; 342 this.graphVisualization.Controller.RunActivity(layoutName); 343 this.graphVisualization.Invalidate(); 344 } 117 this.graphVisualizationInfoView.RelayoutOperatorGraph(); 345 118 } 346 119 347 120 internal void ActivateConnectionTool() { 348 this.graphVisualization .Controller.ActivateTool(ControllerBase.ConnectionToolName);121 this.graphVisualizationInfoView.ActivateConnectionTool(); 349 122 } 350 123 351 124 internal void ActivateZoomAreaTool() { 352 this.graphVisualization .Controller.ActivateTool(ControllerBase.ZoomAreaToolName);125 this.graphVisualizationInfoView.ActivateZoomAreaTool(); 353 126 } 354 127 355 128 internal void ActivateZoomInTool() { 356 this.graphVisualization .Controller.ActivateTool(ControllerBase.ZoomInToolName);129 this.graphVisualizationInfoView.ActivateZoomInTool(); 357 130 } 358 131 359 132 internal void ActivateZoomOutTool() { 360 this.graphVisualization .Controller.ActivateTool(ControllerBase.ZoomOutToolName);133 this.graphVisualizationInfoView.ActivateZoomOutTool(); 361 134 } 362 135 363 136 internal void ActivatePanTool() { 364 this.graphVisualization .Controller.ActivateTool(ControllerBase.PanToolName);137 this.graphVisualizationInfoView.ActivatePanTool(); 365 138 } 366 139 367 140 internal void ActivateSelectTool() { 368 this.graphVisualization .Controller.ActivateTool(ControllerBase.SelectionToolName);141 this.graphVisualizationInfoView.ActivateSelectTool(); 369 142 } 370 371 143 #endregion 372 144 … … 384 156 IOperator op = e.Data.GetData("Value") as IOperator; 385 157 IShapeInfo shapeInfo = Factory.CreateShapeInfo(op); 386 Point controlCoordinates = this.PointToClient(new Point(e.X, e.Y)); 387 PointF viewCoordinates = this.graphVisualization.Controller.View.DeviceToView(controlCoordinates); 388 shapeInfo.Location = new Point((int)viewCoordinates.X, (int)viewCoordinates.Y); 158 Point mouse = new Point(MousePosition.X, MousePosition.Y); 159 Point p = new Point(e.X, e.Y); 160 Point screen = this.PointToScreen(new Point(0, 0)); 161 PointF worldPoint = this.graphVisualizationInfoView.Controller.View.WorldToView(new Point(mouse.X - screen.X, mouse.Y - screen.Y)); 162 163 shapeInfo.Location = Point.Round(worldPoint); 389 164 this.VisualizationInfo.AddShapeInfo(op, shapeInfo); 390 165 }
Note: See TracChangeset
for help on using the changeset viewer.