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 |
|
---|
22 | using System;
|
---|
23 | using HeuristicLab.Common;
|
---|
24 | using HeuristicLab.MainForm;
|
---|
25 |
|
---|
26 | namespace HeuristicLab.Core.Views {
|
---|
27 | /// <summary>
|
---|
28 | /// Base class for editors of engines.
|
---|
29 | /// </summary>
|
---|
30 | [View("Engine View")]
|
---|
31 | [Content(typeof(Engine), true)]
|
---|
32 | [Content(typeof(IEngine), false)]
|
---|
33 | public partial class EngineView : ItemView {
|
---|
34 | private int executionTimeCounter;
|
---|
35 |
|
---|
36 | /// <summary>
|
---|
37 | /// Gets or sets the current engine.
|
---|
38 | /// </summary>
|
---|
39 | /// <remarks>Uses property <see cref="ViewBase.Item"/> of base class <see cref="EditorBase"/>.</remarks>
|
---|
40 | public new IEngine Content {
|
---|
41 | get { return (IEngine)base.Content; }
|
---|
42 | set { base.Content = value; }
|
---|
43 | }
|
---|
44 |
|
---|
45 | /// <summary>
|
---|
46 | /// Initializes a new instance of <see cref="EngineBaseEditor"/>.
|
---|
47 | /// </summary>
|
---|
48 | public EngineView() {
|
---|
49 | InitializeComponent();
|
---|
50 | }
|
---|
51 | public EngineView(IEngine content)
|
---|
52 | : this() {
|
---|
53 | Content = content;
|
---|
54 | }
|
---|
55 |
|
---|
56 | /// <summary>
|
---|
57 | /// Removes the event handlers from the underlying <see cref="IEngine"/>.
|
---|
58 | /// </summary>
|
---|
59 | /// <remarks>Calls <see cref="ViewBase.RemoveItemEvents"/> of base class <see cref="ViewBase"/>.</remarks>
|
---|
60 | protected override void DeregisterContentEvents() {
|
---|
61 | Content.Prepared -= new EventHandler(Content_Prepared);
|
---|
62 | Content.RunningChanged -= new EventHandler(Content_RunningChanged);
|
---|
63 | Content.ExecutionTimeChanged -= new EventHandler(Content_ExecutionTimeChanged);
|
---|
64 | Content.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred);
|
---|
65 | base.DeregisterContentEvents();
|
---|
66 | }
|
---|
67 |
|
---|
68 | /// <summary>
|
---|
69 | /// Adds event handlers to the underlying <see cref="IEngine"/>.
|
---|
70 | /// </summary>
|
---|
71 | /// <remarks>Calls <see cref="ViewBase.AddItemEvents"/> of base class <see cref="ViewBase"/>.</remarks>
|
---|
72 | protected override void RegisterContentEvents() {
|
---|
73 | base.RegisterContentEvents();
|
---|
74 | Content.Prepared += new EventHandler(Content_Prepared);
|
---|
75 | Content.RunningChanged += new EventHandler(Content_RunningChanged);
|
---|
76 | Content.ExecutionTimeChanged += new EventHandler(Content_ExecutionTimeChanged);
|
---|
77 | Content.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred);
|
---|
78 | }
|
---|
79 |
|
---|
80 | /// <summary>
|
---|
81 | /// Updates all controls with the latest data of the model.
|
---|
82 | /// </summary>
|
---|
83 | /// <remarks>Calls <see cref="EditorBase.UpdateControls"/> of base class <see cref="EditorBase"/>.</remarks>
|
---|
84 | protected override void OnContentChanged() {
|
---|
85 | base.OnContentChanged();
|
---|
86 | logTextBox.Clear();
|
---|
87 | if (Content == null) {
|
---|
88 | logTextBox.Enabled = false;
|
---|
89 | executionTimeTextBox.Enabled = false;
|
---|
90 | } else {
|
---|
91 | logTextBox.Enabled = true;
|
---|
92 | UpdateExecutionTimeTextBox();
|
---|
93 | executionTimeTextBox.Enabled = true;
|
---|
94 | }
|
---|
95 | }
|
---|
96 |
|
---|
97 | #region Content Events
|
---|
98 | protected virtual void Content_Prepared(object sender, EventArgs e) {
|
---|
99 | if (InvokeRequired)
|
---|
100 | Invoke(new EventHandler(Content_Prepared), sender, e);
|
---|
101 | else {
|
---|
102 | executionTimeCounter = 0;
|
---|
103 | UpdateExecutionTimeTextBox();
|
---|
104 | Log("Engine prepared");
|
---|
105 | }
|
---|
106 | }
|
---|
107 | protected virtual void Content_RunningChanged(object sender, EventArgs e) {
|
---|
108 | if (InvokeRequired)
|
---|
109 | Invoke(new EventHandler(Content_RunningChanged), sender, e);
|
---|
110 | else {
|
---|
111 | UpdateExecutionTimeTextBox();
|
---|
112 | if (Content.Running) Log("Engine started");
|
---|
113 | else if (Content.Finished) Log("Engine finished");
|
---|
114 | else Log("Engine stopped");
|
---|
115 | }
|
---|
116 | }
|
---|
117 | protected virtual void Content_ExecutionTimeChanged(object sender, EventArgs e) {
|
---|
118 | executionTimeCounter++;
|
---|
119 | if ((executionTimeCounter >= 100) || !Content.Running) {
|
---|
120 | executionTimeCounter = 0;
|
---|
121 | UpdateExecutionTimeTextBox();
|
---|
122 | }
|
---|
123 | }
|
---|
124 | protected virtual void Content_ExceptionOccurred(object sender, EventArgs<Exception> e) {
|
---|
125 | if (InvokeRequired)
|
---|
126 | Invoke(new EventHandler<EventArgs<Exception>>(Content_ExceptionOccurred), sender, e);
|
---|
127 | else
|
---|
128 | Log(Auxiliary.BuildErrorMessage(e.Value));
|
---|
129 | }
|
---|
130 | #endregion
|
---|
131 |
|
---|
132 | #region Helpers
|
---|
133 | protected virtual void UpdateExecutionTimeTextBox() {
|
---|
134 | if (InvokeRequired)
|
---|
135 | Invoke(new Action(UpdateExecutionTimeTextBox));
|
---|
136 | else
|
---|
137 | executionTimeTextBox.Text = Content.ExecutionTime.ToString();
|
---|
138 | }
|
---|
139 | protected virtual void Log(string message) {
|
---|
140 | if (InvokeRequired)
|
---|
141 | Invoke(new Action<string>(Log), message);
|
---|
142 | else {
|
---|
143 | message = DateTime.Now.ToString() + "\t" + message;
|
---|
144 | string[] newLines = message.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
|
---|
145 | string[] lines = new string[logTextBox.Lines.Length + newLines.Length];
|
---|
146 | logTextBox.Lines.CopyTo(lines, 0);
|
---|
147 | newLines.CopyTo(lines, logTextBox.Lines.Length);
|
---|
148 | logTextBox.Lines = lines;
|
---|
149 | }
|
---|
150 | }
|
---|
151 | #endregion
|
---|
152 | }
|
---|
153 | }
|
---|