Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 2701 was 2676, checked in by swagner, 15 years ago

Operator architecture refactoring (#95)

  • continued work on adapting and refactoring HeuristicLab.Data according to the changes in HeuristicLab.Core
  • unified visual appearance of views
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 : ItemView {
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    protected virtual 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    protected virtual void operatorsView_Load(object sender, EventArgs e) {
123      operatorsView.ItemsListView.ContextMenuStrip = operatorsContextMenuStrip;
124    }
125    protected virtual void operatorsContextMenuStrip_Opening(object sender, CancelEventArgs e) {
126      initialOperatorToolStripMenuItem.Enabled = false;
127      initialOperatorToolStripMenuItem.Checked = false;
128      if (operatorsView.ItemsListView.SelectedItems.Count == 1) {
129        IOperator op = (IOperator)operatorsView.ItemsListView.SelectedItems[0].Tag;
130        initialOperatorToolStripMenuItem.Enabled = true;
131        initialOperatorToolStripMenuItem.Tag = op;
132        if (op == OperatorGraph.InitialOperator)
133          initialOperatorToolStripMenuItem.Checked = true;
134      }
135    }
136    protected virtual void initialOperatorToolStripMenuItem_Click(object sender, EventArgs e) {
137      if (initialOperatorToolStripMenuItem.Checked)
138        OperatorGraph.InitialOperator = (IOperator)initialOperatorToolStripMenuItem.Tag;
139      else
140        OperatorGraph.InitialOperator = null;
141    }
142    #endregion
143
144    #region OperatorGraph Events
145    protected virtual void OperatorGraph_InitialOperatorChanged(object sender, EventArgs e) {
146      if (InvokeRequired)
147        Invoke(new EventHandler(OperatorGraph_InitialOperatorChanged), sender, e);
148      else {
149        MarkInitialOperator();
150        graphView.Operator = OperatorGraph.InitialOperator;
151      }
152    }
153    #endregion
154  }
155}
Note: See TracBrowser for help on using the repository browser.