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 e) {
|
---|
122 | if (InvokeRequired) { Invoke((Action<object, EventArgs>)AlgorithmOnExecutionStateChanged, sender, e); return; }
|
---|
123 | if (ShowOnlyFinalResult && (Algorithm.ExecutionState == ExecutionState.Stopped
|
---|
124 | || Algorithm.ExecutionState == ExecutionState.Paused))
|
---|
125 | view.Content = Algorithm.Results;
|
---|
126 | SetEnabledStateOfControls();
|
---|
127 | }
|
---|
128 | #endregion
|
---|
129 |
|
---|
130 | #region Button Events
|
---|
131 | private void stopButton_Click(object sender, EventArgs e) {
|
---|
132 | if (Algorithm != null && Algorithm.ExecutionState == ExecutionState.Started)
|
---|
133 | Algorithm.Stop();
|
---|
134 | }
|
---|
135 | #endregion
|
---|
136 |
|
---|
137 | #region View Events
|
---|
138 | private void View_CaptionChanged(object sender, EventArgs e) {
|
---|
139 | UpdateText();
|
---|
140 | }
|
---|
141 | #endregion
|
---|
142 |
|
---|
143 | #region Context Menu Events
|
---|
144 | private void contextMenuStrip_Opening(object sender, System.ComponentModel.CancelEventArgs e) {
|
---|
145 | var contentView = View as IContentView;
|
---|
146 | var content = contentView != null ? contentView.Content : null;
|
---|
147 |
|
---|
148 | cloneToolStripMenuItem.Enabled = contentView != null && !contentView.Locked && content is IDeepCloneable;
|
---|
149 | }
|
---|
150 |
|
---|
151 | private void closeToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
152 | Close();
|
---|
153 | }
|
---|
154 | private void closeAllToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
155 | foreach (var dockForm in CurrentDockForms) {
|
---|
156 | dockForm.Close();
|
---|
157 | }
|
---|
158 | }
|
---|
159 | private void closeAllButThisToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
160 | foreach (var dockForm in CurrentDockForms.Except(this.ToEnumerable())) {
|
---|
161 | dockForm.Close();
|
---|
162 | }
|
---|
163 | }
|
---|
164 | private IEnumerable<AlgorithmControlForm> CurrentDockForms {
|
---|
165 | get {
|
---|
166 | var dockForms = Pane.Contents.OfType<AlgorithmControlForm>().Where(c => c.Pane == Pane); // Pane.Contents contains DockForms that are not placed on that pane
|
---|
167 | return dockForms.ToList(); // .ToList() necessary because closing a DockForm removes it from the Content collection
|
---|
168 | }
|
---|
169 | }
|
---|
170 |
|
---|
171 | private void cloneToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
172 | var contentView = View as IContentView;
|
---|
173 | if (contentView == null) return;
|
---|
174 |
|
---|
175 | var cloneable = contentView.Content as IDeepCloneable;
|
---|
176 | if (cloneable == null) return;
|
---|
177 |
|
---|
178 | var clone = (IContent)cloneable.Clone();
|
---|
179 |
|
---|
180 | var viewHost = contentView as ViewHost;
|
---|
181 | var newView = viewHost != null ? viewHost.ViewType : contentView.GetType();
|
---|
182 | MainFormManager.MainForm.ShowContent(clone, newView);
|
---|
183 | }
|
---|
184 | #endregion
|
---|
185 | }
|
---|
186 | }
|
---|