Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Core.Views/3.3/OperatorGraphView.cs @ 3362

Last change on this file since 3362 was 3362, checked in by swagner, 14 years ago

Adapted views of HeuristicLab.Core.Views according the new read-only property and renamed method SetEnableStateOfControls into SetEnabledStateOfControls (#973).

File size: 6.4 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.ComponentModel;
24using System.Drawing;
25using System.Windows.Forms;
26using HeuristicLab.MainForm;
27
28namespace HeuristicLab.Core.Views {
29  /// <summary>
30  /// The visual representation of an <see cref="OperatorGraph"/>.
31  /// </summary>
32  [View("OperatorGraph View (Tree)",ShowInViewHost=true)]
33  [Content(typeof(OperatorGraph), false)]
34  public partial class OperatorGraphView : ItemView {
35    /// <summary>
36    /// Gets or sets the operator graph to represent visually.
37    /// </summary>
38    /// <remarks>Uses property <see cref="ViewBase.Item"/> of base class <see cref="ViewBase"/>.
39    /// No own data storage present.</remarks>
40    public new OperatorGraph Content {
41      get { return (OperatorGraph)base.Content; }
42      set { base.Content = value; }
43    }
44
45    /// <summary>
46    /// Initializes a new instance of <see cref="OperatorGraphView"/> with caption "Operator Graph".
47    /// </summary>
48    public OperatorGraphView() {
49      InitializeComponent();
50      Caption = "Operator Graph";
51    }
52    /// <summary>
53    /// Initializes a new instance of <see cref="OperatorGraphView"/>
54    /// with the given <paramref name="operatorGraph"/>.
55    /// </summary>
56    /// <remarks>Calls <see cref="OperatorGraphView()"/>.</remarks>
57    /// <param name="operatorGraph">The operator graph to represent visually.</param>
58    public OperatorGraphView(OperatorGraph content)
59      : this() {
60      Content = content;
61    }
62
63    /// <summary>
64    /// Removes the eventhandlers from the underlying <see cref="IOperatorGraph"/>.
65    /// </summary>
66    /// <remarks>Calls <see cref="ViewBase.RemoveItemEvents"/> of base class <see cref="ViewBase"/>.</remarks>
67    protected override void DeregisterContentEvents() {
68      Content.InitialOperatorChanged -= new EventHandler(Content_InitialOperatorChanged);
69      base.DeregisterContentEvents();
70    }
71
72    /// <summary>
73    /// Adds eventhandlers to the underlying <see cref="IOperatorGraph"/>.
74    /// </summary>
75    /// <remarks>Calls <see cref="ViewBase.AddItemEvents"/> of base class <see cref="ViewBase"/>.</remarks>
76    protected override void RegisterContentEvents() {
77      base.RegisterContentEvents();
78      Content.InitialOperatorChanged += new EventHandler(Content_InitialOperatorChanged);
79    }
80
81    /// <summary>
82    /// Updates all controls with the latest data of the model.
83    /// </summary>
84    /// <remarks>Calls <see cref="ViewBase.UpdateControls"/> of base class <see cref="ViewBase"/>.</remarks>
85    protected override void OnContentChanged() {
86      base.OnContentChanged();
87      Caption = "Operator Graph";
88      operatorsView.Content = null;
89      operatorTreeView.Content = null;
90      if (Content != null) {
91        Caption = Content.ItemName + " (" + Content.GetType().Name + ")";
92        operatorsView.Content = Content.Operators;
93        MarkInitialOperator();
94        operatorTreeView.Content = Content.InitialOperator;
95      }
96      SetEnabledStateOfControls();
97    }
98
99    protected override void OnReadOnlyChanged() {
100      base.OnReadOnlyChanged();
101      SetEnabledStateOfControls();
102    }
103
104    private void SetEnabledStateOfControls() {
105      operatorsView.Enabled = Content != null;
106      operatorTreeView.Enabled = Content != null;
107      operatorsView.ReadOnly = ReadOnly;
108      operatorTreeView.ReadOnly = ReadOnly;
109      operatorsContextMenuStrip.Enabled = Content != null && !ReadOnly;
110    }
111
112
113    protected virtual void MarkInitialOperator() {
114      foreach (ListViewItem item in operatorsView.ItemsListView.Items) {
115        if ((Content.InitialOperator != null) && (((IOperator)item.Tag) == Content.InitialOperator))
116          item.Font = new Font(operatorsView.ItemsListView.Font, FontStyle.Bold);
117        else
118          item.Font = operatorsView.ItemsListView.Font;
119      }
120    }
121
122    #region Operator Tree View Events
123    protected virtual void operatorTreeView_SelectedOperatorChanged(object sender, EventArgs e) {
124      foreach (ListViewItem item in operatorsView.ItemsListView.Items)
125        item.Selected = item.Tag == operatorTreeView.SelectedOperator;
126    }
127    #endregion
128
129    #region Context Menu Events
130    protected virtual void operatorsView_Load(object sender, EventArgs e) {
131      operatorsView.ItemsListView.ContextMenuStrip = operatorsContextMenuStrip;
132    }
133    protected virtual void operatorsContextMenuStrip_Opening(object sender, CancelEventArgs e) {
134      initialOperatorToolStripMenuItem.Enabled = false;
135      initialOperatorToolStripMenuItem.Checked = false;
136      if (operatorsView.ItemsListView.SelectedItems.Count == 1) {
137        IOperator op = (IOperator)operatorsView.ItemsListView.SelectedItems[0].Tag;
138        initialOperatorToolStripMenuItem.Enabled = true;
139        initialOperatorToolStripMenuItem.Tag = op;
140        if (op == Content.InitialOperator)
141          initialOperatorToolStripMenuItem.Checked = true;
142      }
143    }
144    protected virtual void initialOperatorToolStripMenuItem_Click(object sender, EventArgs e) {
145      if (initialOperatorToolStripMenuItem.Checked)
146        Content.InitialOperator = (IOperator)initialOperatorToolStripMenuItem.Tag;
147      else
148        Content.InitialOperator = null;
149    }
150    #endregion
151
152    #region Content Events
153    protected virtual void Content_InitialOperatorChanged(object sender, EventArgs e) {
154      if (InvokeRequired)
155        Invoke(new EventHandler(Content_InitialOperatorChanged), sender, e);
156      else {
157        MarkInitialOperator();
158        operatorTreeView.Content = Content.InitialOperator;
159      }
160    }
161    #endregion
162  }
163}
Note: See TracBrowser for help on using the repository browser.