Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.DebugEngine/ExecutionStackView.cs @ 5152

Last change on this file since 5152 was 4998, checked in by epitzer, 14 years ago

remove resources, add license headers, update plug-in dependencies (#47)

File size: 5.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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 System;
23using System.Collections.Generic;
24using System.Drawing;
25using System.Linq;
26using System.Windows.Forms;
27using HeuristicLab.Collections;
28using HeuristicLab.Common.Resources;
29using HeuristicLab.Core;
30using HeuristicLab.MainForm;
31using HeuristicLab.MainForm.WindowsForms;
32
33namespace HeuristicLab.DebugEngine {
34
35  [View("Execution Stack View")]
36  [Content(typeof(ExecutionStack), IsDefaultView = true)]
37  public partial class ExecutionStackView : AsynchronousContentView {
38    public new ExecutionStack Content {
39      get { return (ExecutionStack)base.Content; }
40      set { base.Content = value; }
41    }
42
43    public ExecutionStackView() {
44      InitializeComponent();
45    }
46
47    protected override void DeregisterContentEvents() {
48      Content.CollectionReset -= Content_Changed;
49      Content.ItemsAdded -= Content_Changed;
50      Content.ItemsRemoved -= Content_Changed;
51      Content.ItemsMoved -= Content_Changed;
52      Content.ItemsReplaced -= Content_Changed;
53      base.DeregisterContentEvents();
54    }
55
56    protected override void RegisterContentEvents() {
57      base.RegisterContentEvents();
58      Content.CollectionReset += Content_Changed;
59      Content.ItemsAdded += Content_Changed;
60      Content.ItemsRemoved += Content_Changed;
61      Content.ItemsMoved += Content_Changed;
62      Content.ItemsReplaced += Content_Changed;
63    }
64
65    #region Event Handlers (Content)
66    void Content_Changed(object sender, CollectionItemsChangedEventArgs<IndexedItem<IOperation>> e) {
67      if (InvokeRequired)
68        Invoke(new EventHandler<CollectionItemsChangedEventArgs<IndexedItem<IOperation>>>(Content_Changed), sender, e);
69      else
70        UpdateExecutionStack();
71    }
72
73
74    private void UpdateExecutionStack() {
75      if (suspended) {
76        groupBox.Text = string.Format("Execution Stack ({0})", CountOperations(Content));
77      } else {
78        treeView.BeginUpdate();
79        treeView.Nodes.Clear();
80        treeView.ImageList.Images.Clear();
81        treeView.ImageList.Images.Add(VS2008ImageLibrary.Method);
82        treeView.ImageList.Images.Add(VS2008ImageLibrary.Module);
83        int totalNodes = AddStackOperations(treeView.Nodes, ((IEnumerable<IOperation>)Content).Reverse());
84        if (treeView.Nodes.Count > 0)
85          treeView.TopNode = treeView.Nodes[0];
86        treeView.ExpandAll();
87        treeView.EndUpdate();
88        groupBox.Text = string.Format("Execution Stack ({0})", totalNodes);
89      }
90    }
91
92    private int CountOperations(IEnumerable<IOperation> operations) {
93      int count = 0;
94      foreach (IOperation op in operations) {
95        count++;
96        OperationCollection ops = op as OperationCollection;
97        if (ops != null)
98          count += CountOperations(ops);
99      }
100      return count;
101    }
102
103    private int AddStackOperations(TreeNodeCollection nodes, IEnumerable<IOperation> operations) {
104      int count = 0;
105      foreach (IOperation op in operations) {
106        if (op is IAtomicOperation) {
107          IAtomicOperation atom = op as IAtomicOperation;
108          TreeNode node = nodes.Add(atom.Operator.Name ?? atom.Operator.ItemName);
109          node.Tag = atom;
110          node.ToolTipText = string.Format("{0}{1}{1}{2}",
111            Utils.TypeName(atom.Operator), Environment.NewLine,
112            Utils.Wrap(atom.Operator.Description ?? atom.Operator.ItemDescription, 60));
113          node.ImageIndex = 0;
114          node.SelectedImageIndex = 0;
115          if (atom.Operator.Breakpoint)
116            node.ForeColor = Color.Red;
117          count++;
118        } else if (op is OperationCollection) {
119          OperationCollection ops = op as OperationCollection;
120          TreeNode node = treeView.Nodes.Add(
121            string.Format("{0} Operation{1}", ops.Count, ops.Count == 1 ? string.Empty : "s"));
122          node.Tag = op;
123          node.ToolTipText = Utils.TypeName(ops);
124          node.ImageIndex = 1;
125          node.SelectedImageIndex = 1;
126          count += AddStackOperations(node.Nodes, ops);
127        }
128      }
129      return count;
130    }
131    #endregion
132
133    protected override void OnContentChanged() {
134      base.OnContentChanged();
135      if (Content == null) {
136        treeView.Nodes.Clear();
137      } else {
138        UpdateExecutionStack();
139      }
140    }
141
142
143    protected override void SetEnabledStateOfControls() {
144      base.SetEnabledStateOfControls();
145    }
146
147    #region Event Handlers (child controls)
148    private void treeView_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e) {
149      if (e.Node != null) {
150        IAtomicOperation op = e.Node.Tag as IAtomicOperation;
151        if (op != null)
152          MainFormManager.MainForm.ShowContent(op.Operator);
153      }
154    }
155    #endregion
156
157    #region Suspension
158    private bool suspended = false;
159    public virtual void SuspendUpdate() {
160      suspended = true;
161      treeView.Nodes.Clear();
162    }
163
164    public virtual void ResumeUpdate() {
165      suspended = false;
166      if (Content != null)
167        UpdateExecutionStack();
168    }
169    #endregion
170  }
171}
Note: See TracBrowser for help on using the repository browser.