Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive_Milestone3/sources/HeuristicLab.AdvancedOptimizationFrontend/3.2/MainForm.cs @ 2901

Last change on this file since 2901 was 2105, checked in by gkronber, 15 years ago
  • added a check for InvokeRequired in AdvancedOptimizationFrontend
  • open a new view for snapshots and finished engines from the hive engine.

#545 (Engine which can be executed in the Hive)

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