Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Persistence Test/HeuristicLab.OptimizationFrontend/3.3/MainForm.cs @ 4062

Last change on this file since 4062 was 2744, checked in by epitzer, 15 years ago

update to new PluginInfrastructure (#802)

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.Linq;
28using System.Text;
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      Type[] creatables = ApplicationManager.Manager.GetTypes(typeof(IEditable)).ToArray();
63      string[] names = new string[creatables.Length];
64      for(int i = 0; i < creatables.Length; i++)
65        names[i] = creatables[i].Name;
66      Array.Sort(names, creatables);
67      foreach(Type type in creatables) {
68        if(!type.IsAbstract) {
69          ToolStripMenuItem item = new ToolStripMenuItem();
70          item.Tag = type;
71          item.Text = "&" + type.Name + "...";
72          item.Click += new EventHandler(newToolStripMenuItem_Click);
73          newToolStripMenuItem.DropDownItems.Add(item);
74
75          item = new ToolStripMenuItem();
76          item.Tag = type;
77          item.Text = "&" + type.Name + "...";
78          item.Click += new EventHandler(newToolStripMenuItem_Click);
79          newToolStripDropDownButton.DropDownItems.Add(item);
80        }
81      }
82    }
83
84    #region IControlManager Members
85    public void ShowControl(object control) {
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    #endregion
99
100    private void EnableDisableItems() {
101      closeToolStripMenuItem.Enabled = false;
102      closeAllToolStripMenuItem.Enabled = false;
103      saveToolStripMenuItem.Enabled = false;
104      saveToolStripButton.Enabled = false;
105      saveAsToolStripMenuItem.Enabled = false;
106      saveAllToolStripMenuItem.Enabled = false;
107      saveAllToolStripButton.Enabled = false;
108      cascadeToolStripMenuItem.Enabled = false;
109      tileHorizontallyToolStripMenuItem.Enabled = false;
110      tileVerticallyToolStripMenuItem.Enabled = false;
111
112      if(ActiveMdiChild != null) {
113        closeToolStripMenuItem.Enabled = true;
114        closeAllToolStripMenuItem.Enabled = true;
115        saveAllToolStripMenuItem.Enabled = true;
116        saveAllToolStripButton.Enabled = true;
117        cascadeToolStripMenuItem.Enabled = true;
118        tileHorizontallyToolStripMenuItem.Enabled = true;
119        tileVerticallyToolStripMenuItem.Enabled = true;
120        EditorForm form = ActiveMdiChild as EditorForm;
121        if(form != null) {
122          if(((Control)form.Editor).Enabled) {
123            saveToolStripMenuItem.Enabled = true;
124            saveToolStripButton.Enabled = true;
125            saveAsToolStripMenuItem.Enabled = true;
126          } else {
127            closeToolStripMenuItem.Enabled = false;
128            closeAllToolStripMenuItem.Enabled = false;
129          }
130        }
131      }
132    }
133
134    #region Open and Save Methods
135    private void Open() {
136      if(openFileDialog.ShowDialog(this) == DialogResult.OK) {
137        lock(locker) runningTasks++;
138        Cursor = Cursors.AppStarting;
139        Task task = new Task(openFileDialog.FileName, null, null);
140        ThreadPool.QueueUserWorkItem(new WaitCallback(AsynchronousLoad), task);
141      }
142    }
143    private void AsynchronousLoad(object state) {
144      Task task = (Task)state;
145      try {
146        task.storable = PersistenceManager.Load(task.filename);
147      } catch(FileNotFoundException ex) {
148        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.",
149          "Reader Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
150      } catch(TypeLoadException typeLoadEx) {
151        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.",
152          "Reader Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
153      }
154
155      LoadFinished(task);
156    }
157    private delegate void TaskFinishedDelegate(Task task);
158    private void LoadFinished(Task task) {
159      if(InvokeRequired)
160        Invoke(new TaskFinishedDelegate(LoadFinished), task);
161      else {
162        IEditor editor = null;
163        if(task.storable != null) {
164          IEditable editable = task.storable as IEditable;
165          if(editable != null)
166            editor = editable.CreateEditor();
167        }
168        if(editor == null)
169          MessageBox.Show("Could not open item. The selected item doesn't provide an editor.", "Editor Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
170        else {
171          editor.Filename = task.filename;
172          ControlManager.Manager.ShowControl(editor);
173        }
174        lock(locker) {
175          runningTasks--;
176          if(runningTasks == 0)
177            Cursor = Cursors.Default;
178        }
179      }
180    }
181    private void Save(EditorForm form) {
182      if(form.Editor.Filename == null)
183        SaveAs(form);
184      else {
185        lock(locker) runningTasks++;
186        Cursor = Cursors.AppStarting;
187        ((Control)form.Editor).Enabled = false;
188        EnableDisableItems();
189        Task task = new Task(form.Editor.Filename, form.Editor.Item, form.Editor);
190        ThreadPool.QueueUserWorkItem(new WaitCallback(AsynchronousSave), task);
191      }
192    }
193    private void SaveAs(EditorForm form) {
194      if(saveFileDialog.ShowDialog(this) == DialogResult.OK) {
195        form.Editor.Filename = saveFileDialog.FileName;
196        Save(form);
197      }
198    }
199    private void AsynchronousSave(object state) {
200      Task task = (Task)state;
201      PersistenceManager.Save(task.storable, task.filename);
202      SaveFinished(task);
203    }
204    private void SaveFinished(Task task) {
205      if(InvokeRequired)
206        Invoke(new TaskFinishedDelegate(SaveFinished), task);
207      else {
208        ((Control)task.editor).Enabled = true;
209        EnableDisableItems();
210        lock(locker) {
211          runningTasks--;
212          if(runningTasks == 0)
213            Cursor = Cursors.Default;
214        }
215      }
216    }
217    #endregion
218
219    private void MainForm_MdiChildActivate(object sender, EventArgs e) {
220      EnableDisableItems();
221    }
222
223    #region Menu Events
224    private void newToolStripMenuItem_Click(object sender, EventArgs e) {
225      ToolStripItem item = (ToolStripItem)sender;
226      Type type = (Type)item.Tag;
227      IEditable editable = (IEditable)Activator.CreateInstance(type);
228      if(editable == null) {
229        MessageBox.Show("The selected item is not editable.", "Editable Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
230      } else {
231        IEditor editor = editable.CreateEditor();
232        if(editor == null) {
233          MessageBox.Show("The selected item doesn't provide an editor.", "Editor Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
234        } else {
235          ControlManager.Manager.ShowControl(editor);
236          EnableDisableItems();
237        }
238      }
239    }
240    private void openToolStripMenuItem_Click(object sender, EventArgs e) {
241      Open();
242    }
243    private void saveToolStripMenuItem_Click(object sender, EventArgs e) {
244      EditorForm form = ActiveMdiChild as EditorForm;
245      Save(form);
246    }
247    private void saveAsToolStripMenuItem_Click(object sender, EventArgs e) {
248      EditorForm form = ActiveMdiChild as EditorForm;
249      SaveAs(form);
250    }
251    private void saveAllToolStripMenuItem_Click(object sender, EventArgs e) {
252      for(int i = 0; i < MdiChildren.Length; i++) {
253        EditorForm form = MdiChildren[i] as EditorForm;
254        if(((Control)form.Editor).Enabled) Save(form);
255      }
256    }
257    private void closeToolStripMenuItem_Click(object sender, EventArgs e) {
258      ActiveMdiChild.Close();
259      EnableDisableItems();
260    }
261    private void closeAllToolStripMenuItem_Click(object sender, EventArgs e) {
262      while(MdiChildren.Length > 0)
263        MdiChildren[0].Close();
264      EnableDisableItems();
265    }
266    private void exitToolStripMenuItem_Click(object sender, EventArgs e) {
267      Application.Exit();
268    }
269    private void availableOperatorsToolStripMenuItem_Click(object sender, EventArgs e) {
270      AvailableOperatorsForm form = new AvailableOperatorsForm();
271      form.MdiParent = this;
272      form.Show();
273    }
274    private void collectGarbageToolStripMenuItem_Click(object sender, EventArgs e) {
275      GC.Collect();
276    }
277    private void cascadeToolStripMenuItem_Click(object sender, EventArgs e) {
278      LayoutMdi(MdiLayout.Cascade);
279    }
280    private void tileHorizontallyToolStripMenuItem_Click(object sender, EventArgs e) {
281      LayoutMdi(MdiLayout.TileHorizontal);
282    }
283    private void tileVerticallyToolStripMenuItem_Click(object sender, EventArgs e) {
284      LayoutMdi(MdiLayout.TileVertical);
285    }
286    private void aboutToolStripMenuItem_Click(object sender, EventArgs e) {
287      AboutDialog dialog = new AboutDialog();
288      dialog.ShowDialog(this);
289      dialog.Dispose();
290    }
291    #endregion
292
293    #region ToolStrip Events
294    private void openToolStripButton_Click(object sender, EventArgs e) {
295      Open();
296    }
297    private void saveToolStripButton_Click(object sender, EventArgs e) {
298      EditorForm form = ActiveMdiChild as EditorForm;
299      Save(form);
300    }
301    private void saveAllToolStripButton_Click(object sender, EventArgs e) {
302      for(int i = 0; i < MdiChildren.Length; i++) {
303        EditorForm form = MdiChildren[i] as EditorForm;
304        if(form != null && ((Control)form.Editor).Enabled) Save(form);
305      }
306    }
307    #endregion
308  }
309}
Note: See TracBrowser for help on using the repository browser.