Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.0/sources/HeuristicLab.OptimizationFrontend/MainForm.cs @ 8360

Last change on this file since 8360 was 690, checked in by gkronber, 16 years ago

merged r675 into the 3.0 branch (#307)

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.Threading;
29using System.Windows.Forms;
30using HeuristicLab.PluginInfrastructure;
31using HeuristicLab.Core;
32using System.IO;
33
34namespace 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(control is IEditor) {
88        EditorForm form = new EditorForm((IEditor)control);
89        form.MdiParent = this;
90        form.Show();
91      } else if(control is IView) {
92        ViewForm form = new ViewForm((IView)control);
93        form.MdiParent = this;
94        form.Show();
95      } else {
96        throw new InvalidOperationException("Control is neither a view nor an editor.");
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      } catch(FileNotFoundException ex) {
149        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.",
150          "Reader Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
151      } catch(TypeLoadException typeLoadEx) {
152        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.",
153          "Reader Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
154      }
155
156      LoadFinished(task);
157    }
158    private delegate void TaskFinishedDelegate(Task task);
159    private void LoadFinished(Task task) {
160      if(InvokeRequired)
161        Invoke(new TaskFinishedDelegate(LoadFinished), task);
162      else {
163        IEditor editor = null;
164        if(task.storable != null) {
165          IEditable editable = task.storable as IEditable;
166          if(editable != null)
167            editor = editable.CreateEditor();
168        }
169        if(editor == null)
170          MessageBox.Show("Could not open item. The selected item doesn't provide an editor.", "Editor Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
171        else {
172          editor.Filename = task.filename;
173          PluginManager.ControlManager.ShowControl(editor);
174        }
175        lock(locker) {
176          runningTasks--;
177          if(runningTasks == 0)
178            Cursor = Cursors.Default;
179        }
180      }
181    }
182    private void Save(EditorForm form) {
183      if(form.Editor.Filename == null)
184        SaveAs(form);
185      else {
186        lock(locker) runningTasks++;
187        Cursor = Cursors.AppStarting;
188        ((Control)form.Editor).Enabled = false;
189        EnableDisableItems();
190        Task task = new Task(form.Editor.Filename, form.Editor.Item, form.Editor);
191        ThreadPool.QueueUserWorkItem(new WaitCallback(AsynchronousSave), task);
192      }
193    }
194    private void SaveAs(EditorForm form) {
195      if(saveFileDialog.ShowDialog(this) == DialogResult.OK) {
196        form.Editor.Filename = saveFileDialog.FileName;
197        Save(form);
198      }
199    }
200    private void AsynchronousSave(object state) {
201      Task task = (Task)state;
202      PersistenceManager.Save(task.storable, task.filename);
203      SaveFinished(task);
204    }
205    private void SaveFinished(Task task) {
206      if(InvokeRequired)
207        Invoke(new TaskFinishedDelegate(SaveFinished), task);
208      else {
209        ((Control)task.editor).Enabled = true;
210        EnableDisableItems();
211        lock(locker) {
212          runningTasks--;
213          if(runningTasks == 0)
214            Cursor = Cursors.Default;
215        }
216      }
217    }
218    #endregion
219
220    private void MainForm_MdiChildActivate(object sender, EventArgs e) {
221      EnableDisableItems();
222    }
223
224    #region Menu Events
225    private void newToolStripMenuItem_Click(object sender, EventArgs e) {
226      ToolStripItem item = (ToolStripItem)sender;
227      Type type = (Type)item.Tag;
228      IEditable editable = (IEditable)Activator.CreateInstance(type);
229      if(editable == null) {
230        MessageBox.Show("The selected item is not editable.", "Editable Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
231      } else {
232        IEditor editor = editable.CreateEditor();
233        if(editor == null) {
234          MessageBox.Show("The selected item doesn't provide an editor.", "Editor Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
235        } else {
236          PluginManager.ControlManager.ShowControl(editor);
237          EnableDisableItems();
238        }
239      }
240    }
241    private void openToolStripMenuItem_Click(object sender, EventArgs e) {
242      Open();
243    }
244    private void saveToolStripMenuItem_Click(object sender, EventArgs e) {
245      EditorForm form = ActiveMdiChild as EditorForm;
246      Save(form);
247    }
248    private void saveAsToolStripMenuItem_Click(object sender, EventArgs e) {
249      EditorForm form = ActiveMdiChild as EditorForm;
250      SaveAs(form);
251    }
252    private void saveAllToolStripMenuItem_Click(object sender, EventArgs e) {
253      for(int i = 0; i < MdiChildren.Length; i++) {
254        EditorForm form = MdiChildren[i] as EditorForm;
255        if(((Control)form.Editor).Enabled) Save(form);
256      }
257    }
258    private void closeToolStripMenuItem_Click(object sender, EventArgs e) {
259      ActiveMdiChild.Close();
260      EnableDisableItems();
261    }
262    private void closeAllToolStripMenuItem_Click(object sender, EventArgs e) {
263      while(MdiChildren.Length > 0)
264        MdiChildren[0].Close();
265      EnableDisableItems();
266    }
267    private void exitToolStripMenuItem_Click(object sender, EventArgs e) {
268      Application.Exit();
269    }
270    private void availableOperatorsToolStripMenuItem_Click(object sender, EventArgs e) {
271      AvailableOperatorsForm form = new AvailableOperatorsForm();
272      form.MdiParent = this;
273      form.Show();
274    }
275    private void collectGarbageToolStripMenuItem_Click(object sender, EventArgs e) {
276      GC.Collect();
277    }
278    private void cascadeToolStripMenuItem_Click(object sender, EventArgs e) {
279      LayoutMdi(MdiLayout.Cascade);
280    }
281    private void tileHorizontallyToolStripMenuItem_Click(object sender, EventArgs e) {
282      LayoutMdi(MdiLayout.TileHorizontal);
283    }
284    private void tileVerticallyToolStripMenuItem_Click(object sender, EventArgs e) {
285      LayoutMdi(MdiLayout.TileVertical);
286    }
287    private void aboutToolStripMenuItem_Click(object sender, EventArgs e) {
288      AboutDialog dialog = new AboutDialog();
289      dialog.ShowDialog(this);
290      dialog.Dispose();
291    }
292    #endregion
293
294    #region ToolStrip Events
295    private void openToolStripButton_Click(object sender, EventArgs e) {
296      Open();
297    }
298    private void saveToolStripButton_Click(object sender, EventArgs e) {
299      EditorForm form = ActiveMdiChild as EditorForm;
300      Save(form);
301    }
302    private void saveAllToolStripButton_Click(object sender, EventArgs e) {
303      for(int i = 0; i < MdiChildren.Length; i++) {
304        EditorForm form = MdiChildren[i] as EditorForm;
305        if(form != null && ((Control)form.Editor).Enabled) Save(form);
306      }
307    }
308    #endregion
309  }
310}
Note: See TracBrowser for help on using the repository browser.