Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/01/10 11:45:00 (14 years ago)
Author:
mkommend
Message:

added context menu to OperatorGraphView (ticket #867)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Operators.Views.GraphVisualization/3.3/OperatorGraphView.cs

    r2875 r2893  
    4040  [Content(typeof(OperatorGraph), false)]
    4141  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;
    4742    public OperatorGraphView() {
    4843      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);
    5447    }
    5548
     
    6659    protected override void OnContentChanged() {
    6760      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;
    7063    }
    7164
    72     private OperatorGraphVisualizationInfo VisualizationInfo {
    73       get { return Content.VisualizationInfo as OperatorGraphVisualizationInfo; }
     65    private GraphVisualizationInfo VisualizationInfo {
     66      get { return Content.VisualizationInfo as GraphVisualizationInfo; }
    7467      set { this.Content.VisualizationInfo = value; }
    7568    }
    7669
    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));
    22078      }
    22179    }
    22280
    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;
    23587      }
    23688    }
    23789
    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();
    31595      }
    31696    }
    31797
    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    }
    324105
    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;
    332111      }
    333112    }
     113    #endregion
    334114
    335115    #region methods for toolbar items
    336 
    337116    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();
    345118    }
    346119
    347120    internal void ActivateConnectionTool() {
    348       this.graphVisualization.Controller.ActivateTool(ControllerBase.ConnectionToolName);
     121      this.graphVisualizationInfoView.ActivateConnectionTool();
    349122    }
    350123
    351124    internal void ActivateZoomAreaTool() {
    352       this.graphVisualization.Controller.ActivateTool(ControllerBase.ZoomAreaToolName);
     125      this.graphVisualizationInfoView.ActivateZoomAreaTool();
    353126    }
    354127
    355128    internal void ActivateZoomInTool() {
    356       this.graphVisualization.Controller.ActivateTool(ControllerBase.ZoomInToolName);
     129      this.graphVisualizationInfoView.ActivateZoomInTool();
    357130    }
    358131
    359132    internal void ActivateZoomOutTool() {
    360       this.graphVisualization.Controller.ActivateTool(ControllerBase.ZoomOutToolName);
     133      this.graphVisualizationInfoView.ActivateZoomOutTool();
    361134    }
    362135
    363136    internal void ActivatePanTool() {
    364       this.graphVisualization.Controller.ActivateTool(ControllerBase.PanToolName);
     137      this.graphVisualizationInfoView.ActivatePanTool();
    365138    }
    366139
    367140    internal void ActivateSelectTool() {
    368       this.graphVisualization.Controller.ActivateTool(ControllerBase.SelectionToolName);
     141      this.graphVisualizationInfoView.ActivateSelectTool();
    369142    }
    370 
    371143    #endregion
    372144
     
    384156        IOperator op = e.Data.GetData("Value") as IOperator;
    385157        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);
    389164        this.VisualizationInfo.AddShapeInfo(op, shapeInfo);
    390165      }
Note: See TracChangeset for help on using the changeset viewer.