1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2008 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 System.Collections.Generic;
|
---|
24 | using System.ComponentModel;
|
---|
25 | using System.Data;
|
---|
26 | using System.Drawing;
|
---|
27 | using System.Text;
|
---|
28 | using System.Threading;
|
---|
29 | using System.Windows.Forms;
|
---|
30 | using HeuristicLab.PluginInfrastructure;
|
---|
31 | using HeuristicLab.Core;
|
---|
32 | using System.IO;
|
---|
33 |
|
---|
34 | namespace HeuristicLab.OptimizationFrontend {
|
---|
35 | public partial class MainForm : Form, IControlManager {
|
---|
36 | #region Inner Types
|
---|
37 | private class Task {
|
---|
38 | public string filename;
|
---|
39 | public IStorable storable;
|
---|
40 | public IEditor editor;
|
---|
41 |
|
---|
42 | private Task() { }
|
---|
43 | public Task(string filename, IStorable storable, IEditor editor) {
|
---|
44 | this.filename = filename;
|
---|
45 | this.storable = storable;
|
---|
46 | this.editor = editor;
|
---|
47 | }
|
---|
48 | }
|
---|
49 | #endregion
|
---|
50 |
|
---|
51 | private object locker;
|
---|
52 | private int runningTasks;
|
---|
53 |
|
---|
54 | public MainForm() {
|
---|
55 | InitializeComponent();
|
---|
56 |
|
---|
57 | locker = new object();
|
---|
58 | runningTasks = 0;
|
---|
59 |
|
---|
60 | DiscoveryService discoveryService = new DiscoveryService();
|
---|
61 |
|
---|
62 | // discover creatable items
|
---|
63 | Type[] creatables = discoveryService.GetTypes(typeof(IEditable));
|
---|
64 | string[] names = new string[creatables.Length];
|
---|
65 | for (int i = 0; i < creatables.Length; i++)
|
---|
66 | names[i] = creatables[i].Name;
|
---|
67 | Array.Sort(names, creatables);
|
---|
68 | foreach (Type type in creatables) {
|
---|
69 | if (!type.IsAbstract) {
|
---|
70 | ToolStripMenuItem item = new ToolStripMenuItem();
|
---|
71 | item.Tag = type;
|
---|
72 | item.Text = "&" + type.Name + "...";
|
---|
73 | item.Click += new EventHandler(newToolStripMenuItem_Click);
|
---|
74 | newToolStripMenuItem.DropDownItems.Add(item);
|
---|
75 |
|
---|
76 | item = new ToolStripMenuItem();
|
---|
77 | item.Tag = type;
|
---|
78 | item.Text = "&" + type.Name + "...";
|
---|
79 | item.Click += new EventHandler(newToolStripMenuItem_Click);
|
---|
80 | newToolStripDropDownButton.DropDownItems.Add(item);
|
---|
81 | }
|
---|
82 | }
|
---|
83 | }
|
---|
84 |
|
---|
85 | #region IControlManager Members
|
---|
86 | public void ShowControl(IControl control) {
|
---|
87 | if (InvokeRequired) Invoke((Action<IControl>)ShowControl, control);
|
---|
88 | else {
|
---|
89 | if (control is IEditor) {
|
---|
90 | EditorForm form = new EditorForm((IEditor)control);
|
---|
91 | form.MdiParent = this;
|
---|
92 | form.Show();
|
---|
93 | } else if (control is IView) {
|
---|
94 | ViewForm form = new ViewForm((IView)control);
|
---|
95 | form.MdiParent = this;
|
---|
96 | form.Show();
|
---|
97 | } else {
|
---|
98 | throw new InvalidOperationException("Control is neither a view nor an editor.");
|
---|
99 | }
|
---|
100 | }
|
---|
101 | }
|
---|
102 | #endregion
|
---|
103 |
|
---|
104 | private void EnableDisableItems() {
|
---|
105 | closeToolStripMenuItem.Enabled = false;
|
---|
106 | closeAllToolStripMenuItem.Enabled = false;
|
---|
107 | saveToolStripMenuItem.Enabled = false;
|
---|
108 | saveToolStripButton.Enabled = false;
|
---|
109 | saveAsToolStripMenuItem.Enabled = false;
|
---|
110 | saveAllToolStripMenuItem.Enabled = false;
|
---|
111 | saveAllToolStripButton.Enabled = false;
|
---|
112 | cascadeToolStripMenuItem.Enabled = false;
|
---|
113 | tileHorizontallyToolStripMenuItem.Enabled = false;
|
---|
114 | tileVerticallyToolStripMenuItem.Enabled = false;
|
---|
115 |
|
---|
116 | if (ActiveMdiChild != null) {
|
---|
117 | closeToolStripMenuItem.Enabled = true;
|
---|
118 | closeAllToolStripMenuItem.Enabled = true;
|
---|
119 | saveAllToolStripMenuItem.Enabled = true;
|
---|
120 | saveAllToolStripButton.Enabled = true;
|
---|
121 | cascadeToolStripMenuItem.Enabled = true;
|
---|
122 | tileHorizontallyToolStripMenuItem.Enabled = true;
|
---|
123 | tileVerticallyToolStripMenuItem.Enabled = true;
|
---|
124 | EditorForm form = ActiveMdiChild as EditorForm;
|
---|
125 | if (form != null) {
|
---|
126 | if (((Control)form.Editor).Enabled) {
|
---|
127 | saveToolStripMenuItem.Enabled = true;
|
---|
128 | saveToolStripButton.Enabled = true;
|
---|
129 | saveAsToolStripMenuItem.Enabled = true;
|
---|
130 | } else {
|
---|
131 | closeToolStripMenuItem.Enabled = false;
|
---|
132 | closeAllToolStripMenuItem.Enabled = false;
|
---|
133 | }
|
---|
134 | }
|
---|
135 | }
|
---|
136 | }
|
---|
137 |
|
---|
138 | #region Open and Save Methods
|
---|
139 | private void Open() {
|
---|
140 | if (openFileDialog.ShowDialog(this) == DialogResult.OK) {
|
---|
141 | lock (locker) runningTasks++;
|
---|
142 | Cursor = Cursors.AppStarting;
|
---|
143 | Task task = new Task(openFileDialog.FileName, null, null);
|
---|
144 | ThreadPool.QueueUserWorkItem(new WaitCallback(AsynchronousLoad), task);
|
---|
145 | }
|
---|
146 | }
|
---|
147 | private void AsynchronousLoad(object state) {
|
---|
148 | Task task = (Task)state;
|
---|
149 | try {
|
---|
150 | task.storable = PersistenceManager.Load(task.filename);
|
---|
151 | }
|
---|
152 | catch (FileNotFoundException ex) {
|
---|
153 | MessageBox.Show("Sorry couldn't open file \"" + task.filename + "\".\nThe file or plugin \"" + ex.FileName + "\" is not available.\nPlease make sure you have all necessary plugins installed.",
|
---|
154 | "Reader Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
---|
155 | }
|
---|
156 | catch (TypeLoadException typeLoadEx) {
|
---|
157 | MessageBox.Show("Sorry couldn't open file \"" + task.filename + "\".\nThe type \"" + typeLoadEx.TypeName + "\" is not available.\nPlease make sure that you have the correct version the plugin installed.",
|
---|
158 | "Reader Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
---|
159 | }
|
---|
160 |
|
---|
161 | LoadFinished(task);
|
---|
162 | }
|
---|
163 | private delegate void TaskFinishedDelegate(Task task);
|
---|
164 | private void LoadFinished(Task task) {
|
---|
165 | if (InvokeRequired)
|
---|
166 | Invoke(new TaskFinishedDelegate(LoadFinished), task);
|
---|
167 | else {
|
---|
168 | IEditor editor = null;
|
---|
169 | if (task.storable != null) {
|
---|
170 | IEditable editable = task.storable as IEditable;
|
---|
171 | if (editable != null)
|
---|
172 | editor = editable.CreateEditor();
|
---|
173 | }
|
---|
174 | if (editor == null)
|
---|
175 | MessageBox.Show("Could not open item. The selected item doesn't provide an editor.", "Editor Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
---|
176 | else {
|
---|
177 | editor.Filename = task.filename;
|
---|
178 | PluginManager.ControlManager.ShowControl(editor);
|
---|
179 | }
|
---|
180 | lock (locker) {
|
---|
181 | runningTasks--;
|
---|
182 | if (runningTasks == 0)
|
---|
183 | Cursor = Cursors.Default;
|
---|
184 | }
|
---|
185 | }
|
---|
186 | }
|
---|
187 | private void Save(EditorForm form) {
|
---|
188 | if (form.Editor.Filename == null)
|
---|
189 | SaveAs(form);
|
---|
190 | else {
|
---|
191 | lock (locker) runningTasks++;
|
---|
192 | Cursor = Cursors.AppStarting;
|
---|
193 | ((Control)form.Editor).Enabled = false;
|
---|
194 | EnableDisableItems();
|
---|
195 | Task task = new Task(form.Editor.Filename, form.Editor.Item, form.Editor);
|
---|
196 | ThreadPool.QueueUserWorkItem(new WaitCallback(AsynchronousSave), task);
|
---|
197 | }
|
---|
198 | }
|
---|
199 | private void SaveAs(EditorForm form) {
|
---|
200 | if (saveFileDialog.ShowDialog(this) == DialogResult.OK) {
|
---|
201 | form.Editor.Filename = saveFileDialog.FileName;
|
---|
202 | Save(form);
|
---|
203 | }
|
---|
204 | }
|
---|
205 | private void AsynchronousSave(object state) {
|
---|
206 | Task task = (Task)state;
|
---|
207 | PersistenceManager.Save(task.storable, task.filename);
|
---|
208 | SaveFinished(task);
|
---|
209 | }
|
---|
210 | private void SaveFinished(Task task) {
|
---|
211 | if (InvokeRequired)
|
---|
212 | Invoke(new TaskFinishedDelegate(SaveFinished), task);
|
---|
213 | else {
|
---|
214 | ((Control)task.editor).Enabled = true;
|
---|
215 | EnableDisableItems();
|
---|
216 | lock (locker) {
|
---|
217 | runningTasks--;
|
---|
218 | if (runningTasks == 0)
|
---|
219 | Cursor = Cursors.Default;
|
---|
220 | }
|
---|
221 | }
|
---|
222 | }
|
---|
223 | #endregion
|
---|
224 |
|
---|
225 | private void MainForm_MdiChildActivate(object sender, EventArgs e) {
|
---|
226 | EnableDisableItems();
|
---|
227 | }
|
---|
228 |
|
---|
229 | #region Menu Events
|
---|
230 | private void newToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
231 | ToolStripItem item = (ToolStripItem)sender;
|
---|
232 | Type type = (Type)item.Tag;
|
---|
233 | IEditable editable = (IEditable)Activator.CreateInstance(type);
|
---|
234 | if (editable == null) {
|
---|
235 | MessageBox.Show("The selected item is not editable.", "Editable Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
---|
236 | } else {
|
---|
237 | IEditor editor = editable.CreateEditor();
|
---|
238 | if (editor == null) {
|
---|
239 | MessageBox.Show("The selected item doesn't provide an editor.", "Editor Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
---|
240 | } else {
|
---|
241 | PluginManager.ControlManager.ShowControl(editor);
|
---|
242 | EnableDisableItems();
|
---|
243 | }
|
---|
244 | }
|
---|
245 | }
|
---|
246 | private void openToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
247 | Open();
|
---|
248 | }
|
---|
249 | private void saveToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
250 | EditorForm form = ActiveMdiChild as EditorForm;
|
---|
251 | Save(form);
|
---|
252 | }
|
---|
253 | private void saveAsToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
254 | EditorForm form = ActiveMdiChild as EditorForm;
|
---|
255 | SaveAs(form);
|
---|
256 | }
|
---|
257 | private void saveAllToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
258 | for (int i = 0; i < MdiChildren.Length; i++) {
|
---|
259 | EditorForm form = MdiChildren[i] as EditorForm;
|
---|
260 | if (((Control)form.Editor).Enabled) Save(form);
|
---|
261 | }
|
---|
262 | }
|
---|
263 | private void closeToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
264 | ActiveMdiChild.Close();
|
---|
265 | EnableDisableItems();
|
---|
266 | }
|
---|
267 | private void closeAllToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
268 | while (MdiChildren.Length > 0)
|
---|
269 | MdiChildren[0].Close();
|
---|
270 | EnableDisableItems();
|
---|
271 | }
|
---|
272 | private void exitToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
273 | Application.Exit();
|
---|
274 | }
|
---|
275 | private void availableOperatorsToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
276 | AvailableOperatorsForm form = new AvailableOperatorsForm();
|
---|
277 | form.MdiParent = this;
|
---|
278 | form.Show();
|
---|
279 | }
|
---|
280 | private void collectGarbageToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
281 | GC.Collect();
|
---|
282 | }
|
---|
283 | private void cascadeToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
284 | LayoutMdi(MdiLayout.Cascade);
|
---|
285 | }
|
---|
286 | private void tileHorizontallyToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
287 | LayoutMdi(MdiLayout.TileHorizontal);
|
---|
288 | }
|
---|
289 | private void tileVerticallyToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
290 | LayoutMdi(MdiLayout.TileVertical);
|
---|
291 | }
|
---|
292 | private void aboutToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
293 | AboutDialog dialog = new AboutDialog();
|
---|
294 | dialog.ShowDialog(this);
|
---|
295 | dialog.Dispose();
|
---|
296 | }
|
---|
297 | #endregion
|
---|
298 |
|
---|
299 | #region ToolStrip Events
|
---|
300 | private void openToolStripButton_Click(object sender, EventArgs e) {
|
---|
301 | Open();
|
---|
302 | }
|
---|
303 | private void saveToolStripButton_Click(object sender, EventArgs e) {
|
---|
304 | EditorForm form = ActiveMdiChild as EditorForm;
|
---|
305 | Save(form);
|
---|
306 | }
|
---|
307 | private void saveAllToolStripButton_Click(object sender, EventArgs e) {
|
---|
308 | for (int i = 0; i < MdiChildren.Length; i++) {
|
---|
309 | EditorForm form = MdiChildren[i] as EditorForm;
|
---|
310 | if (form != null && ((Control)form.Editor).Enabled) Save(form);
|
---|
311 | }
|
---|
312 | }
|
---|
313 | #endregion
|
---|
314 | }
|
---|
315 | }
|
---|