Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.2/sources/HeuristicLab.OptimizationFrontend/3.2/MainForm.cs @ 4483

Last change on this file since 4483 was 2591, checked in by gkronber, 15 years ago

Copied refactored plugin infrastructure from branch and merged changeset r2586:2589 from branch into the trunk. #799

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