Free cookie consent management tool by TermsFeed Policy Generator

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

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

Committing first results of the refactoring of HeuristicLab.Core and related plugins (#95)

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