Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Optimization.Views/3.3/EngineAlgorithmView.cs @ 3367

Last change on this file since 3367 was 3367, checked in by mkommend, 14 years ago

adapted HL.Optimization.Views to new ReadOnly mechanism (ticket #973)

File size: 6.0 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.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.MainForm;
28using HeuristicLab.PluginInfrastructure;
29
30namespace HeuristicLab.Optimization.Views {
31  /// <summary>
32  /// The base class for visual representations of items.
33  /// </summary>
34  [View("EngineAlgorithm View")]
35  [Content(typeof(EngineAlgorithm), true)]
36  public partial class EngineAlgorithmView : AlgorithmView {
37    private List<Type> engineTypes;
38
39    public new EngineAlgorithm Content {
40      get { return (EngineAlgorithm)base.Content; }
41      set { base.Content = value; }
42    }
43
44    /// <summary>
45    /// Initializes a new instance of <see cref="ItemBaseView"/>.
46    /// </summary>
47    public EngineAlgorithmView() {
48      InitializeComponent();
49    }
50
51    /// <summary>
52    /// Intializes a new instance of <see cref="ItemBaseView"/> with the given <paramref name="item"/>.
53    /// </summary>
54    /// <param name="item">The item that should be displayed.</param>
55    public EngineAlgorithmView(EngineAlgorithm content)
56      : this() {
57      Content = content;
58    }
59
60    protected override void DeregisterContentEvents() {
61      Content.EngineChanged -= new EventHandler(Content_EngineChanged);
62      Content.OperatorGraphChanged -= new EventHandler(Content_OperatorGraphChanged);
63      base.DeregisterContentEvents();
64    }
65    protected override void RegisterContentEvents() {
66      base.RegisterContentEvents();
67      Content.EngineChanged += new EventHandler(Content_EngineChanged);
68      Content.OperatorGraphChanged += new EventHandler(Content_OperatorGraphChanged);
69    }
70
71    protected override void OnContentChanged() {
72      base.OnContentChanged();
73
74      if (engineTypes == null) {
75        engineTypes = ApplicationManager.Manager.GetTypes(typeof(IEngine)).
76                      OrderBy(x => {
77                        string name = ItemAttribute.GetName(x);
78                        if (name != null) return name;
79                        else return x.GetPrettyName();
80                      }).ToList();
81        foreach (Type t in engineTypes) {
82          string name = ItemAttribute.GetName(t);
83          if (name == null) name = t.GetPrettyName();
84          engineComboBox.Items.Add(name);
85        }
86      }
87
88      if (Content == null) {
89        engineViewHost.Content = null;
90        createUserDefinedAlgorithmButton.Enabled = false;
91        operatorGraphViewHost.Content = null;
92      } else {
93        if (Content.Engine == null)
94          engineComboBox.SelectedIndex = -1;
95        else
96          engineComboBox.SelectedIndex = engineTypes.IndexOf(Content.Engine.GetType());
97        engineViewHost.ViewType = null;
98        engineViewHost.Content = Content.Engine;
99        operatorGraphViewHost.ViewType = null;
100        operatorGraphViewHost.Content = Content.OperatorGraph;
101        createUserDefinedAlgorithmButton.Enabled = true;
102      }
103      SetEnableStateOfControls();
104    }
105
106    protected override void OnReadOnlyChanged() {
107      base.OnReadOnlyChanged();
108      SetEnableStateOfControls();
109    }
110    private void SetEnableStateOfControls() {
111      engineViewHost.ReadOnly = ReadOnly;
112      if (Content == null)
113        engineComboBox.Enabled = false;
114      else
115        engineComboBox.Enabled = Content.ExecutionState != ExecutionState.Started;
116    }
117
118    protected override void Content_ExecutionStateChanged(object sender, EventArgs e) {
119      if (InvokeRequired)
120        Invoke(new EventHandler(Content_ExecutionStateChanged), sender, e);
121      else {
122        createUserDefinedAlgorithmButton.Enabled = Content.ExecutionState != ExecutionState.Started;
123        base.Content_ExecutionStateChanged(sender, e);
124      }
125    }
126    protected virtual void Content_EngineChanged(object sender, System.EventArgs e) {
127      if (InvokeRequired)
128        Invoke(new EventHandler(Content_EngineChanged), sender, e);
129      else {
130        if (Content.Engine == null)
131          engineComboBox.SelectedIndex = -1;
132        else
133          engineComboBox.SelectedIndex = engineTypes.IndexOf(Content.Engine.GetType());
134        engineViewHost.ViewType = null;
135        engineViewHost.Content = Content.Engine;
136      }
137    }
138    private void Content_OperatorGraphChanged(object sender, EventArgs e) {
139      if (InvokeRequired)
140        Invoke(new EventHandler(Content_OperatorGraphChanged), sender, e);
141      else {
142        operatorGraphViewHost.ViewType = null;
143        operatorGraphViewHost.Content = Content.OperatorGraph;
144      }
145    }
146
147    protected virtual void engineComboBox_SelectedIndexChanged(object sender, EventArgs e) {
148      if (Content != null) {
149        if (engineComboBox.SelectedIndex == -1)
150          Content.Engine = null;
151        else {
152          Type t = engineTypes[engineComboBox.SelectedIndex];
153          if ((Content.Engine == null) || (Content.Engine.GetType() != t))
154            Content.Engine = (IEngine)Activator.CreateInstance(t);
155        }
156      }
157    }
158
159    protected virtual void createUserDefinedAlgorithmButton_Click(object sender, EventArgs e) {
160      MainFormManager.CreateDefaultView(Content.CreateUserDefinedAlgorithm()).Show();
161    }
162  }
163}
Note: See TracBrowser for help on using the repository browser.