Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.AdvancedOptimizationFrontend/3.2/MainForm.cs @ 1688

Last change on this file since 1688 was 1529, checked in by gkronber, 16 years ago

Moved source files of plugins AdvancedOptimizationFrontEnd ... Grid into version-specific sub-folders. #576

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