1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2015 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 |
|
---|
22 | using HeuristicLab.Common;
|
---|
23 | using HeuristicLab.Common.Resources;
|
---|
24 | using HeuristicLab.Core;
|
---|
25 | using HeuristicLab.MainForm;
|
---|
26 | using HeuristicLab.MainForm.WindowsForms;
|
---|
27 | using HeuristicLab.Optimization;
|
---|
28 | using System;
|
---|
29 | using System.Collections.Generic;
|
---|
30 | using System.Linq;
|
---|
31 | using System.Windows.Forms;
|
---|
32 | using WeifenLuo.WinFormsUI.Docking;
|
---|
33 |
|
---|
34 | namespace HeuristicLab.OptimizationExpertSystem {
|
---|
35 | /// <summary>
|
---|
36 | /// Displays the used view.
|
---|
37 | /// </summary>
|
---|
38 | internal partial class AlgorithmControlForm : DockContent {
|
---|
39 | private IContentView view;
|
---|
40 | public IContentView View {
|
---|
41 | get { return this.view; }
|
---|
42 | }
|
---|
43 | private IAlgorithm Algorithm { get; set; }
|
---|
44 | private bool ShowOnlyFinalResult { get; set; }
|
---|
45 |
|
---|
46 | public AlgorithmControlForm(IAlgorithm algorithm, bool showOnlyFinalResult = false) {
|
---|
47 | if (algorithm == null) throw new ArgumentNullException("algorithm");
|
---|
48 | InitializeComponent();
|
---|
49 | Algorithm = algorithm;
|
---|
50 | ShowOnlyFinalResult = showOnlyFinalResult;
|
---|
51 | this.view = MainFormManager.CreateDefaultView(algorithm.Results.GetType());
|
---|
52 | this.stopButton.Text = string.Empty;
|
---|
53 | this.stopButton.Image = VSImageLibrary.Stop;
|
---|
54 |
|
---|
55 | InitializeView();
|
---|
56 | RegisterAlgorithmEvents();
|
---|
57 | SetEnabledStateOfControls();
|
---|
58 | }
|
---|
59 |
|
---|
60 | private void InitializeView() {
|
---|
61 | if (view != null) {
|
---|
62 | var userControl = view as UserControl;
|
---|
63 | if (userControl != null) {
|
---|
64 | switch (userControl.Dock) {
|
---|
65 | case DockStyle.Left:
|
---|
66 | this.ShowHint = DockState.DockLeft;
|
---|
67 | break;
|
---|
68 | case DockStyle.Right:
|
---|
69 | this.ShowHint = DockState.DockRight;
|
---|
70 | break;
|
---|
71 | case DockStyle.Top:
|
---|
72 | this.ShowHint = DockState.DockTop;
|
---|
73 | break;
|
---|
74 | case DockStyle.Bottom:
|
---|
75 | this.ShowHint = DockState.DockBottom;
|
---|
76 | break;
|
---|
77 | }
|
---|
78 |
|
---|
79 | var control = (Control)view;
|
---|
80 | control.Dock = DockStyle.Fill;
|
---|
81 | this.view.CaptionChanged += View_CaptionChanged;
|
---|
82 | UpdateText();
|
---|
83 | viewPanel.Controls.Add(control);
|
---|
84 | }
|
---|
85 | if (!ShowOnlyFinalResult) view.Content = Algorithm.Results;
|
---|
86 | } else {
|
---|
87 | var errorLabel = new Label();
|
---|
88 | errorLabel.Name = "errorLabel";
|
---|
89 | errorLabel.Text = "No view available";
|
---|
90 | errorLabel.AutoSize = false;
|
---|
91 | errorLabel.Dock = DockStyle.Fill;
|
---|
92 | viewPanel.Controls.Add(errorLabel);
|
---|
93 | }
|
---|
94 | }
|
---|
95 |
|
---|
96 | private void RegisterAlgorithmEvents() {
|
---|
97 | Algorithm.ExecutionStateChanged += AlgorithmOnExecutionStateChanged;
|
---|
98 | }
|
---|
99 |
|
---|
100 | protected override void Dispose(bool disposing) {
|
---|
101 | if (View != null)
|
---|
102 | View.CaptionChanged -= View_CaptionChanged;
|
---|
103 | if (disposing && (components != null)) {
|
---|
104 | components.Dispose();
|
---|
105 | }
|
---|
106 | base.Dispose(disposing);
|
---|
107 | }
|
---|
108 |
|
---|
109 | private void SetEnabledStateOfControls() {
|
---|
110 | stopButton.Enabled = Algorithm.ExecutionState == ExecutionState.Started;
|
---|
111 | }
|
---|
112 |
|
---|
113 | private void UpdateText() {
|
---|
114 | if (InvokeRequired)
|
---|
115 | Invoke(new MethodInvoker(UpdateText));
|
---|
116 | else
|
---|
117 | this.Text = this.View.Caption;
|
---|
118 | }
|
---|
119 |
|
---|
120 | #region Algorithm Events
|
---|
121 | private void AlgorithmOnExecutionStateChanged(object sender, EventArgs eventArgs) {
|
---|
122 | if (ShowOnlyFinalResult && (Algorithm.ExecutionState == ExecutionState.Stopped
|
---|
123 | || Algorithm.ExecutionState == ExecutionState.Paused))
|
---|
124 | view.Content = Algorithm.Results;
|
---|
125 | SetEnabledStateOfControls();
|
---|
126 | }
|
---|
127 | #endregion
|
---|
128 |
|
---|
129 | #region Button Events
|
---|
130 | private void stopButton_Click(object sender, EventArgs e) {
|
---|
131 | if (Algorithm != null && Algorithm.ExecutionState == ExecutionState.Started)
|
---|
132 | Algorithm.Stop();
|
---|
133 | }
|
---|
134 | #endregion
|
---|
135 |
|
---|
136 | #region View Events
|
---|
137 | private void View_CaptionChanged(object sender, EventArgs e) {
|
---|
138 | UpdateText();
|
---|
139 | }
|
---|
140 | #endregion
|
---|
141 |
|
---|
142 | #region Context Menu Events
|
---|
143 | private void contextMenuStrip_Opening(object sender, System.ComponentModel.CancelEventArgs e) {
|
---|
144 | var contentView = View as IContentView;
|
---|
145 | var content = contentView != null ? contentView.Content : null;
|
---|
146 |
|
---|
147 | cloneToolStripMenuItem.Enabled = contentView != null && !contentView.Locked && content is IDeepCloneable;
|
---|
148 | }
|
---|
149 |
|
---|
150 | private void closeToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
151 | Close();
|
---|
152 | }
|
---|
153 | private void closeAllToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
154 | foreach (var dockForm in CurrentDockForms) {
|
---|
155 | dockForm.Close();
|
---|
156 | }
|
---|
157 | }
|
---|
158 | private void closeAllButThisToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
159 | foreach (var dockForm in CurrentDockForms.Except(this.ToEnumerable())) {
|
---|
160 | dockForm.Close();
|
---|
161 | }
|
---|
162 | }
|
---|
163 | private IEnumerable<AlgorithmControlForm> CurrentDockForms {
|
---|
164 | get {
|
---|
165 | var dockForms = Pane.Contents.OfType<AlgorithmControlForm>().Where(c => c.Pane == Pane); // Pane.Contents contains DockForms that are not placed on that pane
|
---|
166 | return dockForms.ToList(); // .ToList() necessary because closing a DockForm removes it from the Content collection
|
---|
167 | }
|
---|
168 | }
|
---|
169 |
|
---|
170 | private void cloneToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
171 | var contentView = View as IContentView;
|
---|
172 | if (contentView == null) return;
|
---|
173 |
|
---|
174 | var cloneable = contentView.Content as IDeepCloneable;
|
---|
175 | if (cloneable == null) return;
|
---|
176 |
|
---|
177 | var clone = (IContent)cloneable.Clone();
|
---|
178 |
|
---|
179 | var viewHost = contentView as ViewHost;
|
---|
180 | var newView = viewHost != null ? viewHost.ViewType : contentView.GetType();
|
---|
181 | MainFormManager.MainForm.ShowContent(clone, newView);
|
---|
182 | }
|
---|
183 | #endregion
|
---|
184 | }
|
---|
185 | }
|
---|