Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2205_OptimizationNetworks/HeuristicLab.Networks.Views/3.3/AlgorithmNodeView.cs @ 17945

Last change on this file since 17945 was 11577, checked in by swagner, 10 years ago

#2205: Restructured solution and projects and switched all projects to .NET 4.5

File size: 5.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2014 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 HeuristicLab.Core.Networks.Views;
23using HeuristicLab.Core.Views;
24using HeuristicLab.MainForm;
25using HeuristicLab.Optimization;
26using HeuristicLab.PluginInfrastructure;
27using System;
28using System.Windows.Forms;
29
30namespace HeuristicLab.Networks.Views {
31  [View("AlgorithmNode View")]
32  [Content(typeof(AlgorithmNode), true)]
33  [Content(typeof(IAlgorithmNode), false)]
34  public partial class AlgorithmNodeView : NetworkItemView {
35    protected TypeSelectorDialog typeSelectorDialog;
36
37    public new IAlgorithmNode Content {
38      get { return (IAlgorithmNode)base.Content; }
39      set { base.Content = value; }
40    }
41
42    public AlgorithmNodeView() {
43      InitializeComponent();
44    }
45
46    protected override void Dispose(bool disposing) {
47      if (disposing) {
48        if (typeSelectorDialog != null) typeSelectorDialog.Dispose();
49        if (components != null) components.Dispose();
50      }
51      base.Dispose(disposing);
52    }
53
54    protected override void DeregisterContentEvents() {
55      Content.AlgorithmChanged -= Content_AlgorithmChanged;
56      base.DeregisterContentEvents();
57    }
58    protected override void RegisterContentEvents() {
59      base.RegisterContentEvents();
60      Content.AlgorithmChanged += Content_AlgorithmChanged;
61    }
62
63    protected override void OnContentChanged() {
64      base.OnContentChanged();
65      if (Content == null) {
66        portCollectionView.Content = null;
67        algorithmViewHost.Content = null;
68        runCollectionView.Content = null;
69      } else {
70        portCollectionView.Content = Content.Ports;
71        algorithmViewHost.ViewType = null;
72        algorithmViewHost.Content = Content.Algorithm;
73        runCollectionView.Content = Content.Runs;
74      }
75    }
76
77    protected override void SetEnabledStateOfControls() {
78      base.SetEnabledStateOfControls();
79      portCollectionView.Enabled = Content != null && !ReadOnly;
80      setAlgorithmButton.Enabled = Content != null && !ReadOnly;
81      clearAlgorithmButton.Enabled = Content != null && Content.Algorithm != null && !ReadOnly;
82      algorithmPanel.Enabled = Content != null;
83      runCollectionView.Enabled = Content != null && !ReadOnly;
84    }
85
86    protected virtual void Content_AlgorithmChanged(object sender, System.EventArgs e) {
87      if (InvokeRequired)
88        Invoke(new EventHandler(Content_AlgorithmChanged), sender, e);
89      else {
90        clearAlgorithmButton.Enabled = Content.Algorithm != null && !ReadOnly;
91        algorithmViewHost.ViewType = null;
92        algorithmViewHost.Content = Content.Algorithm;
93      }
94    }
95
96    protected virtual void setAlgorithmButton_Click(object sender, EventArgs e) {
97      if (typeSelectorDialog == null) {
98        typeSelectorDialog = new TypeSelectorDialog();
99        typeSelectorDialog.Caption = "Select Algorithm";
100        typeSelectorDialog.TypeSelector.Configure(typeof(IAlgorithm), false, true);
101      }
102      if (typeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
103        try {
104          Content.Algorithm = (IAlgorithm)typeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
105        }
106        catch (Exception ex) {
107          ErrorHandling.ShowErrorDialog(this, ex);
108        }
109      }
110    }
111    protected virtual void clearAlgorithmButton_Click(object sender, EventArgs e) {
112      Content.Algorithm = null;
113    }
114    protected virtual void algorithmPanel_DragEnterOver(object sender, DragEventArgs e) {
115      e.Effect = DragDropEffects.None;
116      if (!ReadOnly && (e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) is IAlgorithm)) {
117        if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link;  // ALT key
118        else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move;  // SHIFT key
119        else if (e.AllowedEffect.HasFlag(DragDropEffects.Copy)) e.Effect = DragDropEffects.Copy;
120        else if (e.AllowedEffect.HasFlag(DragDropEffects.Move)) e.Effect = DragDropEffects.Move;
121        else if (e.AllowedEffect.HasFlag(DragDropEffects.Link)) e.Effect = DragDropEffects.Link;
122      }
123    }
124    protected virtual void algorithmPanel_DragDrop(object sender, DragEventArgs e) {
125      if (e.Effect != DragDropEffects.None) {
126        IAlgorithm alg = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) as IAlgorithm;
127        if (e.Effect.HasFlag(DragDropEffects.Copy)) alg = (IAlgorithm)alg.Clone();
128        Content.Algorithm = alg;
129      }
130    }
131  }
132}
Note: See TracBrowser for help on using the repository browser.